Class: KnifePlugins::VagrantTest

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/vagrant_test.rb

Instance Method Summary collapse

Instance Method Details

#build_port_forwards(ports) ⇒ Object



79
80
81
# File 'lib/chef/knife/vagrant_test.rb', line 79

def build_port_forwards(ports)
  ports.collect { |k, v| "config.vm.forward_port(#{k}, #{v})" }.join("\n")
end

#build_runlist(runlist) ⇒ Object

TODO - hook into chef/runlist



75
76
77
# File 'lib/chef/knife/vagrant_test.rb', line 75

def build_runlist(runlist)
  runlist.collect { |i| "\"#{i}\"" }.join(",\n")
end

#build_vagrantfileObject

TODO: see if there’s a way to pass this whole thing in as an object or hash or something, instead of writing a file to disk.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/knife/vagrant_test.rb', line 84

def build_vagrantfile
  file = <<-EOF
    Vagrant::Config.run do |config|
      #{build_port_forwards(config[:port_forward])}
      config.vm.box = "#{config[:box]}"
      config.vm.host_name = "#{config[:hostname]}"
      config.vm.customize [ "modifyvm", :id, "--memory", #{config[:memsize]} ]
      config.vm.customize [ "modifyvm", :id, "--name", "#{config[:box]}" ]
      config.vm.box_url = "#{config[:box_url]}"
      config.vm.provision :chef_client do |chef|
        chef.chef_server_url = "#{Chef::Config[:chef_server_url]}"
        chef.validation_key_path = "#{Chef::Config[:validation_key]}"
        chef.validation_client_name = "#{Chef::Config[:validation_client_name]}"
        chef.node_name = "#{config[:hostname]}"
        chef.provisioning_path = "#{Chef::Config[:provisioning_path]}"
        chef.log_level = :#{config[:chef_loglevel].downcase}
        chef.environment = "#{Chef::Config[:environment]}"
        chef.run_list = [
          #{build_runlist(config[:vagrant_run_list])}
        ]
      end
    end
  EOF
  file
end

#cleanup(path) ⇒ Object



114
115
116
# File 'lib/chef/knife/vagrant_test.rb', line 114

def cleanup(path)
  File.delete(path)
end

#runObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef/knife/vagrant_test.rb', line 118

def run
  ui.msg('Loading vagrant environment..')
  Dir.chdir(config[:vagrant_dir])
  vagrantfile = "#{config[:vagrant_dir]}/Vagrantfile"
  write_vagrantfile(vagrantfile, build_vagrantfile)
  @vagrant_env = Vagrant::Environment.new(:cwd => config[:vagrant_dir], :ui_class => Vagrant::UI::Colored)
  @vagrant_env.load!
  begin
    @vagrant_env.cli("up")
  rescue
    raise # I'll put some error handling here later.
  ensure
    if config[:destroy]
      ui.confirm("Destroy vagrant box #{config[:box]} and delete chef node and client")
      args = %w[ destroy --force ]
      @vagrant_env.cli(args)
      config[:yes]
      delete_object(Chef::Node, config[:hostname])
      delete_object(Chef::ApiClient, config[:hostname])
    end
  end
end

#write_vagrantfile(path, content) ⇒ Object



110
111
112
# File 'lib/chef/knife/vagrant_test.rb', line 110

def write_vagrantfile(path, content)
  File.open(path, 'w') { |f| f.write(content) }
end