forked from crc-org/crc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centos_ci.sh
165 lines (140 loc) · 5.09 KB
/
centos_ci.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
#!/bin/bash
# bundle location
BUNDLE_VERSION=4.7.2
BUNDLE=crc_libvirt_$BUNDLE_VERSION.crcbundle
GO_VERSION=1.14.13
# Output command before executing
set -x
# Exit on error
set -e
########################################################
# Exit with message on failure of last executed command
# Arguments:
# $1 - Exit code of last executed command
# $2 - Error message
########################################################
function exit_on_failure() {
if [[ "$1" != 0 ]]; then
echo "$2"
exit 1
fi
}
# Source environment variables of the jenkins slave
# that might interest this worker.
function load_jenkins_vars() {
set +x
if [ -e "jenkins-env" ]; then
cat jenkins-env \
| grep -E "(JENKINS_URL|GIT_BRANCH|GIT_COMMIT|BUILD_NUMBER|ghprbSourceBranch|ghprbActualCommit|BUILD_URL|ghprbPullId|CICO_API_KEY|GITHUB_TOKEN|JOB_NAME|RELEASE_VERSION|RH_REGISTRY_PASSWORD|CRC_BUNDLE_PASSWORD)=" \
| sed 's/^/export /g' \
> ~/.jenkins-env
source ~/.jenkins-env
fi
echo 'CICO: Jenkins ENVs loaded'
}
function install_required_packages() {
# Install EPEL repo
yum -q -y install epel-release
# Get all the deps in
yum -q -y install make \
git \
curl \
qemu-kvm \
libvirt \
libvirt-devel \
jq \
gcc \
unzip \
podman
# Install the required version of golang
curl -L -O https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz
tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
echo 'CICO: Required packages installed'
}
# Create a user which has NOPASSWD sudoer role
function prepare_ci_user() {
groupadd -g 1000 -r crc_ci && useradd -g crc_ci -u 1000 crc_ci
chmod +w /etc/sudoers && echo "crc_ci ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && chmod -w /etc/sudoers
# Copy centos_ci.sh to newly created user home dir
cp centos_ci.sh /home/crc_ci/
mkdir /home/crc_ci/payload
# Copy crc repo content into crc_ci user payload directory for later use
cp -R /root/payload /home/crc_ci/
chown -R crc_ci:crc_ci /home/crc_ci/payload
# Copy the jenkins-env into crc_ci home dir
cp ~/.jenkins-env /home/crc_ci/jenkins-env
}
function setup_golang() {
export PATH=$PATH:/usr/local/go/bin
# Show which version of golang in the offical repo.
go version
# Setup GOPATH
mkdir $HOME/gopath $HOME/gopath/src $HOME/gopath/bin $HOME/gopath/pkg
export GOPATH=$HOME/gopath
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
}
function perform_artifacts_upload() {
set +x
# For PR build, GIT_BRANCH is set to branch name other than origin/master
if [[ "$GIT_BRANCH" = "origin/master" ]]; then
# http://stackoverflow.com/a/22908437/1120530; Using --relative as --rsync-path not working
mkdir -p crc/master/$BUILD_NUMBER/
cp -R out/* crc/master/$BUILD_NUMBER/
RSYNC_PASSWORD=$1 rsync -a --relative crc/master/$BUILD_NUMBER/ [email protected]::minishift/crc/
echo "Find Artifacts here http://artifacts.ci.centos.org/minishift/crc/master/$BUILD_NUMBER ."
else
# http://stackoverflow.com/a/22908437/1120530; Using --relative as --rsync-path not working
mkdir -p pr/$ghprbPullId/
cp -R out/* pr/$ghprbPullId/
RSYNC_PASSWORD=$1 rsync -a --relative pr/$ghprbPullId/ [email protected]::minishift/crc/
echo "Find Artifacts here http://artifacts.ci.centos.org/minishift/crc/pr/$ghprbPullId ."
fi
}
function get_bundle() {
mkdir $HOME/Downloads
curl -L "https://storage.googleapis.com/crc-bundle-github-ci/crc_libvirt_$BUNDLE_VERSION.zip" -o $HOME/Downloads/bundle.zip
unzip -P $CRC_BUNDLE_PASSWORD $HOME/Downloads/bundle.zip -d $HOME/Downloads/
}
function upload_logs() {
set +x
# http://stackoverflow.com/a/22908437/1120530; Using --relative as --rsync-path not working
mkdir -p pr/$ghprbPullId/
cp -R test/e2e/out/test-results/* pr/$ghprbPullId/
# Change the file permission to 0644 so after rsync it can be readable by other user.
chmod 0644 $HOME/.crc/crc.log
cp $HOME/.crc/crc.log pr/$ghprbPullId/crc_$(date '+%Y_%m_%d_%H_%M_%S').log
RSYNC_PASSWORD=$1 rsync -a --relative pr/$ghprbPullId/ [email protected]::minishift/crc/
echo "Find Logs here: http://artifacts.ci.centos.org/minishift/crc/pr/$ghprbPullId ."
}
function run_tests() {
set +e
# In Jenkins slave we have pull secret file in the $HOME/payload/crc_pull_secret
# this is copied over using https://github.com/minishift/minishift-ci-jobs/blob/master/minishift-ci-index.yaml#L99
export PULL_SECRET_FILE=--pull-secret-file=$HOME/payload/crc_pull_secret
export BUNDLE_LOCATION=--bundle-location=$HOME/Downloads/$BUNDLE
make e2e
if [[ $? -ne 0 ]]; then
upload_logs $1
exit 1
fi
}
# Execution starts here
if [[ "$UID" = 0 ]]; then
load_jenkins_vars
install_required_packages
prepare_ci_user
runuser -l crc_ci -c "/bin/bash centos_ci.sh"
else
source ~/jenkins-env # Source environment variables for minishift_ci user
export TERM=xterm-256color
get_bundle
setup_golang
# setup to run e2e tests
cd payload
make
make check
# Retrieve password for rsync and run e2e tests
CICO_PASS=$(echo $CICO_API_KEY | cut -d'-' -f1-2)
run_tests $CICO_PASS
perform_artifacts_upload $CICO_PASS
fi