-
Notifications
You must be signed in to change notification settings - Fork 2
/
jenkins-tool.sh
executable file
·167 lines (146 loc) · 3.86 KB
/
jenkins-tool.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
### == jenkins-tool.sh [-j <jenkins-id>] <action> ==
###
### Example usage:
###
### jenkins-tool.sh [-j stage] exec <path/to/script.groovy>
### ~ execute custom groovy script (on specified [stage] jenkins)
###
### jenkins-tool.sh enable special-jobs [--do]
### ~ enables all .*special-jobs.* on local(host) jenkins
###
### jenkins-tool.sh -j stage queue [pattern] [--do]
### ~ remove all (without pattern) job runs currently waiting in the queue on 'stage' jenkins
###
###
### Example config file:
### cat ~/.jenkins-tool.conf
###
### CLIOPTS="your-proxy:port" # for all connections/servers
### JENKINS_local=http://localhost/jenkins
### CLIOPTS_local="-i $HOME/.ssh/id_rsa"
### JENKINS_stage="https://stage.jenkins.company.example.org:88/"
### CLIOPTS_stage="-noCertificateCheck -noKeyAuth"
set -e
PREVIEW="${PREVIEW:-true}"
SEARCHFOR="${SEARCHFOR:-}"
ACTION="${ACTION:-}"
JENKINS_local="http://localhost/"
JENKINS="${JENKINS:-local}"
CLIOPTS="${CLIOPTS:-}"
CFG="$HOME/.jenkins-tool.conf"
[[ -f "$CFG" ]] && source "$CFG"
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h|help)
ACTION=""
break
;;
enable)
ACTION=enable
;;
disable)
ACTION=disable
;;
queue)
ACTION=queue
;;
exec)
ACTION=script
PREVIEW="false"
shift
SCRIPT=$1
;;
-j|--jenkins)
shift
JENKINS=$1
;;
--do)
PREVIEW="false"
;;
*)
SEARCHFOR="$1"
;;
esac
shift
done
if [[ -z "$ACTION" ]]; then
# show help
sed -nr "s/^###(.*)$/\1/p" "$0"
echo ""
echo "Configured jenkins connections:"
echo ""
if [[ -f "$CFG" ]]; then
cat $CFG
else
echo "No $CFG file yet!"
fi
exit 0
fi
URLVAR="JENKINS_${JENKINS}"
URL="${!URLVAR}"
if [[ -z "${URL}" ]]; then
echo "$URLVAR does not contain jenkins base url!" >&2
exit 1
else
echo "Using jenkins $URL"
fi
OPTSVAR="CLIOPTS_${JENKINS}"
EXTRAOPTS="${!OPTSVAR}"
EXTRAOPTS="$CLIOPTS $EXTRAOPTS"
if [[ "$PREVIEW" = "true" ]]; then
echo "Just preview, use --do to act. execute the change."
elif [[ -z "$SEARCHFOR" && "$ACTION" != "script" ]]; then
read -p "Empty pattern, really run on all items? [yN]: " ANSWER
[[ "$ANSWER" != "y" ]] && exit 0
fi
if [[ "$ACTION" != "script" ]]; then
SCRIPT="$(mktemp)"
trap "rm -f $SCRIPT" EXIT
fi
if [[ "$ACTION" = "enable" || "$ACTION" = "disable" ]]; then
DISABLED="false"
[[ "$ACTION" = "disable" ]] && DISABLED="true"
cat > $SCRIPT <<EOF
for(item in hudson.model.Hudson.instance.items) {
if( "${SEARCHFOR}" == "" || item.name.indexOf("${SEARCHFOR}") >= 0 ) {
println("Disable=${DISABLED} (was " + item.disabled + ") for " + item.name + " was " + item.disabled)
if( ! $PREVIEW ) {
item.disabled = ${DISABLED}
item.save()
}
}
}
EOF
elif [[ "$ACTION" = "queue" ]]; then
cat > $SCRIPT <<EOF
q = hudson.model.Hudson.instance.queue
q.items.findAll {
it.task.name.indexOf("$SEARCHFOR") >= 0
}.each {
if( ! $PREVIEW ) {
q.cancel(it.task);
print("Canceled " + it.task.name);
} else {
print("Would cancel " + it.task.name);
}
}
EOF
fi
CLIDIR=${CLIDIR:-"$HOME/.cache/jenkins-tool"}
if [[ ! -d "$CLIDIR" ]]; then
mkdir -p "$CLIDIR"
fi
CLI="$CLIDIR/$JENKINS-cli.jar"
if [[ ! -x "$CLI" ]]; then
CLIURL="$URL/jnlpJars/jenkins-cli.jar"
echo "Fetching cli.jar from $CLIURL into $CLI"
curl -k -o "$CLI" "$CLIURL"
chmod +x "$CLI"
fi
if ! head -n2 "$CLI" | grep -q 'META-INF/PK'; then
echo "$CLI from ${CLIURL:-??} does not look like jar file!" >&2
exit 1
fi
set -x
java -jar "$CLI" $EXTRAOPTS -s $URL groovy $SCRIPT