@@ -20,15 +20,58 @@ if [ "$1" == "register-managed-instance" ]; then
2020 fi
2121fi
2222
23- # allow ssm-agent to finish it's work
23+ # allow ssm-agent to finish its work
2424sleep 2
2525
26+ PACKAGE_MANAGER=' rpm'
27+
28+ # sets PACKAGE_MANAGER value to name of package manager
29+ # that is passed in as long as it is present on the OS
30+ function check_binary
31+ {
32+ which $1 2> /dev/null
33+ RET_CODE=$?
34+ if [ ${RET_CODE} == 0 ];
35+ then
36+ PACKAGE_MANAGER=$1
37+ echo " Package manager found. Using ${PACKAGE_MANAGER} to install amazon-ssm-agent."
38+ fi
39+ }
40+
41+ check_binary yum
42+ if [ ${PACKAGE_MANAGER} == " yum" ];
43+ then
44+ INSTALL_COMMAND=" yum -y localinstall amazon-ssm-agent.rpm"
45+ else
46+ INSTALL_COMMAND=" rpm -U amazon-ssm-agent.rpm"
47+ fi
48+
49+ function install_agent
50+ {
51+ echo " Installing agent using ${INSTALL_COMMAND} ."
52+ pmOutput=$( ${INSTALL_COMMAND} 2>&1 )
53+ pmExit=$?
54+ echo " Package Manager Output: $pmOutput "
55+ }
56+
57+ function check_error_code
58+ {
59+ if [ " $pmExit " -ne 0 ]; then
60+ # messages returned by rpm / yum when trying to install package that is already installed
61+ if [[ $pmOutput == * " is already installed" * ]] || [[ $pmOutput == * " does not update installed package" * ]]; then
62+ echo " Install was successful"
63+ exit 0
64+ fi
65+
66+ echo " Package manager failed with exit code '$pmExit '"
67+ echo " Package manager output: $pmOutput "
68+ exit 125
69+ fi
70+ }
71+
2672if [[ $( /sbin/init --version 2> /dev/null) =~ upstart ]]; then
2773 echo " upstart detected"
28- echo " Installing agent"
29- pmOutput=$( rpm -U amazon-ssm-agent.rpm 2>&1 )
30- pmExit=$?
31- echo " RPM Output: $pmOutput "
74+ install_agent
3275
3376 if [ " $DO_REGISTER " = true ]; then
3477 /sbin/stop amazon-ssm-agent
@@ -42,16 +85,8 @@ if [[ $(/sbin/init --version 2> /dev/null) =~ upstart ]]; then
4285 /sbin/start amazon-ssm-agent
4386 status amazon-ssm-agent
4487
45- if [ " $pmExit " -ne 0 ]; then
46- if [[ $pmOutput == * " is already installed" * ]]; then
47- echo " Install was successfull"
48- exit 0
49- fi
88+ check_error_code
5089
51- echo " Package manager failed with exit code '$pmExit '"
52- echo " Package manager output: $pmOutput "
53- exit 125
54- fi
5590elif [[ $( systemctl 2> /dev/null) =~ -\. mount ]]; then
5691 if [[ " $( systemctl is-active amazon-ssm-agent.service) " == " active" ]]; then
5792 echo " -> Agent is running in the instance"
@@ -66,10 +101,7 @@ elif [[ $(systemctl 2> /dev/null) =~ -\.mount ]]; then
66101
67102 originalSvc=$( systemctl show -p FragmentPath amazon-ssm-agent.service)
68103
69- echo " Installing agent"
70- pmOutput=$( rpm -U amazon-ssm-agent.rpm 2>&1 )
71- pmExit=$?
72- echo " RPM Output: $pmOutput "
104+ install_agent
73105
74106 updatedSvc=$( systemctl show -p FragmentPath amazon-ssm-agent.service)
75107 if ! [ -z " $originalSvc " ] && ! [ -z " $updatedSvc " ]; then
@@ -100,17 +132,8 @@ elif [[ $(systemctl 2> /dev/null) =~ -\.mount ]]; then
100132 systemctl start amazon-ssm-agent.service
101133 systemctl status amazon-ssm-agent.service
102134
103- if [ " $pmExit " -ne 0 ]; then
104- if [[ $pmOutput == * " is already installed" * ]]; then
105- echo " Install was successful"
106- exit 0
107- fi
108-
109- echo " Package manager failed with exit code '$pmExit '"
110- echo " Package manager output: $pmOutput "
111- exit 125
112- fi
135+ check_error_code
113136else
114137 echo " The amazon-ssm-agent is not supported on this platform. Please visit the documentation for the list of supported platforms" 1>&2
115138 exit 124
116- fi
139+ fi
0 commit comments