knife-xapi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +201 -0
- data/README.rdoc +38 -0
- data/lib/chef/knife/xapi_base.rb +239 -0
- data/lib/chef/knife/xapi_guest_create.rb +169 -0
- data/lib/knife-xapi/version.rb +3 -0
- metadata +115 -0
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
=Knife Xapi
|
2
|
+
This plugin gives knife the ability to create guests on a XAPI compatable hyper visor
|
3
|
+
|
4
|
+
==Installation
|
5
|
+
This plugin is distributed as a Ruby Gem. To install it, run:
|
6
|
+
gem install knife-xapi
|
7
|
+
|
8
|
+
==Configuration
|
9
|
+
Config options are extenable in the knife.rb the folowing k/v pairs are implemented
|
10
|
+
|
11
|
+
Chef::Config[:knife][:xenserver_host] :: The API Host to connect to
|
12
|
+
Chef::Config[:knife][:xenserver_username] :: The User name to connect to the api with
|
13
|
+
Chef::Config[:knife][:xenserver_password] :: The Password (if not set will prompt on commandline)
|
14
|
+
Chef::Config[:knife][:xapi_vm_template] :: Set a default template to be used when creating Guests
|
15
|
+
Chef::Config[:knife][:xapi_install_repo] :: The install repo config option to set when using Xen builtin templates
|
16
|
+
Chef::Config[:knife][:xapi_sr] :: The Storage Repository to provision from, uses pool/hypervisor default when not set
|
17
|
+
Chef::Config[:knife][:xapi_disk_size] :: Default VM disk size (8g if not specified)
|
18
|
+
Chef::Config[:knife][:xapi_cpus] :: The Default CPUs to provision for guests (2 if not specified)
|
19
|
+
Chef::Config[:knife][:xapi_mem] = mem :: The Defaul ammount of Memory for guests (1g if not specified)
|
20
|
+
Chef::Config[:knife][:xapi_kernel_params] :: Optional Boot paramaters to pass to the guest
|
21
|
+
Chef::Config[:knife][:xapi_bootstrap] :: Not implemented yet, but will be the bootstrap script to execute after guest create
|
22
|
+
|
23
|
+
==Usage
|
24
|
+
|
25
|
+
Basic usage to create a VM from existing VM template:
|
26
|
+
knife xapi guest create "NewBox" "public" --xapi-vm-template "MyBaseBox" --host http://sandbox/
|
27
|
+
|
28
|
+
|
29
|
+
More verbose example using a kickstart file and booting the Centos 5 default template:
|
30
|
+
knife xapi guest create "MySpiffyBox" "pub_network" --host http://sandbox/ -B "dns=8.8.8.8 ks=http://192.168.6.4/repo/ks/default.ks ip=192.168.6.7 netmask=255.255.255.0 gateway=192.168.6.1" -R http://192.168.6.5/repo/centos/5/os/x86_64 -C 4 -M 4g -D 5g
|
31
|
+
* -B is boot args where i am assigning all the centos/rhel boot args for Ip setup and kickstart file
|
32
|
+
* -R is the repo URL used by xenserver to start the net install
|
33
|
+
* -C is the number of cpus for this gues
|
34
|
+
* -M is the Memory size
|
35
|
+
* -D is the disk size
|
36
|
+
|
37
|
+
==Source
|
38
|
+
|
@@ -0,0 +1,239 @@
|
|
1
|
+
# Xapi Base Module
|
2
|
+
#
|
3
|
+
# Description:: Setup the Session and auth for xapi
|
4
|
+
# Author:: Jesse Nelson <spheromak@gmail.com>
|
5
|
+
#
|
6
|
+
# Copyright:: Copyright (c) 2012, Jesse Nelson
|
7
|
+
#
|
8
|
+
# License:: Apache License, Version 2.0
|
9
|
+
#
|
10
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
|
+
# you may not use this file except in compliance with the License.
|
12
|
+
# You may obtain a copy of the License at
|
13
|
+
#
|
14
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
15
|
+
#
|
16
|
+
# Unless required by applicable law or agreed to in writing, software
|
17
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
18
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19
|
+
# See the License for the specific language governing permissions and
|
20
|
+
# limitations under the License.
|
21
|
+
|
22
|
+
XAPI_TEMP_REGEX ||= /^CentOS 5.*\(64-bit\)/
|
23
|
+
|
24
|
+
require 'chef/knife'
|
25
|
+
require 'units/standard'
|
26
|
+
|
27
|
+
class Chef::Knife
|
28
|
+
module XapiBase
|
29
|
+
|
30
|
+
|
31
|
+
def self.included(includer)
|
32
|
+
|
33
|
+
includer.class_eval do
|
34
|
+
deps do
|
35
|
+
require 'xenapi'
|
36
|
+
require 'highline'
|
37
|
+
require 'highline/import'
|
38
|
+
require 'readline'
|
39
|
+
end
|
40
|
+
|
41
|
+
option :host,
|
42
|
+
:short => "-h SERVER_URL",
|
43
|
+
:long => "--host SERVER_URL",
|
44
|
+
:description => "The url to the xenserver, http://somehost.local.lan/",
|
45
|
+
:proc => Proc.new { |host| Chef::Config[:knife][:xenserver_host] = host }
|
46
|
+
|
47
|
+
option :xenserver_password,
|
48
|
+
:short => "-K PASSWORD",
|
49
|
+
:long => "--xenserver-password PASSWORD",
|
50
|
+
:description => "Your xenserver password",
|
51
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:xenserver_password] = key }
|
52
|
+
|
53
|
+
option :xenserver_username,
|
54
|
+
:short => "-A USERNAME",
|
55
|
+
:long => "--xenserver-username USERNAME",
|
56
|
+
:description => "Your xenserver username",
|
57
|
+
:proc => Proc.new { |username| Chef::Config[:knife][:xenserver_username] = username }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# highline setup
|
62
|
+
def h
|
63
|
+
@highline ||= ui.highline
|
64
|
+
end
|
65
|
+
|
66
|
+
# setup and return an authed xen api instance
|
67
|
+
def xapi
|
68
|
+
@xapi ||= begin
|
69
|
+
|
70
|
+
session = XenApi::Client.new( Chef::Config[:knife][:xenserver_host] )
|
71
|
+
|
72
|
+
# get the password from the user
|
73
|
+
password = Chef::Config[:knife][:xenserver_password] || nil
|
74
|
+
username = Chef::Config[:knife][:xenserver_username] || "root"
|
75
|
+
if password.nil? or password.empty?
|
76
|
+
password = ask("Enter password for user #{username}: " ) { |input| input.echo = "*" }
|
77
|
+
end
|
78
|
+
session.login_with_password(username, password)
|
79
|
+
|
80
|
+
session
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# get template by name_label
|
85
|
+
def get_template(template)
|
86
|
+
xapi.VM.get_by_name_label(template).first
|
87
|
+
end
|
88
|
+
#
|
89
|
+
# find a template matching what the user provided
|
90
|
+
#
|
91
|
+
# returns a ref to the vm or nil if nothing found
|
92
|
+
#
|
93
|
+
def find_template(template=XAPI_TEMP_REGEX)
|
94
|
+
# if we got a string then try to find that template exact
|
95
|
+
# if no exact template matches, search
|
96
|
+
if template.is_a?(String)
|
97
|
+
found = get_template(template)
|
98
|
+
return found if found
|
99
|
+
end
|
100
|
+
|
101
|
+
# make sure our nil template gets set to default
|
102
|
+
if template.nil?
|
103
|
+
template = XAPI_TEMP_REGEX
|
104
|
+
end
|
105
|
+
|
106
|
+
Chef::Log.debug "Name: #{template.class}"
|
107
|
+
# quick and dirty string to regex
|
108
|
+
unless template.is_a?(Regexp)
|
109
|
+
template = /#{template}/
|
110
|
+
end
|
111
|
+
|
112
|
+
# loop over all vm's and find the template
|
113
|
+
# Wish there was a better API method for this, and there might be
|
114
|
+
# but i couldn't find it
|
115
|
+
Chef::Log.debug "Using regex: #{template}"
|
116
|
+
xapi.VM.get_all_records().each_value do |vm|
|
117
|
+
if vm["is_a_template"] and vm["name_label"] =~ template
|
118
|
+
Chef::Log.debug "Matched: #{h.color(vm["name_label"], :yellow )}"
|
119
|
+
found = vm # we're gonna go with the last found
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# ensure return values
|
124
|
+
if found
|
125
|
+
puts "Using Template: #{h.color(found["name_label"], :cyan)}"
|
126
|
+
return get_template(found["name_label"]) # get the ref to this one
|
127
|
+
end
|
128
|
+
return nil
|
129
|
+
end
|
130
|
+
|
131
|
+
# generate a random mac address
|
132
|
+
def generate_mac
|
133
|
+
("%02x"%(rand(64)*4|2))+(0..4).inject(""){|s,x|s+":%02x"%rand(256)}
|
134
|
+
end
|
135
|
+
|
136
|
+
# add a new vif
|
137
|
+
def add_vif_by_name(vm_ref, dev_num, net_name)
|
138
|
+
puts "Looking up vif for: #{h.color(net_name, :cyan)}"
|
139
|
+
network_ref = xapi.network.get_by_name_label(net_name).first
|
140
|
+
if network_ref.nil?
|
141
|
+
ui.warn "#{h.color(net_name,:red)} not found, moving on"
|
142
|
+
return
|
143
|
+
end
|
144
|
+
|
145
|
+
mac = generate_mac
|
146
|
+
puts "Provisioning: #{h.color(net_name, :cyan)}, #{h.color(mac,:green)}, #{h.color(network_ref, :yellow)}"
|
147
|
+
|
148
|
+
vif = {
|
149
|
+
'device' => dev_num.to_s,
|
150
|
+
'network' => network_ref,
|
151
|
+
'VM' => vm_ref,
|
152
|
+
'MAC' => generate_mac,
|
153
|
+
'MTU' => "1500",
|
154
|
+
"other_config" => {},
|
155
|
+
"qos_algorithm_type" => "",
|
156
|
+
"qos_algorithm_params" => {}
|
157
|
+
}
|
158
|
+
vif_ref = xapi.VIF.create(vif)
|
159
|
+
vif_ref
|
160
|
+
end
|
161
|
+
|
162
|
+
# remove all vifs on a record
|
163
|
+
def clear_vm_vifs(record)
|
164
|
+
record["VIFs"].each do |vif|
|
165
|
+
Chef::Log.debug "Removing vif: #{h.color(vif, :red, :bold)}"
|
166
|
+
xapi.VIF.destroy(vif)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# returns sr_ref to the default sr on pool
|
171
|
+
def find_default_sr()
|
172
|
+
xapi.pool.get_default_SR( xapi.pool.get_all()[0] )
|
173
|
+
end
|
174
|
+
|
175
|
+
# return an SR record from the name_label
|
176
|
+
def get_sr_by_name(name)
|
177
|
+
sr_ref = xapi.SR.get_by_name_label(name)
|
178
|
+
if sr_ref.empty? or sr_ref.nil?
|
179
|
+
ui.error "SR name #{h.color( name ) } not found"
|
180
|
+
return nil
|
181
|
+
end
|
182
|
+
sr = xapi.SR.get_record( sr_ref )
|
183
|
+
end
|
184
|
+
|
185
|
+
# convert 1g/1m/1t to bytes
|
186
|
+
def input_to_bytes(size)
|
187
|
+
case size
|
188
|
+
when /g|gb/i
|
189
|
+
size.to_i.gb.to_bytes.to_i
|
190
|
+
when /m|mb/i
|
191
|
+
size.to_i.mb.to_bytes.to_i
|
192
|
+
when /t|tb/i
|
193
|
+
size.to_i.tb.to_bytes.to_i
|
194
|
+
else
|
195
|
+
size.to_i.gb.to_bytes.to_i
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# create a vdi return ref
|
200
|
+
def create_vdi(name, sr_ref, size)
|
201
|
+
vdi_record = {
|
202
|
+
"name_label" => "#{name}",
|
203
|
+
"name_description" => "Root disk for #{name} created by knfie xapi",
|
204
|
+
"SR" => sr_ref,
|
205
|
+
"virtual_size" => input_to_bytes(size).to_s,
|
206
|
+
"type" => "system",
|
207
|
+
"sharable" => false,
|
208
|
+
"read_only" => false,
|
209
|
+
"other_config" => {},
|
210
|
+
}
|
211
|
+
|
212
|
+
vdi_ref = xapi.VDI.create( vdi_record )
|
213
|
+
ui.msg "VDI: #{h.color( name, :cyan )} #{h.color( vdi_ref, :cyan )} created"
|
214
|
+
vdi_ref
|
215
|
+
end
|
216
|
+
|
217
|
+
# create vbd and return a ref
|
218
|
+
def create_vbd(vm_ref, vdi_ref, position)
|
219
|
+
vbd_record = {
|
220
|
+
"VM" => vm_ref,
|
221
|
+
"VDI" => vdi_ref,
|
222
|
+
"empty" => false,
|
223
|
+
"other_config" => {"owner"=>""},
|
224
|
+
"userdevice" => position.to_s,
|
225
|
+
"bootable" => true,
|
226
|
+
"mode" => "RW",
|
227
|
+
"qos_algorithm_type" => "",
|
228
|
+
"qos_algorithm_params" => {},
|
229
|
+
"qos_supported_algorithms" => [],
|
230
|
+
"type" => "Disk"
|
231
|
+
}
|
232
|
+
|
233
|
+
vbd_ref = xapi.VBD.create(vbd_record)
|
234
|
+
ui.msg "VBD: #{h.color vbd_ref, :cyan } created"
|
235
|
+
vbd_ref
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Jesse Nelson (<spheromak@gmail.com>)
|
3
|
+
#
|
4
|
+
# Based on xapi plugin written by
|
5
|
+
# Author:: Adam Jacob (<adam@opscode.com>)
|
6
|
+
#
|
7
|
+
# Copyright:: Copyright (c) 2012 Jesse Nelson
|
8
|
+
#
|
9
|
+
# License:: Apache License, Version 2.0
|
10
|
+
#
|
11
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
12
|
+
# you may not use this file except in compliance with the License.
|
13
|
+
# You may obtain a copy of the License at
|
14
|
+
#
|
15
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
#
|
17
|
+
# Unless required by applicable law or agreed to in writing, software
|
18
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
19
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20
|
+
# See the License for the specific language governing permissions and
|
21
|
+
# limitations under the License.
|
22
|
+
#
|
23
|
+
|
24
|
+
|
25
|
+
require 'chef/knife/xapi_base'
|
26
|
+
|
27
|
+
class Chef
|
28
|
+
class Knife
|
29
|
+
class XapiGuestCreate < Knife
|
30
|
+
include Chef::Knife::XapiBase
|
31
|
+
|
32
|
+
banner "knife xapi guest create NAME [NETWORKS] (options)"
|
33
|
+
|
34
|
+
option :bootstrap,
|
35
|
+
:short => "-B BOOTSTRAP",
|
36
|
+
:long => "--xapi-bootstrap BOOTSTRAP",
|
37
|
+
:description => "bootstrap template to push to the server",
|
38
|
+
:proc => Proc.new { |bootstrap| Chef::Config[:knife][:xapi_bootstrap] = bootstrap }
|
39
|
+
|
40
|
+
option :vm_template,
|
41
|
+
:short => "-T Template Name Label",
|
42
|
+
:long => "--xapi-vm-template",
|
43
|
+
:description => "xapi template name to create from. accepts an string or regex",
|
44
|
+
:proc => Proc.new { |template| Chef::Config[:knife][:xapi_vm_template] = template }
|
45
|
+
|
46
|
+
option :install_repo,
|
47
|
+
:short => "-R If you're using a builtin template you will need to specify a repo url",
|
48
|
+
:long => "--xapi-install-repo",
|
49
|
+
:description => "Install repo for this template (if needed)",
|
50
|
+
:proc => Proc.new { |repo| Chef::Config[:knife][:xapi_install_repo] = repo }
|
51
|
+
|
52
|
+
option :storage_repo,
|
53
|
+
:short => "-S Storage repo to provision VM from",
|
54
|
+
:long => "--xapi-sr",
|
55
|
+
:description => "The Xen SR to use, If blank will use pool/hypervisor default",
|
56
|
+
:proc => Proc.new { |sr| Chef::Config[:knife][:xapi_sr] = sr }
|
57
|
+
|
58
|
+
option :kernel_params,
|
59
|
+
:short => "-B Set of kernel boot params to pass to the vm",
|
60
|
+
:long => "--xapi-kernel-params",
|
61
|
+
:description => "You can add more boot options to the vm e.g.: \"ks='http://foo.local/ks'\"",
|
62
|
+
:proc => Proc.new {|kernel| Chef::Config[:knife][:xapi_kernel_params] = kernel }
|
63
|
+
|
64
|
+
option :disk_size,
|
65
|
+
:short => "-D Size of disk. 1g 512m etc",
|
66
|
+
:long => "--xapi-disk-size",
|
67
|
+
:description => "The size of the root disk, use 'm' 'g' 't' if no unit specified assumes g",
|
68
|
+
:proc => Proc.new {|disk| Chef::Config[:knife][:xapi_disk_size] = disk }
|
69
|
+
|
70
|
+
option :cpus,
|
71
|
+
:short => "-C Number of VCPUs to provision",
|
72
|
+
:long => "--xapi-cpus",
|
73
|
+
:description => "Number of VCPUS this vm should have 1 4 8 etc",
|
74
|
+
:proc => Proc.new {|cpu| Chef::Config[:knife][:xapi_cpus] = cpu }
|
75
|
+
|
76
|
+
option :mem,
|
77
|
+
:short => "-M Ammount of memory to provision",
|
78
|
+
:long => "--xapi-mem",
|
79
|
+
:description => "Ammount of memory the VM should have specify with m g etc 512m, 2g if no unit spcified it assumes gigabytes",
|
80
|
+
:proc => Proc.new {|mem| Chef::Config[:knife][:xapi_mem] = mem }
|
81
|
+
|
82
|
+
# destroy/remove VM refs and exit
|
83
|
+
def cleanup(vm_ref)
|
84
|
+
ui.warn "Clenaing up work and exiting"
|
85
|
+
xapi.VM.destroy(vm_ref)
|
86
|
+
exit 1
|
87
|
+
end
|
88
|
+
|
89
|
+
def run
|
90
|
+
server_name = @name_args[0]
|
91
|
+
$stdout.sync = true
|
92
|
+
|
93
|
+
# get the template vm we are going to build from
|
94
|
+
template_ref = find_template( Chef::Config[:knife][:xapi_vm_template] )
|
95
|
+
|
96
|
+
ui.msg "Cloning Guest from Template: #{h.color(template_ref, :bold, :cyan )}"
|
97
|
+
vm_ref = xapi.VM.clone(template_ref, server_name)
|
98
|
+
|
99
|
+
# begin
|
100
|
+
xapi.VM.set_name_description(vm_ref, "VM built by knife-xapi as #{server_name}")
|
101
|
+
|
102
|
+
# configure the install repo
|
103
|
+
repo = Chef::Config[:knife][:xapi_install_repo] || "http://isoredirect.centos.org/centos/5/os/x86_64/"
|
104
|
+
ui.msg "Setting Install Repo: #{h.color(repo,:bold, :cyan)}"
|
105
|
+
xapi.VM.set_other_config(vm_ref, { "install-repository" => repo } )
|
106
|
+
|
107
|
+
cpus = Chef::Config[:knife][:xapi_cpus] || 2
|
108
|
+
ui.msg "Setting up CPUS #{ h.color cpus, :cyan }"
|
109
|
+
xapi.VM.set_VCPUs_max( vm_ref, cpus.to_s )
|
110
|
+
xapi.VM.set_VCPUs_at_startup( vm_ref, cpus.to_s )
|
111
|
+
|
112
|
+
memory_size = input_to_bytes( Chef::Config[:knife][:xapi_mem] || "1g" ).to_s
|
113
|
+
ui.msg "Mem size: #{ h.color( memory_size, :cyan)}"
|
114
|
+
|
115
|
+
# static-min <= dynamic-min = dynamic-max = static-max
|
116
|
+
xapi.VM.set_memory_limits(vm_ref, memory_size, memory_size, memory_size, memory_size)
|
117
|
+
|
118
|
+
# setup the Boot args
|
119
|
+
args = Chef::Config[:knife][:xapi_kernel_params] || "graphical utf8"
|
120
|
+
ui.msg "Setting Boot Args: #{h.color args, :cyan}"
|
121
|
+
xapi.VM.set_PV_args( vm_ref, args )
|
122
|
+
|
123
|
+
# TODO: validate that the vm gets a network here
|
124
|
+
networks = @name_args[1..-1]
|
125
|
+
# if the user has provided networks
|
126
|
+
if networks.length >= 1
|
127
|
+
clear_vm_vifs( xapi.VM.get_record( vm_ref ) )
|
128
|
+
networks.each_with_index do |net, index|
|
129
|
+
add_vif_by_name(vm_ref, index, net)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
sr_ref = find_default_sr
|
134
|
+
if Chef::Config[:knife][:xapi_sr]
|
135
|
+
sr_ref = get_sr_by_name( Chef::Config[:knife][:xapi_sr] )
|
136
|
+
end
|
137
|
+
|
138
|
+
if sr_ref.nil?
|
139
|
+
ui.error "SR specified not found or can't be used Aborting"
|
140
|
+
cleanup(vm_ref)
|
141
|
+
end
|
142
|
+
ui.msg "SR: #{h.color sr_ref, :cyan} created"
|
143
|
+
|
144
|
+
# Create the VDI
|
145
|
+
disk_size = Chef::Config[:knife][:xapi_disk_size] || "8g"
|
146
|
+
vdi_ref = create_vdi("#{server_name}-root", sr_ref, disk_size )
|
147
|
+
|
148
|
+
# Attach the VDI to the VM
|
149
|
+
vbd_ref = create_vbd(vm_ref, vdi_ref, 0)
|
150
|
+
|
151
|
+
ui.msg "Provisioning new Guest: #{h.color(vm_ref, :bold, :cyan )}"
|
152
|
+
provisioned = xapi.VM.provision(vm_ref)
|
153
|
+
|
154
|
+
ui.msg "Starting new Guest: #{h.color( provisioned, :cyan)} "
|
155
|
+
xapi.Async.VM.start(vm_ref, false, true)
|
156
|
+
|
157
|
+
# rescue Exception => e
|
158
|
+
# ui.msg "#{h.color 'ERROR:'} #{h.color( e.message, :red )}"
|
159
|
+
# ui.error "#{h.color( e.backtrace, :yellow)}"
|
160
|
+
#
|
161
|
+
# cleanup(vm_ref)
|
162
|
+
# end
|
163
|
+
# TODO: bootstrap
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knife-xapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jesse Nelson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: chef
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.14
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.14
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: xenapi
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: highline
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: units
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Xen API Support for Chef's Knife Command
|
79
|
+
email: spheromak@gmail.com
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files:
|
83
|
+
- LICENSE
|
84
|
+
- README.rdoc
|
85
|
+
files:
|
86
|
+
- LICENSE
|
87
|
+
- README.rdoc
|
88
|
+
- lib/chef/knife/xapi_base.rb
|
89
|
+
- lib/chef/knife/xapi_guest_create.rb
|
90
|
+
- lib/knife-xapi/version.rb
|
91
|
+
homepage: https://github.com/spheromak/knife-xapi
|
92
|
+
licenses: []
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.8.19
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: Xen API Support for Chef's Knife Command
|
115
|
+
test_files: []
|