Class: OpsBuild::Commands::Build

Inherits:
Thor
  • Object
show all
Defined in:
lib/ops_build/commands/build.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.shared_optionsObject



4
5
6
7
8
9
# File 'lib/ops_build/commands/build.rb', line 4

def self.shared_options
  option :ec2_region, type: :string, aliases: '-R', desc: 'AWS EC2 region', default: 'us-east-1'
  option :aws_access, type: :string, aliases: '-A', desc: 'AWS Access key'
  option :aws_secret, type: :string, aliases: '-S', desc: 'AWS Secret key'
  option :params,     type: :string, aliases: '-p', desc: 'path to JSON as params'
end

Instance Method Details

#packer(template) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ops_build/commands/build.rb', line 15

def packer(template)
  packer = Packer.new
  berkshelf = Berkshelf.new(dir: options[:berk_dir], silent: false)
  params = if options[:params]
             raise "JSON #{options[:params]} not found!" unless File.exists?(options[:params])
             JSON.parse(File.read(options[:params]), symbolize_names: true)
           else
             {}
           end

  OpsBuild.logger.info("Building VM using packer from template #{template}")

  aws_access_key = options[:aws_access] || ENV['AWS_ACCESS_KEY']
  aws_secret_key = options[:aws_secret] || ENV['AWS_SECRET_KEY']
  aws_region     = options[:ec2_region] || ENV['AWS_EC2_REGION']

  # Add some config variables
  packer.add_user_variable(:aws_access_key, aws_access_key) if aws_access_key
  packer.add_user_variable(:aws_secret_key, aws_secret_key) if aws_secret_key
  packer.add_user_variable(:aws_region, aws_region) if aws_region
  packer.add_user_variable(:cookbook_path, berkshelf.dir)
  params.each { |k, v| packer.add_user_variable(k, v) }

  begin
    # Install missing cookbooks
    berkshelf.install if options[:vendor]

    # Load cookbooks to correct dir.
    berkshelf.vendor if options[:vendor]

    # Validate packer template
    packer.validate(template)

    # Run packer
    packer.build(template)
  rescue => e
    OpsBuild.logger.error(e.message)
    exit(1)
  ensure
    OpsBuild.logger.info("Cleaning up cookbooks/packer files from system.")
    berkshelf.cleanup
    packer.cleanup
  end
end

#vagrant(path) ⇒ Object



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ops_build/commands/build.rb', line 64

def vagrant(path)
  path = File.expand_path(path)
  raise "Vagrantfile #{path} not found!" unless File.exists?(path)

  # TODO: parse jason params -> 'base_url'

  env = { 'VAGRANT_CWD' => File.dirname(path) }
  if options[:params]
   raise "JSON #{options[:params]} not found!" unless File.exists?(options[:params])
   JSON.parse(File.read(options[:params])).each do |k, v|
     env[k.to_s.upcase] = v
   end
  end

  OpsBuild.logger.info('Running vagrant up')
  Utils::execute(
      "vagrant up #{options[:only]}", # still correct even if --only not provided, because nil.to_s == ""
      log_prefix: 'vagrant:',
      env: env)

  uuid = SecureRandom.uuid
  OpsBuild.logger.info("Running vagrant ssh cmd 'info --manifest-mf > /vagrant/#{uuid}'")
  Utils::execute(
  "vagrant ssh #{options[:only]} -c 'info --manifest-mf > /vagrant/#{uuid}'",
  log_prefix: 'vagrant:',
  env: env)
  info_path = File.join(env['VAGRANT_CWD'], uuid)
  FileUtils.cp(info_path, "#{options[:output]}.metadata")

  OpsBuild.logger.info('Running vagrant package')
  Utils::execute(
      "vagrant package #{options[:only]} --output #{options[:output]}",
      log_prefix: 'vagrant:',
      env: env)
ensure
  OpsBuild.logger.info('Running vagrant destroy')
  Utils::execute(
      'vagrant destroy -f',
      log_prefix: 'vagrant',
      env: env
  )
  # TODO: vboxmanage
end