github.com/pquerna/agent@v2.1.8+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 systemd exists 49 command -v systemctl > /dev/null 50 BK_SYSTEMD_EXISTS=$? 51 52 # Check if the system is Ubuntu 14.10. Systemd is broken on this release, so if 53 # even if systemd exists on that system, skip using it. 54 command -v lsb_release > /dev/null && lsb_release -d | grep -q "Ubuntu 14.10" 55 BK_IS_UBUNTU_14_10=$? 56 57 # Check if upstart exists 58 command -v initctl > /dev/null 59 BK_UPSTART_EXISTS=$? 60 61 # Check if the system is Amazon linux which has an old and broken upstart. 62 [ -f /etc/system-release ] && grep -qi "Amazon Linux" /etc/system-release 63 BK_IS_AMAZON_LINUX=$? 64 65 # Install the relevant system process 66 if [ $BK_SYSTEMD_EXISTS -eq 0 ] && [ $BK_IS_UBUNTU_14_10 -eq 1 ]; then 67 if [ ! -f /lib/systemd/system/buildkite-agent.service ]; then 68 cp /usr/share/buildkite-agent/systemd/buildkite-agent.service /lib/systemd/system/buildkite-agent.service 69 fi 70 71 START_COMMAND="sudo systemctl enable buildkite-agent && sudo systemctl start buildkite-agent" 72 elif [ $BK_UPSTART_EXISTS -eq 0 ] && [ $BK_IS_AMAZON_LINUX -eq 1 ]; then 73 if [ ! -f /etc/init/buildkite-agent.conf ]; then 74 # If the system has the old .env file, install the old upstart script, and 75 # let them know they should upgrade. Because the upstart script is no 76 # longer considered a `config` file in the debian package, when you upgrade 77 # the agent, it rm's it from /etc/init, boo. So we need to put it back. 78 if [ -f /etc/buildkite-agent/buildkite-agent.env ]; then 79 echo "======================= IMPORTANT UPGRADE NOTICE ==========================" 80 echo "" 81 echo "Hey!" 82 echo "" 83 echo "Sorry to be a pain, but we've deprecated use of the" 84 echo "/etc/buildkite-agent/buildkite-agent.env ENV file as a way of configuring the agent." 85 echo "It's had some issues and the approach wasn't very cross platform." 86 echo "" 87 echo "We've switched to using a proper config file that you can find here:" 88 echo "" 89 echo "/etc/buildkite-agent/buildkite-agent.cfg" 90 echo "" 91 echo "Everything should continue to work as is (we'll still use the .env file for now)." 92 echo "To upgrade, all you need to do is edit the new config file and copy across the settings" 93 echo "your .env file, then run:" 94 echo "" 95 echo "sudo service buildkite-agent stop" 96 echo "sudo rm /etc/buildkite-agent/buildkite-agent.env" 97 echo "sudo cp /usr/share/buildkite-agent/upstart/buildkite-agent.conf /etc/init/buildkite-agent.conf" 98 echo "sudo service buildkite-agent start" 99 echo "" 100 echo "Then next time you upgrade, you won't see this annoying message :)" 101 echo "" 102 echo "If you have any questions, feel free to email me at: keith@buildkite.com" 103 echo "" 104 echo "~ Keith" 105 echo "" 106 echo "==========================================================================" 107 echo "" 108 109 cp /usr/share/buildkite-agent/upstart/buildkite-agent-using-env.conf /etc/init/buildkite-agent.conf 110 else 111 cp /usr/share/buildkite-agent/upstart/buildkite-agent.conf /etc/init/buildkite-agent.conf 112 fi 113 fi 114 115 START_COMMAND="sudo service buildkite-agent start" 116 elif [ -d /etc/init.d ]; then 117 if [ ! -f /etc/init.d/buildkite-agent ]; then 118 cp /usr/share/buildkite-agent/lsb/buildkite-agent.sh /etc/init.d/buildkite-agent 119 fi 120 121 START_COMMAND="sudo /etc/init.d/buildkite-agent start" 122 else 123 # If all the others fails, warn them and just let them run it the old 124 # fasioned way. 125 echo "============================== WARNING ===================================" 126 echo "" 127 echo "The Buildkite Agent could not find a suitable system service to install." 128 echo "Please open an issue at https://github.com/buildkite/agent and let us know" 129 echo "" 130 echo "==========================================================================" 131 echo "" 132 133 START_COMMAND="sudo /usr/bin/buildkite-agent start" 134 fi 135 136 # Nice welcome message on install 137 if [ "$OPERATION" = "install" ] ; then 138 cat <<"TXT" 139 _ _ _ _ _ _ _ _ 140 | | (_) | | | | (_) | | | 141 | |__ _ _ _| | __| | | ___| |_ ___ __ _ __ _ ___ _ __ | |_ 142 | '_ \| | | | | |/ _` | |/ / | __/ _ \ / _` |/ _` |/ _ \ '_ \| __| 143 | |_) | |_| | | | (_| | <| | || __/ | (_| | (_| | __/ | | | |_ 144 |_.__/ \__,_|_|_|\__,_|_|\_\_|\__\___| \__,_|\__, |\___|_| |_|\__| 145 __/ | 146 |___/ 147 148 TXT 149 150 echo "You now need to add your agent token to \"/etc/buildkite-agent/buildkite-agent.cfg\"" 151 echo "and then you can start your agent by running \"$START_COMMAND\"" 152 fi 153 154 # Crude method of causing all the agents to restart 155 if [ "$OPERATION" = "upgrade" ] ; then 156 # Restart agents that have a process name of "buildkite-agent v1.2.3.4" 157 for KILLPID in `ps ax | grep 'buildkite-agent v' | awk ' { print $1;}'`; do 158 kill $KILLPID > /dev/null 2>&1 || true 159 done 160 161 # Restart agents that have a process name of "buildkite-agent start" 162 for KILLPID in `ps ax | grep 'buildkite-agent start' | awk ' { print $1;}'`; do 163 kill $KILLPID > /dev/null 2>&1 || true 164 done 165 fi 166 167 # Make sure all the the folders created are owned by the buildkite-agent user # 168 # on install 169 if [ "$OPERATION" = "install" ] ; then 170 if [ "$BK_USER_EXISTS" = "true" ]; then 171 # Make sure /etc/buildkite-agent is owned by the user 172 chown -R buildkite-agent:buildkite-agent /etc/buildkite-agent 173 174 # Only chown the /var/lib/buildkite-agent folder if it was created 175 if [ -d /var/lib/buildkite-agent ]; then 176 chown -R buildkite-agent:buildkite-agent /var/lib/buildkite-agent 177 fi 178 fi 179 fi 180 181 exit 0