Skip to content

Commit 0c44030

Browse files
armnejadcjinaws
authored andcommitted
1 parent 4bed1c3 commit 0c44030

File tree

2 files changed

+79
-33
lines changed

2 files changed

+79
-33
lines changed

Tools/src/update/linux/install.sh

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,58 @@ if [ "$1" == "register-managed-instance" ]; then
2020
fi
2121
fi
2222

23-
# allow ssm-agent to finish it's work
23+
# allow ssm-agent to finish its work
2424
sleep 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+
2672
if [[ $(/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
5590
elif [[ $(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
113136
else
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

Tools/src/update/linux/uninstall.sh

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,35 @@ function error_exit
1111
exit 1
1212
}
1313

14+
PACKAGE_MANAGER='rpm'
15+
16+
# sets PACKAGE_MANAGER value to name of package manager
17+
# that is passed in as long as it is present on the OS
18+
function check_binary
19+
{
20+
which $1 2>/dev/null
21+
RET_CODE=$?
22+
if [ ${RET_CODE} == 0 ];
23+
then
24+
PACKAGE_MANAGER=$1
25+
echo "Package manager found. Using ${PACKAGE_MANAGER} to install amazon-ssm-agent."
26+
fi
27+
}
28+
29+
check_binary yum
30+
if [ ${PACKAGE_MANAGER} == "yum" ];
31+
then
32+
UNINSTALL_COMMAND="yum -y remove amazon-ssm-agent"
33+
else
34+
UNINSTALL_COMMAND="rpm --erase amazon-ssm-agent"
35+
fi
36+
1437
if [[ $(/sbin/init --version 2> /dev/null) =~ upstart ]]; then
1538
echo "Checking if the agent is installed"
1639
if [ "$(rpm -q amazon-ssm-agent)" != "package amazon-ssm-agent is not installed" ]; then
1740
echo "-> Agent is installed in this instance"
18-
echo "Uninstalling the agent"
19-
rpm --erase amazon-ssm-agent
41+
echo "Uninstalling the agent using ${UNINSTALL_COMMAND}"
42+
${UNINSTALL_COMMAND}
2043
sleep 1
2144
else
2245
echo "-> Agent is not installed in this instance"
@@ -25,8 +48,8 @@ elif [[ $(systemctl 2> /dev/null) =~ -\.mount ]]; then
2548
echo "Checking if the agent is installed"
2649
if [[ "$(systemctl status amazon-ssm-agent.service)" != *"Loaded: not-found"* ]]; then
2750
echo "-> Agent is installed in this instance"
28-
echo "Uninstalling the agent"
29-
rpm --erase amazon-ssm-agent
51+
echo "Uninstalling the agent using ${UNINSTALL_COMMAND}"
52+
${UNINSTALL_COMMAND}
3053
sleep 1
3154
else
3255
echo "-> Agent is not installed in this instance"

0 commit comments

Comments
 (0)