knife-remotekvm 0.0.1

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.
File without changes
File without changes
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
2
+ require 'knife-remotekvm/version'
3
+ Gem::Specification.new do |s|
4
+ s.name = 'knife-remotekvm'
5
+ s.version = KnifeRemoteKVM::VERSION.version
6
+ s.summary = 'Create KVM nodes'
7
+ s.author = 'Chris Roberts'
8
+ s.email = 'chrisroberts.code@gmail.com'
9
+ s.homepage = 'http://github.com/heavywater/knife-remotekvm'
10
+ s.description = "Remote KVM"
11
+ s.require_path = 'lib'
12
+ s.files = Dir.glob('**/*')
13
+ s.add_dependency 'chef'
14
+ end
@@ -0,0 +1,180 @@
1
+ require 'knife-remotekvm/helpers'
2
+
3
+ module RemoteKVM
4
+ class RemotekvmCreate < Chef::Knife
5
+
6
+ include RemoteKVM::Helpers
7
+
8
+ deps do
9
+ Chef::Knife::Bootstrap.load_deps
10
+ end
11
+
12
+ banner 'knife remotekvm create NODE_NAME'
13
+
14
+ option :kvm_node,
15
+ :short => '-k FQDN|IP',
16
+ :long => '--kvm-node FQDN|IP',
17
+ :description => 'kvm enabled node',
18
+ :required => true
19
+
20
+ option :kvm_ssh_user,
21
+ :short => '-X USERNAME',
22
+ :long => '--kvm-ssh-user USERNAME'
23
+
24
+ option :kvm_template,
25
+ :long => '--kvm-template [template]',
26
+ :description => 'kvm template to clone',
27
+ :default => 'precise64'
28
+
29
+ option :kvm_memory,
30
+ :long => '--kvm-memory [MBs]',
31
+ :description => 'Amount of memory in megabytes',
32
+ :default => 512
33
+
34
+ option :kvm_vcpus,
35
+ :long => '--kvm-vcpus',
36
+ :description => 'Number of virtual CPUs to configure',
37
+ :default => 1
38
+
39
+ option :kvm_maxvcpus,
40
+ :long => '--kvm-maxvcpus',
41
+ :description => 'Number of virtual CPUs guest is allowed to hotplug',
42
+ :default => 1
43
+ # TODO: Pass auth
44
+ #option :kvm_ssh_password,
45
+ # :short => '-S PASSWORD',
46
+ # :long => '--kvm-ssh-password PASSWORD'
47
+
48
+ # All the bootstrap options since we just proxy
49
+ option :ssh_user,
50
+ :short => "-x USERNAME",
51
+ :long => "--ssh-user USERNAME",
52
+ :description => "The ssh username",
53
+ :default => "ubuntu"
54
+
55
+ option :ssh_password,
56
+ :short => "-P PASSWORD",
57
+ :long => "--ssh-password PASSWORD",
58
+ :description => "The ssh password",
59
+ :default => "ubuntu"
60
+
61
+ option :ssh_port,
62
+ :short => "-p PORT",
63
+ :long => "--ssh-port PORT",
64
+ :description => "The ssh port",
65
+ :default => "22",
66
+ :proc => Proc.new { |key| Chef::Config[:knife][:ssh_port] = key }
67
+
68
+ option :ssh_gateway,
69
+ :short => "-G GATEWAY",
70
+ :long => "--ssh-gateway GATEWAY",
71
+ :description => "The ssh gateway",
72
+ :proc => Proc.new { |key| Chef::Config[:knife][:ssh_gateway] = key }
73
+
74
+ option :identity_file,
75
+ :short => "-i IDENTITY_FILE",
76
+ :long => "--identity-file IDENTITY_FILE",
77
+ :description => "The SSH identity file used for authentication"
78
+
79
+ option :prerelease,
80
+ :long => "--prerelease",
81
+ :description => "Install the pre-release chef gems"
82
+
83
+ option :bootstrap_version,
84
+ :long => "--bootstrap-version VERSION",
85
+ :description => "The version of Chef to install",
86
+ :proc => lambda { |v| Chef::Config[:knife][:bootstrap_version] = v }
87
+
88
+ option :bootstrap_proxy,
89
+ :long => "--bootstrap-proxy PROXY_URL",
90
+ :description => "The proxy server for the node being bootstrapped",
91
+ :proc => Proc.new { |p| Chef::Config[:knife][:bootstrap_proxy] = p }
92
+
93
+ option :use_sudo,
94
+ :long => "--sudo",
95
+ :description => "Execute the bootstrap via sudo",
96
+ :boolean => true
97
+
98
+ option :template_file,
99
+ :long => "--template-file TEMPLATE",
100
+ :description => "Full path to location of template to use",
101
+ :default => false
102
+
103
+ option :run_list,
104
+ :short => "-r RUN_LIST",
105
+ :long => "--run-list RUN_LIST",
106
+ :description => "Comma separated list of roles/recipes to apply",
107
+ :proc => lambda { |o| o.split(/[\s,]+/) },
108
+ :default => []
109
+
110
+ option :first_boot_attributes,
111
+ :short => "-j JSON_ATTRIBS",
112
+ :long => "--json-attributes",
113
+ :description => "A JSON string to be added to the first run of chef-client",
114
+ :proc => lambda { |o| JSON.parse(o) },
115
+ :default => {}
116
+
117
+ option :host_key_verify,
118
+ :long => "--[no-]host-key-verify",
119
+ :description => "Verify host key, enabled by default.",
120
+ :boolean => true,
121
+ :default => true
122
+
123
+
124
+ attr_reader :kvm_name
125
+
126
+ def initialize(*args)
127
+ super
128
+ config[:distro] = 'chef-full'
129
+ end
130
+
131
+ def run
132
+ tries = 5
133
+ @kvm_name = config[:chef_node_name] = name_args.first
134
+ ip_address = create_new_container
135
+ begin
136
+ bootstrap_container(ip_address)
137
+ rescue Errno::EHOSTUNREACH
138
+ if(tries > 0)
139
+ tries -= 1
140
+ puts "Failed to connect. Will wait and retry (#{tries} retries remaining)"
141
+ retry
142
+ else
143
+ raise
144
+ end
145
+ end
146
+ end
147
+
148
+ private
149
+
150
+ def kvm_base
151
+ config[:kvm_node]
152
+ end
153
+
154
+ def create_new_container
155
+ knife_ssh(
156
+ kvm_base,
157
+ "sudo /usr/local/bin/knife_kvm create #{kvm_name} " <<
158
+ "--memory #{config[:kvm_memory]} --vcpus #{config[:kvm_vcpus]} " <<
159
+ "--maxvcpus #{config[:kvm_maxvcpus]}"
160
+ ).stdout.to_s.split(':').last.to_s.strip
161
+ end
162
+
163
+ def bootstrap_container(ip_address)
164
+ bootstrap = Chef::Knife::Bootstrap.new
165
+ bootstrap.config = config
166
+ bootstrap.name_args = [ip_address]
167
+ bootstrap.run
168
+ end
169
+
170
+ def knife_ssh(addr, command)
171
+ cmd = "ssh -o StrictHostKeyChecking=no #{"{config[:kvm_ssh_user]}@" if config[:kvm_ssh_user]}#{addr} #{command}"
172
+ so = Mixlib::ShellOut.new(cmd,
173
+ :logger => Chef::Log.logger,
174
+ :live_stream => $stdout
175
+ ).run_command
176
+ so.error!
177
+ so
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,26 @@
1
+ require 'knife-remotekvm/helpers'
2
+
3
+ module RemoteKVM
4
+ class RemotekvmDelete < Chef::Knife::Ssh
5
+
6
+ include RemoteKVM::Helpers
7
+
8
+ banner 'knife remotekvm delete NODE_NAME'
9
+
10
+ option :kvm_node,
11
+ :short => '-k FQDN|IP',
12
+ :long => '--kvm-node FQDN|IP',
13
+ :description => 'KVM enabled node',
14
+ :required => true
15
+
16
+ option :kvm_ssh_user,
17
+ :short => '-X USERNAME',
18
+ :long => '--kvm-ssh-user USERNAME'
19
+
20
+ def run
21
+ ui.confirm "Are you sure you want to delete KVM node: #{name_args.first}"
22
+ knife_ssh(config[:kvm_node], "sudo knife_kvm delete #{name_args.first}")
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ require 'knife-remotekvm/helpers'
2
+
3
+ module RemoteKVM
4
+ class RemotekvmInfo < Chef::Knife::Ssh
5
+
6
+ include RemoteKVM::Helpers
7
+
8
+ banner 'knife remotekvm info NODE_NAME'
9
+
10
+ option :kvm_node,
11
+ :short => '-k FQDN|IP',
12
+ :long => '--kvm-node FQDN|IP',
13
+ :description => 'KVM enabled node',
14
+ :required => true
15
+
16
+ option :kvm_ssh_user,
17
+ :short => '-X USERNAME',
18
+ :long => '--kvm-ssh-user USERNAME'
19
+
20
+ def run
21
+ knife_ssh(config[:kvm_node], "sudo knife_kvm info #{name_args.first}")
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'knife-remotekvm/helpers'
2
+
3
+ module RemoteKVM
4
+ class RemotekvmList < Chef::Knife::Ssh
5
+
6
+ include RemoteKVM::Helpers
7
+
8
+ banner 'knife remotekvm list'
9
+
10
+ option :kvm_node,
11
+ :short => '-k FQDN|IP',
12
+ :long => '--kvm-node FQDN|IP',
13
+ :description => 'KVM enabled node',
14
+ :required => true
15
+
16
+ option :kvm_ssh_user,
17
+ :short => '-X USERNAME',
18
+ :long => '--kvm-ssh-user USERNAME'
19
+
20
+ def run
21
+ knife_ssh(config[:kvm_node], "sudo knife_kvm list")
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'knife-remotekvm/helpers'
2
+
3
+ module RemoteKVM
4
+ class RemotekvmModify < Chef::Knife::Ssh
5
+
6
+ include RemoteKVM::Helpers
7
+
8
+ banner 'knife remotekvm modify NODE_NAME'
9
+
10
+ option :kvm_node,
11
+ :short => '-k FQDN|IP',
12
+ :long => '--kvm-node FQDN|IP',
13
+ :description => 'KVM enabled node',
14
+ :required => true
15
+
16
+ option :kvm_ssh_user,
17
+ :short => '-X USERNAME',
18
+ :long => '--kvm-ssh-user USERNAME'
19
+
20
+ def run
21
+ ui.say "Sorry, not ready yet"
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module RemoteKVM
2
+ module Helpers
3
+ def knife_ssh(addr, command, args={})
4
+ cmd = "ssh -o StrictHostKeyChecking=no #{"{config[:lxc_ssh_user]}@" if config[:kvm_ssh_user]}#{addr} #{command}"
5
+ so = Mixlib::ShellOut.new(cmd,
6
+ :logger => Chef::Log.logger,
7
+ :live_stream => $stdout
8
+ ).run_command
9
+ so.error!
10
+ so
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module KnifeRemoteKVM
2
+ class Version < Gem::Version
3
+ end
4
+ VERSION = Version.new('0.0.1')
5
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-remotekvm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Roberts
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: &13861080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *13861080
25
+ description: Remote KVM
26
+ email: chrisroberts.code@gmail.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - knife-remotekvm.gemspec
32
+ - CHANGELOG.md
33
+ - README.md
34
+ - knife-remotekvm-0.0.1.gem
35
+ - lib/knife-remotekvm/version.rb
36
+ - lib/knife-remotekvm/helpers.rb
37
+ - lib/chef/knife/remotekvm_delete.rb
38
+ - lib/chef/knife/remotekvm_info.rb
39
+ - lib/chef/knife/remotekvm_create.rb
40
+ - lib/chef/knife/remotekvm_list.rb
41
+ - lib/chef/knife/remotekvm_modify.rb
42
+ homepage: http://github.com/heavywater/knife-remotekvm
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.17
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Create KVM nodes
66
+ test_files: []
67
+ has_rdoc: