Class: OpsWorks::Stack

Inherits:
Resource show all
Defined in:
lib/opsworks/stack.rb

Overview

rubocop:disable ClassLength

Constant Summary collapse

AVAILABLE_CHEF_VERSIONS =
%w(0.9 11.4 11.10)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

account, client, #initialize

Constructor Details

This class inherits a constructor from OpsWorks::Resource

Instance Attribute Details

#custom_jsonObject

Returns the value of attribute custom_json.



13
14
15
# File 'lib/opsworks/stack.rb', line 13

def custom_json
  @custom_json
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/opsworks/stack.rb', line 13

def id
  @id
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/opsworks/stack.rb', line 13

def name
  @name
end

Class Method Details

.activeObject



27
28
29
# File 'lib/opsworks/stack.rb', line 27

def self.active
  all.select(&:active?)
end

.allObject



17
18
19
20
21
22
23
24
25
# File 'lib/opsworks/stack.rb', line 17

def self.all
  client.describe_stacks.data[:stacks].map do |hash|
    new(
      id: hash[:stack_id],
      name: hash[:name],
      custom_json: JSON.parse(hash.fetch(:custom_json, '{}'))
    )
  end
end

.find_by_name(name) ⇒ Object



31
32
33
# File 'lib/opsworks/stack.rb', line 31

def self.find_by_name(name)
  all.find { |stack| stack.name == name }
end

.latest_chef_versionObject



35
36
37
# File 'lib/opsworks/stack.rb', line 35

def self.latest_chef_version
  AVAILABLE_CHEF_VERSIONS.last
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/opsworks/stack.rb', line 113

def active?
  instances.any?(&:online?)
end

#appsObject



39
40
41
# File 'lib/opsworks/stack.rb', line 39

def apps
  @apps ||= initialize_apps
end

#create_app(name, options = {}) ⇒ Object



130
131
132
133
134
# File 'lib/opsworks/stack.rb', line 130

def create_app(name, options = {})
  options = options.slice(:type, :shortname)
  options.merge!(stack_id: id, name: name)
  self.class.client.create_app(options)
end

#custom_json_at(key) ⇒ Object



117
118
119
# File 'lib/opsworks/stack.rb', line 117

def custom_json_at(key)
  JsonPath.new(key).first(custom_json)
end

#deploy_app(app, args = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/opsworks/stack.rb', line 102

def deploy_app(app, args = {})
  fail 'App not found' unless app && app.id
  create_deployment(
    app_id: app.id,
    command: {
      name: 'deploy',
      args: args
    }
  )
end

#execute_recipe(recipe) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/opsworks/stack.rb', line 93

def execute_recipe(recipe)
  create_deployment(
    command: {
      name: 'execute_recipes',
      args: { 'recipes' => [recipe] }
    }
  )
end

#find_app_by_name(name) ⇒ Object



51
52
53
# File 'lib/opsworks/stack.rb', line 51

def find_app_by_name(name)
  apps.find { |app| app.name == name }
end

#find_permission_by_user(name) ⇒ Object



47
48
49
# File 'lib/opsworks/stack.rb', line 47

def find_permission_by_user(name)
  permissions.find { |permission| permission.user == name }
end

#instancesObject



55
56
57
# File 'lib/opsworks/stack.rb', line 55

def instances
  @instances ||= initialize_instances
end

#layersObject



59
60
61
# File 'lib/opsworks/stack.rb', line 59

def layers
  @layers ||= initialize_layers
end

#permissionsObject



43
44
45
# File 'lib/opsworks/stack.rb', line 43

def permissions
  @permissions ||= initialize_permissions
end

#set_custom_json_at(key, value) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/opsworks/stack.rb', line 121

def set_custom_json_at(key, value)
  self.custom_json = replace_hash_at_path(custom_json, key, value)

  self.class.client.update_stack(
    stack_id: id,
    custom_json: JSON.pretty_generate(custom_json)
  )
end

#update_chef(options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/opsworks/stack.rb', line 63

def update_chef(options)
  params = {
    stack_id: id,
    configuration_manager: { name: 'Chef', version: options[:version] },
    chef_configuration: {
      manage_berkshelf: options[:manage_berkshelf],
      berkshelf_version: options[:berkshelf_version]
    }
  }
  if options[:cookbook_git_url]
    params[:custom_cookbooks_source] = {
      type: 'git',
      url: options[:cookbook_git_url],
      revision: options[:cookbook_branch] || 'master'
    }
  elsif options[:cookbook_s3_url]
    params[:custom_cookbooks_source] = {
      type: 's3',
      url: options[:cookbook_s3_url],
      username: options[:cookbook_username],
      password: options[:cookbook_password]
    }
  end
  self.class.client.update_stack(params)
end

#update_custom_cookbooksObject



89
90
91
# File 'lib/opsworks/stack.rb', line 89

def update_custom_cookbooks
  create_deployment(command: { name: 'update_custom_cookbooks' })
end