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.



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

def custom_json
  @custom_json
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.activeObject



25
26
27
# File 'lib/opsworks/stack.rb', line 25

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

.allObject



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

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

.find_by_name(name) ⇒ Object



29
30
31
# File 'lib/opsworks/stack.rb', line 29

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

.latest_chef_versionObject



33
34
35
# File 'lib/opsworks/stack.rb', line 33

def self.latest_chef_version
  AVAILABLE_CHEF_VERSIONS.last
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/opsworks/stack.rb', line 83

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

#appsObject



37
38
39
# File 'lib/opsworks/stack.rb', line 37

def apps
  @apps ||= initialize_apps
end

#custom_json_at(key) ⇒ Object



87
88
89
# File 'lib/opsworks/stack.rb', line 87

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

#deploy_app(app) ⇒ Object



78
79
80
81
# File 'lib/opsworks/stack.rb', line 78

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

#execute_recipe(recipe) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/opsworks/stack.rb', line 69

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

#find_app_by_name(name) ⇒ Object



49
50
51
# File 'lib/opsworks/stack.rb', line 49

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

#find_permission_by_user(name) ⇒ Object



45
46
47
# File 'lib/opsworks/stack.rb', line 45

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

#instancesObject



53
54
55
# File 'lib/opsworks/stack.rb', line 53

def instances
  @instances ||= initialize_instances
end

#permissionsObject



41
42
43
# File 'lib/opsworks/stack.rb', line 41

def permissions
  @permissions ||= initialize_permissions
end

#set_custom_json_at(key, value) ⇒ Object



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

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: custom_json.to_json
  )
end

#update_custom_cookbooksObject



65
66
67
# File 'lib/opsworks/stack.rb', line 65

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

#upgrade_chef(version, options = {}) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/opsworks/stack.rb', line 57

def upgrade_chef(version, options = {})
  self.class.client.update_stack(
    stack_id: id,
    configuration_manager: { name: 'Chef', version: version },
    chef_configuration: { manage_berkshelf: options[:manage_berkshelf] }
  )
end