Path: blob/develop/awscli/customizations/codedeploy/systems.py
1567 views
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import ctypes14import os15import subprocess16from awscli.utils import create_nested_client1718DEFAULT_CONFIG_FILE = 'codedeploy.onpremises.yml'192021class System:22UNSUPPORTED_SYSTEM_MSG = (23'Only Ubuntu Server, Red Hat Enterprise Linux Server and '24'Windows Server operating systems are supported.'25)2627def __init__(self, params):28self.session = params.session29self.s3 = create_nested_client(30self.session,31's3',32region_name=params.region33)3435def validate_administrator(self):36raise NotImplementedError('validate_administrator')3738def install(self, params):39raise NotImplementedError('install')4041def uninstall(self, params):42raise NotImplementedError('uninstall')434445class Windows(System):46CONFIG_DIR = r'C:\ProgramData\Amazon\CodeDeploy'47CONFIG_FILE = 'conf.onpremises.yml'48CONFIG_PATH = r'{0}\{1}'.format(CONFIG_DIR, CONFIG_FILE)49INSTALLER = 'codedeploy-agent.msi'5051def validate_administrator(self):52if not ctypes.windll.shell32.IsUserAnAdmin():53raise RuntimeError(54'You must run this command as an Administrator.'55)5657def install(self, params):58if 'installer' in params:59self.INSTALLER = params.installer6061process = subprocess.Popen(62[63'powershell.exe',64'-Command', 'Stop-Service',65'-Name', 'codedeployagent'66],67stdout=subprocess.PIPE,68stderr=subprocess.PIPE69)70(output, error) = process.communicate()71not_found = (72"Cannot find any service with service name 'codedeployagent'"73)74if process.returncode != 0 and not_found not in error:75raise RuntimeError(76'Failed to stop the AWS CodeDeploy Agent:\n{0}'.format(error)77)7879response = self.s3.get_object(Bucket=params.bucket, Key=params.key)80with open(self.INSTALLER, 'wb') as f:81f.write(response['Body'].read())8283subprocess.check_call(84[85r'.\{0}'.format(self.INSTALLER),86'/quiet',87'/l', r'.\codedeploy-agent-install-log.txt'88],89shell=True90)91subprocess.check_call([92'powershell.exe',93'-Command', 'Restart-Service',94'-Name', 'codedeployagent'95])9697process = subprocess.Popen(98[99'powershell.exe',100'-Command', 'Get-Service',101'-Name', 'codedeployagent'102],103stdout=subprocess.PIPE,104stderr=subprocess.PIPE105)106(output, error) = process.communicate()107if "Running" not in output:108raise RuntimeError(109'The AWS CodeDeploy Agent did not start after installation.'110)111112def uninstall(self, params):113process = subprocess.Popen(114[115'powershell.exe',116'-Command', 'Stop-Service',117'-Name', 'codedeployagent'118],119stdout=subprocess.PIPE,120stderr=subprocess.PIPE121)122(output, error) = process.communicate()123not_found = (124"Cannot find any service with service name 'codedeployagent'"125)126if process.returncode == 0:127self._remove_agent()128elif not_found not in error:129raise RuntimeError(130'Failed to stop the AWS CodeDeploy Agent:\n{0}'.format(error)131)132133def _remove_agent(self):134process = subprocess.Popen(135[136'wmic',137'product', 'where', 'name="CodeDeploy Host Agent"',138'call', 'uninstall', '/nointeractive'139],140stdout=subprocess.PIPE,141stderr=subprocess.PIPE142)143(output, error) = process.communicate()144if process.returncode != 0:145raise RuntimeError(146'Failed to uninstall the AWS CodeDeploy Agent:\n{0}'.format(147error148)149)150151152class Linux(System):153CONFIG_DIR = '/etc/codedeploy-agent/conf'154CONFIG_FILE = DEFAULT_CONFIG_FILE155CONFIG_PATH = '{0}/{1}'.format(CONFIG_DIR, CONFIG_FILE)156INSTALLER = 'install'157158def validate_administrator(self):159if os.geteuid() != 0:160raise RuntimeError('You must run this command as sudo.')161162def install(self, params):163if 'installer' in params:164self.INSTALLER = params.installer165166self._update_system(params)167self._stop_agent(params)168169response = self.s3.get_object(Bucket=params.bucket, Key=params.key)170with open(self.INSTALLER, 'wb') as f:171f.write(response['Body'].read())172173subprocess.check_call(174['chmod', '+x', './{0}'.format(self.INSTALLER)]175)176177credentials = self.session.get_credentials()178environment = os.environ.copy()179environment['AWS_REGION'] = params.region180environment['AWS_ACCESS_KEY_ID'] = credentials.access_key181environment['AWS_SECRET_ACCESS_KEY'] = credentials.secret_key182if credentials.token is not None:183environment['AWS_SESSION_TOKEN'] = credentials.token184subprocess.check_call(185['./{0}'.format(self.INSTALLER), 'auto'],186env=environment187)188189def uninstall(self, params):190process = self._stop_agent(params)191if process.returncode == 0:192self._remove_agent(params)193194def _update_system(self, params):195raise NotImplementedError('preinstall')196197def _remove_agent(self, params):198raise NotImplementedError('remove_agent')199200def _stop_agent(self, params):201process = subprocess.Popen(202['service', 'codedeploy-agent', 'stop'],203stdout=subprocess.PIPE,204stderr=subprocess.PIPE205)206(output, error) = process.communicate()207if process.returncode != 0 and params.not_found_msg not in error:208raise RuntimeError(209'Failed to stop the AWS CodeDeploy Agent:\n{0}'.format(error)210)211return process212213214class Ubuntu(Linux):215def _update_system(self, params):216subprocess.check_call(['apt-get', '-y', 'update'])217subprocess.check_call(['apt-get', '-y', 'install', 'ruby2.0'])218219def _remove_agent(self, params):220subprocess.check_call(['dpkg', '-r', 'codedeploy-agent'])221222def _stop_agent(self, params):223params.not_found_msg = 'codedeploy-agent: unrecognized service'224return Linux._stop_agent(self, params)225226227class RHEL(Linux):228def _update_system(self, params):229subprocess.check_call(['yum', '-y', 'install', 'ruby'])230231def _remove_agent(self, params):232subprocess.check_call(['yum', '-y', 'erase', 'codedeploy-agent'])233234def _stop_agent(self, params):235params.not_found_msg = 'Redirecting to /bin/systemctl stop codedeploy-agent.service'236return Linux._stop_agent(self, params)237238239