knife-vagrant 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +19 -33
- data/lib/chef/knife/vagrant_server.rb +232 -0
- metadata +2 -2
- data/lib/chef/knife/vagrant_test.rb +0 -141
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
knife-vagrant
|
2
2
|
========
|
3
|
-
|
3
|
+
knife-vagrant tries to mirror the basic command structure of the knife-ec2 plugin for chef. The plugin will manage a series of Vagrantfiles in a tree to simulate a cloud environment.
|
4
4
|
|
5
5
|
Requirements
|
6
6
|
-------------------
|
@@ -21,38 +21,24 @@ knife-vagrant is available on rubygems.org.
|
|
21
21
|
|
22
22
|
Usage
|
23
23
|
-------------------
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
-p, --port-forward PORTS Port forwarding. Host port, VM port separated by a colon. E.G. to forward 80 on the
|
43
|
-
host machine to 8080 on the VM, -p 80:8080.
|
44
|
-
To list multiple forwards separate with a comma, e.g. "-p 80:8080,22:2222"
|
45
|
-
--print-after Show the data after a destructive operation
|
46
|
-
-D, --vagrant-dir PATH Path to vagrant project directory. Defaults to cwd (/Users/mgarrett/vagrant) if not specified
|
47
|
-
-r, --vagrant-run-list RUN_LIST Comma separated list of roles/recipes to apply
|
48
|
-
-V, --verbose More verbose output. Use twice for max verbosity
|
49
|
-
-v, --version Show chef version
|
50
|
-
-y, --yes Say yes to all prompts for confirmation
|
51
|
-
-h, --help Show this message
|
52
|
-
|
53
|
-
#### Example
|
54
|
-
|
55
|
-
knife vagrant test -b base -H vagrant-mgarrett01 -r role[vagrant] -m 2048 -p 22:2222,8080:8080 -b box64 -U http://files.vagrantup.com/lucid64.box -xy
|
24
|
+
** VAGRANT COMMANDS **
|
25
|
+
knife vagrant server [SUB-COMMAND]
|
26
|
+
knife vagrant server ssh [hostname]
|
27
|
+
knife vagrant server list
|
28
|
+
knife vagrant server create (options)
|
29
|
+
knife vagrant server delete [hostname] (args)
|
30
|
+
|
31
|
+
#### Examples
|
32
|
+
|
33
|
+
knife vagrant server create -H vagrant01 -r role[vagrant] -m 2048 -p 22:2222,8080:8080 -U http://files.vagrantup.com/lucid64.box
|
34
|
+
|
35
|
+
knife vagrant server list
|
36
|
+
testbox00
|
37
|
+
testbox01
|
38
|
+
testbox03
|
39
|
+
|
40
|
+
knife vagrant server ssh testbox00
|
41
|
+
[vagrant@testbox00 ~]$
|
56
42
|
|
57
43
|
Disclaimer
|
58
44
|
-------------------
|
@@ -0,0 +1,232 @@
|
|
1
|
+
# knife-vagrant
|
2
|
+
# knife plugin for spinning up a vagrant instance and testing a runlist.
|
3
|
+
|
4
|
+
module KnifePlugins
|
5
|
+
|
6
|
+
class VagrantServer < Chef::Knife
|
7
|
+
banner "knife vagrant server [SUB-COMMAND]"
|
8
|
+
|
9
|
+
def run
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
class VagrantServerSsh < VagrantServer
|
14
|
+
banner "knife vagrant server ssh [hostname]"
|
15
|
+
deps do
|
16
|
+
require 'vagrant'
|
17
|
+
require 'vagrant/cli'
|
18
|
+
end
|
19
|
+
|
20
|
+
option :hostname,
|
21
|
+
:short => '-H HOSTNAME',
|
22
|
+
:long => '--hostname HOSTNAME',
|
23
|
+
:description => 'Hostname to be set as hostname on vagrant box when provisioned'
|
24
|
+
|
25
|
+
option :vagrant_dir,
|
26
|
+
:short => '-D PATH',
|
27
|
+
:long => '--vagrant-dir PATH',
|
28
|
+
:description => "Path to vagrant project directory. Defaults to cwd (#{Dir.pwd}) if not specified",
|
29
|
+
:default => Dir.pwd + "/vagrant"
|
30
|
+
|
31
|
+
def run
|
32
|
+
unless config[:hostname] || name_args.size >= 1
|
33
|
+
ui.fatal "Please provide a box name."
|
34
|
+
show_usage
|
35
|
+
exit 1
|
36
|
+
end
|
37
|
+
|
38
|
+
if name_args.size >= 1 then
|
39
|
+
hostname = name_args.first
|
40
|
+
else
|
41
|
+
hostname = config[:hostname]
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
@vagrant_env = Vagrant::Environment.new(:cwd => "#{config[:vagrant_dir]}/#{hostname}/", :ui_class => Vagrant::UI::Colored)
|
46
|
+
@vagrant_env.load!
|
47
|
+
@vagrant_env.cli("ssh")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
class VagrantServerList < Chef::Knife
|
53
|
+
banner "knife vagrant server list"
|
54
|
+
|
55
|
+
deps do
|
56
|
+
require 'vagrant'
|
57
|
+
require 'vagrant/cli'
|
58
|
+
end
|
59
|
+
|
60
|
+
def run
|
61
|
+
@vagrant_env = Vagrant::Environment.new(:ui_class => Vagrant::UI::Colored)
|
62
|
+
@vagrant_env.load!
|
63
|
+
@vagrant_env.cli("box","list")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
class VagrantServerCreate < VagrantServer
|
69
|
+
banner "knife vagrant server create (options)"
|
70
|
+
|
71
|
+
deps do
|
72
|
+
require 'vagrant'
|
73
|
+
require 'vagrant/cli'
|
74
|
+
end
|
75
|
+
|
76
|
+
option :port_forward,
|
77
|
+
:short => '-p PORTS',
|
78
|
+
:long => '--port-forward PORTS',
|
79
|
+
:description => "Port forwarding. Host port, VM port separated by a colon. E.G. to forward 80 on the host machine to 8080 on the VM, -p 80:8080. To list multiple forwards separate with a comma, e.g. \"-p 80:8080,22:2222\"",
|
80
|
+
:proc => lambda { |o| Hash[o.split(/,/).collect { |a| a.split(/:/) }] },
|
81
|
+
:default => {}
|
82
|
+
|
83
|
+
option :vagrant_dir,
|
84
|
+
:short => '-D PATH',
|
85
|
+
:long => '--vagrant-dir PATH',
|
86
|
+
:description => "Path to vagrant project directory. Defaults to cwd (#{Dir.pwd}) if not specified",
|
87
|
+
:default => Dir.pwd + "/vagrant"
|
88
|
+
|
89
|
+
option :vagrant_run_list,
|
90
|
+
:short => "-r RUN_LIST",
|
91
|
+
:long => "--vagrant-run-list RUN_LIST",
|
92
|
+
:description => "Comma separated list of roles/recipes to apply",
|
93
|
+
:proc => lambda { |o| o.split(/[\s,]+/) },
|
94
|
+
:default => ["role[base]"]
|
95
|
+
|
96
|
+
option :hostname,
|
97
|
+
:short => '-H HOSTNAME',
|
98
|
+
:long => '--hostname HOSTNAME',
|
99
|
+
:description => 'Hostname to be set as hostname on vagrant box when provisioned'
|
100
|
+
|
101
|
+
option :box_url,
|
102
|
+
:short => '-U URL',
|
103
|
+
:long => '--box-url URL',
|
104
|
+
:description => 'URL of pre-packaged vbox template. Can be a local path or an HTTP URL. Defaults to ./package.box',
|
105
|
+
:default => "#{Dir.pwd}/package.box"
|
106
|
+
|
107
|
+
option :memsize,
|
108
|
+
:short => '-m MEMORY',
|
109
|
+
:long => '--memsize MEMORY',
|
110
|
+
:description => 'Amount of RAM to allocate to provisioned VM, in MB. Defaults to 1024',
|
111
|
+
:default => 1024
|
112
|
+
|
113
|
+
option :chef_loglevel,
|
114
|
+
:short => '-l LEVEL',
|
115
|
+
:long => '--chef-loglevel LEVEL',
|
116
|
+
:description => 'Logging level for the chef-client process that runs inside the provisioned VM. Default is INFO',
|
117
|
+
:default => 'INFO'
|
118
|
+
|
119
|
+
# TODO - hook into chef/runlist
|
120
|
+
def build_runlist(runlist)
|
121
|
+
runlist.collect { |i| "\"#{i}\"" }.join(",\n")
|
122
|
+
end
|
123
|
+
|
124
|
+
def build_port_forwards(ports)
|
125
|
+
ports.collect { |k, v| "config.vm.forward_port(#{k}, #{v})" }.join("\n")
|
126
|
+
end
|
127
|
+
|
128
|
+
# 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.
|
129
|
+
def build_vagrantfile
|
130
|
+
file = <<-EOF
|
131
|
+
Vagrant::Config.run do |config|
|
132
|
+
#{build_port_forwards(config[:port_forward])}
|
133
|
+
config.vm.box = "#{config[:hostname]}"
|
134
|
+
config.vm.host_name = "#{config[:hostname]}"
|
135
|
+
config.vm.customize [ "modifyvm", :id, "--memory", #{config[:memsize]} ]
|
136
|
+
config.vm.customize [ "modifyvm", :id, "--name", "#{config[:hostname]}" ]
|
137
|
+
config.vm.box_url = "#{config[:box_url]}"
|
138
|
+
config.vm.provision :chef_client do |chef|
|
139
|
+
chef.chef_server_url = "#{Chef::Config[:chef_server_url]}"
|
140
|
+
chef.validation_key_path = "#{Chef::Config[:validation_key]}"
|
141
|
+
chef.validation_client_name = "#{Chef::Config[:validation_client_name]}"
|
142
|
+
chef.node_name = "#{config[:hostname]}"
|
143
|
+
chef.log_level = :#{config[:chef_loglevel].downcase}
|
144
|
+
chef.environment = "#{Chef::Config[:environment]}"
|
145
|
+
chef.run_list = [
|
146
|
+
#{build_runlist(config[:vagrant_run_list])}
|
147
|
+
]
|
148
|
+
end
|
149
|
+
end
|
150
|
+
EOF
|
151
|
+
file
|
152
|
+
end
|
153
|
+
|
154
|
+
def write_vagrantfile(path, content)
|
155
|
+
File.open(path, 'w') { |f| f.write(content) }
|
156
|
+
end
|
157
|
+
|
158
|
+
#
|
159
|
+
def run
|
160
|
+
puts "Initializing..."
|
161
|
+
vagrantfile = "Vagrantfile"
|
162
|
+
vagrantdir = "#{config[:vagrant_dir]}/#{config[:hostname]}/"
|
163
|
+
|
164
|
+
unless File.directory?(vagrantdir)
|
165
|
+
Dir.mkdir(vagrantdir)
|
166
|
+
end
|
167
|
+
write_vagrantfile("#{vagrantdir}/#{vagrantfile}", build_vagrantfile)
|
168
|
+
@vagrant_env = Vagrant::Environment.new(:cwd => vagrantdir, :ui_class => Vagrant::UI::Colored)
|
169
|
+
@vagrant_env.load!
|
170
|
+
|
171
|
+
begin
|
172
|
+
@vagrant_env.cli("up")
|
173
|
+
rescue
|
174
|
+
raise # I'll put some error handling here later.
|
175
|
+
ensure
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
#
|
181
|
+
class VagrantServerDelete < Chef::Knife
|
182
|
+
banner "knife vagrant server delete [hostname] (args)"
|
183
|
+
|
184
|
+
deps do
|
185
|
+
require 'vagrant'
|
186
|
+
require 'vagrant/cli'
|
187
|
+
require 'chef/node'
|
188
|
+
require 'chef/api_client'
|
189
|
+
end
|
190
|
+
|
191
|
+
option :hostname,
|
192
|
+
:short => '-H HOSTNAME',
|
193
|
+
:long => '--hostname HOSTNAME',
|
194
|
+
:description => 'Hostname to be set as hostname on vagrant box when provisioned'
|
195
|
+
option :vagrant_dir,
|
196
|
+
:short => '-D PATH',
|
197
|
+
:long => '--vagrant-dir PATH',
|
198
|
+
:description => "Path to vagrant project directory. Defaults to cwd (#{Dir.pwd}) if not specified",
|
199
|
+
:default => Dir.pwd + "/vagrant"
|
200
|
+
|
201
|
+
def run
|
202
|
+
unless config[:hostname] || name_args.size >= 1
|
203
|
+
ui.fatal "Please provide a hostname."
|
204
|
+
show_usage
|
205
|
+
exit 1
|
206
|
+
end
|
207
|
+
|
208
|
+
if name_args.size >= 1 then
|
209
|
+
hostname = name_args.first
|
210
|
+
else
|
211
|
+
hostname = config[:hostname]
|
212
|
+
end
|
213
|
+
|
214
|
+
vagrantfile = "Vagrantfile"
|
215
|
+
vagrantdir = "#{config[:vagrant_dir]}/#{hostname}/"
|
216
|
+
|
217
|
+
# confirm delete
|
218
|
+
ui.confirm("Destroy vagrant box #{hostname} and delete chef node and client")
|
219
|
+
|
220
|
+
#Dir.chdir("#{config[:vagrant_dir]}/#{config[:hostname]}/")
|
221
|
+
@vagrant_env = Vagrant::Environment.new(:cwd => vagrantdir, :ui_class => Vagrant::UI::Colored)
|
222
|
+
@vagrant_env.load!
|
223
|
+
@vagrant_env.cli("box","remove",hostname)
|
224
|
+
delete_object(Chef::Node, hostname)
|
225
|
+
delete_object(Chef::ApiClient, hostname)
|
226
|
+
Dir.delete( vagrantdir )
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knife-vagrant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,7 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- README.md
|
22
|
-
- lib/chef/knife/
|
22
|
+
- lib/chef/knife/vagrant_server.rb
|
23
23
|
homepage: https://github.com/garrettux/knife-vagrant
|
24
24
|
licenses: []
|
25
25
|
post_install_message:
|
@@ -1,141 +0,0 @@
|
|
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
|
-
option :port_forward,
|
19
|
-
:short => '-p PORTS',
|
20
|
-
:long => '--port-forward PORTS',
|
21
|
-
:description => "Port forwarding. Host port, VM port separated by a colon. E.G. to forward 80 on the host machine to 8080 on the VM, -p 80:8080. To list multiple forwards separate with a comma, e.g. \"-p 80:8080,22:2222\"",
|
22
|
-
:proc => lambda { |o| Hash[o.split(/,/).collect { |a| a.split(/:/) }] },
|
23
|
-
:default => {}
|
24
|
-
|
25
|
-
option :vagrant_dir,
|
26
|
-
:short => '-D PATH',
|
27
|
-
:long => '--vagrant-dir PATH',
|
28
|
-
:description => "Path to vagrant project directory. Defaults to cwd (#{Dir.pwd}) if not specified",
|
29
|
-
:default => Dir.pwd
|
30
|
-
|
31
|
-
option :vagrant_run_list,
|
32
|
-
:short => "-r RUN_LIST",
|
33
|
-
:long => "--vagrant-run-list RUN_LIST",
|
34
|
-
:description => "Comma separated list of roles/recipes to apply",
|
35
|
-
:proc => lambda { |o| o.split(/[\s,]+/) },
|
36
|
-
:default => []
|
37
|
-
|
38
|
-
option :box,
|
39
|
-
:short => '-b BOX',
|
40
|
-
:long => '--box BOX',
|
41
|
-
:description => 'Name of vagrant box to be provisioned',
|
42
|
-
:default => false
|
43
|
-
|
44
|
-
option :hostname,
|
45
|
-
:short => '-H HOSTNAME',
|
46
|
-
:long => '--hostname HOSTNAME',
|
47
|
-
:description => 'Hostname to be set as hostname on vagrant box when provisioned',
|
48
|
-
:default => 'vagrant_test'
|
49
|
-
|
50
|
-
option :box_url,
|
51
|
-
:short => '-U URL',
|
52
|
-
:long => '--box-url URL',
|
53
|
-
:description => 'URL of pre-packaged vbox template. Can be a local path or an HTTP URL. Defaults to ./package.box',
|
54
|
-
:default => "#{Dir.pwd}/package.box"
|
55
|
-
|
56
|
-
option :memsize,
|
57
|
-
:short => '-m MEMORY',
|
58
|
-
:long => '--memsize MEMORY',
|
59
|
-
:description => 'Amount of RAM to allocate to provisioned VM, in MB. Defaults to 1024',
|
60
|
-
:default => 1024
|
61
|
-
|
62
|
-
option :chef_loglevel,
|
63
|
-
:short => '-l LEVEL',
|
64
|
-
:long => '--chef-loglevel LEVEL',
|
65
|
-
:description => 'Logging level for the chef-client process that runs inside the provisioned VM. Default is INFO',
|
66
|
-
:default => 'INFO'
|
67
|
-
|
68
|
-
option :destroy,
|
69
|
-
:short => '-x',
|
70
|
-
:long => '--destroy',
|
71
|
-
:description => 'Destroy vagrant box and delete chef node/client when finished',
|
72
|
-
:default => false
|
73
|
-
|
74
|
-
# TODO - hook into chef/runlist
|
75
|
-
def build_runlist(runlist)
|
76
|
-
runlist.collect { |i| "\"#{i}\"" }.join(",\n")
|
77
|
-
end
|
78
|
-
|
79
|
-
def build_port_forwards(ports)
|
80
|
-
ports.collect { |k, v| "config.vm.forward_port(#{k}, #{v})" }.join("\n")
|
81
|
-
end
|
82
|
-
|
83
|
-
# 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
|
-
def build_vagrantfile
|
85
|
-
file = <<-EOF
|
86
|
-
Vagrant::Config.run do |config|
|
87
|
-
#{build_port_forwards(config[:port_forward])}
|
88
|
-
config.vm.box = "#{config[:box]}"
|
89
|
-
config.vm.host_name = "#{config[:hostname]}"
|
90
|
-
config.vm.customize [ "modifyvm", :id, "--memory", #{config[:memsize]} ]
|
91
|
-
config.vm.customize [ "modifyvm", :id, "--name", "#{config[:box]}" ]
|
92
|
-
config.vm.box_url = "#{config[:box_url]}"
|
93
|
-
config.vm.provision :chef_client do |chef|
|
94
|
-
chef.chef_server_url = "#{Chef::Config[:chef_server_url]}"
|
95
|
-
chef.validation_key_path = "#{Chef::Config[:validation_key]}"
|
96
|
-
chef.validation_client_name = "#{Chef::Config[:validation_client_name]}"
|
97
|
-
chef.node_name = "#{config[:hostname]}"
|
98
|
-
chef.log_level = :#{config[:chef_loglevel].downcase}
|
99
|
-
chef.environment = "#{Chef::Config[:environment]}"
|
100
|
-
chef.run_list = [
|
101
|
-
#{build_runlist(config[:vagrant_run_list])}
|
102
|
-
]
|
103
|
-
end
|
104
|
-
end
|
105
|
-
EOF
|
106
|
-
file
|
107
|
-
end
|
108
|
-
|
109
|
-
def write_vagrantfile(path, content)
|
110
|
-
File.open(path, 'w') { |f| f.write(content) }
|
111
|
-
end
|
112
|
-
|
113
|
-
def cleanup(path)
|
114
|
-
File.delete(path)
|
115
|
-
end
|
116
|
-
|
117
|
-
def run
|
118
|
-
ui.msg('Loading vagrant environment..')
|
119
|
-
Dir.chdir(config[:vagrant_dir])
|
120
|
-
vagrantfile = "#{config[:vagrant_dir]}/Vagrantfile"
|
121
|
-
write_vagrantfile(vagrantfile, build_vagrantfile)
|
122
|
-
@vagrant_env = Vagrant::Environment.new(:cwd => config[:vagrant_dir], :ui_class => Vagrant::UI::Colored)
|
123
|
-
@vagrant_env.load!
|
124
|
-
begin
|
125
|
-
@vagrant_env.cli("up")
|
126
|
-
rescue
|
127
|
-
raise # I'll put some error handling here later.
|
128
|
-
ensure
|
129
|
-
if config[:destroy]
|
130
|
-
ui.confirm("Destroy vagrant box #{config[:box]} and delete chef node and client")
|
131
|
-
args = %w[ destroy --force ]
|
132
|
-
@vagrant_env.cli(args)
|
133
|
-
config[:yes]
|
134
|
-
delete_object(Chef::Node, config[:hostname])
|
135
|
-
delete_object(Chef::ApiClient, config[:hostname])
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
end
|
141
|
-
end
|