Module: MotherBrain::Mixin::AttributeSetting

Extended by:
Forwardable
Includes:
MB::Mixin::Services, Logging
Defined in:
lib/mb/mixin/attribute_setting.rb

Instance Method Summary collapse

Methods included from Logging

add_argument_header, dev, filename, #log_exception, logger, #logger, reset, set_logger, setup

Instance Method Details

#set_component_versions(env_id, plugin, component_versions) ⇒ Object

Set the appropriate attributes at the environment level to the desired version for each component given

Examples:

setting the versions of multiple components on an environment


set_component_versions("test-environment",
  "component_one" => "1.0.0",
  "component_two" => "2.3.0"
)

Parameters:

  • env_id (String)

    the name identifier of the environment to modify

  • plugin (MB::Plugin)

    the plugin to use for finding the appropriate version attributes

  • component_versions (Hash)

    Hash of components and the versions to set them to

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mb/mixin/attribute_setting.rb', line 28

def set_component_versions(env_id, plugin, component_versions)
  default_attributes = Hash.new

  component_versions.each do |component_name, version|
    version_hash = Hash.from_dotted_path(version_attribute(plugin, component_name), version)
    default_attributes.deep_merge!(version_hash)
  end

  log.info "Setting component versions #{component_versions}"

  unless env = chef_connection.environment.find(env_id)
    raise EnvironmentNotFound.new(env_id)
  end

  env.default_attributes.merge!(default_attributes)
  env.save
end

#set_cookbook_versions(env_id, cookbook_versions) ⇒ Object

Lock the cookbook versions on the target environment from the given hash of cookbooks and versions

Examples:

setting cookbook versions on an environment


set_cookbook_versions("test-environment",
  "league" => "1.74.2",
  "pvpnet" => "3.2.0"
)

Parameters:

  • env_id (String)

    the name identifier of the environment to modify

  • cookbook_versions (Hash)

    Hash of cookbooks and the versions to set them to

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mb/mixin/attribute_setting.rb', line 64

def set_cookbook_versions(env_id, cookbook_versions)
  cookbook_versions = expand_constraints(expand_latest_versions(cookbook_versions))
  satisfies_constraints?(cookbook_versions)
  log.info "Setting cookbook versions #{cookbook_versions}"

  unless env = chef_connection.environment.find(env_id)
    raise EnvironmentNotFound.new(env_id)
  end

  env.cookbook_versions.merge!(cookbook_versions)
  env.save
end

#set_environment_attributes(env_id, new_attributes) ⇒ Object

Set environment level attributes on an environment from a hash containing keys identifying attributes using the dot notation syntax and values of those attributes as values.

Examples:

setting multiple attributes on an environment


set_environment_attributes("test-environment",
  "foo"      => "bar",
  "baz.quux" => 42
)

Parameters:

  • env_id (String)

    the name identifier of the environment to modify

  • new_attributes (Hash)

    Hash of attributes and values



92
93
94
95
96
97
98
99
100
101
# File 'lib/mb/mixin/attribute_setting.rb', line 92

def set_environment_attributes(env_id, new_attributes)
  default_attributes = Hash.new

  new_attributes.each do |attribute, value|
    attribute_hash = Hash.from_dotted_path(attribute.to_s, value.to_s)
    default_attributes.deep_merge!(attribute_hash)
  end

  set_environment_attributes_from_hash(env_id, default_attributes)
end

#set_environment_attributes_from_file(env_id, filepath, type = :json) ⇒ Object

Set environment level attributes on an environment from the contents of the file at the given filepath

Parameters:

  • env_id (String)

    the name of the environment to modify

  • filepath (String)

    path to the file to read attributes from

  • type (Symbol) (defaults to: :json)

    the type of the contents found within the file

Raises:

  • (InvalidAttributesFile)

    if the contents of the attributes file are missing or not well formed

  • (ArgumentError)

    if an unknown value for the ‘type’ parameter is specified



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mb/mixin/attribute_setting.rb', line 145

def set_environment_attributes_from_file(env_id, filepath, type = :json)
  contents = File.read(filepath)

  case type
  when :json
    set_environment_attributes_from_json(env_id, contents)
  else
    raise ArgumentError, "cannot read attributes from files of type: #{type}"
  end
rescue MultiJson::DecodeError => ex
  raise InvalidAttributesFile, "Invalid JSON in #{filepath}: #{ex}"
end

#set_environment_attributes_from_hash(env_id, new_attributes) ⇒ Object

Set arbitrary attributes at the environment level

Examples:

setting multiple attributes on an environment


set_environment_attributes_from_hash("test-environment",
  "foo" => "bar",
  "baz  => {
    "quux" => 42
  }
)

Parameters:

  • env_id (String)

    the name of the environment to modify

  • new_attributes (Hash)

    Hash of attributes to set on the environment

Raises:



120
121
122
123
124
125
126
127
128
129
# File 'lib/mb/mixin/attribute_setting.rb', line 120

def set_environment_attributes_from_hash(env_id, new_attributes)
  log.info "Setting environment attributes: #{new_attributes}"

  unless env = chef_connection.environment.find(env_id)
    raise EnvironmentNotFound.new(env_id)
  end

  env.default_attributes.deep_merge!(new_attributes)
  env.save
end

#set_environment_attributes_from_json(env_id, json) ⇒ Object

Set environment level attributes on an environment from the given JSON string

Parameters:

  • env_id (String)

    the name of the environment to modify

  • json (String)

    the json to use as attributes



164
165
166
# File 'lib/mb/mixin/attribute_setting.rb', line 164

def set_environment_attributes_from_json(env_id, json)
  set_environment_attributes_from_hash(env_id, MultiJson.decode(json))
end