knife-vagrant 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +47 -0
  2. data/lib/chef/knife/vagrant_test.rb +134 -0
  3. metadata +48 -0
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ knife-vagrant
2
+ ========
3
+ First pass at knife-vagrant, a knife plugin that will create a Vagrant box, then run a set of Chef recipes in it. Eventually will also run cucumber tests and report the results.
4
+
5
+ Requirements
6
+ -------------------
7
+
8
+ - Virtualbox 4.1.X
9
+ - Vagrant (http://vagrantup.com/)
10
+ - chef gem install (client only)
11
+
12
+ Installation
13
+ -------------------
14
+ #### Script Install
15
+ Copy https://github.com/garrettux/knife-vagrant/blob/master/lib/chef/knife/vagrant_test.rb to your .chef/plugins/knife directory.
16
+
17
+ #### Gem Install
18
+ Not ready yet, will be available on rubygems.org later.
19
+
20
+ Usage
21
+ -------------------
22
+ knife vagrant test (options)
23
+
24
+ -b, --box BOX Name of vagrant box to be provisioned
25
+
26
+ -U, --box-url URL URL of pre-packaged vbox template. Can be a local path or an HTTP URL. Defaults to ./package.box
27
+
28
+ -l, --chef-loglevel LEVEL Logging level for the chef-client process that runs inside the provisioned VM. Default is INFO
29
+
30
+ -x, --destroy Destroy vagrant box and delete chef node/client when finished
31
+
32
+ -E, --environment ENVIRONMENT Set the Chef environment
33
+
34
+ -H, --hostname HOSTNAME Hostname to be set as hostname on vagrant box when provisioned
35
+
36
+ -m, --memsize MEMORY Amount of RAM to allocate to provisioned VM, in MB. Defaults to 1024
37
+
38
+ -D, --vagrant-dir PATH Path to vagrant project directory. Defaults to cwd (/home/mgarrett/knife-vagrant) if not specified
39
+
40
+ -r, --vagrant-run-list RUN_LIST Comma separated list of roles/recipes to apply
41
+
42
+ -y, --yes Say yes to all prompts for confirmation
43
+
44
+ Disclaimer
45
+ -------------------
46
+
47
+ This is my first stab at it, so I can't make any promises as to how well it works just yet.
@@ -0,0 +1,134 @@
1
+ # knife-vagrant
2
+ # knife plugin for spinning up a vagrant instance and testing a runlist.
3
+
4
+ module KnifePlugins
5
+ class VagrantTest < Chef::Knife
6
+
7
+ banner "knife vagrant test (options)"
8
+
9
+ deps do
10
+ require 'rubygems'
11
+ require 'pp'
12
+ require 'vagrant'
13
+ require 'vagrant/cli'
14
+ require 'chef/node'
15
+ require 'chef/api_client'
16
+ end
17
+
18
+ # Default is nil here because if :cwd passed to the Vagrant::Environment object is nil,
19
+ # it defaults to Dir.pwd, which is the cwd of the running process.
20
+ option :vagrant_dir,
21
+ :short => '-D PATH',
22
+ :long => '--vagrant-dir PATH',
23
+ :description => "Path to vagrant project directory. Defaults to cwd (#{Dir.pwd}) if not specified",
24
+ :default => nil
25
+
26
+ option :vagrant_run_list,
27
+ :short => "-r RUN_LIST",
28
+ :long => "--vagrant-run-list RUN_LIST",
29
+ :description => "Comma separated list of roles/recipes to apply",
30
+ :proc => lambda { |o| o.split(/[\s,]+/) },
31
+ :default => []
32
+
33
+ option :box,
34
+ :short => '-b BOX',
35
+ :long => '--box BOX',
36
+ :description => 'Name of vagrant box to be provisioned',
37
+ :default => false
38
+
39
+ option :hostname,
40
+ :short => '-H HOSTNAME',
41
+ :long => '--hostname HOSTNAME',
42
+ :description => 'Hostname to be set as hostname on vagrant box when provisioned',
43
+ :default => 'vagrant_test'
44
+
45
+ option :box_url,
46
+ :short => '-U URL',
47
+ :long => '--box-url URL',
48
+ :description => 'URL of pre-packaged vbox template. Can be a local path or an HTTP URL. Defaults to ./package.box',
49
+ :default => "#{Dir.pwd}/package.box"
50
+
51
+ option :memsize,
52
+ :short => '-m MEMORY',
53
+ :long => '--memsize MEMORY',
54
+ :description => 'Amount of RAM to allocate to provisioned VM, in MB. Defaults to 1024',
55
+ :default => 1024
56
+
57
+ option :chef_loglevel,
58
+ :short => '-l LEVEL',
59
+ :long => '--chef-loglevel LEVEL',
60
+ :description => 'Logging level for the chef-client process that runs inside the provisioned VM. Default is INFO',
61
+ :default => 'INFO'
62
+
63
+ option :destroy,
64
+ :short => '-x',
65
+ :long => '--destroy',
66
+ :description => 'Destroy vagrant box and delete chef node/client when finished',
67
+ :default => false
68
+
69
+ # TODO - hook into chef/runlist
70
+ def build_runlist(runlist)
71
+ runlist.collect { |i| "\"#{i}\"" }.join(",\n")
72
+ end
73
+
74
+ # 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.
75
+ def build_vagrantfile
76
+ file = <<-EOF
77
+ Vagrant::Config.run do |config|
78
+ config.vm.forward_port(22, 2222)
79
+ config.vm.box = "#{config[:box]}"
80
+ config.vm.host_name = "#{config[:hostname]}"
81
+ config.vm.customize do |vm|
82
+ vm.memory_size = #{config[:memsize]}
83
+ vm.name = "#{config[:box]}"
84
+ end
85
+ config.vm.box_url = "#{config[:box_url]}"
86
+ config.vm.provision :chef_client do |chef|
87
+ chef.chef_server_url = "#{Chef::Config[:chef_server_url]}"
88
+ chef.validation_key_path = "#{Chef::Config[:validation_key]}"
89
+ chef.validation_client_name = "#{Chef::Config[:validation_client_name]}"
90
+ chef.node_name = "#{config[:hostname]}"
91
+ chef.provisioning_path = "#{Chef::Config[:provisioning_path]}"
92
+ chef.log_level = :#{config[:chef_loglevel].downcase}
93
+ chef.environment = "#{Chef::Config[:environment]}"
94
+ chef.run_list = [
95
+ #{build_runlist(config[:vagrant_run_list])}
96
+ ]
97
+ end
98
+ end
99
+ EOF
100
+ file
101
+ end
102
+
103
+ def write_vagrantfile(path, content)
104
+ File.open(path, 'w') { |f| f.write(content) }
105
+ end
106
+
107
+ def cleanup(path)
108
+ File.delete(path)
109
+ end
110
+
111
+ def run
112
+ ui.msg('Loading vagrant environment..')
113
+ Dir.chdir(config[:vagrant_dir])
114
+ vagrantfile = "#{config[:vagrant_dir]}/Vagrantfile"
115
+ write_vagrantfile(vagrantfile, build_vagrantfile)
116
+ @vagrant_env = Vagrant::Environment.new(:cwd => config[:vagrant_dir], :ui_class => Vagrant::UI::Colored)
117
+ @vagrant_env.load!
118
+ begin
119
+ @vagrant_env.cli("up")
120
+ rescue
121
+ raise # I'll put some error handling here later.
122
+ ensure
123
+ if config[:destroy]
124
+ ui.confirm("Destroy vagrant box #{config[:box]} and delete chef node and client")
125
+ config[:yes] = true unless config[:yes]
126
+ @vagrant_env.cli("destroy")
127
+ delete_object(Chef::Node, config[:hostname])
128
+ delete_object(Chef::ApiClient, config[:hostname])
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-vagrant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Garrett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: knife-vagrant, a knife plugin that will create a Vagrant box, then run
15
+ a set of Chef recipes in it.
16
+ email: garrettux@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - lib/chef/knife/vagrant_test.rb
23
+ homepage: https://github.com/garrettux/knife-vagrant
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: knife-vagrant, a knife plugin that will create a Vagrant box, then run a
47
+ set of Chef recipes in it.
48
+ test_files: []