Files
one/env/bin/appenv
T

259 lines
6.8 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2010-2012, C12G Labs S.L. #
# #
# Licensed under the C12G Commercial Open-source License (the #
# "License"); you may not use this file except in compliance #
# with the License. You may obtain a copy of the License as part #
# of the software distribution. #
# #
# Unless agreed to in writing, software distributed under the #
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES #
# OR CONDITIONS OF ANY KIND, either express or implied. See the #
# License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end
$: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/apptools/env"
$: << RUBY_LIB_LOCATION+"/cli"
$: << '.'
require 'onechef'
require 'base64'
require 'command_parser'
require 'appenv_helper'
require 'one_helper/onetemplate_helper'
require 'pp'
NAME={
:name => "name",
:short => "-n name",
:large => "--name name",
:format => String,
:description => "Name of the node"
}
DESCRIPTION={
:name => "description",
:short => "-d description",
:large => "--description description",
:format => String,
:description => "description of the node"
}
TEMPLATES={
:name => "templates",
:short => "-t t1,t2",
:large => "--templates t1,t2",
:format => Array,
:description => "Templates compatible with the node"
}
TEMPLATE={
:name => "template",
:short => "-t template",
:large => "--template template",
:format => String,
:description => "Template used to instantiate the node"
}
VARIABLES={
:name => "variables",
:short => "-d v1=d1,v2=d2",
:large => "--data v1=d1,v2=d2",
:format => Array,
:description => "Specific variables"
}
NODE={
:name => "node_file",
:short => "-f node_file",
:large => "--node node_file",
:format => String,
:description => "Node file"
}
COOKBOOKS={
:name => "cookbooks",
:short => "-c cookbooks",
:large => "--cookbooks cookbooks",
:format => String,
:description => "URL to extra cookbooks"
}
cmd = CommandParser::CmdParser.new(ARGV) do
usage "`appenv` <command> [<args>] [<options>]"
version OpenNebulaHelper::ONE_VERSION
helper = AppEnvHelper.new
template_helper = OneTemplateHelper.new
set :format, :node, AppEnvHelper.to_id_desc do |arg|
helper.to_id(arg)
end
set :format, :node_list, AppEnvHelper.list_to_id_desc do |arg|
helper.list_to_id(arg)
end
set :format, :templateid, OneTemplateHelper.to_id_desc do |arg|
template_helper.to_id(arg)
end
addnode_desc = <<-EOT.unindent
Adds a new node
EOT
command :create, addnode_desc, [:node_file],
:options => [NAME, TEMPLATES, COOKBOOKS, DESCRIPTION] do
node=OpenNebula::ChefConf.new
node.node=File.read args.first
node.name=options[:name] if options[:name]
node.templates=options[:templates] if options[:templates]
node.cookbooks=options[:cookbooks] if options[:cookbooks]
node.description=options[:description] if options[:description]
client=Client.new
res=node.save(client)
if OpenNebula.is_error? res[0]
STDERR.puts(res[1].message)
-1
else
puts "ID: #{res.first.id}"
0
end
end
delete_desc = <<-EOT.unindent
Deletes the given node
States: ANY
EOT
command :delete, delete_desc, [:range, :node_list] do
helper.perform_actions(args[0],options,"deleted") do |node|
node.delete
end
end
show_desc = <<-EOT.unindent
Show defined node
EOT
command :show, show_desc, [:node], :options=>OpenNebulaHelper::XML do
helper.show_resource(args[0],options)
end
list_desc = <<-EOT.unindent
Lists defined node
EOT
command :list, list_desc,:options=>OpenNebulaHelper::XML do
helper.list_pool( options )
end
update_desc = <<-EOT.unindent
Update a defined node
EOT
command :update, update_desc, [:node],
:options=>[DESCRIPTION, TEMPLATES, NODE, COOKBOOKS] do
client=OpenNebula::Client.new
doc=OpenNebula::ChefDoc.new(
OpenNebula::ChefDoc.build_xml(args[0]),
client)
doc.info
node=doc.node
if options[:node_file]
text=File.read(options[:node_file])
node.node=text
end
node.name=options[:name] if options[:name]
node.templates=options[:templates] if options[:templates]
node.cookbooks=options[:cookbooks] if options[:cookbooks]
node.description=options[:description] if options[:description]
doc.update(node.to_json)
0
end
instantiate_desc = <<-EOT.unindent
Instantiate a template with a node
EOT
command :instantiate, instantiate_desc, [:node], :options=>[VARIABLES,
TEMPLATE] do
if !options[:template]
STDERR.puts "Template not specified"
next 255
end
client=OpenNebula::Client.new
xml=OpenNebula::ChefDoc.build_xml(args[0].to_i)
chef_doc=OpenNebula::ChefDoc.new(xml, client)
chef_doc.info
# Check template
res=template_helper.to_id(options[:template])
if res.first!=0
STDERR.puts(res.last)
next 255
end
template_id=res.last
xml=OpenNebula::ChefDoc.build_xml(template_id)
template=OpenNebula::Template.new(xml, client)
template.info
if chef_doc.node['templates']
allowed=false
chef_doc.node['templates'].each do |t|
if t==template.id.to_s || t==template.name
allowed=true
break
end
end
if !allowed
STDERR.puts "Template not compatible with this node"
next 255
end
end
res=chef_doc.instantiate(options[:template], options[:variables]||{})
if OpenNebula.is_error? res
STDERR.puts res.message
next 255
end
puts "VM ID: #{res.id}"
0
end
end