Module: Wombat::Common
- Included in:
- Aws, BuildRunner, Crypto, DeleteRunner, DeployRunner, InitRunner, LatestRunner, ListRunner, OutputRunner, UpdateRunner
- Defined in:
- lib/wombat/common.rb
Instance Method Summary collapse
- #audio? ⇒ Boolean
- #banner(msg) ⇒ Object
- #bootstrap_aws ⇒ Object
- #build_nodes ⇒ Object
- #calculate_templates ⇒ Object
- #conf ⇒ Object
- #create_infranodes_json ⇒ Object
- #duration(total) ⇒ Object
- #info(msg) ⇒ Object
- #infranodes ⇒ Object
- #is_mac? ⇒ Boolean
- #is_valid_json?(file) ⇒ Boolean
- #linux ⇒ Object
- #lock ⇒ Object
- #logs ⇒ Object
- #parse_log(log, cloud) ⇒ Object
- #update_lock(cloud) ⇒ Object
- #update_template(cloud) ⇒ Object
- #warn(msg) ⇒ Object
- #wombat ⇒ Object
- #workstations ⇒ Object
Instance Method Details
#audio? ⇒ Boolean
131 132 133 |
# File 'lib/wombat/common.rb', line 131 def audio? is_mac? && conf['audio'] end |
#banner(msg) ⇒ Object
10 11 12 |
# File 'lib/wombat/common.rb', line 10 def (msg) puts "==> #{msg}" end |
#bootstrap_aws ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/wombat/common.rb', line 49 def bootstrap_aws @workstation_passwd = wombat['workstations']['password'] rendered = ERB.new(File.read("#{conf['template_dir']}/bootstrap-aws.erb"), nil, '-').result(binding) Dir.mkdir("#{conf['packer_dir']}/scripts", 0755) unless File.exist?("#{conf['packer_dir']}/scripts") File.open("#{conf['packer_dir']}/scripts/bootstrap-aws.txt", 'w') { |file| file.puts rendered } ("Generated: #{conf['packer_dir']}/scripts/bootstrap-aws.txt") end |
#build_nodes ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/wombat/common.rb', line 79 def build_nodes build_nodes = {} 1.upto(wombat['build-nodes']['count'].to_i) do |i| build_nodes["build-node-#{i}"] = i end build_nodes end |
#calculate_templates ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/wombat/common.rb', line 140 def calculate_templates globs = "*.json" Dir.chdir(conf['packer_dir']) do Array(globs). map { |glob| result = Dir.glob("#{glob}"); result.empty? ? glob : result }. flatten. sort. delete_if { |file| file =~ /\.variables\./ }. map { |template| template.sub(/\.json$/, '') } end end |
#conf ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wombat/common.rb', line 112 def conf conf = wombat['conf'] conf ||= {} conf['files_dir'] ||= 'files' conf['key_dir'] ||= 'keys' conf['cookbook_dir'] ||= 'cookbooks' conf['packer_dir'] ||= 'packer' conf['log_dir'] ||= 'logs' conf['stack_dir'] ||= 'stacks' conf['template_dir'] ||= 'templates' conf['timeout'] ||= 7200 conf['audio'] ||= false conf end |
#create_infranodes_json ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/wombat/common.rb', line 95 def create_infranodes_json infranodes_file_path = File.join(conf['files_dir'], 'infranodes-info.json') if File.exists?(infranodes_file_path) && is_valid_json?(infranodes_file_path) current_state = JSON(File.read(infranodes_file_path)) else current_state = nil end return if current_state == infranodes # yay idempotence File.open(infranodes_file_path, 'w') do |f| f.puts JSON.pretty_generate(infranodes) end end |
#duration(total) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/wombat/common.rb', line 22 def duration(total) total = 0 if total.nil? minutes = (total / 60).to_i seconds = (total - (minutes * 60)) format("%dm%.2fs", minutes, seconds) end |
#info(msg) ⇒ Object
14 15 16 |
# File 'lib/wombat/common.rb', line 14 def info(msg) puts " #{msg}" end |
#infranodes ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/wombat/common.rb', line 70 def infranodes unless wombat['infranodes'].nil? wombat['infranodes'].sort else puts 'No infranodes listed in wombat.yml' {} end end |
#is_mac? ⇒ Boolean
127 128 129 |
# File 'lib/wombat/common.rb', line 127 def is_mac? (/darwin/ =~ RUBY_PLATFORM) != nil end |
#is_valid_json?(file) ⇒ Boolean
265 266 267 268 269 270 271 272 |
# File 'lib/wombat/common.rb', line 265 def is_valid_json?(file) begin JSON.parse(file) true rescue JSON::ParserError => e false end end |
#linux ⇒ Object
108 109 110 |
# File 'lib/wombat/common.rb', line 108 def linux wombat['linux'].nil? ? 'ubuntu' : wombat['linux'] end |
#lock ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/wombat/common.rb', line 40 def lock if !File.exist?('wombat.lock') warn('No wombat.lock found') return 1 else JSON.parse(File.read('wombat.lock')) end end |
#logs ⇒ Object
135 136 137 138 |
# File 'lib/wombat/common.rb', line 135 def logs path = "#{conf['log_dir']}/#{cloud}*.log" Dir.glob(path).reject { |l| !l.match(wombat['linux']) } end |
#parse_log(log, cloud) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wombat/common.rb', line 57 def parse_log(log, cloud) regex = case cloud when 'gcp' 'A disk image was created:' when 'azure' 'OSDiskUri:' else "#{wombat['aws']['region']}:" end File.read(log).split("\n").grep(/#{regex}/) {|x| x.split[1]}.last end |
#update_lock(cloud) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/wombat/common.rb', line 152 def update_lock(cloud) copy = {} copy = wombat # Check that the copy contains a key for the named cloud unless copy.key?(cloud) throw "The Cloud '#{cloud}' is not specified in Wombat" end # Determine the region/location/zone for the specific cloud case cloud when 'aws' region = copy['aws']['region'] when 'azure' region = copy['azure']['location'] when 'gce' region = copy['gce']['zone'] end linux = copy['linux'] copy['amis'] = { region => {} } if logs.length == 0 warn('No logs found - skipping lock update') else logs.each do |log| case log when /build-node/ copy['amis'][region]['build-node'] ||= {} num = log.split('-')[3] copy['amis'][region]['build-node'].store(num, parse_log(log, cloud)) when /workstation/ copy['amis'][region]['workstation'] ||= {} num = log.split('-')[2] copy['amis'][region]['workstation'].store(num, parse_log(log, cloud)) when /infranodes/ copy['amis'][region]['infranodes'] ||= {} name = log.split('-')[2] copy['amis'][region]['infranodes'].store(name, parse_log(log, cloud)) else instance = log.match("#{cloud}-(.*)-#{linux}\.log")[1] copy['amis'][region].store(instance, parse_log(log, cloud)) end end copy['last_updated'] = Time.now.gmtime.strftime('%Y%m%d%H%M%S') ('Updating wombat.lock') File.open('wombat.lock', 'w') do |f| f.write(JSON.pretty_generate(copy)) end end end |
#update_template(cloud) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/wombat/common.rb', line 204 def update_template(cloud) if lock == 1 warn('No lock - skipping template creation') else # Determine the region/location/zone for the specific cloud case cloud when 'aws' region = lock['aws']['region'] template_file = "cfn.json.erb" @chef_server_ami = lock['amis'][region]['chef-server'] @automate_ami = lock['amis'][region]['automate'] @compliance_ami = lock['amis'][region]['compliance'] @availability_zone = lock['aws']['az'] @iam_roles = lock['aws']['iam_roles'] when 'azure' region = lock['azure']['location'] @storage_account = lock['azure']['storage_account'] template_file = "arm.json.erb" @chef_server_uri = lock['amis'][region]['chef-server'] @automate_uri = lock['amis'][region]['automate'] @compliance_uri = lock['amis'][region]['compliance'] # set an admin password for the machines that are created @password = "aAan0orxevCma4gG" when 'gce' region = lock['gce']['zone'] end if lock['amis'][region].key?('build-node') @build_nodes = lock['build-nodes']['count'].to_i @build_node_ami = {} 1.upto(@build_nodes) do |i| @build_node_ami[i] = lock['amis'][region]['build-node'][i.to_s] end end @infra = {} infranodes.each do |name, _rl| @infra[name] = lock['amis'][region]['infranodes'][name] end if lock['amis'][region].key?('workstation') @workstations = lock['workstations']['count'].to_i @workstation_ami = {} 1.upto(@workstations) do |i| @workstation_ami[i] = lock['amis'][region]['workstation'][i.to_s] end end @demo = lock['name'] @version = lock['version'] @ttl = lock['ttl'] rendered_cfn = ERB.new(File.read("#{conf['template_dir']}/#{template_file}"), nil, '-').result(binding) Dir.mkdir(conf['stack_dir'], 0755) unless File.exist?(conf['stack_dir']) File.open("#{conf['stack_dir']}/#{@demo}.json", 'w') { |file| file.puts rendered_cfn } ("Generated: #{conf['stack_dir']}/#{@demo}.json") end end |
#warn(msg) ⇒ Object
18 19 20 |
# File 'lib/wombat/common.rb', line 18 def warn(msg) puts ">>> #{msg}" end |
#wombat ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wombat/common.rb', line 29 def wombat @wombat_yml ||= ENV['WOMBAT_YML'] unless ENV['WOMBAT_YML'].nil? @wombat_yml ||= 'wombat.yml' if !File.exist?(@wombat_yml) warn('No wombat.yml found, copying example') gen_dir = "#{File.expand_path("../..", File.dirname(__FILE__))}/generator_files" FileUtils.cp_r "#{gen_dir}/wombat.yml", Dir.pwd end YAML.load(File.read(@wombat_yml)) end |
#workstations ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/wombat/common.rb', line 87 def workstations workstations = {} 1.upto(wombat['workstations']['count'].to_i) do |i| workstations["workstation-#{i}"] = i end workstations end |