forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystems.py
More file actions
235 lines (197 loc) · 7.48 KB
/
systems.py
File metadata and controls
235 lines (197 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import ctypes
import os
import subprocess
DEFAULT_CONFIG_FILE = 'codedeploy.onpremises.yml'
class System:
UNSUPPORTED_SYSTEM_MSG = (
'Only Ubuntu Server, Red Hat Enterprise Linux Server and '
'Windows Server operating systems are supported.'
)
def __init__(self, params):
self.session = params.session
self.s3 = self.session.create_client(
's3',
region_name=params.region
)
def validate_administrator(self):
raise NotImplementedError('validate_administrator')
def install(self, params):
raise NotImplementedError('install')
def uninstall(self, params):
raise NotImplementedError('uninstall')
class Windows(System):
CONFIG_DIR = r'C:\ProgramData\Amazon\CodeDeploy'
CONFIG_FILE = 'conf.onpremises.yml'
CONFIG_PATH = r'{0}\{1}'.format(CONFIG_DIR, CONFIG_FILE)
INSTALLER = 'codedeploy-agent.msi'
def validate_administrator(self):
if not ctypes.windll.shell32.IsUserAnAdmin():
raise RuntimeError(
'You must run this command as an Administrator.'
)
def install(self, params):
if 'installer' in params:
self.INSTALLER = params.installer
process = subprocess.Popen(
[
'powershell.exe',
'-Command', 'Stop-Service',
'-Name', 'codedeployagent'
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(output, error) = process.communicate()
not_found = (
"Cannot find any service with service name 'codedeployagent'"
)
if process.returncode != 0 and not_found not in error:
raise RuntimeError(
'Failed to stop the AWS CodeDeploy Agent:\n{0}'.format(error)
)
response = self.s3.get_object(Bucket=params.bucket, Key=params.key)
with open(self.INSTALLER, 'wb') as f:
f.write(response['Body'].read())
subprocess.check_call(
[
r'.\{0}'.format(self.INSTALLER),
'/quiet',
'/l', r'.\codedeploy-agent-install-log.txt'
],
shell=True
)
subprocess.check_call([
'powershell.exe',
'-Command', 'Restart-Service',
'-Name', 'codedeployagent'
])
process = subprocess.Popen(
[
'powershell.exe',
'-Command', 'Get-Service',
'-Name', 'codedeployagent'
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(output, error) = process.communicate()
if "Running" not in output:
raise RuntimeError(
'The AWS CodeDeploy Agent did not start after installation.'
)
def uninstall(self, params):
process = subprocess.Popen(
[
'powershell.exe',
'-Command', 'Stop-Service',
'-Name', 'codedeployagent'
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(output, error) = process.communicate()
not_found = (
"Cannot find any service with service name 'codedeployagent'"
)
if process.returncode == 0:
self._remove_agent()
elif not_found not in error:
raise RuntimeError(
'Failed to stop the AWS CodeDeploy Agent:\n{0}'.format(error)
)
def _remove_agent(self):
process = subprocess.Popen(
[
'wmic',
'product', 'where', 'name="CodeDeploy Host Agent"',
'call', 'uninstall', '/nointeractive'
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(output, error) = process.communicate()
if process.returncode != 0:
raise RuntimeError(
'Failed to uninstall the AWS CodeDeploy Agent:\n{0}'.format(
error
)
)
class Linux(System):
CONFIG_DIR = '/etc/codedeploy-agent/conf'
CONFIG_FILE = DEFAULT_CONFIG_FILE
CONFIG_PATH = '{0}/{1}'.format(CONFIG_DIR, CONFIG_FILE)
INSTALLER = 'install'
def validate_administrator(self):
if os.geteuid() != 0:
raise RuntimeError('You must run this command as sudo.')
def install(self, params):
if 'installer' in params:
self.INSTALLER = params.installer
self._update_system(params)
self._stop_agent(params)
response = self.s3.get_object(Bucket=params.bucket, Key=params.key)
with open(self.INSTALLER, 'wb') as f:
f.write(response['Body'].read())
subprocess.check_call(
['chmod', '+x', './{0}'.format(self.INSTALLER)]
)
credentials = self.session.get_credentials()
environment = os.environ.copy()
environment['AWS_REGION'] = params.region
environment['AWS_ACCESS_KEY_ID'] = credentials.access_key
environment['AWS_SECRET_ACCESS_KEY'] = credentials.secret_key
if credentials.token is not None:
environment['AWS_SESSION_TOKEN'] = credentials.token
subprocess.check_call(
['./{0}'.format(self.INSTALLER), 'auto'],
env=environment
)
def uninstall(self, params):
process = self._stop_agent(params)
if process.returncode == 0:
self._remove_agent(params)
def _update_system(self, params):
raise NotImplementedError('preinstall')
def _remove_agent(self, params):
raise NotImplementedError('remove_agent')
def _stop_agent(self, params):
process = subprocess.Popen(
['service', 'codedeploy-agent', 'stop'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(output, error) = process.communicate()
if process.returncode != 0 and params.not_found_msg not in error:
raise RuntimeError(
'Failed to stop the AWS CodeDeploy Agent:\n{0}'.format(error)
)
return process
class Ubuntu(Linux):
def _update_system(self, params):
subprocess.check_call(['apt-get', '-y', 'update'])
subprocess.check_call(['apt-get', '-y', 'install', 'ruby2.0'])
def _remove_agent(self, params):
subprocess.check_call(['dpkg', '-r', 'codedeploy-agent'])
def _stop_agent(self, params):
params.not_found_msg = 'codedeploy-agent: unrecognized service'
return Linux._stop_agent(self, params)
class RHEL(Linux):
def _update_system(self, params):
subprocess.check_call(['yum', '-y', 'install', 'ruby'])
def _remove_agent(self, params):
subprocess.check_call(['yum', '-y', 'erase', 'codedeploy-agent'])
def _stop_agent(self, params):
params.not_found_msg = 'Redirecting to /bin/systemctl stop codedeploy-agent.service'
return Linux._stop_agent(self, params)