diff --git a/init.d/codedeploy-agent b/init.d/codedeploy-agent index d4eadf42..33849406 100755 --- a/init.d/codedeploy-agent +++ b/init.d/codedeploy-agent @@ -17,11 +17,14 @@ # the deployment artifacts on to this instance. ### END INIT INFO +# Source function library. +. /etc/rc.d/init.d/functions 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" @@ -29,34 +32,54 @@ BIN="/opt/codedeploy-agent/bin/codedeploy-agent" start() { echo -n $"Starting $prog:" cd $AGENT_ROOT - nohup $BIN start >/dev/null &1 # Try to start the server + if [ $USER ]; 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 + fi exit $? } stop() { echo -n $"Stopping $prog:" cd $AGENT_ROOT - nohup $BIN stop >/dev/null &1 # Try to stop the server + if [ $USER ]; 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 + fi exit $? } restart() { echo -n $"Restarting $prog:" cd $AGENT_ROOT - nohup $BIN restart >/dev/null &1 # Try to restart the server + if [ $USER ]; 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 + 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 diff --git a/lib/instance_agent/platform/linux_util.rb b/lib/instance_agent/platform/linux_util.rb index e0e59b65..402f308d 100644 --- a/lib/instance_agent/platform/linux_util.rb +++ b/lib/instance_agent/platform/linux_util.rb @@ -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() diff --git a/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb b/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb index 61a6b31e..b3d7d751 100644 --- a/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb +++ b/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb @@ -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 @@ -140,4 +141,4 @@ def parse_context(context) end end end -end \ No newline at end of file +end diff --git a/lib/instance_agent/plugins/codedeploy/application_specification/script_info.rb b/lib/instance_agent/plugins/codedeploy/application_specification/script_info.rb index 526751b8..95ce6354 100644 --- a/lib/instance_agent/plugins/codedeploy/application_specification/script_info.rb +++ b/lib/instance_agent/plugins/codedeploy/application_specification/script_info.rb @@ -5,7 +5,7 @@ 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?) @@ -13,6 +13,7 @@ def initialize(location, opts = {}) end @location = location @runas = opts[:runas] + @sudo = opts[:sudo] @timeout = opts[:timeout] || 3600 @timeout = @timeout.to_i if(@timeout <= 0) @@ -24,4 +25,4 @@ def initialize(location, opts = {}) end end end -end \ No newline at end of file +end diff --git a/test/instance_agent/platform/linux_util_test.rb b/test/instance_agent/platform/linux_util_test.rb new file mode 100644 index 00000000..c51c959b --- /dev/null +++ b/test/instance_agent/platform/linux_util_test.rb @@ -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 +