bin/mu-self-update
#!/bin/sh
# Copyright:: Copyright (c) 2014 eGlobalTech, Inc., all rights reserved
#
# Licensed under the BSD-3 license (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the root of the project or at
#
# http://egt-labs.com/mu/LICENSE.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ -t 0 ]; then
BOLD=`tput bold`
NORM=`tput sgr0`
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
PINK=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
fi
set -e
#################################################
################## SET VARIABLES ################
#################################################
scriptpath="`dirname $0`"
USER=`whoami`
HOMEDIR="`eval echo ~$USER`"
if [ -z $MU_INSTALLDIR ];then
MU_INSTALLDIR="/opt/mu"
fi
bindir="$MU_INSTALLDIR/bin"
MU_LIBDIR="$MU_INSTALLDIR/lib"
MU_DATADIR="$MU_INSTALLDIR/var"
#################################################
################## Updated Cmds ################
#################################################
DEFAULT_BRANCH="master"
usage()
{
echo "Updates Mu scripts in $MU_INSTALLDIR/bin. Optionally refreshes from git."
echo "Usage: $0 [-b <branch>] [-f [-c <commit>] ] [-d] [-u] [-r]"
echo " -f: Forcibly re-sync $MU_LIBDIR from Git. Saves your"
echo " working changes unless -d is specified."
echo " -c <commit> (requires -f): Reset to a specific commit."
echo " -b <branch>: Use a branch other than $DEFAULT_BRANCH."
echo " -d: Discard local changes to current branch."
echo " -u: Update chef artifacts after update, regardless of cookbook changes."
exit 1
}
rebuild_chef_artifacts=0
run_chef_client=0
while getopts "fb:c:sdruh" opt; do
case $opt in
f)
force_sync=1
;;
c)
force_commit=$OPTARG
;;
b)
branch=$OPTARG
;;
d)
discard=1
;;
r)
run_chef_client=1
;;
u)
rebuild_chef_artifacts=1
;;
h)
usage
;;
\?)
usage
;;
esac
done
if [ "$force_commit" != "" -a "$force_sync" != "1" ];then
usage
fi
mkdir -p $MU_DATADIR/tmp
/bin/cp -a $MU_LIBDIR/cookbooks $MU_DATADIR/tmp/cookbook_changes.$$
mkdir -p $MU_DATADIR/tmp/berks_changes.$$
/bin/cp -a $MU_LIBDIR/Berksfile* $MU_DATADIR/tmp/berks_changes.$$/
/bin/rm -f $MU_DATADIR/tmp/cookbook_changes.$$/cookbooks/mu-tools/files/default/Mu_CA.pem
cd $MU_LIBDIR
lastbranch="`git branch | egrep '^\* ' | awk '{print $2}'`"
if [ "$branch" == "" ];then
branch=$lastbranch
fi
export MUBRANCH=$branch
# Stash unchecked local changes so we can put things back how we found them
if [ "$discard" != "1" ]; then
stashed_changes=1
if [ "`git stash | grep '^No local changes to save'`" ];then
stashed_changes=0
else
echo "${GREEN}Stashing changes from working tree${NORM}"
git --no-pager stash list | head -1
fi
fi
# Swap branches, if pertinent
if [ "$branch" != "$lastbranch" ];then
echo "${YELLOW}Switching from $lastbranch to $branch${NORM} in `pwd`"
set +e
git fetch
set -e
git checkout $branch
else
set +e
git fetch
set -e
git checkout -f $branch
fi
if [ "$force_sync" == "1" ]; then
echo "${RED}Forcibly resetting $repo_name to branch $branch${NORM}"
if [ ! -z $force_commit ];then
# We got asked to reset to a particular commit, do so
echo "${RED}Using comment $force_commit${NORM}"
git reset --hard $force_commit
else
git reset --hard
fi
echo "${YELLOW}Updating $repo_name branch $branch from origin${NORM}"
git pull origin $branch
else
echo "${YELLOW}Updating $repo_name branch $branch from origin${NORM}"
set +e
git pull origin $branch
set -e
fi
git config branch.${branch}.remote origin
git config branch.${branch}.merge refs/heads/$branch
CHEF_CLIENT_VERSION=`grep '^CHEF_CLIENT_VERSION=' /opt/mu/lib/install/installer |cut -d\" -f2`
# Make sure any new bootstrappy stuff has been applied
chef-apply /opt/mu/lib/cookbooks/mu-master/recipes/init.rb
# Make sure bundler will use a recent git binary
if [ -d /usr/local/git-current ];then
export PATH="/usr/local/git-current/bin:${PATH}"
fi
if [ "`diff -r $MU_LIBDIR/cookbooks $MU_DATADIR/tmp/cookbook_changes.$$`" != "" ];then
rebuild_chef_artifacts=1
set -- "-u" "$@"
elif [ "`diff -r $MU_LIBDIR/Berksfile $MU_DATADIR/tmp/berks_changes.$$/Berksfile`" != "" ];then
rebuild_chef_artifacts=1
set -- "-u" "$@"
elif [ "`diff -r $MU_LIBDIR/Berksfile.lock $MU_DATADIR/tmp/berks_changes.$$/Berksfile.lock`" != "" ];then
rebuild_chef_artifacts=1
set -- "-u" "$@"
fi
set +e
/sbin/service nagios stop
/sbin/service mu-momma-cat stop
set -e
echo "${GREEN}Cleaning gems in ${BOLD}/usr/local/ruby-current${NORM}${GREEN}${NORM}"
cd $MU_LIBDIR/modules
/usr/local/ruby-current/bin/bundle update
set +e
/usr/local/ruby-current/bin/bundle clean --force
set -e
cd
DIST_VERSION=`rpm -qa \*-release\* | grep -Ei "redhat|^centos" | cut -d"-" -f3`
# IS_AMAZON=0
if [ "$DIST_VERSION" == "" ];then # funny package name in Amazon Linux
DIST_VERSION=6
# IS_AMAZON=1
elif [ "$DIST_VERSION" == "server" ];then # funny package name in RHEL6
DIST_VERSION="6"
else
DIST_VERSION="7"
fi
grep ^chef_license /etc/chef/client.rb || echo "chef_license 'accept'" >> /etc/chef/client.rb
if ! rpm -q chef;then
yes | rpm -ivh https://packages.chef.io/files/stable/chef/${CHEF_CLIENT_VERSION}/el/${DIST_VERSION}/chef-${CHEF_CLIENT_VERSION}-1.el${DIST_VERSION}.x86_64.rpm
else
cur_chef_ver="`rpm -q chef | cut -d\- -f2`"
if [ "${cur_chef_ver}" != "${CHEF_CLIENT_VERSION}" ];then
yes | rpm -Uvh https://packages.chef.io/files/stable/chef/${CHEF_CLIENT_VERSION}/el/${DIST_VERSION}/chef-${CHEF_CLIENT_VERSION}-1.el${DIST_VERSION}.x86_64.rpm
fi
fi
/opt/chef/bin/chef-apply $MU_LIBDIR/cookbooks/mu-master/recipes/init.rb
/bin/rm -rf $MU_DATADIR/tmp/cookbook_changes.$$
/bin/rm -rf $MU_DATADIR/tmp/berks_changes.$$
/bin/rm -rf /root/.berkshelf/
if [ "$rebuild_chef_artifacts" == "1" ];then
cd $MU_LIBDIR && berks install && berks update
$bindir/mu-upload-chef-artifacts -p
fi
# Make double sure our purely-mu cookbooks are uploaded and ready for platform
# repos to reference.
$bindir/mu-upload-chef-artifacts -r mu
# Now a regular upload for platform repos.
$bindir/mu-upload-chef-artifacts
for dir in $MU_LIBDIR /opt/chef/embedded /opt/opscode/embedded /usr/local/ruby-current/;do
echo "${GREEN}Sanitizing permissions in ${BOLD}$dir${NORM}${GREEN}${NORM}"
for tree in `ls -1 $dir/`;do
find $dir/$tree -type d -exec chmod go+rx {} \;
find $dir/$tree -type f -exec chmod go+r {} \;
done
done
chmod go+rx $MU_LIBDIR/bin/*
$bindir/mu-configure -n
set -e
if [ "$branch" != "$lastbranch" -a "$discard" != "1" ];then
echo "${GREEN}You are now on ${BOLD}$branch${NORM}${GREEN} (you were on ${BOLD}$lastbranch${NORM}${GREEN})${NORM}"
fi
if [ "$stashed_changes" == "1" ];then
if [ "$branch" != "$lastbranch" ];then
echo "${GREEN}You have stashed changes from your working tree, see ${BOLD}git stash list${NORM}"
else
echo "${GREEN}Restoring stashed changes from your working tree${NORM}"
git stash pop
fi
fi