Class: Moonshot::DeploymentMechanism::CodeDeploy

Inherits:
Object
  • Object
show all
Includes:
CredsHelper, Moonshot::DoctorHelper, ResourcesHelper
Defined in:
lib/moonshot/deployment_mechanism/code_deploy.rb

Overview

This mechanism is used to deploy software to an auto-scaling group within a stack. It currently only works with the S3Bucket ArtifactRepository.

Usage: class MyApp < Moonshot::CLI

self.artifact_repository = S3Bucket.new('foobucket')
self.deployment_mechanism = CodeDeploy.new(asg: 'AutoScalingGroup')

end

Constant Summary collapse

DEFAULT_ROLE_NAME =
'CodeDeployRole'

Instance Attribute Summary

Attributes included from ResourcesHelper

#resources

Instance Method Summary collapse

Methods included from Moonshot::DoctorHelper

#doctor_hook

Methods included from CredsHelper

#as_client, #cd_client, #cf_client, #ec2_client, #iam_client, #s3_client

Constructor Details

#initialize(asg: [], optional_asg: [], role: DEFAULT_ROLE_NAME, app_name: nil, group_name: nil, config_name: 'CodeDeployDefault.OneAtATime') ⇒ CodeDeploy

rubocop:disable Metrics/ParameterLists

Parameters:

  • asg (Array, String) (defaults to: [])

    The logical name of the AutoScalingGroup to create and manage a Deployment Group for in CodeDeploy.

  • optional_asg (Array, String) (defaults to: [])

    The logical name of the AutoScalingGroup to create and manage a Deployment Group for in CodeDeploy. This ASG doesn’t have to exist. If it does, it will be added to the Deployment Group.

  • role (String) (defaults to: DEFAULT_ROLE_NAME)

    IAM role with AWSCodeDeployRole policy. CodeDeployRole is considered as default role if its not specified.

  • app_name (String, nil) (defaults to: nil)

    (nil) The name of the CodeDeploy Application. By default, this is the same as the stack name, and probably what you want. If you have multiple deployments in a single Stack, they must have unique names.

  • group_name (String, nil) (defaults to: nil)

    (nil) The name of the CodeDeploy Deployment Group. By default, this is the same as app_name.

  • config_name (String) (defaults to: 'CodeDeployDefault.OneAtATime')

    Name of the Deployment Config to use for CodeDeploy, By default we use CodeDeployDefault.OneAtATime.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 41

def initialize(
  asg: [],
  optional_asg: [],
  role: DEFAULT_ROLE_NAME,
  app_name: nil,
  group_name: nil,
  config_name: 'CodeDeployDefault.OneAtATime'
)
  @asg_logical_ids = Array(asg)
  @optional_asg_logical_ids = Array(optional_asg)
  @app_name = app_name
  @group_name = group_name
  @codedeploy_role = role
  @codedeploy_config = config_name
  @ignore_app_stop_failures = false
end

Instance Method Details

#deploy_cli_hook(parser) ⇒ Object Also known as: push_cli_hook



107
108
109
110
111
112
113
114
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 107

def deploy_cli_hook(parser)
  parser.on('--ignore-app-stop-failures', TrueClass, 'Continue deployment on ApplicationStop failures') do |v|
    puts "ignore = #{v}"
    @ignore_app_stop_failures = v
  end

  parser
end

#deploy_hook(artifact_repo, version_name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 82

def deploy_hook(artifact_repo, version_name)
  success = true
  deployment_id = nil

  ilog.start_threaded 'Creating Deployment' do |s|
    res = create_deployment(artifact_repo, version_name)
    deployment_id = res.deployment_id
    s.continue "Created Deployment #{deployment_id.blue}."
    success = wait_for_deployment(deployment_id, s)
  end

  handle_deployment_failure(deployment_id) unless success
end

#post_create_hookObject

rubocop:enable Metrics/ParameterLists



59
60
61
62
63
64
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 59

def post_create_hook
  create_application_if_needed
  create_deployment_group_if_needed

  wait_for_asg_capacity
end

#post_delete_hookObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 96

def post_delete_hook
  ilog.start 'Cleaning up CodeDeploy Application' do |s|
    if application_exists?
      cd_client.delete_application(application_name: app_name)
      s.success "Deleted CodeDeploy Application '#{app_name}'."
    else
      s.success "CodeDeploy Application '#{app_name}' does not exist."
    end
  end
end

#post_update_hookObject



66
67
68
69
70
71
72
73
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 66

def post_update_hook
  post_create_hook

  unless deployment_group_ok? # rubocop:disable Style/GuardClause
    delete_deployment_group
    create_deployment_group_if_needed
  end
end

#status_hookObject



75
76
77
78
79
80
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 75

def status_hook
  t = Moonshot::UnicodeTable.new('')
  application = t.add_leaf("CodeDeploy Application: #{app_name}")
  application.add_line(code_deploy_status_msg)
  t.draw_children
end