From 4c451afee4d43eda26e6b9664d7adc94e786daa3 Mon Sep 17 00:00:00 2001 From: ccloes Date: Wed, 20 Jan 2016 18:03:58 -0800 Subject: [PATCH 1/9] Update to include RHEL 7 logic --- init.d/codedeploy-agent | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index 33849406..bb54bbaf 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -18,7 +18,11 @@ ### END INIT INFO # Source function library. -. /etc/rc.d/init.d/functions +if [ -f /etc/rc.d/init.d/functions ]; then + . /etc/rc.d/init.d/functions + ## Refers to version of OS supported (example: RHEL 6) + OS_SUPPORT="6" +fi RETVAL=0 [ -f /etc/profile ] && [ "`stat --format '%U %G' /etc/profile`" == "root root" ] && source /etc/profile @@ -32,7 +36,7 @@ BIN="/opt/codedeploy-agent/bin/codedeploy-agent" start() { echo -n $"Starting $prog:" cd $AGENT_ROOT - if [ $USER ]; then + if [[ $USER && $OS_SUPPORT == "6" ]]; then daemon --user=$USER $BIN start >/dev/null &1 # Try to start the server else nohup $BIN start >/dev/null &1 # Try to start the server @@ -43,7 +47,7 @@ start() { stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT - if [ $USER ]; then + if [[ $USER && $OS_SUPPORT == "6" ]]; then daemon --user=$USER $BIN stop >/dev/null &1 # Try to stop the server else nohup $BIN stop >/dev/null &1 # Try to stop the server @@ -54,7 +58,7 @@ stop() { restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT - if [ $USER ]; then + if [[ $USER && $OS_SUPPORT == "6" ]]; then daemon --user=$USER $BIN restart >/dev/null &1 # Try to restart the server else nohup $BIN restart >/dev/null &1 # Try to restart the server @@ -64,7 +68,7 @@ restart() { status() { cd $AGENT_ROOT - if [ $USER ]; then + if [[ $USER && $OS_SUPPORT == "6" ]]; then daemon --user=$USER $BIN status # Status of the server else $BIN status # Status of the server @@ -75,7 +79,7 @@ status() { update() { echo -n $"Updating $prog:" cd $AGENT_ROOT - if [ $USER ]; then + if [[ $USER && $OS_SUPPORT == "6" ]]; then daemon --user=$USER sudo $INSTALLER auto #Update the agent else $INSTALLER auto #Update the agent From cde431d72b36f6a004546b694d2f0ed6db0478f8 Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 15:08:12 -0800 Subject: [PATCH 2/9] Updated to include OS support --- init.d/codedeploy-agent | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index bb54bbaf..b27821b3 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -17,13 +17,19 @@ # the deployment artifacts on to this instance. ### END INIT INFO -# Source function library. +# determine OS if [ -f /etc/rc.d/init.d/functions ]; then . /etc/rc.d/init.d/functions - ## Refers to version of OS supported (example: RHEL 6) - OS_SUPPORT="6" + OS="RHEL" + echo "This is RHEL" +elif [ "`lsb_release -i | grep -i ubuntu | wc -l`" ]; then + OS="UBUNTU" + echo "This is UBUNTU" +else + OS="OTHER" fi +COMMAND=$1 RETVAL=0 [ -f /etc/profile ] && [ "`stat --format '%U %G' /etc/profile`" == "root root" ] && source /etc/profile @@ -36,8 +42,10 @@ BIN="/opt/codedeploy-agent/bin/codedeploy-agent" start() { echo -n $"Starting $prog:" cd $AGENT_ROOT - if [[ $USER && $OS_SUPPORT == "6" ]]; then + if [[ $USER && $OS == "RHEL" ]]; then daemon --user=$USER $BIN start >/dev/null &1 # Try to start the server + elif [[ $USER && $OS == "UBUNTU" ]]; then + echo "This is here" else nohup $BIN start >/dev/null &1 # Try to start the server fi @@ -47,8 +55,10 @@ start() { stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT - if [[ $USER && $OS_SUPPORT == "6" ]]; then + if [[ $USER && $OS == "RHEL" ]]; then daemon --user=$USER $BIN stop >/dev/null &1 # Try to stop the server + elif [[ $USER && $OS == "UBUNTU" ]]; then + echo "This is here" else nohup $BIN stop >/dev/null &1 # Try to stop the server fi @@ -58,8 +68,10 @@ stop() { restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT - if [[ $USER && $OS_SUPPORT == "6" ]]; then + if [[ $USER && $OS == "RHEL" ]]; then daemon --user=$USER $BIN restart >/dev/null &1 # Try to restart the server + elif [[ $USER && $OS == "UBUNTU" ]]; then + echo "This is here" else nohup $BIN restart >/dev/null &1 # Try to restart the server fi @@ -68,8 +80,10 @@ restart() { status() { cd $AGENT_ROOT - if [[ $USER && $OS_SUPPORT == "6" ]]; then + if [[ $USER && $OS == "RHEL" ]]; then daemon --user=$USER $BIN status # Status of the server + elif [[ $USER && $OS == "UBUNTU" ]]; then + echo "This is here" else $BIN status # Status of the server fi @@ -79,14 +93,16 @@ status() { update() { echo -n $"Updating $prog:" cd $AGENT_ROOT - if [[ $USER && $OS_SUPPORT == "6" ]]; then + if [[ $USER && $OS == "RHEL" ]]; then daemon --user=$USER sudo $INSTALLER auto #Update the agent + elif [[ $USER && $OS == "UBUNTU" ]]; then + echo "This is here" else $INSTALLER auto #Update the agent fi } -case "$1" in +case "$COMMAND" in start) start ;; From 9a412fdb52e60d0aed6f16af24a2e2d6c10f24ed Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 16:12:19 -0800 Subject: [PATCH 3/9] Updated to fix nohup status issue --- init.d/codedeploy-agent | 44 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index b27821b3..166a4930 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -17,18 +17,6 @@ # the deployment artifacts on to this instance. ### END INIT INFO -# determine OS -if [ -f /etc/rc.d/init.d/functions ]; then - . /etc/rc.d/init.d/functions - OS="RHEL" - echo "This is RHEL" -elif [ "`lsb_release -i | grep -i ubuntu | wc -l`" ]; then - OS="UBUNTU" - echo "This is UBUNTU" -else - OS="OTHER" -fi - COMMAND=$1 RETVAL=0 [ -f /etc/profile ] && [ "`stat --format '%U %G' /etc/profile`" == "root root" ] && source /etc/profile @@ -42,10 +30,8 @@ BIN="/opt/codedeploy-agent/bin/codedeploy-agent" start() { echo -n $"Starting $prog:" cd $AGENT_ROOT - if [[ $USER && $OS == "RHEL" ]]; then - daemon --user=$USER $BIN start >/dev/null &1 # Try to start the server - elif [[ $USER && $OS == "UBUNTU" ]]; then - echo "This is here" + if [ $USER ]; then + nohup sudo -u $USER $BIN start >/dev/null &1 # Try to start the server else nohup $BIN start >/dev/null &1 # Try to start the server fi @@ -55,10 +41,8 @@ start() { stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT - if [[ $USER && $OS == "RHEL" ]]; then - daemon --user=$USER $BIN stop >/dev/null &1 # Try to stop the server - elif [[ $USER && $OS == "UBUNTU" ]]; then - echo "This is here" + if [ $USER ]; then + nohup sudo -u $USER $BIN stop >/dev/null &1 # Try to stop the server else nohup $BIN stop >/dev/null &1 # Try to stop the server fi @@ -68,10 +52,8 @@ stop() { restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT - if [[ $USER && $OS == "RHEL" ]]; then - daemon --user=$USER $BIN restart >/dev/null &1 # Try to restart the server - elif [[ $USER && $OS == "UBUNTU" ]]; then - echo "This is here" + if [ $USER ]; then + nohup sudo -u $USER $BIN restart >/dev/null &1 # Try to restart the server else nohup $BIN restart >/dev/null &1 # Try to restart the server fi @@ -80,10 +62,8 @@ restart() { status() { cd $AGENT_ROOT - if [[ $USER && $OS == "RHEL" ]]; then - daemon --user=$USER $BIN status # Status of the server - elif [[ $USER && $OS == "UBUNTU" ]]; then - echo "This is here" + if [ $USER ]; then + sudo -u $USER $BIN status # Status of the server else $BIN status # Status of the server fi @@ -93,13 +73,7 @@ status() { update() { echo -n $"Updating $prog:" cd $AGENT_ROOT - if [[ $USER && $OS == "RHEL" ]]; then - daemon --user=$USER sudo $INSTALLER auto #Update the agent - elif [[ $USER && $OS == "UBUNTU" ]]; then - echo "This is here" - else - $INSTALLER auto #Update the agent - fi + $INSTALLER auto #Update the agent } case "$COMMAND" in From 49118a7ec2dae24283d8ee6672f60fd60a0a2de3 Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 18:01:32 -0800 Subject: [PATCH 4/9] Updated to include -c option for sudo --- init.d/codedeploy-agent | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index 166a4930..31c03a8c 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -31,7 +31,7 @@ start() { echo -n $"Starting $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -u $USER $BIN start >/dev/null &1 # Try to start the server + nohup sudo -c -u $USER $BIN start >/dev/null &1 # Try to start the server else nohup $BIN start >/dev/null &1 # Try to start the server fi @@ -42,7 +42,7 @@ stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -u $USER $BIN stop >/dev/null &1 # Try to stop the server + nohup sudo -c -u $USER $BIN stop >/dev/null &1 # Try to stop the server else nohup $BIN stop >/dev/null &1 # Try to stop the server fi @@ -53,7 +53,7 @@ restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -u $USER $BIN restart >/dev/null &1 # Try to restart the server + nohup sudo -c -u $USER $BIN restart >/dev/null &1 # Try to restart the server else nohup $BIN restart >/dev/null &1 # Try to restart the server fi @@ -63,7 +63,7 @@ restart() { status() { cd $AGENT_ROOT if [ $USER ]; then - sudo -u $USER $BIN status # Status of the server + sudo -c -u $USER $BIN status # Status of the server else $BIN status # Status of the server fi From a2c77ce63f71ca0c383cb55839ff428efbc6fcbe Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 19:32:41 -0800 Subject: [PATCH 5/9] Updated to move nohup --- init.d/codedeploy-agent | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index 31c03a8c..e5b21928 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -31,7 +31,7 @@ start() { echo -n $"Starting $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -c -u $USER $BIN start >/dev/null &1 # Try to start the server + sudo -c -u $USER nohup $BIN start >/dev/null &1 # Try to start the server else nohup $BIN start >/dev/null &1 # Try to start the server fi @@ -42,7 +42,7 @@ stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -c -u $USER $BIN stop >/dev/null &1 # Try to stop the server + sudo -c -u $USER nohup $BIN stop >/dev/null &1 # Try to stop the server else nohup $BIN stop >/dev/null &1 # Try to stop the server fi @@ -53,7 +53,7 @@ restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -c -u $USER $BIN restart >/dev/null &1 # Try to restart the server + sudo -c -u $USER nohup $BIN restart >/dev/null &1 # Try to restart the server else nohup $BIN restart >/dev/null &1 # Try to restart the server fi From 545749362df18639b8129e74e8fe3f7ccd5d2a15 Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 19:42:23 -0800 Subject: [PATCH 6/9] Revert "Updated to move nohup" This reverts commit a2c77ce63f71ca0c383cb55839ff428efbc6fcbe. --- init.d/codedeploy-agent | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index e5b21928..31c03a8c 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -31,7 +31,7 @@ start() { echo -n $"Starting $prog:" cd $AGENT_ROOT if [ $USER ]; then - sudo -c -u $USER nohup $BIN start >/dev/null &1 # Try to start the server + nohup sudo -c -u $USER $BIN start >/dev/null &1 # Try to start the server else nohup $BIN start >/dev/null &1 # Try to start the server fi @@ -42,7 +42,7 @@ stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT if [ $USER ]; then - sudo -c -u $USER nohup $BIN stop >/dev/null &1 # Try to stop the server + nohup sudo -c -u $USER $BIN stop >/dev/null &1 # Try to stop the server else nohup $BIN stop >/dev/null &1 # Try to stop the server fi @@ -53,7 +53,7 @@ restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT if [ $USER ]; then - sudo -c -u $USER nohup $BIN restart >/dev/null &1 # Try to restart the server + nohup sudo -c -u $USER $BIN restart >/dev/null &1 # Try to restart the server else nohup $BIN restart >/dev/null &1 # Try to restart the server fi From 4c8bcf47c39f018f0bd78fd5f76259f279cf123a Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 19:43:11 -0800 Subject: [PATCH 7/9] Including systemd service file --- init.d/codedeploy-agent.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 init.d/codedeploy-agent.service diff --git a/init.d/codedeploy-agent.service b/init.d/codedeploy-agent.service new file mode 100644 index 00000000..09be6a66 --- /dev/null +++ b/init.d/codedeploy-agent.service @@ -0,0 +1,12 @@ +[Unit] +Description=AWS CodeDeploy Host Agent + +[Service] +Type=forking +ExecStart=/opt/codedeploy-agent/bin/codedeploy-agent start +ExecStop=/opt/codedeploy-agent/bin/codedeploy-agent stop +User=codedeploy +RemainAfterExit=no + +[Install] +WantedBy=multi-user.target From ce8a74ccf74c703f7d5f4845af4623b47640ddc3 Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 19:48:33 -0800 Subject: [PATCH 8/9] Updated to fix -i option --- init.d/codedeploy-agent | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index 31c03a8c..dfd4eb4c 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -31,7 +31,7 @@ start() { echo -n $"Starting $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -c -u $USER $BIN start >/dev/null &1 # Try to start the server + nohup sudo -i -u $USER $BIN start >/dev/null &1 # Try to start the server else nohup $BIN start >/dev/null &1 # Try to start the server fi @@ -42,7 +42,7 @@ stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -c -u $USER $BIN stop >/dev/null &1 # Try to stop the server + nohup sudo -i -u $USER $BIN stop >/dev/null &1 # Try to stop the server else nohup $BIN stop >/dev/null &1 # Try to stop the server fi @@ -53,7 +53,7 @@ restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT if [ $USER ]; then - nohup sudo -c -u $USER $BIN restart >/dev/null &1 # Try to restart the server + nohup sudo -i -u $USER $BIN restart >/dev/null &1 # Try to restart the server else nohup $BIN restart >/dev/null &1 # Try to restart the server fi @@ -63,7 +63,7 @@ restart() { status() { cd $AGENT_ROOT if [ $USER ]; then - sudo -c -u $USER $BIN status # Status of the server + sudo -i -u $USER $BIN status # Status of the server else $BIN status # Status of the server fi From d49aaebf0d48cede54df07234aeced7082a75929 Mon Sep 17 00:00:00 2001 From: ccloes Date: Thu, 18 Feb 2016 19:57:44 -0800 Subject: [PATCH 9/9] Updated to include sudo changes to agent --- lib/instance_agent/platform/linux_util.rb | 2 +- lib/instance_agent/plugins/codedeploy/command_executor.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/instance_agent/platform/linux_util.rb b/lib/instance_agent/platform/linux_util.rb index e8f020d5..402f308d 100644 --- a/lib/instance_agent/platform/linux_util.rb +++ b/lib/instance_agent/platform/linux_util.rb @@ -28,7 +28,7 @@ def self.prepare_script_command(script, absolute_cmd_path) # command as the code deploy agent user absolute_cmd_path end - + def self.quit() # Send kill signal to parent and exit Process.kill('TERM', Process.ppid) diff --git a/lib/instance_agent/plugins/codedeploy/command_executor.rb b/lib/instance_agent/plugins/codedeploy/command_executor.rb index 3ef33f7b..78b37984 100644 --- a/lib/instance_agent/plugins/codedeploy/command_executor.rb +++ b/lib/instance_agent/plugins/codedeploy/command_executor.rb @@ -11,7 +11,7 @@ module InstanceAgent module Plugins module CodeDeployPlugin ARCHIVES_TO_RETAIN = 5 - + class CommandExecutor class << self attr_reader :command_methods