Class: OpsTasks::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/ops_tasks/deployment.rb

Direct Known Subclasses

Scale

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Deployment



6
7
8
9
10
11
12
13
14
# File 'lib/ops_tasks/deployment.rb', line 6

def initialize(args)
  @client = AWS::OpsWorks::Client.new
  @layer_id = args[:layer_id]
  @recipe = args[:recipe]
  @stack_id = args[:stack_id]
  @slack_channel = args[:room]
  @project = args[:project]
  @run_in_background = args[:background]
end

Instance Method Details

#announce_log(id) ⇒ Object



105
106
107
108
109
110
# File 'lib/ops_tasks/deployment.rb', line 105

def announce_log(id)
  "Chef".
    says("<a href='#{log_url(id)}'>failure log</a>").
    to_channel(@slack_channel)
  puts log_url(id)
end

#announce_status(task, deployment_id) ⇒ Object

def status(deployment_id)

@client.describe_deployments(:deployment_ids => [deployment_id])[:deployments].first[:status]

end



89
90
91
92
93
# File 'lib/ops_tasks/deployment.rb', line 89

def announce_status(task, deployment_id)
  return false if notifications_disabled?
  status = assess_status(deployment_id)
  "Chef".says("#{@project} #{task} #{status}").to_channel(@slack_channel)
end

#assess_status(deployment_id) ⇒ Object



95
96
97
98
99
# File 'lib/ops_tasks/deployment.rb', line 95

def assess_status(deployment_id)
  @client.describe_deployments(
    :deployment_ids => [deployment_id]
  )[:deployments].first[:status]
end

#configureObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ops_tasks/deployment.rb', line 49

def configure
  print "#{@project}: Preparing configuration... "
  id = @client.create_deployment(
    :stack_id => @stack_id,
    :instance_ids => instance_ids,
    :command => {
      name: "configure"
    }
  )[:deployment_id]
  puts "successful"
  return id
end

#deployObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ops_tasks/deployment.rb', line 22

def deploy
  print "#{@project}: Preparing deployment... "
  id = @client.create_deployment(
    :stack_id => @stack_id,
    :instance_ids => instance_ids,
    :command => {
      name: "execute_recipes",
      args: {"recipes" => [@recipe]}
    }
  )[:deployment_id]
  puts "successful"
  return id
end

#deployment_failed?(id) ⇒ Boolean



101
102
103
# File 'lib/ops_tasks/deployment.rb', line 101

def deployment_failed?(id)
  assess_status(id) == 'failed'
end

#instance_idsObject



16
17
18
19
20
# File 'lib/ops_tasks/deployment.rb', line 16

def instance_ids
  client = AWS::OpsWorks::Client.new
  instance_objects = client.describe_instances(:layer_id => @layer_id)
  return instance_objects.instances.map{|i| i.instance_id}.to_a
end

#log_url(deployment_id) ⇒ Object



74
75
76
77
78
# File 'lib/ops_tasks/deployment.rb', line 74

def log_url(deployment_id)
  @client.describe_commands(
    :deployment_id => deployment_id
  )[:log_url]
end

#notifications_disabled?Boolean



81
82
83
# File 'lib/ops_tasks/deployment.rb', line 81

def notifications_disabled?
  ENV["#{@project}_room_notifications"] == 'false'
end

#poll_api_for_status(deployment_id, running_status = 'running') ⇒ Object



112
113
114
115
# File 'lib/ops_tasks/deployment.rb', line 112

def poll_api_for_status(deployment_id, running_status = 'running')
  sleep 1 until assess_status(deployment_id) != running_status
  puts assess_status(deployment_id)
end

#setupObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ops_tasks/deployment.rb', line 36

def setup
  print "#{@project}: Preparing setup... "
  id = @client.create_deployment(
    :stack_id => @stack_id,
    :instance_ids => instance_ids,
    :command => {
      name: "setup"
    }
  )[:deployment_id]
  puts "successful"
  return id
end

#update_cookbooksObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ops_tasks/deployment.rb', line 62

def update_cookbooks
  print "#{@project}: Preparing cookbook update... "

  id = @client.create_deployment(
    :stack_id => @stack_id,
    :instance_ids => instance_ids,
    :command => {name: 'update_custom_cookbooks'}
  )[:deployment_id]
  puts "successful"
  return id
end

#wait_for_completion(deployment_id, task = "deployment") ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/ops_tasks/deployment.rb', line 117

def wait_for_completion(deployment_id, task="deployment")
  print "#{@project}: Running... "
  announce_status(task, deployment_id)
  poll_api_for_status(deployment_id)
  announce_status(task, deployment_id)
  announce_log(deployment_id) if deployment_failed?(deployment_id)
  Process.daemon if @run_in_background
end