-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-restore
executable file
·179 lines (164 loc) · 7.31 KB
/
jenkins-restore
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
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash -ex
[ "${HOME}" != "" ] || export HOME="/var/lib/jenkins"
REPO=""
DEFAULT_BRANCH=master
USE_BACKUP=false
LOCALCLI_KEY=${HOME}/.ssh/id_dsa
BACKUP_REF_DIR="${HOME}/plugin-ref"
LATEST_PLUGINS=false
DISABLE_JOBS=true
DISABLE_NODES=true
USED_PLUGINS=false
while [[ $# -gt 0 ]] ; do
opt=$1; shift
case $opt in
-h|--home) JENKINS_HOME=$1; shift ;;
-r|--repository) REPO=$1; shift ;;
-b|--branch) DEFAULT_BRANCH=$1; shift;;
-B|--use-backup) USE_BACKUP=true ;;
--do-not-disable-nodes) DISABLE_NODES=false;;
--do-not-disable-jobs) DISABLE_JOSS=false;;
-k|--key) LOCALCLI_KEY=$1; shift;;
-K|--kinit) KINIT=$1; shift ;;
-u|--jenkins-url)JENKINS_URL=$1; shift;;
-w|--workspace) BACKUP_REF_DIR=$1; shift;;
-U|--used-plugins ) USED_PLUGINS=true;;
-L|--latest-plugins) LATEST_PLUGINS=true ;;
esac
done
if [ "X$JENKINS_HOME" = "X" ] ; then JENKINS_HOME=/var/lib/jenkins ; fi
if [ "X${REPO}" = "X" ] ; then
echo "ERROR: Missing backup repository. Usage $0 -r https://github.com/user/repo [-b|--branch default-git-branch]"
exit 1
fi
JENKINS_PORT=$(pgrep -x -a -f ".*httpPort=.*" | tail -1 | tr ' ' '\n' | grep httpPort | sed 's|.*=||')
JAVA_CMD=$(pgrep -x -a -f ".*httpPort=.*" | tail -1 | tr ' ' '\n' | grep java | head -1)
LOCAL_JENKINS_URL=http://localhost:${JENKINS_PORT}
export JENKINS_PLUGIN_REF="${BACKUP_REF_DIR}/plugins"
script_dir=$(cd $(dirname $0); /bin/pwd)
jenkins_prefix=""
if [ "X$JENKINS_URL" = "X" ] ; then JENKINS_URL="http://$(hostname -f):${JENKINS_PORT}" ; fi
JENKINS_URL=$(echo "$JENKINS_URL" | sed 's|/*$||')
if [ -f ${JENKINS_HOME}/jenkins_prefix ] ; then
jenkins_prefix=$(cat ${JENKINS_HOME}/jenkins_prefix)
JENKINS_URL="${JENKINS_URL}/${jenkins_prefix}"
fi
#branch=$(${JAVA_CMD} -jar ${JENKINS_HOME}/jenkins-cli.jar -i $LOCALCLI_KEY -s ${LOCAL_JENKINS_URL}/${jenkins_prefix} version)
branch=$(rpm -q --queryformat '%{VERSION}\n' jenkins)
if [ "X$branch" = "X" ] ; then
echo "Error: Unable to find jenkins version"
exit 1
fi
GIT_BRANCH=$(hostname -s)-${branch}
GIT_DIR=${BACKUP_REF_DIR}/${GIT_BRANCH}
KR5_FILE=""
mkdir -p "${BACKUP_REF_DIR}"
cd "${BACKUP_REF_DIR}"
if [ "X$KINIT" != "X" ] ; then
KR5_FILE=${BACKUP_REF_DIR}/krb5cc_$(whoami)
export KRB5CCNAME=FILE:${KR5_FILE}
kinit $(echo $KINIT | cut -d: -f1) -k -t $(echo $KINIT | cut -d: -f2)
fi
rm -rf $GIT_DIR
git config --global http.emptyAuth true
git clone $REPO $GIT_DIR
cd $GIT_DIR
if $USE_BACKUP ; then
GIT_BRANCH=${DEFAULT_BRANCH}
elif [ $(git branch -a | grep "remotes/origin/${GIT_BRANCH}$" | wc -l) -eq 0 ] ; then
GIT_BRANCH=${DEFAULT_BRANCH}
fi
git checkout $GIT_BRANCH
git clean -fdx
git checkout -q --force $GIT_BRANCH
git reset --hard origin/$GIT_BRANCH
if [ "X$KR5_FILE" != "X" ] ; then rm -f $KR5_FILE ; fi
rm -rf .git
#Decrypt jenkins config files
find .jenkins-backup -name 'cksum' -type f | sed 's|/cksum$||;s|^.jenkins-backup/||' | while IFS= read -r file; do
[ -e "${file}" ] && ${script_dir}/jenkins-encrypt.py -d -P $LOCALCLI_KEY "${file}"
done
rm -rf .jenkins-backup
#Recreate nextBuildNumber files
cat nextBuildNumber | while IFS= read -r line; do
job=$(echo "$line" | sed 's|=[0-9]*$||')
num=$(echo "$line" | sed 's|.*=||')
if [ -d "jobs/${job}" ] ; then echo $num > "jobs/${job}/nextBuildNumber" ; fi
done
rm -f nextBuildNumber
#Relocate jenkins parameters
for f in $(grep -a "@JENKINS_URL@" ${GIT_DIR}/*.xml | sed 's|.xml:.*|.xml|' | sort | uniq) ; do
sed -i -e "s|@JENKINS_URL@|${JENKINS_URL}|" $f
done
for k in LOCAL_JENKINS_DSA_KEY LOCAL_JENKINS_URL JENKINS_PREFIX ; do
if [ $(grep -a "@$k@" ${GIT_DIR}/config.xml | wc -l) -gt 0 ] ; then
v=""
case $k in
JENKINS_PREFIX) v="${jenkins_prefix}" ;;
LOCAL_JENKINS_URL) v="${LOCAL_JENKINS_URL}/${jenkins_prefix}" ;;
LOCAL_JENKINS_DSA_KEY) v="${LOCALCLI_KEY}" ;;
esac
sed -i -e "s|@$k@|${v}|" ${GIT_DIR}/config.xml
fi
done
mkdir -p ${JENKINS_HOME}/workspace
mkdir -p ${JENKINS_HOME}/builds
sed -i -e 's|<workspaceDir>.*</workspaceDir>|<workspaceDir>${JENKINS_HOME}/workspace/${ITEM_FULLNAME}</workspaceDir>|' ${GIT_DIR}/config.xml
sed -i -e 's|<buildsDir>.*</buildsDir>|<buildsDir>${JENKINS_HOME}/builds/${ITEM_FULLNAME}</buildsDir>|' ${GIT_DIR}/config.xml
#Relocate Workspace for nodes if needed
if [ -f jenkins_prefix ] ; then
old_jenkins_prefix=$(cat jenkins_prefix)
if [ "${old_jenkins_prefix}" != "" -a "${old_jenkins_prefix}" != "${jenkins_prefix}" ] ; then
find nodes -name config.xml -type f | xargs --no-run-if-empty sed -i -e "s|/${old_jenkins_prefix}</remoteFS>|/${jenkins_prefix}</remoteFS>|"
fi
rm -f jenkins_prefix
fi
#Set slots for master nodes
MASTER_EXEC=$(getconf _NPROCESSORS_ONLN)
if [ $MASTER_EXEC -lt 2 ] ; then MASTER_EXEC=2; fi
sed -i -e "s|<numExecutors>[0-9]*</numExecutors>|<numExecutors>${MASTER_EXEC}</numExecutors>|" ${GIT_DIR}/config.xml
if $USED_PLUGINS ; then
#Find jenkins plugins in use
rm -f $GIT_DIR/plugins-used.conf ; touch $GIT_DIR/plugins-used.conf
find $GIT_DIR -name '*.xml' -type f | while IFS= read -r file; do
(grep -a ' plugin=' "${file}" | sed 's|.* plugin="||;s|@.*||') >> $GIT_DIR/plugins-used.conf
done
grep -E '^blueocean(|-.*):' | sed -e 's|:.*$||' >> $GIT_DIR/plugins-used.conf || true
grep -E '^(pipeline-build-step|pipeline-stage-view):' | sed -e 's|:.*$||' >> $GIT_DIR/plugins-used.conf || true
else
sed -e 's|:.*$||' $GIT_DIR/pluginslist.conf > $GIT_DIR/plugins-used.conf
fi
#Force install default plugins
[ -e ${script_dir}/etc/default_plugins.txt ] && cat ${script_dir}/etc/default_plugins.txt >> $GIT_DIR/plugins-used.conf
rm -rf $GIT_DIR/plugins-filter.conf && touch $GIT_DIR/plugins-filter.conf
for p in $(cat $GIT_DIR/plugins-used.conf | sort -u) ; do
grep "^$p:" $GIT_DIR/pluginslist.conf >> $GIT_DIR/plugins-filter.conf || true
done
if $LATEST_PLUGINS ; then sed -i -e 's|:.*|:latest|' $GIT_DIR/plugins-filter.conf ; fi
rm -rf ${JENKINS_PLUGIN_REF}/*.lock
${script_dir}/install-plugins.sh $GIT_DIR/pluginslist.conf $GIT_DIR/plugins-filter.conf >install-plugin.log 2>&1
rm -rf ${JENKINS_PLUGIN_REF}/tmp ${JENKINS_PLUGIN_REF}/deps-plugins.txt ${JENKINS_PLUGIN_REF}/deps-plugins.txt.uniq
rm -f $GIT_DIR/pluginslist.conf $GIT_DIR/plugins-filter.conf $GIT_DIR/plugins-used.conf
#Disable all jobs
if $DISABLE_JOBS ; then
find ${GIT_DIR}/jobs -mindepth 2 -name 'config.xml' -type f | grep -v '/configurations/' | while IFS= read -r file; do
sed -i -e 's|<disabled>false|<disabled>true|' "${file}"
done
fi
#disabled all nodes
if $DISABLE_NODES ; then
DISABLE_MSG="<temporaryOfflineCause class=\"hudson.slaves.OfflineCause\$UserCause\"><timestamp>$(date +%s)</timestamp><description><holder><owner>hudson.slaves.Messages</owner></holder><key>SlaveComputer.DisconnectedBy</key><args><string>localcli</string><string></string></args></description><userId>localcli</userId></temporaryOfflineCause>"
find ${GIT_DIR}/nodes -mindepth 2 -name 'config.xml' -type f | while IFS= read -r file; do
sed -i -e "s|<slave>|<slave>\n ${DISABLE_MSG}|" "${file}"
done
fi
#Sync jenkins configuration back to jenkins home
rsync -av --delete ${JENKINS_PLUGIN_REF}/ ${JENKINS_HOME}/plugins/
#Sync all the rest. We do it for top level files to keep the symlinks
find . -maxdepth 1 -mindepth 1 -name '*' | while IFS= read -r file; do
if [ -d "${file}" ] ; then
rsync -av "${file}/" "${JENKINS_HOME}/${file}/"
else
cp -f "${file}" "${JENKINS_HOME}/${file}"
fi
done