Last active
May 9, 2018 01:33
-
-
Save hsergei/539807e3988f092a0ecf09103eb5f869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Make the build fail on errors. | |
# not yet | |
#set -e | |
MAXRETRY=6 | |
RETRYDELAY=10 | |
RETRYMULT=2 | |
# Strip leading/trailing quotes if present. | |
# Also convert a comma-separated list to a whitespace-separated one. | |
packages=`echo $packages | sed 's/^"\(.*\)"$/\1/' | sed 's/,/ /g'` | |
packages_versioned=`echo $packages_versioned | sed 's/^"\(.*\)"$/\1/' | sed 's/,/ /g'` | |
gavs=`echo $gavs | sed 's/^"\(.*\)"$/\1/' | sed 's/,/ /g'` | |
rpms=`echo $rpms | sed 's/^"\(.*\)"$/\1/' | sed 's/,/ /g'` | |
nexus_url="https://spinnaker-nexus.build.msap.io/nexus" | |
repo="yum" | |
groups="secops" | |
packaging="rpm" | |
package_type="rpm" | |
packages="keycloak-1.0-13" | |
with_release="true" | |
use_packages="packages" | |
echo "Curl:" | |
which curl | |
echo `date "+%Y%m%d-%H%M%S "`"====================================" | |
echo `date "+%Y%m%d-%H%M%S "`"salt_role=\"$salt_role\"" | |
echo `date "+%Y%m%d-%H%M%S "`"rpms=\"$rpms\" (packages to install using yum)" | |
echo `date "+%Y%m%d-%H%M%S "`"upgrade=\"$upgrade\" (should we run OS/packages update?)" | |
echo `date "+%Y%m%d-%H%M%S "`"nexus_url=\"$nexus_url\"" | |
echo `date "+%Y%m%d-%H%M%S "`"repo=\"$repo\" (nexus repository id, default is \"yum\")" | |
echo `date "+%Y%m%d-%H%M%S "`"groups=\"$groups\" (nexus groups to search artifacts)" | |
echo `date "+%Y%m%d-%H%M%S "`"gavs=\"$gavs\" (GAV coordinates of artifacts to install from nexus)" | |
echo `date "+%Y%m%d-%H%M%S "`"packaging=\"$packaging\" (artifact packaging)" | |
echo `date "+%Y%m%d-%H%M%S "`"package_type=\"$package_type\" (artifact packaging)" | |
echo `date "+%Y%m%d-%H%M%S "`"packages=\"$packages\" (artifacts to install from nexus)" | |
echo `date "+%Y%m%d-%H%M%S "`"packages_versioned=\"$packages_versioned\" (artifacts to install from nexus)" | |
echo `date "+%Y%m%d-%H%M%S "`"with_release=\"$with_release\" (set to \"true\" if rpm name matches \"<name>-<version>-<release>\", default is \"\" for \"<name>-<version>\")" | |
echo `date "+%Y%m%d-%H%M%S "`"use_packages=\"$use_packages\" (one of: \"packages_versioned\" (default), \"packages\", \"gavs\")" | |
echo `date "+%Y%m%d-%H%M%S "`"====================================" | |
if [ "X$package_type" != "X" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== replacing packaging with package_type ===" | |
packaging=$package_type | |
fi | |
if [ "X$use_packages" = "Xpackages" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== using packages and gavs, ignoring packages_versioned ===" | |
elif [ "X$use_packages" = "Xpackages_versioned" ]; then | |
packages=$packages_versioned | |
packages_versioned="" | |
echo `date "+%Y%m%d-%H%M%S "`"=== using packages_versioned and gavs, ignoring packages ===" | |
elif [ "X$use_packages" = "Xgavs" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== using gavs only, ignoring packages and packages_versioned ===" | |
packages="" | |
packages_versioned="" | |
fi | |
# Write the salt role to env file | |
echo `date "+%Y%m%d-%H%M%S "`"=== writing salt_role to /etc/default/mulesoft-env ===" | |
sudo tee /etc/default/mulesoft-env <<EOS | |
salt_role=$salt_role | |
EOS | |
function provision_deb() { | |
echo "not implemented" | |
} | |
function provision_rpm() { | |
# running yum upgrade if requested | |
if [ "X$upgrade" = "Xtrue" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== running \"yum -y update\" (upgrade=\"$upgrade\") ===" | |
sudo yum -y update | |
else | |
echo `date "+%Y%m%d-%H%M%S "`"=== will not run \"yum -y update\" (upgrade=\"$upgrade\") ===" | |
fi | |
# For backward compatibility convert packages into gavs | |
# Artifacts can be passed as gavs or as packages | |
if [ "X$packages" != "X" ]; then | |
for package in $packages; do | |
# default RPM file name format is | |
# %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm | |
# some of our rpms include release and some do not | |
# for the latter include with_release=yes to parse version/release | |
# for the rpm with only version in the filename with_release should be empty (default) | |
tmp0=${package%%.rpm} | |
tmp1=${tmp0%%.i386} | |
tmp2=${tmp1%%.x86_64} | |
rpm_release=${tmp2##*-} | |
tmp3=${tmp2%-*} | |
rpm_version=${tmp3##*-} | |
rpm_name=${tmp3%-*} | |
if [ "X$with_release" != "X" ]; then | |
rpm_version="$rpm_version-$rpm_release" | |
else | |
rpm_name="$rpm_name-$rpm_version" | |
rpm_version=$rpm_release | |
fi | |
echo `date "+%Y%m%d-%H%M%S "`"=== Artifact package \"$package\" to GAV \"*:$rpm_name:$rpm_version\" ===" | |
gavs="$gavs *:$rpm_name:$rpm_version" | |
done | |
fi | |
# Install artifacts from nexus | |
p="service/local/artifact/maven" | |
# will download all artifacts first, then install with a single yum command | |
# collect downloaded files in rpmfiles | |
rpmfiles="" | |
for gav in $gavs; do | |
g=${gav%%:*} | |
v=${gav##*:} | |
a=${gav#*:} | |
a=${a%:*} | |
echo `date "+%Y%m%d-%H%M%S "`"=== artifact GAV \"$g:$a:$v\" ===" | |
# try resolve the artifact in spinnaker and buildpacks | |
RESOLVED='NO' | |
# G in $gav will be '*' for artifacts listed in $packages / $packages_versioned | |
# and specific group for artifacts listed in $gavs | |
# for '*' we will search in every group listed in $groups | |
# to be able get artifacts in spinnaker and in buildpacksrpm | |
search_groups=$g | |
if [ "X$g" = "X*" ]; then | |
search_groups=$groups | |
fi | |
for g in $search_groups; do | |
url="${nexus_url}/${p}/resolve?a=${a}&c=&e=&g=${g}&r=${repo}&v=${v}&p=${packaging}" | |
echo "resolve URL : $url" | |
ROUT='/tmp/resolve.out' | |
echo "=== resolv.conf ====" | |
cat /etc/resolv.conf | |
ping -c 3 spinnaker-nexus.build.msap.io | |
echo | telnet spinnaker-nexus.build.msap.io 443 | |
echo "sudo curl $url" | |
sudo curl $url | |
echo `date "+%Y%m%d-%H%M%S "`"=== searching \"${a}\" version \"${v}\" in group \"$g\" ===" | |
out=`sudo curl -s --location -w "%{http_code}" -o ${ROUT} $url` | |
http_code=$out | |
if [ "X$http_code" = "X404" ]; then | |
if /bin/grep 'not found in local storage of repository' $ROUT >/dev/null; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$g:$a:$v\" does not exist ===" | |
else | |
echo `date "+%Y%m%d-%H%M%S "`"=== http_code=\"$http_code\" curl output ===" | |
cat $ROUT | |
fi | |
elif [ "X$http_code" != "X200" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$g:$a:$v\" resolve failed http_code=\"$http_code\" curl output : ===" | |
cat $ROUT | |
else | |
# found the artifact | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$g:$a:$v\" found OK ===" | |
RESOLVED="$g:$a:$v" | |
break | |
fi | |
# search in the next nexus group | |
done | |
if [ "X$RESOLVED" = "XNO" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"${a}-${v}.${packaging}\" not in nexus, will try install with yum ===" | |
# rpms list will be installed later with yum | |
# rpmfiles is the list of artifacts found and downloaded from nexus | |
rpms="$rpms ${a}-${v}.${packaging}" | |
# continue with next artifact's g:a:v | |
continue | |
fi | |
# if artifact was resolved in nexus - try downloading it | |
# download the artifact using g:a:v that we were able to resolve in nexus | |
ATTEMPT=1 | |
RETRY=$RETRYDELAY | |
url="${nexus_url}/${p}/content?a=${a}&c=&e=&g=${g}&r=${repo}&v=${v}&p=${packaging}" | |
#echo `date "+%Y%m%d-%H%M%S "`"=== artifact download url \"$url\" ===" | |
while true; do | |
# destination filename | |
rpmfile="${a}-${v}.${packaging}" | |
echo `date "+%Y%m%d-%H%M%S "`"=== $ATTEMPT of $MAXRETRY downloading \"$rpmfile\" ===" | |
out=`sudo curl -s --location -w "%{http_code} %{filename_effective} %{content_type}" -o "${rpmfile}" $url` | |
http_code=${out%% *} | |
ct=${out#* * } | |
if [ "X$http_code" = "X200" ]; then | |
md5=`/usr/bin/md5sum "$rpmfile" | cut -d \ -f 1` | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$rpmfile\" downloaded OK, md5=$md5 http_code=\"$http_code\" content_type=\"$ct\" ===" | |
rpmfiles="$rpmfiles $rpmfile" | |
break | |
fi | |
if [ $ATTEMPT -ge $MAXRETRY ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== can not download artifact \"$gav\" http_code=\"$http_code\" content_type=\"$ct\" ===" | |
exit 1 | |
else | |
echo `date "+%Y%m%d-%H%M%S "`"=== $ATTEMPT of $MAXRETRY download failed http_code=\"$http_code\" content_type=\"$ct\", retry in \"$RETRY\" sec ===" | |
ATTEMPT=$((ATTEMPT + 1)) | |
sleep $RETRY | |
RETRY=$((RETRY*RETRYMULT)) | |
fi | |
# try download RPM again | |
done | |
# next gav | |
done | |
# install all downloaded artifacts with a single yum install to support interdependencies | |
if [ "X$rpmfiles" != "X" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== yum -y install $rpmfiles ===" | |
if sudo yum -y install $rpmfiles; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$rpmfiles\" installed OK ===" | |
sudo rm -f $rpmfiles | |
else | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$rpmfiles\" install failed ===" | |
exit 1 | |
fi | |
fi | |
# Install packages available in public repositories if $rpms is not empty | |
# $rpms may be passed as an argument or assigned when GAV artifact is not found in nexus | |
if [ "X$rpms" != "X" ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== installing packages \"$rpms\" ===" | |
ATTEMPT=1 | |
RETRY=$RETRYDELAY | |
while true; do | |
if sudo yum -y install $rpms; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$rpms\" installed OK ===" | |
break | |
else | |
if [ $ATTEMPT -ge $MAXRETRY ]; then | |
echo `date "+%Y%m%d-%H%M%S "`"=== \"$rpms\" can not be installed ===" | |
exit -1 | |
break | |
else | |
echo `date "+%Y%m%d-%H%M%S "`"=== $ATTEMPT of $MAXRETRY install failed, will retry in \"$RETRY\" sec ===" | |
ATTEMPT=$((ATTEMPT + 1)) | |
sleep $RETRY | |
RETRY=$((RETRY*RETRYMULT)) | |
fi | |
fi | |
done | |
fi | |
} | |
function main() { | |
if [[ "$packaging" == "deb" ]]; then | |
provision_deb | |
elif [[ "$packaging" == "rpm" ]]; then | |
provision_rpm | |
fi | |
echo `date "+%Y%m%d-%H%M%S "`"=== stopping salt-minion and removing minion_id and keys ===" | |
sudo /etc/init.d/salt-minion stop | |
sudo /bin/rm -f /etc/salt/pki/minion/* /etc/salt/minion_id /etc/salt/pki/minion/minion_master.pub | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment