This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from legal90/redesign-data-bags
- Loading branch information
Showing
11 changed files
with
193 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,64 @@ | ||
# Chef class | ||
class Chef | ||
# Chef::Recipe class | ||
class Recipe | ||
# Chef::Recipe::Stash class | ||
class Stash | ||
# rubocop:disable Metrics/AbcSize | ||
def self.settings(node) | ||
begin | ||
if Chef::Config[:solo] | ||
begin | ||
databag_item = Chef::DataBagItem.load('stash', 'stash')['local'] | ||
rescue | ||
Chef::Log.info('No stash data bag found') | ||
end | ||
else | ||
begin | ||
databag_item = Chef::EncryptedDataBagItem.load('stash', 'stash')[node.chef_environment] | ||
rescue | ||
Chef::Log.info('No stash encrypted data bag found') | ||
end | ||
end | ||
ensure | ||
databag_item ||= {} | ||
settings = Chef::Mixin::DeepMerge.deep_merge(databag_item, node['stash'].to_hash) | ||
settings['database']['port'] ||= Stash.default_database_port(settings['database']['type']) | ||
end | ||
|
||
settings | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
module Stash | ||
# Stash::Library module | ||
module Library | ||
# Merges Stash settings from data bag and node attributes. | ||
# Data dag settings always has a higher priority. | ||
# | ||
# @return [Hash] Settings hash | ||
def merge_stash_settings | ||
@settings_from_data_bag ||= settings_from_data_bag | ||
settings = Chef::Mixin::DeepMerge.deep_merge( | ||
@settings_from_data_bag, | ||
node['stash'].to_hash) | ||
|
||
def self.default_database_port(type) | ||
case type | ||
when 'mysql' | ||
3306 | ||
when 'postgresql' | ||
5432 | ||
when 'sqlserver' | ||
1433 | ||
else | ||
Chef::Log.warn("Unsupported database type (#{type}) in Stash cookbook.") | ||
Chef::Log.warn('Please add to Stash cookbook or hard set Stash database port.') | ||
nil | ||
settings['database']['port'] ||= | ||
case settings['database']['type'] | ||
when 'mysql' then 3306 | ||
when 'postgresql' then 5432 | ||
when 'sqlserver' then 1433 | ||
else fatal "Unsupported database type: #{settings['database']['type']}" | ||
end | ||
end | ||
|
||
def self.check_for_old_attributes!(node) | ||
backup_attrs = %w(backup_path baseurl password user cron) | ||
show_warn = false | ||
backup_attrs.each do |attr| | ||
next if node['stash']['backup_client'][attr].nil? | ||
settings | ||
end | ||
|
||
node.default['stash']['backup'][attr] = node['stash']['backup_client'][attr] | ||
Chef::Log.warn "node['stash']['backup_client']['#{attr}'] has been changed to node['stash']['backup']['#{attr}']" | ||
show_warn = true | ||
end | ||
Chef::Log.warn <<-EOH if show_warn | ||
# rubocop:disable Metrics/AbcSize | ||
def handle_old_stash_attributes! | ||
backup_attrs = %w(backup_path baseurl password user cron) | ||
show_warn = false | ||
backup_attrs.each do |attr| | ||
next if node['stash']['backup_client'][attr].nil? | ||
|
||
node.default['stash']['backup'][attr] = node['stash']['backup_client'][attr] | ||
Chef::Log.warn "node['stash']['backup_client']['#{attr}'] has been changed to node['stash']['backup']['#{attr}']" | ||
show_warn = true | ||
end | ||
Chef::Log.warn <<-EOH if show_warn | ||
This renaming introduces the common approach for both of backup strategies: | ||
'backup_client' and 'backup_diy'. Attributes mentioned above will be gracefully | ||
converted for you, but this warning and conversion will be removed in the next | ||
major release of the 'stash' cookbook. | ||
EOH | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
|
||
private | ||
|
||
# Fetches Stash settings from the data bag | ||
# | ||
# @return [Hash] Settings hash | ||
def settings_from_data_bag | ||
begin | ||
item = data_bag_item(node['stash']['data_bag_name'], | ||
node['stash']['data_bag_item'])['stash'] | ||
return item if item.is_a?(Hash) | ||
rescue | ||
Chef::Log.info('No stash data bag found') | ||
end | ||
{} | ||
end | ||
end | ||
end | ||
|
||
::Chef::Recipe.send(:include, Stash::Library) | ||
::Chef::Resource.send(:include, Stash::Library) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters