|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +$:.unshift File.join(File.dirname(File.expand_path('..', __FILE__)), 'lib') |
| 4 | +require 'docopt' |
| 5 | + |
| 6 | +require 'aws/codedeploy/local/deployer' |
| 7 | + |
| 8 | +# Initialize the deployer first to initialize the configuration values so they can be used as part of the help message |
| 9 | +begin |
| 10 | + AWS::CodeDeploy::Local::Deployer.new |
| 11 | +rescue |
| 12 | + # If we fail to initialize all the configuration values correctly just grab the default location for the config for the help message |
| 13 | + InstanceAgent::Config.config[:root_dir] = AWS::CodeDeploy::Local::Deployer::CONF_DEFAULT_LOCATION |
| 14 | +end |
| 15 | + |
| 16 | +doc = <<DOCOPT |
| 17 | +CodeDeploy Local Deployments. |
| 18 | +
|
| 19 | +Usage: |
| 20 | + #{__FILE__} [--bundle-location <location>] [--type <type>] [--application-folder <application-folder>|--deployment-group-id <deployment-group-id>] [--configuration-file <configuration-file>] [--event <event>]... |
| 21 | + #{__FILE__} -h | --help |
| 22 | + #{__FILE__} -v | --version |
| 23 | +
|
| 24 | +Options: |
| 25 | + -l, --bundle-location <location> Optional Bundle Location. The prefix and suffix determine whether this location is locally accessible or online (s3 or github). Defaults to current directory [default: #{Dir.pwd}] |
| 26 | + -t, --type <type> Optional Bundle type choice from tgz, tar, zip, or directory [default: directory] |
| 27 | + -g, --application-folder <application-folder> Optional application folder specifies the folder in which the local deployments will be executed [default: #{AWS::CodeDeploy::Local::Deployer::DEFAULT_DEPLOYMENT_GROUP_ID}]. Your configuration shows it would be placed in #{InstanceAgent::Config.config[:root_dir]}/<application-folder>. To configure a custom deployment-root folder see https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-agent-configuration.html. If you want to target a previously deployed revision make sure to use the correct deployment group id by looking it up using the AWS CLI: https://docs.aws.amazon.com/cli/latest/reference/deploy/get-deployment-group.html |
| 28 | + -g, --deployment-group-id <deployment-group-id> Optional Deployment Group Id - an alternative to application-folder. The application-folder used during a CodeDeploy deployment is the deployment group's id. Only one can be specified. |
| 29 | + -e, --event <event> Optional set of override lifecycle events to run. Any number of lifecycle events can be provided one after another (order matters). If none specificed runs only default events found in the Appspec file using CodeDeploy's ordering. Please note if you don't specify DownloadBundle and Install events they will always precede all your custom events. Those events extract your local bundle / download it as well as install the revision in the correct location. The only events that can run before them are the ones that do so today. See https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html |
| 30 | + -c, --configuration-file <configuration-file> Optional agent configuration file location which is by default set to /etc/codedeploy-agent/conf/codedeployagent.yml for linux hosts and C:/ProgramData/Amazon/CodeDeploy/conf.yml for Windows. See https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-agent-configuration.html |
| 31 | + -h, --help Show this message. |
| 32 | + -v, --version Show version. |
| 33 | +
|
| 34 | +Examples: |
| 35 | + #{__FILE__} |
| 36 | + #{__FILE__} --bundle-location /path/to/local/bundle/dir |
| 37 | + #{__FILE__} --bundle-location s3://mybucket/bundle.tgz --type tgz |
| 38 | + #{__FILE__} --bundle-location /path/to/local/bundle.tgz --type tgz --application-folder local-deployments |
| 39 | + #{__FILE__} --bundle-location /path/to/local/bundle.zip --type zip --deployment-group-id 217ba5c8-5dd1-4774-89c6-30b107ac5dca |
| 40 | + #{__FILE__} --bundle-location s3://mybucket/bundle.zip?versionId=1234&etag=47e8 --type tgz |
| 41 | + #{__FILE__} --bundle-location https://api.github.com/repos/awslabs/aws-codedeploy-sample-tomcat/zipball/master --type zip |
| 42 | + #{__FILE__} --bundle-location /path/to/local/bundle.tgz --type tgz -e ApplicationStop -e DownloadBundle -e Install -e ApplicationStart -e HealthCheck -e CustomHook |
| 43 | +
|
| 44 | + Stop Previously Deployed Application: |
| 45 | + #{__FILE__} --bundle-location s3://mybucket/bundle.tgz --type tgz --deployment-group-id 217ba5c8-5dd1-4774-89c6-30b107ac5dca -e ApplicationStop |
| 46 | + #{__FILE__} --bundle-location /path/to/local/bundle.zip --type zip --deployment-group-id 217ba5c8-5dd1-4774-89c6-30b107ac5dca -e ApplicationStop |
| 47 | +
|
| 48 | +Specifying AWS Credentials: |
| 49 | + #{__FILE__} allows you to provide your aws access key, secret key, and region in multiple ways. It uses the AWS SDK. |
| 50 | + This means that if you're using an ec2 host with proper roles configured for the host with proper permissions you don't need to do anything else |
| 51 | + to give permission for this host to download your bundle from S3. See https://docs.aws.amazon.com/codedeploy/latest/userguide/getting-started-create-iam-instance-profile.html |
| 52 | + The AWS SDK also provides a few other ways to provide credentials. See https://docs.aws.amazon.com/sdkforruby/api/index.html |
| 53 | +
|
| 54 | +DOCOPT |
| 55 | + |
| 56 | +begin |
| 57 | + args = Docopt::docopt(doc, version: '1.0') |
| 58 | + AWS::CodeDeploy::Local::Deployer.new(args['--configuration-file']).execute_events(args) |
| 59 | +rescue Docopt::Exit => e |
| 60 | + puts e.message |
| 61 | + exit(false) |
| 62 | +rescue AWS::CodeDeploy::Local::CLIValidator::ValidationError,InstanceAgent::Plugins::CodeDeployPlugin::ScriptError,InstanceMetadata::InstanceMetadataError,SystemCallError => e |
| 63 | + puts "ERROR: #{e.message}" |
| 64 | + exit(false) |
| 65 | +rescue Aws::Errors::MissingCredentialsError => e |
| 66 | + puts "ERROR: Unable to download from S3 without AWS Credentials set. Please see help message for how to set AWS Credentials (#{__FILE__} --help)" |
| 67 | + exit(false) |
| 68 | +end |
| 69 | + |
| 70 | +puts "Successfully deployed your bundle locally" |
0 commit comments