112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/chef/knife/cosmic_template_register.rb', line 112
def run
validate_base_options
Chef::Log.debug("Validate template name")
if locate_config_value(:name)
templatename = locate_config_value(:name)
else
templatename = @name_args.first
unless /^[a-zA-Z0-9][a-zA-Z0-9_\-# ]*$/.match templatename then
ui.error "Invalid templatename."
exit 1
end
end
unless locate_config_value(:ostypeid) then
ui.error "No os type id specified"
exit 1
end
unless /^http(s)?\:\/\//.match locate_config_value(:url) then
ui.error "URL (#{locate_config_value(:url)}) is not a well formatted url"
exit 1
end
unless (locate_config_value(:bits) == 64 or locate_config_value(:bits) == 32 ) then
ui.error "Bits must be 32 or 64"
exit 1
end
if (locate_config_value(:zone) == -1)
zoneid = -1
else
Chef::Log.debug("Resolving zone #{locate_config_value(:zone)}\n")
zone = connection.get_zone(locate_config_value(:zone))
if ! zone then
ui.error "Unable to resolve zone #{locate_config_value(:zone)}\n"
exit 1
end
zoneid = zone['id']
end
print "#{ui.color("Registring template: #{templatename}", :magenta)}\n"
params = {
'command' => 'registerTemplate',
'name' => templatename,
'displaytext' => locate_config_value(:displaytext),
'format' => locate_config_value(:format),
'hypervisor' => locate_config_value(:hypervisor),
'ostypeid' => locate_config_value(:ostypeid),
'url' => locate_config_value(:url),
'zoneid' => zoneid,
'bits' => locate_config_value(:bits),
}
params['isdynamicallyscalable'] = locate_config_value(:isdynamicallyscalable) if locate_config_value(:isdynamicallyscalable)
params['isextractable'] = locate_config_value(:isextractable) if locate_config_value(:isextractable)
params['ispublic'] = locate_config_value(:public) if locate_config_value(:public)
params['isfeatured'] = locate_config_value(:featured) if locate_config_value(:featured)
params['passwordenabled'] = locate_config_value(:passwordenabled) if locate_config_value(:passwordenabled)
params['sshkeyenabled'] = locate_config_value(:sshkeyenabled) if locate_config_value(:sshkeyenabled)
params['requireshvm'] = locate_config_value(:requireshvm) if locate_config_value(:requireshvm)
json = connection.send_request(params)
if ! json then
ui.error "Template #{templatename} not registered\n"
exit 1
end
print "TemplateId #{json['template'][0]['id']} is being created\n"
end
|