Path: blob/develop/awscli/customizations/codedeploy/install.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 errno14import os15import shutil16import sys1718from awscli.customizations.commands import BasicCommand19from awscli.customizations.codedeploy.utils import \20validate_region, validate_s3_location, validate_instance212223class Install(BasicCommand):24NAME = 'install'2526DESCRIPTION = (27'Configures and installs the AWS CodeDeploy Agent on the on-premises '28'instance.'29)3031ARG_TABLE = [32{33'name': 'config-file',34'synopsis': '--config-file <path>',35'required': True,36'help_text': (37'Required. The path to the on-premises instance configuration '38'file.'39)40},41{42'name': 'override-config',43'action': 'store_true',44'default': False,45'help_text': (46'Optional. Overrides the on-premises instance configuration '47'file.'48)49},50{51'name': 'agent-installer',52'synopsis': '--agent-installer <s3-location>',53'required': False,54'help_text': (55'Optional. The AWS CodeDeploy Agent installer file.'56)57}58]5960def _run_main(self, parsed_args, parsed_globals):61params = parsed_args62params.session = self._session63validate_region(params, parsed_globals)64validate_instance(params)65params.system.validate_administrator()66self._validate_override_config(params)67self._validate_agent_installer(params)6869try:70self._create_config(params)71self._install_agent(params)72except Exception as e:73sys.stdout.flush()74sys.stderr.write(75'ERROR\n'76'{0}\n'77'Install the AWS CodeDeploy Agent on the on-premises instance '78'by following the instructions in "Configure Existing '79'On-Premises Instances by Using AWS CodeDeploy" in the AWS '80'CodeDeploy User Guide.\n'.format(e)81)8283def _validate_override_config(self, params):84if os.path.isfile(params.system.CONFIG_PATH) and \85not params.override_config:86raise RuntimeError(87'The on-premises instance configuration file already exists. '88'Specify --override-config to update the existing on-premises '89'instance configuration file.'90)9192def _validate_agent_installer(self, params):93validate_s3_location(params, 'agent_installer')94if 'bucket' not in params:95params.bucket = 'aws-codedeploy-{0}'.format(params.region)96if 'key' not in params:97params.key = 'latest/{0}'.format(params.system.INSTALLER)98params.installer = params.system.INSTALLER99else:100start = params.key.rfind('/') + 1101params.installer = params.key[start:]102103def _create_config(self, params):104sys.stdout.write(105'Creating the on-premises instance configuration file... '106)107try:108os.makedirs(params.system.CONFIG_DIR)109except OSError as e:110if e.errno != errno.EEXIST:111raise e112if params.config_file != params.system.CONFIG_PATH:113shutil.copyfile(params.config_file, params.system.CONFIG_PATH)114sys.stdout.write('DONE\n')115116def _install_agent(self, params):117sys.stdout.write('Installing the AWS CodeDeploy Agent... ')118params.system.install(params)119sys.stdout.write('DONE\n')120121122