forked from Azure/azure-devops-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-cli-command.sh
115 lines (105 loc) · 2.73 KB
/
run-cli-command.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
function print_usage() {
cat <<EOF
Command
$0
Arguments
--command|-c [Required]: Jenkins cli command to run
--command_input_file|-cif : File path containing the input to the CLI command
--jenkins_url|-j : Jenkins URL, defaulted to "http://localhost:8080/"
--jenkins_username|-ju : Jenkins user name, defaulted to "admin"
--jenkins_password|-jp : Jenkins password. If not specified and the user name is "admin", the initialAdminPassword will be used
EOF
}
function throw_if_empty() {
local name="$1"
local value="$2"
if [ -z "$value" ]; then
>&2 echo "Parameter '$name' cannot be empty."
print_usage
exit -1
fi
}
#set defaults
jenkins_url="http://localhost:8080/"
jenkins_username="admin"
while [[ $# > 0 ]]
do
key="$1"
shift
case $key in
--command|-c)
command="$1"
shift
;;
--command_input_file|-cif)
command_input_file="$1"
shift
;;
--jenkins_url|-j)
jenkins_url="$1"
shift
;;
--jenkins_username|-ju)
jenkins_username="$1"
shift
;;
--jenkins_password|-jp)
jenkins_password="$1"
shift
;;
--help|-help|-h)
print_usage
exit 13
;;
*)
>&2 echo "ERROR: Unknown argument '$key' to script '$0'"
exit -1
esac
done
throw_if_empty jenkins_username $jenkins_username
if [ "$jenkins_username" != "admin" ]; then
throw_if_empty jenkins_password $jenkins_password
fi
function retry_until_successful {
counter=0
"${@}"
while [ $? -ne 0 ]; do
if [[ "$counter" -gt 20 ]]; then
exit 1
else
let counter++
fi
sleep 5
"${@}"
done;
}
function retry_until_successful_with_input {
input_file=$1
shift
counter=0
cat "$input_file" | "${@}"
while [ $? -ne 0 ]; do
if [[ "$counter" -gt 20 ]]; then
exit 1
else
let counter++
fi
sleep 5
cat "$input_file" | "${@}"
done;
}
if [ ! -e jenkins-cli.jar ]; then
>&2 echo "Downloading Jenkins CLI..."
retry_until_successful wget ${jenkins_url}jnlpJars/jenkins-cli.jar -O jenkins-cli.jar
fi
if [ -z "$jenkins_password" ]; then
# NOTE: Intentionally setting this after the first retry_until_successful to ensure the initialAdminPassword file exists
jenkins_password=`sudo cat /var/lib/jenkins/secrets/initialAdminPassword`
fi
>&2 echo "Running \"${command}\"..."
if [ -z "${command_input_file}" ]; then
retry_until_successful java -jar jenkins-cli.jar -s "${jenkins_url}" -auth "${jenkins_username}":"${jenkins_password}" $command
else
retry_until_successful_with_input "${command_input_file}" java -jar jenkins-cli.jar -s "${jenkins_url}" -auth "${jenkins_username}":"${jenkins_password}" $command
fi