Class: OpsDeploy

Inherits:
Object
  • Object
show all
Defined in:
lib/ops_deploy.rb,
lib/ops_deploy/version.rb

Defined Under Namespace

Classes: CLI, DeploymentWaiter, Waiter

Constant Summary collapse

VERSION =
'0.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ OpsDeploy

Returns a new instance of OpsDeploy.



14
15
16
17
18
19
20
# File 'lib/ops_deploy.rb', line 14

def initialize(config = nil)
  config ||= {
    region: 'us-east-1'
  }

  @opsworks = Aws::OpsWorks::Client.new(config)
end

Instance Attribute Details

#deployments_callbackObject

Returns the value of attribute deployments_callback.



11
12
13
# File 'lib/ops_deploy.rb', line 11

def deployments_callback
  @deployments_callback
end

#instances_check_callbackObject

Returns the value of attribute instances_check_callback.



12
13
14
# File 'lib/ops_deploy.rb', line 12

def instances_check_callback
  @instances_check_callback
end

#waiterObject

Returns the value of attribute waiter.



10
11
12
# File 'lib/ops_deploy.rb', line 10

def waiter
  @waiter
end

Instance Method Details

#check_instances(stack_id_name_or_object) ⇒ Object



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

def check_instances(stack_id_name_or_object)
  stack = find_stack(stack_id_name_or_object)

  running_instances = @opsworks.describe_instances(stack_id: stack.stack_id).instances.select do |instance|
    instance.status == 'online'
  end

  waiters = []
  running_instances.each do |instance|
    waiters << OpsDeploy::InstanceResponseWaiter.new(@opsworks, instance, @instances_check_callback)
  end

  @waiter = Thread.new(waiters) do |check_threads|
    check_threads.each(&:run)
    check_threads.each(&:join)
  end

  @waiter.run
end

#find_app(stack, application_id_name_or_object) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ops_deploy.rb', line 118

def find_app(stack, application_id_name_or_object)
  found_app = nil

  if application_id_name_or_object.is_a?(String)
    begin
      if application_id_name_or_object.match(/^[0-9a-f\-]+$/)
        found_app = @opsworks.describe_apps(app_ids: [application_id_name_or_object]).apps.first
      end
    rescue Aws::OpsWorks::Errors::ResourceNotFoundException
      found_app = nil
    end

    if found_app.nil?
      @opsworks.describe_apps(stack_id: stack.stack_id).apps.each do |app|
        if app.name == application_id_name_or_object
          found_app = app
          break
        end
      end
    end
  elsif application_id_name_or_object.nil?
    apps = @opsworks.describe_apps(stack_id: stack.stack_id).apps
    found_app = apps.first if apps.count == 1
  end

  found_app = application_id_name_or_object if found_app.nil?
  error_message = "Invalid app #{found_app} (#{application_id_name_or_object})."
  invalid_app_error = StandardError.new(error_message)
  fail invalid_app_error unless found_app.is_a?(Aws::OpsWorks::Types::App)

  found_app
end

#find_stack(stack_id_name_or_object) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ops_deploy.rb', line 88

def find_stack(stack_id_name_or_object)
  found_stack = nil

  if stack_id_name_or_object.is_a?(String)
    begin
      if stack_id_name_or_object.match(/^[0-9a-f\-]+$/)
        found_stack = @opsworks.describe_stacks(stack_ids: [stack_id_name_or_object]).stacks.first
      end
    rescue Aws::OpsWorks::Errors::ResourceNotFoundException
      found_stack = nil
    end

    if found_stack.nil?
      @opsworks.describe_stacks.stacks.each do |stack|
        if stack.name == stack_id_name_or_object
          found_stack = stack
          break
        end
      end
    end
  end

  found_stack = stack_id_name_or_object if found_stack.nil?
  error_message = "Invalid stack #{found_stack} (#{stack_id_name_or_object})."
  invalid_stack_error = StandardError.new(error_message)
  fail invalid_stack_error unless found_stack.is_a?(Aws::OpsWorks::Types::Stack)

  found_stack
end

#stacksObject



22
23
24
# File 'lib/ops_deploy.rb', line 22

def stacks
  @opsworks.describe_stacks.stacks
end

#start_deployment(stack_id_name_or_object, application_id_name_or_object = nil, migrate = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ops_deploy.rb', line 26

def start_deployment(stack_id_name_or_object,
                     application_id_name_or_object = nil,
                     migrate = false)
  stack = find_stack(stack_id_name_or_object)
  app = find_app(stack, application_id_name_or_object)

  command = { name: 'deploy' }
  command['args'] = { migrate: ['true'] } if migrate

  resp = @opsworks.create_deployment(stack_id: stack.stack_id,
                                     app_id: app.app_id,
                                     command: command)

  (resp && resp.deployment_id)
end

#wait_for_deployments(stack_id_name_or_object) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ops_deploy.rb', line 42

def wait_for_deployments(stack_id_name_or_object)
  stack = find_stack(stack_id_name_or_object)

  running_deployments = @opsworks.describe_deployments(stack_id: stack.stack_id)
                        .deployments.select do |deployment|
    deployment.status == 'running'
  end

  if running_deployments.empty?
    false
  else
    waiters = []

    running_deployments.each do |deployment|
      waiters << OpsDeploy::DeploymentWaiter.new(@opsworks, deployment, @deployments_callback)
    end

    @waiter = Thread.new(waiters) do |deploy_threads|
      deploy_threads.each(&:run)
      deploy_threads.each(&:join)
    end

    @waiter.run
  end
end