forked from juanluisbaptiste/docker-otrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_otrs_latest_version.sh
executable file
·135 lines (117 loc) · 4.27 KB
/
check_otrs_latest_version.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
#!/bin/bash
# Script to periodically check for new OTRS versions and automatically update
# the Dockerfile with the new version, test that the image builds and
# commit/push the new version so the automatic docker image build starts.
. ./otrs/util_functions.sh
VERBOSE=1
OTRS_LATEST="http://ftp.otrs.org/pub/otrs/otrs-latest.tar.gz"
OTRS_GIT_URL="git@github.com:juanluisbaptiste/docker-otrs.git"
OTRS_UPDATE_LOG="./check_otrs_version.log"
GIT_PUSH=0
#Supported OTRS versions to avoid breaking the image if the major version upgrade
#breaks the image
declare -A OTRS_SUPPORTED_VERSIONS=(
[4]=0 [5]=0 [6]=1
)
ERROR_CODE="ERROR"
trap 'control_c' SIGINT
# return 0 if program version is equal or greater than check version
function check_version() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
}
#Run if user hits control-c
function control_c() {
print_info "\n***Cleaning up ***\n"
cleanup
exit $?
}
function cleanup(){
#Remove temp directory
print_info "Removing temp directory..."
rm -fr ${tempdir}
return $?
}
function verbose(){
message="$1"
code="$2"
date="$(date '+[%x %r]' )"
out="${date} ${message}"
if [ "${code}" != "" ] && [ ${code} == ${ERROR_CODE} ]; then
>&2 print_error "${out}"
elif [ ${VERBOSE} -eq 1 ];then
print_info "${out}"
fi
echo -e ${out} >> ${OTRS_UPDATE_LOG}
}
#Work inside a temp directory
tempdir="$(mktemp -d --suffix -docker-otrs)"
cd ${tempdir}
verbose "********** Checking latest OTRS version **********"
verbose "Downloading OTRS source tarball..."
#Download latest version and get the version from the RELEASE file inside the
#tarball.
wget -q ${OTRS_LATEST}
filename=$(basename ${OTRS_LATEST})
dirname=$(tar tf ${filename} | head -1 | cut -f1 -d"/")
releasefile=$(tar zxvf ${filename} ${dirname}/RELEASE)
otrs_version=$(cat ${releasefile}|grep VERSION|cut -d'=' -f2|tr -d ' ')
#Check if OTRS version is supported by this script
major_version=$(echo ${otrs_version}|cut -d'.' -f1)
[[ ! -n "${OTRS_SUPPORTED_VERSIONS[${major_version}]}" ]] && verbose "ERROR!! Current OTRS version is not supported: ${otrs_version}" && exit 1
#Get version in Dockerfile
docker_otrs_version=$(wget -q -O - https://raw.githubusercontent.com/juanluisbaptiste/docker-otrs/master/otrs/Dockerfile|grep OTRS_VERSION|grep ENV|cut -d'=' -f2|cut -d'-' -f1)
#Compare versions and there's a newer one, update the Dockerfile, commit
#and push
verbose "Checking versions..."
#check_version ${otrs_version} ${docker_otrs_version}
if [ $? -eq 0 ]; then
verbose "New OTRS version available!"
verbose "Updating to OTRS docker image to version ${otrs_version}"
#Get rpm file version to replace on Dockerfile
for i in "01" "02" "03"; do
rpm_version="${otrs_version}-${i}"
verbose "Querying RPM packages version"
OTRS_LATEST_RPM="http://ftp.otrs.org/pub/otrs/RPMS/rhel/7/otrs-${rpm_version}.noarch.rpm"
wget -q ${OTRS_LATEST_RPM}
if [ $? -eq 0 ];then
verbose "RPM package version: ${rpm_version}"
#otrs_version="${rpm_version}"
break
fi
verbose "ERROR: Could not find rpm version !" ${ERROR_CODE} && exit 1
done
#Clone git repo to update OTRS version
git clone ${OTRS_GIT_URL}
[ $? -gt 0 ] && verbose "ERROR: Could not clone git repository." && exit 1
cd docker-otrs/
verbose "Update Dockerfile..."
#TODO: Replace with a dockerfile build parameter
sed -i -r "s/(ENV OTRS_VERSION *= *).*/\1${rpm_version}/" otrs/Dockerfile
#Build image to test it builds ok with the new version
verbose "Build image..."
docker build --rm otrs/
#If the image builds ok, commit and push
if [ $? -eq 0 ];then
verbose "Updating version in drone.io build file"
sed -i s/${docker_otrs_version}/${otrs_version}/g .drone.yml
verbose "Commit changes..."
out="$(git commit -a -m "Automatic OTRS version update: ${docker_otrs_version} -> ${otrs_version}")"
if [ $? -gt 0 ];then
verbose "ERROR: Could not commit changes !: ${out}" ${ERROR_CODE} && exit 1
fi
if [ ${GIT_PUSH} -eq 1 ]; then
verbose "Push changes..."
out="$(git push)"
if [ $? -gt 0 ];then
verbose "ERROR: Could not push changes !: ${out}" ${ERROR_CODE} && exit 1
fi
fi
verbose "SUCESS !! docker image updated to latest version."
fi
cd ..
else
verbose "No new version available."
fi
cd ..
#Cleanup
cleanup