github.com/stevenmatthewt/agent@v3.5.4+incompatible/packaging/linux/scripts/after-install-and-upgrade.sh (about) 1 # $1 will be the version being upgraded from if this is an upgrade 2 if [ "$1" = "" ] ; then 3 OPERATION="install" 4 else 5 OPERATION="upgrade" 6 fi 7 8 # Find out whether or not the buildkite-agent user exists 9 if [ -z "$(getent passwd buildkite-agent)" ]; then 10 BK_USER_EXISTS="false" 11 else 12 BK_USER_EXISTS="true" 13 fi 14 15 # Add the buildkite user if it doesn't exist on installation 16 if [ "$OPERATION" = "install" ] ; then 17 if [ "$BK_USER_EXISTS" = "false" ]; then 18 # Create the buildkite system user and set it's home to /var/lib/buildkite 19 useradd --system --no-create-home -d /var/lib/buildkite-agent buildkite-agent 20 21 # The user exists now! 22 BK_USER_EXISTS=true 23 fi 24 25 # We create it's home folder in a seperate command so it doesn't blow up if 26 # the folder already exists 27 mkdir -p /var/lib/buildkite-agent 28 fi 29 30 # Create the /etc/buildkite-agent folder if it's not there 31 if [ ! -d /etc/buildkite-agent ]; then 32 mkdir -p /etc/buildkite-agent 33 fi 34 35 # Install the config template if it's not there 36 if [ ! -f /etc/buildkite-agent/buildkite-agent.cfg ]; then 37 cp /usr/share/buildkite-agent/buildkite-agent.cfg /etc/buildkite-agent/buildkite-agent.cfg 38 39 # Set the default permission to 0600 so only the owning user can read/write to the file 40 chmod 0600 /etc/buildkite-agent/buildkite-agent.cfg 41 fi 42 43 # Copy the hooks if they aren't there 44 if [ ! -d /etc/buildkite-agent/hooks ]; then 45 cp -r /usr/share/buildkite-agent/hooks /etc/buildkite-agent 46 fi 47 48 # Check if the system is Ubuntu 14.10. Systemd is broken on this release, so if 49 # even if systemd exists on that system, skip using it. 50 command -v lsb_release > /dev/null && lsb_release -d | grep -q "Ubuntu 14.10" 51 BK_IS_UBUNTU_14_10=$? 52 53 # Check if systemd exists 54 command -v systemctl > /dev/null 55 BK_SYSTEMD_EXISTS=$? 56 57 # Check if upstart exists 58 command -v initctl > /dev/null 59 BK_UPSTART_EXISTS=$? 60 61 # Check if upstart is version 0.6.5 as seen on Amazon linux, RHEL6 & CentOS-6 62 BK_UPSTART_TOO_OLD=0 63 if [ $BK_UPSTART_EXISTS -eq 0 ]; then 64 BK_UPSTART_VERSION="$(initctl --version | awk 'BEGIN{FS="[ ()]"} NR==1{print $4}')" 65 if [ "$BK_UPSTART_VERSION" = "0.6.5" ]; then 66 BK_UPSTART_TOO_OLD=1 67 fi 68 fi 69 70 # Install the relevant system process 71 if [ $BK_SYSTEMD_EXISTS -eq 0 ] && [ $BK_IS_UBUNTU_14_10 -eq 1 ]; then 72 cp /usr/share/buildkite-agent/systemd/buildkite-agent.service /lib/systemd/system/buildkite-agent.service 73 cp /usr/share/buildkite-agent/systemd/buildkite-agent@.service /lib/systemd/system/buildkite-agent@.service 74 75 START_COMMAND="sudo systemctl enable buildkite-agent && sudo systemctl start buildkite-agent" 76 elif [ $BK_UPSTART_EXISTS -eq 0 ] && [ $BK_UPSTART_TOO_OLD -eq 0 ]; then 77 if [ ! -f /etc/init/buildkite-agent.conf ]; then 78 cp /usr/share/buildkite-agent/upstart/buildkite-agent.conf /etc/init/buildkite-agent.conf 79 fi 80 81 START_COMMAND="sudo service buildkite-agent start" 82 elif [ -d /etc/init.d ]; then 83 if [ ! -f /etc/init.d/buildkite-agent ]; then 84 cp /usr/share/buildkite-agent/lsb/buildkite-agent.sh /etc/init.d/buildkite-agent 85 command -v chkconfig > /dev/null && chkconfig --add buildkite-agent 86 fi 87 88 START_COMMAND="sudo /etc/init.d/buildkite-agent start" 89 else 90 # If all the others fails, warn them and just let them run it the old 91 # fasioned way. 92 echo "============================== WARNING ===================================" 93 echo "" 94 echo "The Buildkite Agent could not find a suitable system service to install." 95 echo "Please open an issue at https://github.com/buildkite/agent and let us know" 96 echo "" 97 echo "==========================================================================" 98 echo "" 99 100 START_COMMAND="sudo /usr/bin/buildkite-agent start" 101 fi 102 103 # Nice welcome message on install 104 if [ "$OPERATION" = "install" ] ; then 105 cat <<"TXT" 106 _ _ _ _ _ _ _ _ 107 | | (_) | | | | (_) | | | 108 | |__ _ _ _| | __| | | ___| |_ ___ __ _ __ _ ___ _ __ | |_ 109 | '_ \| | | | | |/ _` | |/ / | __/ _ \ / _` |/ _` |/ _ \ '_ \| __| 110 | |_) | |_| | | | (_| | <| | || __/ | (_| | (_| | __/ | | | |_ 111 |_.__/ \__,_|_|_|\__,_|_|\_\_|\__\___| \__,_|\__, |\___|_| |_|\__| 112 __/ | 113 |___/ 114 115 TXT 116 117 echo "You now need to add your agent token to \"/etc/buildkite-agent/buildkite-agent.cfg\"" 118 echo "and then you can start your agent by running \"$START_COMMAND\"" 119 fi 120 121 # Crude method of causing all the agents to restart 122 if [ "$OPERATION" = "upgrade" ] ; then 123 # Restart agents that have a process name of "buildkite-agent v1.2.3.4" 124 for KILLPID in `ps ax | grep 'buildkite-agent v' | awk ' { print $1;}'`; do 125 kill $KILLPID > /dev/null 2>&1 || true 126 done 127 128 # Restart agents that have a process name of "buildkite-agent start" 129 for KILLPID in `ps ax | grep 'buildkite-agent start' | awk ' { print $1;}'`; do 130 kill $KILLPID > /dev/null 2>&1 || true 131 done 132 fi 133 134 # Make sure all the the folders created are owned by the buildkite-agent user # 135 # on install 136 if [ "$OPERATION" = "install" ] ; then 137 if [ "$BK_USER_EXISTS" = "true" ]; then 138 # Make sure /etc/buildkite-agent is owned by the user 139 chown -R buildkite-agent:buildkite-agent /etc/buildkite-agent 140 141 # Only chown the /var/lib/buildkite-agent folder if it was created 142 if [ -d /var/lib/buildkite-agent ]; then 143 chown -R buildkite-agent:buildkite-agent /var/lib/buildkite-agent 144 fi 145 fi 146 fi 147 148 exit 0