Module: MotherBrain::Mixin::AttributeSetting
- Extended by:
- Forwardable
- Includes:
- MB::Mixin::Services, Logging
- Defined in:
- lib/mb/mixin/attribute_setting.rb
Instance Method Summary collapse
-
#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.
-
#set_cookbook_versions(env_id, cookbook_versions) ⇒ Object
Lock the cookbook versions on the target environment from the given hash of cookbooks and versions.
-
#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.
-
#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.
-
#set_environment_attributes_from_hash(env_id, new_attributes) ⇒ Object
Set arbitrary attributes at the environment level.
-
#set_environment_attributes_from_json(env_id, json) ⇒ Object
Set environment level attributes on an environment from the given JSON string.
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
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
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 = ((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.
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
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
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
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 |