|
1 | 1 | #!/usr/bin/env ruby
|
| 2 | +# frozen_string_literal: true |
| 3 | + |
2 | 4 | #
|
3 | 5 | # createstackmoduleoutputs.rb
|
4 | 6 | # Author: erhard.wais@boehringer-ingelheim.com
|
|
16 | 18 | require 'json'
|
17 | 19 | require 'open3'
|
18 | 20 |
|
19 |
| -INSPECAWS = "" |
20 |
| -INSPECAWSTAG = "" |
21 |
| -CAPTUREFROMSTACK = 'terraform-config-inspect --json' |
22 |
| -ENVNAME = 'KITCHEN_SUITE_NAME' |
23 |
| -OUTPUTSTF = './stackmodulesoutputs.tf' |
24 |
| -BANNER = "# This file has been created automatically.\n\n" |
25 |
| -BANNER2 = "name: stackdefault\n" + "supports:\n" + " - platform: aws\n" + "depends:\n" + " - name: inspec-aws\n" + " git: https://github.com/inspec/inspec-aws\n" + " tag: v1.33.0\n" + "# Begin - blueprint inspec profiles\n" |
26 |
| - |
27 |
| -stdout, stderr, status = Open3.capture3(CAPTUREFROMSTACK) |
28 |
| - |
29 |
| -myEnv = ENV.fetch(ENVNAME,"default") |
30 |
| -moduleNames = Hash.new |
31 |
| -moduleSources = Hash.new |
| 21 | +CAPTUREFROMSTACK = 'terraform-config-inspect --json' |
| 22 | +ENVNAME = 'KITCHEN_SUITE_NAME' |
| 23 | +OUTPUTSTF = './stackmodulesoutputs.tf' |
| 24 | +BANNER = "# This file has been created automatically.\n\n" |
| 25 | +INSPECYMLTMPLFILE = './test/integration/default/inspec.yml.tmpl' |
| 26 | +INSPECYMLTMPLSTR = <<~MYYML |
| 27 | + --- |
| 28 | + name: stackdefault |
| 29 | + supports: |
| 30 | + - platform: aws |
| 31 | + depends: |
| 32 | + - name: inspec-aws |
| 33 | + git: https://github.com/inspec/inspec-aws |
| 34 | + tag: v1.33.0 |
| 35 | +MYYML |
| 36 | + |
| 37 | +INSPECYMLHEAD = if File.exist?(INSPECYMLTMPLFILE) |
| 38 | + File.read(INSPECYMLTMPLFILE) |
| 39 | + else |
| 40 | + INSPECYMLTMPLSTR |
| 41 | + end |
| 42 | + |
| 43 | +stdout, _stderr, _status = Open3.capture3(CAPTUREFROMSTACK) |
| 44 | + |
| 45 | +myEnv = ENV.fetch(ENVNAME, 'default') |
| 46 | +moduleNames = {} |
| 47 | +moduleSources = {} |
32 | 48 | uniqueBP = Hash.new(0)
|
33 | 49 |
|
34 |
| -CAPTUREFROMFIXTURE = "terraform-config-inspect ./test/fixtures/#{myEnv} --json" |
| 50 | +CAPTUREFROMFIXTURE = "terraform-config-inspect ./test/fixtures/#{myEnv} --json" |
35 | 51 |
|
36 |
| -stdoutfixture, stderrfixture, statusfixture = Open3.capture3(CAPTUREFROMFIXTURE) |
| 52 | +stdoutfixture, _stderrfixture, _statusfixture = Open3.capture3(CAPTUREFROMFIXTURE) |
37 | 53 |
|
38 | 54 | stackName = File.basename(Dir.getwd)
|
39 | 55 | outputTF = File.open(OUTPUTSTF, 'w')
|
|
45 | 61 | modoutTF.write(BANNER)
|
46 | 62 | allBPsRB.write(BANNER)
|
47 | 63 | inspecYML.write(BANNER)
|
48 |
| -inspecYML.write(BANNER2) |
| 64 | +inspecYML.write(INSPECYMLHEAD) |
49 | 65 |
|
50 | 66 | # get module section from main.json
|
51 | 67 | allModules = JSON.parse(stdout)['module_calls']
|
52 | 68 | allModulesfixtures = JSON.parse(stdoutfixture)['module_calls']
|
53 | 69 |
|
54 |
| -fixturemodulename = "" |
55 |
| -allModulesfixtures.each do |singleModule| |
56 |
| - fixturemodulename = singleModule[0] |
| 70 | +fixturemodulename = '' |
| 71 | +allModulesfixtures.each do |singlemodule| |
| 72 | + fixturemodulename = singlemodule[0] |
57 | 73 | end
|
58 | 74 |
|
59 | 75 | # for each module
|
60 |
| -allModules.each do |singleModule| |
61 |
| - |
| 76 | +allModules.each do |singlemodule| |
62 | 77 | # get modulename and attributes
|
63 |
| - name = singleModule[0] |
64 |
| - attribute = singleModule[1] |
| 78 | + name = singlemodule[0] |
| 79 | + attribute = singlemodule[1] |
65 | 80 |
|
66 | 81 | moduleOut = name
|
67 | 82 | moduleValue = "#{name}.*"
|
68 | 83 | fileName = attribute['source']
|
69 |
| - moduleBP = File.basename(fileName[0..fileName.index('git?ref')], ".*") |
| 84 | + moduleBP = File.basename(fileName[0..fileName.index('git?ref')], '.*') |
70 | 85 |
|
71 | 86 | # create helper hashes to track which module uses which BP and how often
|
72 | 87 | moduleNames[moduleOut] = moduleBP
|
73 | 88 | moduleSources[moduleBP] = fileName
|
74 | 89 | uniqueBP[moduleBP] += 1
|
75 | 90 |
|
76 |
| -# create outputX.tf |
77 |
| - outputTF.write("output \"module_#{moduleOut.gsub(/-/,"_")}\" {\n") |
78 |
| - outputTF.write(" value = module.#{moduleValue}\n}\n") |
| 91 | + # create outputX.tf |
| 92 | + outputTF.write("output \"module_#{moduleOut.gsub(/-/, '_')}\" {\n") |
| 93 | + outputTF.write(" value = module.#{moduleValue}\n}\n") |
79 | 94 | end
|
80 | 95 |
|
81 | 96 | # create moduleoutputs.tf
|
82 |
| -stackOut = stackName.gsub(/-/,"_") |
83 |
| -stackValue = "module.#{stackOut}_default" |
| 97 | +stackOut = stackName.gsub(/-/, '_') |
| 98 | +# FIXME#stackValue = "module.#{stackOut}_default" |
84 | 99 |
|
85 | 100 | modoutTF.write("output \"module_#{stackOut}\" {\n")
|
86 | 101 | modoutTF.write(" value = module.#{fixturemodulename}.*\n}\n")
|
87 | 102 |
|
88 | 103 | uniqueBP.each do |name, count|
|
89 |
| - duplicates = Array.new |
| 104 | + duplicates = [] |
90 | 105 |
|
91 |
| - moduleNames.select { |k, v| v == name }.keys.each {|dup| duplicates << dup} |
| 106 | + moduleNames.select { |_k, v| v == name }.each_key { |dup| duplicates << dup } |
92 | 107 |
|
93 | 108 | if count == 1
|
94 |
| - moduleValue = "module.#{fixturemodulename}.module_#{duplicates[0].gsub(/-/,"_")}.*" |
| 109 | + moduleValue = "module.#{fixturemodulename}.module_#{duplicates[0].gsub(/-/, '_')}.*" |
95 | 110 | moduleV2 = "module.#{duplicates[0]}.*"
|
96 | 111 | else
|
97 |
| - moduleValue = "concat(" |
98 |
| - duplicates.each_index {|i| moduleValue += "module.#{fixturemodulename}.module_#{duplicates[i].gsub(/-/,"_")}.*,"} |
99 |
| - moduleValue = moduleValue[0...-1] + ")" |
| 112 | + moduleValue = 'concat(' |
| 113 | + duplicates.each_index { |i| moduleValue += "module.#{fixturemodulename}.module_#{duplicates[i].gsub(/-/, '_')}.*," } |
| 114 | + moduleValue = "#{moduleValue[0...-1]})" |
100 | 115 |
|
101 |
| - moduleV2 = "concat(" |
102 |
| - duplicates.each_index {|i| moduleV2 += "module.#{duplicates[i]}.*,"} |
103 |
| - moduleV2 = moduleV2[0...-1] + ")" |
| 116 | + moduleV2 = 'concat(' |
| 117 | + duplicates.each_index { |i| moduleV2 += "module.#{duplicates[i]}.*," } |
| 118 | + moduleV2 = "#{moduleV2[0...-1]})" |
104 | 119 | end
|
105 | 120 |
|
106 |
| - moduleOut = name.gsub(/-/,"_") |
107 |
| - modoutTF.write( "output \"module_#{moduleOut}\" {\n") |
108 |
| - modoutTF.write( " value = #{moduleValue}\n}\n") |
| 121 | + moduleOut = name.gsub(/-/, '_') |
| 122 | + modoutTF.write("output \"module_#{moduleOut}\" {\n") |
| 123 | + modoutTF.write(" value = #{moduleValue}\n}\n") |
109 | 124 |
|
110 | 125 | # write stackmoduleoutputs.tf
|
111 |
| - outputTF.write("output \"module_#{moduleOut.gsub(/-/,"_")}\" {\n") |
| 126 | + outputTF.write("output \"module_#{moduleOut.gsub(/-/, '_')}\" {\n") |
112 | 127 | outputTF.write(" value = #{moduleV2}\n}\n")
|
113 | 128 |
|
114 | 129 | # read the source/filename from the helper hash
|
115 |
| - fileName = moduleSources.select { |k, v| k == name }[name] |
| 130 | + fileName = moduleSources.select { |k, _v| k == name }[name] |
116 | 131 |
|
117 | 132 | # check if is repository and if it is a BI blueprint
|
118 | 133 | hasGit = fileName.index('git::')
|
119 | 134 | hasGitRef = fileName.index('.git?ref')
|
120 | 135 | isBP = fileName.index('blueprint')
|
121 | 136 |
|
122 |
| - if isBP |
123 |
| - # write blueprints.rb |
124 |
| - allBPsRB.write("include_controls '#{moduleOut}'\n") |
125 |
| - |
126 |
| - # write inspec.yml |
127 |
| - inspecYML.write(" - name: #{moduleOut}\n") |
128 |
| - |
129 |
| - if hasGit |
130 |
| - # puts " ... Tag #{fileName[(hasGitRef + 9)..]} " |
131 |
| - # puts " ... url #{fileName[(hasGit + 5)..(hasGitRef + 3)]} " |
132 |
| - # add +9 = length of .git?ref + 1 |
133 |
| - inspecYML.write(" git: #{fileName[(hasGit + 5)..(hasGitRef + 3)]}\n") |
134 |
| - inspecYML.write(" tag: #{fileName[(hasGitRef + 9)..]}\n") |
135 |
| - inspecYML.write(" relative_path: test/integration/#{myEnv}\n") |
136 |
| - else |
137 |
| - inspecYML.write(" path: ../../../#{fileName}\n") |
138 |
| - end |
| 137 | + next unless isBP |
| 138 | + |
| 139 | + # write blueprints.rb |
| 140 | + allBPsRB.write("include_controls '#{moduleOut}'\n") |
| 141 | + |
| 142 | + # write inspec.yml |
| 143 | + inspecYML.write(" - name: #{moduleOut}\n") |
| 144 | + |
| 145 | + if hasGit |
| 146 | + # puts " ... Tag #{fileName[(hasGitRef + 9)..]} " |
| 147 | + # puts " ... url #{fileName[(hasGit + 5)..(hasGitRef + 3)]} " |
| 148 | + # add +9 = length of .git?ref + 1 |
| 149 | + inspecYML.write(" git: #{fileName[(hasGit + 5)..(hasGitRef + 3)]}\n") |
| 150 | + inspecYML.write(" tag: #{fileName[(hasGitRef + 9)..]}\n") |
| 151 | + inspecYML.write(" relative_path: test/integration/#{myEnv}\n") |
| 152 | + else |
| 153 | + inspecYML.write(" path: ../../../#{fileName}\n") |
139 | 154 | end
|
140 | 155 | end
|
141 | 156 |
|
142 |
| -outputTF.close unless outputTF.nil? |
143 |
| -modoutTF.close unless modoutTF.nil? |
144 |
| -allBPsRB.close unless allBPsRB.nil? |
| 157 | +outputTF&.close |
| 158 | +modoutTF&.close |
| 159 | +allBPsRB&.close |
145 | 160 | inspecYML.close unless allBPsRB.nil?
|
146 | 161 |
|
147 | 162 | # pretty format modified tf files, so that a cyclic execution of terraform fmt is prohibited.
|
148 |
| -TFFMT = "terraform fmt" |
| 163 | +TFFMT = 'terraform fmt' |
149 | 164 | stdouttffmt, stderrtffmt, statustffmt = Open3.capture3(TFFMT)
|
150 | 165 | pp stdouttffmt
|
151 | 166 | pp stderrtffmt
|
|
0 commit comments