#!/usr/bin/env ruby $:.unshift File.join(File.dirname(File.expand_path('..', __FILE__)), 'lib') Gem.use_paths(nil, Gem.path << "/opt/codedeploy-agent/vendor") Gem.use_paths(nil, Gem.path << File.join(File.dirname(File.expand_path('..', __FILE__)), 'vendor')) require 'docopt' require 'aws/codedeploy/local/deployer' require 'instance_agent' require 'instance_agent/config' # Initialize the deployer first to initialize the configuration values so they can be used as part of the help message begin AWS::CodeDeploy::Local::Deployer.new rescue # If we fail to initialize all the configuration values correctly just grab the default location for the config for the help message InstanceAgent::Config.config[:root_dir] = AWS::CodeDeploy::Local::Deployer::CONF_DEFAULT_LOCATION end doc = <] [--type ] [--file-exists-behavior ] [--deployment-group ] [--deployment-group-name ] [--application-name ] [--events ] [--agent-configuration-file ] [--appspec-filename ] Options ******* -l, --bundle-location The location of the application revision bundle. If you do not specify a location, the tool uses the directory you are currently working in by default. [default: #{Dir.pwd}] -t, --type The format of the application revision bundle. Supported types include tgz, tar, zip, and directory. If you do not specify a type, the tool uses directory by default. If you specify --type, you must also specify --bundle-location. [default: directory] -b, --file-exists-behavior Indicates how files are handled that already exist in a deployment target location but weren't part of a previous successful deployment. Options include DISALLOW, OVERWRITE, RETAIN. [default: #{InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR}]. Note: this setting can be overriden during individual deployments using the appspec file, which takes precedence over this option setting during that deployment installation. See also: "create-deployment" in the AWS CLI Reference for AWS CodeDeploy. -g, --deployment-group The path to the folder that is the target location for the content to be deployed. If you do not specify a folder, the tool creates one named default-local-deployment-group inside your deployment root directory. For each local deployment you create, the tool creates a subdirectory inside this folder with names like d-98761234-local. Your configuration shows it would be placed in #{InstanceAgent::Config.config[:root_dir]}/. You can use the :root_dir: variable in an agent configuration file to configure a custom deployment root folder. If you want to deploy to a location previously deployed to by AWS CodeDeploy, you must specify its deployment group ID, which you can look up by using the AWS CLI. [default: #{AWS::CodeDeploy::Local::Deployer::DEFAULT_DEPLOYMENT_GROUP_ID}]. See also: "get-deployment-group” in the AWS CLI Reference for AWS CodeDeploy. -d, --deployment-group-name Indicates the deployment group name to be used during the CodeDeploy hook execution, can be accessed on the DEPLOYMENT_GROUP_NAME environment variable. If you do not specify a name, "LocalFleet" will be used. -a, --application-name Indicates the application name to be used during the CodeDeploy hook execution, can be accessed on the APPLICATION_NAME environment variable. If you do not specify a name, the "bundle-location" will be used. -e, --events A set of lifecycle event hooks you want to run, in order, instead of the events listed in the AppSpec file. Multiple hook names must be separated by commas. If you don't specify DownloadBundle and Install events in the --events list, they will run before all the event hooks you do specify. If you include DownloadBundle and Install in the --events list, they can be preceded only by events that normally run before them in AWS CodeDeploy deployments. -c, --agent-configuration-file The location of a configuration file to use for the deployment, if you store it in a location other than the default. A configuration file specifies alternatives to other default values and behaviors for a deployment. By default, configuration files are stored as /etc/codedeploy-agent/conf/codedeployagent.yml (Amazon Linux, RHEL, or Ubuntu Server instances) or C:/ProgramData/Amazon/CodeDeploy/conf.yml (Windows Server). -A, --appspec-filename The name of the appspec file to use for the deployment, if you set it to a name other than the default. By default, appspec files are named "appspec.yml" or "appspec.yaml". -h, --help Displays a summary of help content. -v, --version Displays the tool's version number. Examples ******** #{__FILE__} #{__FILE__} --bundle-location /path/to/local/bundle/directory #{__FILE__} --bundle-location /path/to/local/bundle.tgz --type tgz --deployment-group my-deployment-group #{__FILE__} --bundle-location /path/to/local/bundle/directory --type directory --deployment-group my-deployment-group Deploy a bundle from Amazon S3 ************************** #{__FILE__} --bundle-location s3://mybucket/bundle.zip?versionId=1234&etag=47e8 --type zip Deploy a bundle from a public GitHub repository ************************************************** #{__FILE__} --bundle-location https://github.com/awslabs/aws-codedeploy-sample-tomcat --type zip #{__FILE__} --bundle-location https://api.github.com/repos/awslabs/aws-codedeploy-sample-tomcat/zipball/master --type zip Deploy a bundle specifying certain lifecycle events ************************************************************ #{__FILE__} --bundle-location /path/to/local/bundle.tar --type tar --deployment-group my-deployment-group --events ApplicationStop,DownloadBundle,Install,ApplicationStart Stop a previously deployed application **************************************** #{__FILE__} --bundle-location /path/to/local/bundle.tgz --type tgz --deployment-group 1234abcd-5dd1-4774-89c6-30b107ac5dcas --events ApplicationStop Deploy using a specific deployment group ID ************************************************************************ #{__FILE__} --bundle-location C:/path/to/local/bundle/directory --deployment-group 1234abcd-5dd1-4774-89c6-30b107ac5dca #{__FILE__} --bundle-location C:/path/to/local/bundle.zip --type zip --deployment-group 1234abcd-5dd1-4774-89c6-30b107ac5dca DOCOPT begin args = Docopt::docopt(doc, version: '1.0') AWS::CodeDeploy::Local::Deployer.new(args['--agent-configuration-file']).execute_events(args) rescue Docopt::Exit => e puts e.message exit(false) rescue AWS::CodeDeploy::Local::CLIValidator::ValidationError,SystemCallError,ArgumentError => e puts "ERROR: #{e.message}" exit(false) rescue Aws::Errors::MissingCredentialsError => e puts "ERROR: We were unable to download content from the Amazon S3 bucket due to missing or invalid credentials. For information about specifying your AWS credentials, see https://aws.amazon.com/blogs/security/a-new-and-standardized-way-to-manage-credentials-in-the-aws-sdks" exit(false) rescue InstanceAgent::Plugins::CodeDeployPlugin::ScriptError => e exit(false) end puts "AppSpec file valid. Local deployment successful"