Class: Paramsync::Config
- Inherits:
-
Object
- Object
- Paramsync::Config
- Defined in:
- lib/paramsync/config.rb
Constant Summary collapse
- CONFIG_FILENAMES =
%w( paramsync.yml )- VALID_CONFIG_KEYS =
%w( sync ssm paramsync )- VALID_SSM_CONFIG_KEYS =
%w( accounts kms )- VALID_PARAMSYNC_CONFIG_KEYS =
%w( verbose chomp delete color )
Instance Attribute Summary collapse
-
#base_dir ⇒ Object
Returns the value of attribute base_dir.
-
#config_file ⇒ Object
Returns the value of attribute config_file.
-
#kms_client ⇒ Object
Returns the value of attribute kms_client.
-
#kms_key ⇒ Object
Returns the value of attribute kms_key.
-
#ssm_accounts ⇒ Object
Returns the value of attribute ssm_accounts.
-
#sync_targets ⇒ Object
Returns the value of attribute sync_targets.
-
#target_allowlist ⇒ Object
Returns the value of attribute target_allowlist.
Class Method Summary collapse
-
.discover(dir: nil) ⇒ Object
discover the nearest config file.
Instance Method Summary collapse
- #chomp? ⇒ Boolean
- #color? ⇒ Boolean
- #delete? ⇒ Boolean
-
#initialize(path: nil, targets: nil) ⇒ Config
constructor
A new instance of Config.
- #parse! ⇒ Object
- #verbose? ⇒ Boolean
Constructor Details
#initialize(path: nil, targets: nil) ⇒ Config
Returns a new instance of Config.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/paramsync/config.rb', line 33 def initialize(path: nil, targets: nil) if path.nil? or File.directory?(path) self.config_file = Paramsync::Config.discover(dir: path) elsif File.exist?(path) self.config_file = path else raise Paramsync::ConfigFileNotFound.new end if self.config_file.nil? or not File.exist?(self.config_file) or not File.readable?(self.config_file) raise Paramsync::ConfigFileNotFound.new end self.config_file = File.(self.config_file) self.base_dir = File.dirname(self.config_file) self.target_allowlist = targets parse! end |
Instance Attribute Details
#base_dir ⇒ Object
Returns the value of attribute base_dir.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def base_dir @base_dir end |
#config_file ⇒ Object
Returns the value of attribute config_file.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def config_file @config_file end |
#kms_client ⇒ Object
Returns the value of attribute kms_client.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def kms_client @kms_client end |
#kms_key ⇒ Object
Returns the value of attribute kms_key.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def kms_key @kms_key end |
#ssm_accounts ⇒ Object
Returns the value of attribute ssm_accounts.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def ssm_accounts @ssm_accounts end |
#sync_targets ⇒ Object
Returns the value of attribute sync_targets.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def sync_targets @sync_targets end |
#target_allowlist ⇒ Object
Returns the value of attribute target_allowlist.
15 16 17 |
# File 'lib/paramsync/config.rb', line 15 def target_allowlist @target_allowlist end |
Class Method Details
.discover(dir: nil) ⇒ Object
discover the nearest config file
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/paramsync/config.rb', line 19 def discover(dir: nil) dir ||= Dir.pwd CONFIG_FILENAMES.each do |filename| full_path = File.join(dir, filename) if File.exist?(full_path) return full_path end end dir == "/" ? nil : self.discover(dir: File.dirname(dir)) end |
Instance Method Details
#chomp? ⇒ Boolean
56 57 58 |
# File 'lib/paramsync/config.rb', line 56 def chomp? @do_chomp end |
#color? ⇒ Boolean
64 65 66 |
# File 'lib/paramsync/config.rb', line 64 def color? @use_color end |
#delete? ⇒ Boolean
60 61 62 |
# File 'lib/paramsync/config.rb', line 60 def delete? @do_delete end |
#parse! ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/paramsync/config.rb', line 68 def parse! raw = {} begin raw = YAML.load(ERB.new(File.read(self.config_file)).result) rescue raise Paramsync::ConfigFileInvalid.new("Unable to parse config file as YAML") end if raw.is_a? FalseClass # this generally means an empty config file raw = {} end if not raw.is_a? Hash raise Paramsync::ConfigFileInvalid.new("Config file must form a hash") end raw['ssm'] ||= {} if not raw['ssm'].is_a? Hash raise Paramsync::ConfigFileInvalid.new("'ssm' must be a hash") end if (raw['ssm'].keys - VALID_SSM_CONFIG_KEYS) != [] raise Paramsync::ConfigFileInvalid.new("Only the following keys are valid in the ssm config: #{VALID_SSM_CONFIG_KEYS.join(", ")}") end self.ssm_accounts = raw['ssm']['accounts'] raw['paramsync'] ||= {} if not raw['paramsync'].is_a? Hash raise Paramsync::ConfigFileInvalid.new("'paramsync' must be a hash") end if (raw['paramsync'].keys - VALID_PARAMSYNC_CONFIG_KEYS) != [] raise Paramsync::ConfigFileInvalid.new("Only the following keys are valid in the 'paramsync' config block: #{VALID_PARAMSYNC_CONFIG_KEYS.join(", ")}") end # verbose: default false @is_verbose = raw['paramsync']['verbose'] ? true : false if ENV['PARAMSYNC_VERBOSE'] @is_verbose = true end # chomp: default true if raw['paramsync'].has_key?('chomp') @do_chomp = raw['paramsync']['chomp'] ? true : false else @do_chomp = true end # delete: default false @do_delete = raw['paramsync']['delete'] ? true : false raw['sync'] ||= [] if not raw['sync'].is_a? Array raise Paramsync::ConfigFileInvalid.new("'sync' must be an array") end # color: default true if raw['paramsync'].has_key?('color') @use_color = raw['paramsync']['color'] ? true : false else @use_color = true end if raw['ssm'].has_key?('kms') self.kms_client = Aws::KMS::Client.new(**{ region: raw['ssm']['kms']['region'], credentials: raw['ssm']['kms']['role'] ? Aws::AssumeRoleCredentials.new( client: Aws::STS::Client.new(region: raw['ssm']['kms']['region']), role_arn: raw['ssm']['kms']['role'], role_session_name: "paramsync" ) : nil, }.compact) self.kms_key = raw['ssm']['kms']['arn'] end self.sync_targets = [] raw['sync'].each do |target| if target.is_a? Hash if target['chomp'].nil? target['chomp'] = self.chomp? end if target['delete'].nil? target['delete'] = self.delete? end if target['account'] account = self.ssm_accounts[target['account']] if account.nil? raise Paramsync::ConfigFileInvalid.new("Account '#{target['account']}' is not defined") end end end if not self.target_allowlist.nil? # unnamed targets cannot be allowlisted next if target['name'].nil? # named targets must be on the allowlist next if not self.target_allowlist.include?(target['name']) end self.sync_targets << Paramsync::SyncTarget.new(config: target, account: account ? account['role'] : nil, base_dir: self.base_dir) end end |
#verbose? ⇒ Boolean
52 53 54 |
# File 'lib/paramsync/config.rb', line 52 def verbose? @is_verbose end |