Skip to content
33 changes: 28 additions & 5 deletions init.d/codedeploy-agent
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,69 @@
# the deployment artifacts on to this instance.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses systemctl to run start, stop, restart commands. systemctl doesn't stop the agent for the first time when you issue the stop command. This seems to be happening only on RHEL boxes. Could you please address this issue before we could release the changes?
Thanks.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is already merged, I will add a new PR with logic to check for RHEL 7. Will that suffice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be fine to do so. Thanks a lot!


RETVAL=0
[ -f /etc/profile ] && [ "`stat --format '%U %G' /etc/profile`" == "root root" ] && source /etc/profile

prog="codedeploy-agent"
USER=""
AGENT_ROOT="/opt/codedeploy-agent/"
INSTALLER="/opt/codedeploy-agent/bin/install"
BIN="/opt/codedeploy-agent/bin/codedeploy-agent"

start() {
echo -n $"Starting $prog:"
cd $AGENT_ROOT
nohup $BIN start >/dev/null </dev/null 2>&1 # Try to start the server
if [ $USER ]; then
daemon --user=$USER $BIN start >/dev/null </dev/null 2>&1 # Try to start the server
else
nohup $BIN start >/dev/null </dev/null 2>&1 # Try to start the server
fi
exit $?
}

stop() {
echo -n $"Stopping $prog:"
cd $AGENT_ROOT
nohup $BIN stop >/dev/null </dev/null 2>&1 # Try to stop the server
if [ $USER ]; then
daemon --user=$USER $BIN stop >/dev/null </dev/null 2>&1 # Try to stop the server
else
nohup $BIN stop >/dev/null </dev/null 2>&1 # Try to stop the server
fi
exit $?
}

restart() {
echo -n $"Restarting $prog:"
cd $AGENT_ROOT
nohup $BIN restart >/dev/null </dev/null 2>&1 # Try to restart the server
if [ $USER ]; then
daemon --user=$USER $BIN restart >/dev/null </dev/null 2>&1 # Try to restart the server
else
nohup $BIN restart >/dev/null </dev/null 2>&1 # Try to restart the server
fi
exit $?
}

status() {
cd $AGENT_ROOT
$BIN status # Status of the server
if [ $USER ]; then
daemon --user=$USER $BIN status # Status of the server
else
$BIN status # Status of the server
fi
exit $?
}

update() {
echo -n $"Updating $prog:"
cd $AGENT_ROOT
$INSTALLER auto #Update the agent
if [ $USER ]; then
daemon --user=$USER sudo $INSTALLER auto #Update the agent
else
$INSTALLER auto #Update the agent
fi
}

case "$1" in
Expand Down
23 changes: 18 additions & 5 deletions lib/instance_agent/platform/linux_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ def self.supported_oses()
['linux']
end

def self.prepare_script_command(script, absolute_path)
script_command = absolute_path
if(!script.runas.nil?)
script_command = 'su ' + script.runas + ' -c ' + absolute_path
def self.prepare_script_command(script, absolute_cmd_path)
runas = !!script.runas
sudo = !!script.sudo

if runas && sudo
return 'sudo su ' + script.runas + ' -c ' + absolute_cmd_path
end

if runas && !sudo
return 'su ' + script.runas + ' -c ' + absolute_cmd_path
end
script_command

if !runas && sudo
return 'sudo ' + absolute_cmd_path
end

# If neither sudo or runas is specified, execute the
# command as the code deploy agent user
absolute_cmd_path
end

def self.quit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def parse_hooks(hooks_hash)
current_hook_scripts << InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ScriptInfo.new(script['location'].to_s.strip,
{
:runas => script.has_key?('runas') && !script['runas'].nil? ? script['runas'].to_s.strip : nil,
:sudo => script['sudo'],
:timeout => script['timeout']
})
else
Expand Down Expand Up @@ -140,4 +141,4 @@ def parse_context(context)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ module ApplicationSpecification
#Helper Class for storing data parsed from hook script maps
class ScriptInfo

attr_reader :location, :runas, :timeout
attr_reader :location, :runas, :sudo, :timeout
def initialize(location, opts = {})
location = location.to_s
if(location.empty?)
raise AppSpecValidationException, 'Scripts need a location value'
end
@location = location
@runas = opts[:runas]
@sudo = opts[:sudo]
@timeout = opts[:timeout] || 3600
@timeout = @timeout.to_i
if(@timeout <= 0)
Expand All @@ -24,4 +25,4 @@ def initialize(location, opts = {})
end
end
end
end
end
35 changes: 35 additions & 0 deletions test/instance_agent/platform/linux_util_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'test_helper'

class LinuxUtilTest < InstanceAgentTestCase
context 'Testing building command with sudo' do
setup do
@script_mock = Struct.new :sudo, :runas
end

should 'return command with sudo with runas user deploy' do
mock = @script_mock.new true, "deploy"
assert_equal 'sudo su deploy -c my_script.sh',
InstanceAgent::LinuxUtil.prepare_script_command(mock, "my_script.sh")
end

should 'return command without sudo with runas user deploy' do
mock = @script_mock.new nil, "deploy"
assert_equal 'su deploy -c my_script.sh',
InstanceAgent::LinuxUtil.prepare_script_command(mock, "my_script.sh")
end

should 'return command without sudo or runas user' do
mock = @script_mock.new nil, nil
assert_equal 'my_script.sh',
InstanceAgent::LinuxUtil.prepare_script_command(mock, "my_script.sh")
end

should 'return command with sudo' do
mock = @script_mock.new true, nil
assert_equal 'sudo my_script.sh',
InstanceAgent::LinuxUtil.prepare_script_command(mock, "my_script.sh")
end

end
end