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

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: [], role: 'CodeDeployRole', app_name: nil, group_name: nil, config_name: 'CodeDeployDefault.OneAtATime') ⇒ CodeDeploy

Returns a new instance of CodeDeploy.

Parameters:

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

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

  • role (String) (defaults to: 'CodeDeployRole')

    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.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 32

def initialize(
    asg: [],
    role: 'CodeDeployRole',
    app_name: nil,
    group_name: nil,
    config_name: 'CodeDeployDefault.OneAtATime')
  @asg_logical_ids = asg.is_a?(Array) ? asg : [asg]
  @app_name = app_name
  @group_name = group_name
  @codedeploy_role = role
  @codedeploy_config = config_name
end

Instance Method Details

#deploy_hook(artifact_repo, version_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 68

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

  ilog.start_threaded 'Creating Deployment' do |s|
    res = cd_client.create_deployment(
      application_name: app_name,
      deployment_group_name: group_name,
      revision: revision_for_artifact_repo(artifact_repo, version_name),
      deployment_config_name: @codedeploy_config,
      description: "Deploying version #{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



45
46
47
48
49
50
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 45

def post_create_hook
  create_application_if_needed
  create_deployment_group_if_needed

  wait_for_asg_capacity
end

#post_delete_hookObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 88

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



52
53
54
55
56
57
58
59
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 52

def post_update_hook
  post_create_hook

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

#status_hookObject



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

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