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