cps-property-generator 0.2.21 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a84961bcf8da280ccdd5c864c2f4de638e640a9
4
- data.tar.gz: 61ceec2b37075a6f9264270b5b9f7c36ca989bde
3
+ metadata.gz: ee35c7e51b0de1094566e73a39784a663442903c
4
+ data.tar.gz: 16ade81e5e6922856112a7516dd768e029935374
5
5
  SHA512:
6
- metadata.gz: 829a849b5da0302c499cd2c3917f006e1ee2d6f4b7618c883be46993a7ea5fbb769acb0cd78e4b96a13563919a98fa7245f6af4f25fc4b4c62e3f42d412ce64d
7
- data.tar.gz: 04e023ae6e05618d30a9134451444e7404d57e11da2b7864acc551c71f16f264eba0ddc356aee469db5d3f7c68a4927b4e11abcd001bcb24917ea8bed3ddedd8
6
+ metadata.gz: 81d1e90f92865481d6c6ce7242c0bba8c1802ab92ef8c04b961310ef4aa730b50358a68addc1912530ebc045aa3edd77525dfeffcda9bb6be83096a12f334a4c
7
+ data.tar.gz: 4363ca29e85b30e93ab04615fabc809cc285592f9e3f49e9b640ec36ce5bd81e59be05f533c1556d49ae9b4fd8c9c365f0d9b950963743901a7740c0f4d3a521
@@ -1,11 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- #
3
2
  require 'thor'
4
3
  require 'yaml'
5
- require_relative '../lib/generator/generator.rb'
6
- require_relative '../lib/linter/linter.rb'
7
- class GeneratorCLI < ::Thor
4
+ require_relative '../lib/generator/generator'
5
+ require_relative '../lib/linter/linter'
8
6
 
7
+ class GeneratorCLI < ::Thor
9
8
  desc 'generate', 'Generate properties'
10
9
  option 'project_path', banner: 'PROJECT_PATH', type: :string, desc: 'Path to the property project to generate properties for'
11
10
  option 'output', banner: 'OUTPUT', type: :string, desc: 'Output path for locally dumping generated outputs', :default => '/tmp/'
@@ -16,7 +15,6 @@ class GeneratorCLI < ::Thor
16
15
  option 'upload_all', banner: 'UPLOAD_ALL', type: :boolean, desc: 'Whether to upload all envs and accounts to a single bucket', :default => false
17
16
 
18
17
  def generate
19
-
20
18
  generator = PropertyGenerator::Generator.new(options)
21
19
  out = generator.generate
22
20
  if options['upload']
@@ -1,7 +1,6 @@
1
1
  module PropertyGenerator
2
2
  require 'yaml'
3
3
  class Config
4
-
5
4
  attr_accessor :configs
6
5
 
7
6
  def initialize(project_path)
@@ -29,4 +28,4 @@ module PropertyGenerator
29
28
  YAML.load_file(file)
30
29
  end
31
30
  end
32
- end
31
+ end
@@ -11,20 +11,20 @@ module PropertyGenerator
11
11
  # purpose: initialize globals and configs
12
12
  # serve as a broker between tasks
13
13
  def initialize(options)
14
- project_path = File.expand_path(options['project_path'])
14
+ project_path = File.expand_path(options['project_path'])
15
15
  @configs = PropertyGenerator::Config.new(project_path)
16
16
  @globals = PropertyGenerator::Globals.new(project_path, @configs)
17
17
  @globals = @globals.globals
18
18
  @accounts = @configs.accounts
19
19
 
20
- @output_path = "#{File.expand_path(options['output'])}/properties/#{SecureRandom.hex}"
20
+ @output_path = "#{File.expand_path(options['output'])}/properties/#{SecureRandom.hex}"
21
21
  puts "Properties will be output here #{@output_path}"
22
22
  @service_list = PropertyGenerator.read_services(project_path)
23
23
  end
24
24
 
25
25
  def generate
26
26
  output = []
27
- @service_list.each do | service, path|
27
+ @service_list.each do |service, path|
28
28
  PropertyGenerator.config_enforcer(@configs.environment_configs)
29
29
  service_instance = PropertyGenerator::Service.new(YAML.load_file(path), @configs, @globals)
30
30
  service_instance.service
@@ -42,8 +42,8 @@ module PropertyGenerator
42
42
 
43
43
  if config['upload_all']
44
44
  _upload_files(out.sort) do |file|
45
- file_region = file.split("/")[-2]
46
- file_account = file.split("/")[-3]
45
+ file_region = file.split('/')[-2]
46
+ file_account = file.split('/')[-3]
47
47
 
48
48
  PropertyGenerator.sync(upload_region, file_account, upload_bucket, file, file_region)
49
49
  end
@@ -53,9 +53,9 @@ module PropertyGenerator
53
53
  abort("The specified account (#{upload_account}) is not configured, please add it to config/config.yml")
54
54
  end
55
55
 
56
- upload_out = out.select { |file| file.include?("#{upload_account}") && file.include?("#{upload_region}") }
56
+ upload_out = out.select { |file| file.include?(upload_account) && file.include?(upload_region) }
57
57
  _upload_files(upload_out) do |file|
58
- file_region = file.split("/")[-2]
58
+ file_region = file.split('/')[-2]
59
59
  PropertyGenerator.sync(upload_region, upload_account, upload_bucket, file, file_region)
60
60
  end
61
61
  end
@@ -70,7 +70,5 @@ module PropertyGenerator
70
70
  end.each(&:join)
71
71
  end
72
72
  end
73
-
74
73
  end
75
74
  end
76
-
@@ -14,10 +14,9 @@ module PropertyGenerator
14
14
  @globals ||= condense_globals
15
15
  end
16
16
 
17
-
18
17
  def get_main_global
19
18
  top_level = {}
20
- if File.exists?("#{@project_path}/globals/globals.yml")
19
+ if File.exist?("#{@project_path}/globals/globals.yml")
21
20
  top_level = YAML.load_file("#{@project_path}/globals/globals.yml")
22
21
  end
23
22
  top_level
@@ -26,9 +25,10 @@ module PropertyGenerator
26
25
  def get_account_globals
27
26
  data = {}
28
27
  @accounts.each do |account|
29
- next unless Dir.exists?("#{@project_path}/globals/accounts/#{account}")
28
+ next unless Dir.exist?("#{@project_path}/globals/accounts/#{account}")
29
+
30
30
  account_default_file = "#{@project_path}/globals/accounts/#{account}/#{account}.yml"
31
- data[account] = YAML.load_file(account_default_file) if File.exists?(account_default_file)
31
+ data[account] = YAML.load_file(account_default_file) if File.exist?(account_default_file)
32
32
  end
33
33
  data
34
34
  end
@@ -36,14 +36,16 @@ module PropertyGenerator
36
36
  def get_environment_globals
37
37
  data = {}
38
38
  @accounts.each do |account|
39
- next unless Dir.exists?("#{@project_path}/globals/accounts/#{account}/environments")
39
+ next unless Dir.exist?("#{@project_path}/globals/accounts/#{account}/environments")
40
+
40
41
  data[account] = {}
41
42
  @environments.each do |env|
42
- next unless File.exists?("#{@project_path}/globals/accounts/#{account}/environments/#{env}.yml")
43
+ next unless File.exist?("#{@project_path}/globals/accounts/#{account}/environments/#{env}.yml")
44
+
43
45
  data[account][env] = YAML.load_file("#{@project_path}/globals/accounts/#{account}/environments/#{env}.yml")
44
46
  unless data[account][env]['encrypted'].nil?
45
47
  encrypted = data[account][env]['encrypted'].dup
46
- not_encrypted = data[account][env].reject { |k,_| k == 'encrypted' }
48
+ not_encrypted = data[account][env].reject { |k, _| k == 'encrypted' }
47
49
  data[account][env] = not_encrypted.deep_merge(encrypted)
48
50
  end
49
51
  end
@@ -51,8 +53,7 @@ module PropertyGenerator
51
53
  data
52
54
  end
53
55
 
54
-
55
- #merge environment globals with account globals.
56
+ # merge environment globals with account globals.
56
57
  def condense_globals
57
58
  condensed = {}
58
59
  # get account and the environmental hash's for said account
@@ -62,7 +63,7 @@ module PropertyGenerator
62
63
  # nothing to do here if everything is empty
63
64
  return condensed if environment_globals.empty? && account_globals.empty? && main_global.empty?
64
65
 
65
- environment_globals.each do |account, env_global |
66
+ environment_globals.each do |account, env_global|
66
67
  # get the env and the values
67
68
  env_global.each do |env, hash|
68
69
  account_globals[account] ||= {}
@@ -85,6 +86,5 @@ module PropertyGenerator
85
86
  end
86
87
  condensed
87
88
  end
88
-
89
89
  end
90
90
  end
@@ -1,9 +1,9 @@
1
1
  module PropertyGenerator
2
-
3
2
  class Service
4
3
  require 'active_support/core_ext/hash'
5
4
 
6
5
  attr_accessor :service
6
+ attr_reader :additional_options
7
7
 
8
8
  def initialize(service_data, config, globals)
9
9
  @service_data = service_data
@@ -28,24 +28,16 @@ module PropertyGenerator
28
28
  @additional_options['secretlabels'] = @service_data['secretlabels'].nil? ? nil : @service_data['secretlabels']
29
29
  end
30
30
 
31
- def additional_options
32
- @additional_options
33
- end
34
-
35
- def service
36
- @service
37
- end
38
-
39
31
  def configmap_name
40
32
  @configmapname
41
33
  end
42
34
 
43
35
  def interpolate
44
36
  environments = @environments
45
- #read in config
46
- #interate through environment and substitute config for values for that environment
47
- environments.each do | env|
48
- #get the map of config for a env
37
+ # read in config
38
+ # interate through environment and substitute config for values for that environment
39
+ environments.each do |env|
40
+ # get the map of config for a env
49
41
  interpolations = @environment_configs[env]['interpolations']
50
42
 
51
43
  # Recursively interate through the properties for an environment and gsub the config
@@ -59,7 +51,7 @@ module PropertyGenerator
59
51
 
60
52
  def interpolate_nested_properties(service_env, interpolations)
61
53
  interpolations.each do |matcher_key, matcher_value|
62
- service_env.each { |k,v| service_env[k] = v.gsub("{#{matcher_key}}", matcher_value) if v.class == String && v.include?("{#{matcher_key}}")}
54
+ service_env.each { |k, v| service_env[k] = v.gsub("{#{matcher_key}}", matcher_value) if v.class == String && v.include?("{#{matcher_key}}") }
63
55
  service_env.values.each do |v|
64
56
  interpolate_nested_properties(v, interpolations) if v.class == Hash
65
57
  v.each_with_index do |val, idx|
@@ -70,7 +62,7 @@ module PropertyGenerator
70
62
  end
71
63
 
72
64
  def merge_env_default(data, environments)
73
- #creates a hash of the environments merged with the defaults
65
+ # creates a hash of the environments merged with the defaults
74
66
  # {service => {env1 => {properties},
75
67
  # env2 => {properties}
76
68
  # }
@@ -80,7 +72,7 @@ module PropertyGenerator
80
72
 
81
73
  environments.each do |env|
82
74
  default_clone = default.dup
83
- #if nil, use set to environments as nothing to merge env with
75
+ # if nil, use set to environments as nothing to merge env with
84
76
  data['environments'] ||= {}
85
77
  data['environments'][env] ||= {}
86
78
  environment_data = data['environments'][env].dup
@@ -99,7 +91,7 @@ module PropertyGenerator
99
91
  end
100
92
 
101
93
  def merge_service_with_globals(globals_data, service_data, environments)
102
- #service will now overwrite globals, merging will be done for each environment
94
+ # service will now overwrite globals, merging will be done for each environment
103
95
  output = {}
104
96
  envs = environments
105
97
  envs.each do |env|
@@ -113,7 +105,5 @@ module PropertyGenerator
113
105
  end
114
106
  output
115
107
  end
116
-
117
108
  end
118
-
119
109
  end
@@ -1,10 +1,10 @@
1
1
  require_relative '../helpers/helpers'
2
+
2
3
  module PropertyGenerator
3
4
  require 'json'
4
5
  require 'fileutils'
5
6
  require 'aws-sdk-s3'
6
7
  class << self
7
-
8
8
  def test_runner(object, test_list)
9
9
  results = {}
10
10
  test_list.each do |test|
@@ -14,9 +14,9 @@ module PropertyGenerator
14
14
  end
15
15
 
16
16
  def get_list_of_files(path, ignore_list)
17
- #Returns a list of files in given path
18
- #Ignores files in a given ignore list
19
- Dir.glob(path + "/**/*").select{ |e| File.file? e unless ignore_list.include?(e.split('/')[(e.split('/')).length - 1])}
17
+ # Returns a list of files in given path
18
+ # Ignores files in a given ignore list
19
+ Dir.glob("#{path}/**/*").select { |e| File.file?(e) unless ignore_list.include?(e.split('/')[-1]) }
20
20
  end
21
21
 
22
22
  def valid_paths(path)
@@ -26,7 +26,7 @@ module PropertyGenerator
26
26
  begin
27
27
  YAML.load_file(file_path)
28
28
  valid_paths << file_path
29
- rescue
29
+ rescue StandardError
30
30
  next
31
31
  end
32
32
  end
@@ -39,7 +39,7 @@ module PropertyGenerator
39
39
  list_of_file_paths.each do |file_path|
40
40
  begin
41
41
  YAML.load_file(file_path)
42
- rescue
42
+ rescue StandardError
43
43
  invalid_paths << file_path
44
44
  end
45
45
  end
@@ -57,21 +57,21 @@ module PropertyGenerator
57
57
  def writer(service_name, finalized, configs, output_path, additional_options)
58
58
  output = []
59
59
  envs = configs.environments
60
- environmental_configs = configs.environment_configs
61
- envs.each do | env|
62
- account = environmental_configs[env]["account"]
63
- region = environmental_configs[env]["region"]
64
- hash = { "properties" => finalized[env] }
60
+ environmental_configs = configs.environment_configs
61
+ envs.each do |env|
62
+ account = environmental_configs[env]['account']
63
+ region = environmental_configs[env]['region']
64
+ hash = { 'properties' => finalized[env] }
65
65
  ['configname', 'stringdata', 'configlabels', 'secretlabels'].each do |setting|
66
- hash[setting] = additional_options[setting] if [setting] unless additional_options[setting].nil?
66
+ hash[setting] = additional_options[setting] unless additional_options[setting].nil?
67
67
  end
68
68
  json = JSON.pretty_generate(hash)
69
- #IF users are specifing a vpc then we will drop property files under the dir that corresponds to the vpc
70
- if environmental_configs[env].key?("vpc") && !environmental_configs[env]["vpc"].nil?
71
- vpc_dir = "#{output_path}/#{account}/#{region}/#{environmental_configs[env]["vpc"]}"
69
+ # IF users are specifing a vpc then we will drop property files under the dir that corresponds to the vpc
70
+ if environmental_configs[env].key?('vpc') && !environmental_configs[env]['vpc'].nil?
71
+ vpc_dir = "#{output_path}/#{account}/#{region}/#{environmental_configs[env]['vpc']}"
72
72
  FileUtils.mkdir_p("#{vpc_dir}/") unless Dir.exist?(vpc_dir)
73
- File.write("#{output_path}/#{account}/#{region}/#{environmental_configs[env]["vpc"]}/#{service_name}.json", json)
74
- output << "#{output_path}/#{account}/#{region}/#{environmental_configs[env]["vpc"]}/#{service_name}.json"
73
+ File.write("#{output_path}/#{account}/#{region}/#{environmental_configs[env]['vpc']}/#{service_name}.json", json)
74
+ output << "#{output_path}/#{account}/#{region}/#{environmental_configs[env]['vpc']}/#{service_name}.json"
75
75
  else
76
76
  FileUtils.mkdir_p("#{output_path}/#{account}/#{region}/") unless Dir.exist?("#{output_path}/#{account}/#{region}/")
77
77
  File.write("#{output_path}/#{account}/#{region}/#{service_name}.json", json)
@@ -83,22 +83,22 @@ module PropertyGenerator
83
83
 
84
84
  def sync(region, account, bucket, file, file_region)
85
85
  s3 = Aws::S3::Resource.new(region: region)
86
- filename = file.split("/").last
86
+ filename = file.split('/').last
87
87
  puts "Destination: #{account}/#{file_region}/#{filename}"
88
88
  puts "Uploading: #{file}"
89
89
  obj = s3.bucket(bucket).object("#{account}/#{file_region}/#{filename}")
90
90
  obj.upload_file(file)
91
91
  end
92
92
 
93
- #Force users to specify VPCs for all environments if specified for one environment.
94
- #This allows us to skip having conditional logic downstream in CPS requests
93
+ # Force users to specify VPCs for all environments if specified for one environment.
94
+ # This allows us to skip having conditional logic downstream in CPS requests
95
95
  def config_enforcer(environment_configs)
96
96
  a_vpc_exists = false
97
97
  environment_configs.each do |environment, config|
98
- if config.key?("vpc")
98
+ if config.key?('vpc')
99
99
  a_vpc_exists = true
100
100
  elsif a_vpc_exists
101
- raise("If you are using VPCs then a VPC must be specified for all environments in the environment_configs")
101
+ raise('If you are using VPCs then a VPC must be specified for all environments in the environment_configs')
102
102
  end
103
103
  end
104
104
  end
@@ -1,50 +1,53 @@
1
1
  module PropertyGenerator
2
2
  require 'yaml'
3
3
  class ConfigLinter
4
+ TESTS = [
5
+ 'config_has_correct_keys',
6
+ 'environment_configs_match_environments_list',
7
+ 'environment_configs_have_valid_region_and_account_values',
8
+ 'environment_configs_have_well_formatted_interpolations',
9
+ 'config_file_is_present'
10
+ ].freeze
4
11
 
5
12
  attr_accessor :configs
6
13
 
7
- def initialize(path)
14
+ def initialize(path, ignored_tests)
8
15
  @configs = check_for_config(path)
16
+ @ignored_tests = ignored_tests
9
17
  end
10
18
 
11
19
  def run_config_tests
12
20
  if @configs == {}
13
21
  tests = ['config_file_is_present']
14
22
  else
15
- tests = ['config_has_correct_keys',
16
- 'environment_configs_match_environments_list',
17
- 'environment_configs_have_valid_region_and_account_values',
18
- 'environment_configs_have_well_formatted_interpolations',
19
- 'config_file_is_present']
23
+ tests = TESTS
20
24
  end
21
- results = PropertyGenerator.test_runner(self, tests)
22
- results
25
+ tests -= @ignored_tests
26
+
27
+ PropertyGenerator.test_runner(self, tests)
23
28
  end
24
29
 
25
30
  def check_for_config(path)
26
- #Tries to load the config file - if is unable to load config.yml it returns an empty hash
27
- #Empty hash is returned so that the rest of the files are still able to be linted instead of stopping at this point.
28
- begin
29
- YAML.load_file(path)
30
- rescue
31
- {}
32
- end
31
+ # Tries to load the config file - if is unable to load config.yml it returns an empty hash
32
+ # Empty hash is returned so that the rest of the files are still able to be linted instead of stopping at this point.
33
+
34
+ YAML.load_file(path)
35
+ rescue StandardError
36
+ {}
33
37
  end
34
38
 
35
39
  def config_file_is_present
36
- status = {status: 'pass', error: ''}
40
+ status = { status: 'pass', error: '' }
37
41
  if @configs == {}
38
42
  status[:status] = 'fail'
39
- status[:error] = "Config.yml file is missing, it is required."
43
+ status[:error] = 'Config.yml file is missing, it is required.'
40
44
  end
41
45
  status
42
46
  end
43
47
 
44
48
  def config_has_correct_keys
45
- status = {status: 'pass', error: ''}
46
- config_keys = ['environments', 'accounts',
47
- 'environment_configs']
49
+ status = { status: 'pass', error: '' }
50
+ config_keys = ['environments', 'accounts', 'environment_configs']
48
51
  if @configs.keys != config_keys
49
52
  status[:status] = 'fail'
50
53
  status[:error] = "Config keys should be 'environments', 'accounts', and 'environment_configs'."
@@ -53,40 +56,37 @@ module PropertyGenerator
53
56
  end
54
57
 
55
58
  def environment_configs_match_environments_list
56
- status = {status: 'pass', error: ''}
59
+ status = { status: 'pass', error: '' }
57
60
  if @configs['environments'] != @configs['environment_configs'].keys
58
61
  status[:status] = 'fail'
59
- status[:error] = "Environments in environment_configs do not match environments listed in config environments."
62
+ status[:error] = 'Environments in environment_configs do not match environments listed in config environments.'
60
63
  end
61
64
  status
62
65
  end
63
66
 
64
67
  def environment_configs_have_valid_region_and_account_values
65
- status = {status: 'pass', error: ''}
66
- environments_missmatch_values = []
67
- any_missmatches = false
68
- @configs['environment_configs'].keys.each do |environment|
69
- unless (@configs['environment_configs'][environment].key?('region')) && @configs['accounts'].include?(@configs['environment_configs'][environment]['account'])
70
- environments_missmatch_values << environment
71
- any_missmatches = true
72
- end
73
- if any_missmatches
74
- status[:status] = 'fail'
75
- status[:error] = "Environments: #{environments_missmatch_values} in environment_configs have a region or account value not listed in top level."
76
- end
68
+ status = { status: 'pass', error: '' }
69
+ environments_missmatch_values = @configs['environment_configs'].reject do |_, env_config|
70
+ env_config.key?('region') && @configs['accounts'].include?(env_config['account'])
71
+ end.keys
72
+
73
+ unless environments_missmatch_values.empty?
74
+ status[:status] = 'fail'
75
+ status[:error] = "Environments: #{environments_missmatch_values} in environment_configs have a region or account value not listed in top level."
77
76
  end
77
+
78
78
  status
79
79
  end
80
80
 
81
81
  def environment_configs_have_well_formatted_interpolations
82
- status = {status: 'pass', error: ''}
82
+ status = { status: 'pass', error: '' }
83
83
  environments_with_bad_interpolations = []
84
84
  any_mistakes = false
85
85
  @configs['environment_configs'].keys.each do |environment|
86
86
  if @configs['environment_configs'][environment]['interpolations'].class == Hash
87
87
  @configs['environment_configs'][environment]['interpolations'].each do |interpolation, value|
88
88
  if value.class != String && value.class != Integer && value.class != Float && value.class != Fixnum
89
- environments_with_bad_interpolations << {environment => interpolation}
89
+ environments_with_bad_interpolations << { environment => interpolation }
90
90
  any_mistakes = true
91
91
  end
92
92
  end
@@ -101,6 +101,5 @@ module PropertyGenerator
101
101
  end
102
102
  status
103
103
  end
104
-
105
104
  end
106
105
  end