Class: OpsworksWrapper::Deployer

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

Instance Method Summary collapse

Constructor Details

#initialize(app_id) ⇒ Deployer

Returns a new instance of Deployer.



8
9
10
# File 'lib/opsworks_wrapper.rb', line 8

def initialize(app_id)
  @app_id = app_id
end

Instance Method Details

#app_idObject



24
25
26
# File 'lib/opsworks_wrapper.rb', line 24

def app_id
  @app_id
end

#clientObject



20
21
22
# File 'lib/opsworks_wrapper.rb', line 20

def client
  @client ||= Aws::OpsWorks::Client.new
end

#create_deployment(command, instance_ids, timeout) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/opsworks_wrapper.rb', line 93

def create_deployment(command, instance_ids, timeout)
  deployment_config = {
      stack_id: opsworks_app[:stack_id],
      app_id: app_id,
      instance_ids: instance_ids,
      command: command,
      comment: "Git Sha: #{current_sha}"
  }

  deployment = client.create_deployment(deployment_config)
  puts "Deployment created: #{deployment[:deployment_id]}".blue
  puts "Running Command: #{command[:name]} ".light_blue

  begin
    wait_until_deployed(deployment[:deployment_id], timeout)
    puts "Deployment successful".green
    true
  rescue Aws::Waiters::Errors::WaiterFailed => e
    puts  "Failed to deploy: #{e.message}".red
    false
  end
end

#create_deployment_exclude(command, layer_to_exclude, timeout) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/opsworks_wrapper.rb', line 85

def create_deployment_exclude(command, layer_to_exclude, timeout)
  all_instance_ids = get_instances.map(&:instance_id)
  excluded_instance_ids = get_instances(layer_to_exclude).map(&:instance_id)
  included_instance_ids = all_instance_ids - excluded_instance_ids

  create_deployment(command, included_instance_ids, timeout)
end

#current_shaObject



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

def current_sha
  @current_sha ||= `git rev-parse HEAD`.chomp
end

#deploy(layer_name = nil, timeout = 600) ⇒ Object

deploy to specified layer or all layers (default)



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opsworks_wrapper.rb', line 67

def deploy(layer_name = nil, timeout = 600)
  if layer_name
    puts "Deploying on #{layer_name} layer".light_white.bold
    instance_ids = get_instances(layer_name).map(&:instance_id)
  else
    puts "Deploying on all layers".light_white.bold
    instance_ids = nil
  end

  create_deployment({name: 'deploy'}, instance_ids, timeout)
end

#deploy_exclude(layer_name, timeout = 600) ⇒ Object

deploy to all layers except specified layer



80
81
82
83
# File 'lib/opsworks_wrapper.rb', line 80

def deploy_exclude(layer_name, timeout = 600)
  puts "Deploying to all layers except #{layer_name}".light_white.bold
  create_deployment_exclude({name: 'deploy'}, layer_name, timeout)
end

#get_instances(layer_name = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/opsworks_wrapper.rb', line 49

def get_instances(layer_name = nil)
  if layer_name == nil
    data = client.describe_instances(stack_id: opsworks_app[:stack_id])
  else
    layer_id = layers[layer_name].layer_id
    data = client.describe_instances(layer_id: layer_id)
  end

  data.instances
end

#get_opsworks_appObject



41
42
43
44
45
46
47
# File 'lib/opsworks_wrapper.rb', line 41

def get_opsworks_app
  data = client.describe_apps(app_ids: [app_id])
  unless data[:apps] && data[:apps].count == 1
    raise Error, "App #{app_id} not found.", error.backtrace
  end
  data[:apps].first
end

#get_opsworks_layersObject



32
33
34
35
36
37
38
39
# File 'lib/opsworks_wrapper.rb', line 32

def get_opsworks_layers
  data = client.describe_layers(stack_id: opsworks_app[:stack_id])
  layers = {}
  data.layers.each do |layer|
    layers[layer.name] = layer
  end
  layers
end

#layersObject



28
29
30
# File 'lib/opsworks_wrapper.rb', line 28

def layers
  @layers ||= get_opsworks_layers
end

#opsworks_appObject



16
17
18
# File 'lib/opsworks_wrapper.rb', line 16

def opsworks_app
  @opsworks_app ||= get_opsworks_app
end

#update_cookbooks(timeout = 150) ⇒ Object

update cookbooks on all layers



61
62
63
64
# File 'lib/opsworks_wrapper.rb', line 61

def update_cookbooks(timeout = 150)
  puts 'Updating cookbooks'.light_white.bold
  create_deployment({name: 'update_custom_cookbooks'}, nil, timeout)
end