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) ⇒ CodeDeploy

Returns a new instance of CodeDeploy.

Parameters:

  • asg (String)

    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 and Deployment Group. 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.



26
27
28
29
30
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 26

def initialize(asg:, role: 'CodeDeployRole', app_name: nil)
  @asg_logical_id = asg
  @app_name = app_name
  @codedeploy_role = role
end

Instance Method Details

#deploy_hook(artifact_repo, version_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 55

def deploy_hook(artifact_repo, version_name)
  ilog.start_threaded 'Creating Deployment' do |s|
    res = cd_client.create_deployment(
      application_name: app_name,
      deployment_group_name: app_name,
      revision: revision_for_artifact_repo(artifact_repo, version_name),
      deployment_config_name: 'CodeDeployDefault.OneAtATime',
      description: "Deploying version #{version_name}"
    )
    deployment_id = res.deployment_id
    s.continue "Created Deployment #{deployment_id.blue}."
    wait_for_deployment(deployment_id, s)
  end
end

#post_create_hookObject



32
33
34
35
36
37
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 32

def post_create_hook
  create_application_if_needed
  create_deployment_group_if_needed

  wait_for_asg_capacity
end

#post_delete_hookObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 70

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



39
40
41
42
43
44
45
46
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 39

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



48
49
50
51
52
53
# File 'lib/moonshot/deployment_mechanism/code_deploy.rb', line 48

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