Files
one/stage/bin/appstage
T

311 lines
8.3 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2010-2013, C12G Labs S.L. #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or 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+"/oneapps/stage"
$: << RUBY_LIB_LOCATION+"/cli"
$: << '.'
require 'onechef'
require 'base64'
require 'oneapps/version'
require 'command_parser'
require 'appstage_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 "`appstage` <command> [<args>] [<options>]"
version OneApps::VERSION_TEXT
set :option, CommandParser::VERSION
helper = AppStageHelper.new
template_helper = OneTemplateHelper.new
before_proc do
helper.set_client(options)
template_helper.set_client(options)
end
set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg|
OpenNebulaHelper.rname_to_id(arg, "GROUP")
end
set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg|
OpenNebulaHelper.rname_to_id(arg, "USER")
end
set :format, :node, AppStageHelper.to_id_desc do |arg|
helper.to_id(arg)
end
set :format, :node_list, AppStageHelper.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, false, ChefDocPool::INFO_ALL )
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
chgrp_desc = <<-EOT.unindent
Changes the node group
EOT
command :chgrp, chgrp_desc,[:range, :node_list], :groupid do
helper.perform_actions(args[0],options,"Group changed") do |node|
node.chown(-1, args[1].to_i)
end
end
chown_desc = <<-EOT.unindent
Changes the node owner and group
EOT
command :chown, chown_desc, [:range, :node_list], :userid,
[:groupid,nil] do
gid = args[2].nil? ? -1 : args[2].to_i
helper.perform_actions(args[0],options,
"Owner/Group changed") do |node|
node.chown(args[1].to_i, gid)
end
end
chmod_desc = <<-EOT.unindent
Changes the node permissions
EOT
command :chmod, chmod_desc, [:range, :imageid_list], :octet do
helper.perform_actions(args[0],options,
"Permissions changed") do |node|
node.chmod_octet(args[1])
end
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::Template.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