Module: Common
- Included in:
- BuildRunner, DeleteRunner, DeployRunner, InitRunner, 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
- #gen_ssh_key ⇒ Object
- #gen_x509_cert(hostname) ⇒ Object
- #info(msg) ⇒ Object
- #infranodes ⇒ Object
- #is_mac? ⇒ 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
176 177 178 |
# File 'lib/wombat/common.rb', line 176 def audio? is_mac? && conf['audio'] end |
#banner(msg) ⇒ Object
11 12 13 |
# File 'lib/wombat/common.rb', line 11 def (msg) puts "==> #{msg}" end |
#bootstrap_aws ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/wombat/common.rb', line 48 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
126 127 128 129 130 131 132 |
# File 'lib/wombat/common.rb', line 126 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
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/wombat/common.rb', line 184 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
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/wombat/common.rb', line 158 def conf conf = wombat['conf'] conf ||= {} 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
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/wombat/common.rb', line 142 def create_infranodes_json if File.exists?("#{conf['packer_dir']}/file/infranodes-info.json") current_state = JSON(File.read('files/infranodes-info.json')) else current_state = nil end return if current_state == infranodes # yay idempotence File.open("#{conf['packer_dir']}/files/infranodes-info.json", 'w') do |f| f.puts JSON.pretty_generate(infranodes) end end |
#duration(total) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/wombat/common.rb', line 23 def duration(total) total = 0 if total.nil? minutes = (total / 60).to_i seconds = (total - (minutes * 60)) format("%dm%.2fs", minutes, seconds) end |
#gen_ssh_key ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/wombat/common.rb', line 94 def gen_ssh_key rsa_key = OpenSSL::PKey::RSA.new 2048 type = rsa_key.ssh_type data = [rsa_key.to_blob].pack('m0') openssh_format = "#{type} #{data}" Dir.mkdir(conf['key_dir'], 0755) unless File.exist?(conf['key_dir']) if File.exist?("#{conf['key_dir']}/public.pub") && File.exist?("#{conf['key_dir']}/private.pem") puts 'An SSH keypair already exists' else File.open("#{conf['key_dir']}/public.pub", 'w') { |file| file.puts openssh_format } File.open("#{conf['key_dir']}/private.pem", 'w') { |file| file.puts rsa_key.to_pem } puts 'SSH Keypair created' end end |
#gen_x509_cert(hostname) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 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 |
# File 'lib/wombat/common.rb', line 56 def gen_x509_cert(hostname) rsa_key = OpenSSL::PKey::RSA.new(2048) public_key = rsa_key.public_key subject = "/C=AU/ST=New South Wales/L=Sydney/O=#{wombat['org']}/OU=wombats/CN=#{wombat['domain_prefix']}#{hostname}.#{wombat['domain']}" cert = OpenSSL::X509::Certificate.new cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject) cert.not_before = Time.now cert.not_after = Time.now + 365 * 24 * 60 * 60 cert.public_key = public_key cert.serial = 0x0 cert.version = 2 ef = OpenSSL::X509::ExtensionFactory.new ef.subject_certificate = cert ef.issuer_certificate = cert cert.extensions = [ ef.create_extension('basicConstraints', 'CA:TRUE', true), ef.create_extension('subjectKeyIdentifier', 'hash'), # ef.create_extension("keyUsage", "cRLSign,keyCertSign", true), ] cert.add_extension ef.create_extension('authorityKeyIdentifier', 'keyid:always,issuer:always') cert.sign(rsa_key, OpenSSL::Digest::SHA256.new) Dir.mkdir(conf['key_dir'], 0755) unless File.exist?(conf['key_dir']) if File.exist?("#{conf['key_dir']}/#{hostname}.crt") && File.exist?("#{conf['key_dir']}/#{hostname}.key") puts "An x509 certificate already exists for #{hostname}" else File.open("#{conf['key_dir']}/#{hostname}.crt", 'w') { |file| file.puts cert.to_pem } File.open("#{conf['key_dir']}/#{hostname}.key", 'w') { |file| file.puts rsa_key.to_pem } puts "Certificate created for #{wombat['domain_prefix']}#{hostname}.#{wombat['domain']}" end end |
#info(msg) ⇒ Object
15 16 17 |
# File 'lib/wombat/common.rb', line 15 def info(msg) puts " #{msg}" end |
#infranodes ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/wombat/common.rb', line 118 def infranodes unless wombat['infranodes'].nil? wombat['infranodes'].sort else puts 'No infranodes listed in wombat.yml' end end |
#is_mac? ⇒ Boolean
172 173 174 |
# File 'lib/wombat/common.rb', line 172 def is_mac? (/darwin/ =~ RUBY_PLATFORM) != nil end |
#linux ⇒ Object
154 155 156 |
# File 'lib/wombat/common.rb', line 154 def linux wombat['linux'].nil? ? 'ubuntu' : wombat['linux'] end |
#lock ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/wombat/common.rb', line 39 def lock if !File.exists?('wombat.lock') warn('No wombat.lock found') return 1 else JSON.parse(File.read('wombat.lock')) end end |
#logs ⇒ Object
180 181 182 |
# File 'lib/wombat/common.rb', line 180 def logs Dir.glob("#{conf['log_dir']}/#{cloud}*.log").reject { |l| !l.match(wombat['linux']) } end |
#parse_log(log, cloud) ⇒ Object
113 114 115 116 |
# File 'lib/wombat/common.rb', line 113 def parse_log(log, cloud) regex = cloud == 'gcp' ? "A disk image was created:" : "#{wombat['aws']['region']}:" File.read(log).split("\n").grep(/#{regex}/) {|x| x.split[1]}.last end |
#update_lock(cloud) ⇒ Object
196 197 198 199 200 201 202 203 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 |
# File 'lib/wombat/common.rb', line 196 def update_lock(cloud) copy = {} copy = wombat region = copy[cloud]['region'] 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
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 264 265 |
# File 'lib/wombat/common.rb', line 233 def update_template(cloud) if lock == 1 warn('No lock - skipping template creation') else region = lock['aws']['region'] @chef_server_ami = lock['amis'][region]['chef-server'] @automate_ami = lock['amis'][region]['automate'] @compliance_ami = lock['amis'][region]['compliance'] @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 @infra = {} infranodes.each do |name, _rl| @infra[name] = lock['amis'][region]['infranodes'][name] end @workstations = lock['workstations']['count'].to_i @workstation_ami = {} 1.upto(@workstations) do |i| @workstation_ami[i] = lock['amis'][region]['workstation'][i.to_s] end @availability_zone = lock['aws']['az'] @iam_roles = lock['aws']['iam_roles'] @demo = lock['name'] @version = lock['version'] @ttl = lock['ttl'] rendered_cfn = ERB.new(File.read("#{conf['template_dir']}/cfn.json.erb"), 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
19 20 21 |
# File 'lib/wombat/common.rb', line 19 def warn(msg) puts ">>> #{msg}" end |
#wombat ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/wombat/common.rb', line 30 def wombat if !File.exists?('wombat.yml') warn('No wombat.yml found, copying example') gen_dir = "#{File.("../..", File.dirname(__FILE__))}/generator_files" FileUtils.cp_r "#{gen_dir}/wombat.yml", Dir.pwd end YAML.load(File.read('wombat.yml')) end |
#workstations ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/wombat/common.rb', line 134 def workstations workstations = {} 1.upto(wombat['workstations']['count'].to_i) do |i| workstations["workstation-#{i}"] = i end workstations end |