fluke/devops-client/lib/devops-client/handler/server.rb
2016-02-02 16:01:48 +07:00

177 lines
4.3 KiB
Ruby

require "devops-client/handler/handler"
require "devops-client/options/server_options"
require "devops-client/output/server"
require "devops-client/handler/project"
class Server < Handler
output_with Output::Server
def initialize(host, def_options={})
@host, @options = host, def_options
@options_parser = ServerOptions.new(ARGV, def_options)
end
def handle
@options, @args = @options_parser.parse_options_for!(current_command)
case current_command
when :list
list_handler
output
when :show
show_handler
output
when :create
create_handler
when :delete
delete_handler
when :bootstrap
bootstrap_handler
when :sync
sync_handler
when :pause
pause_handler
when :unpause
unpause_handler
when :reserve
reserve_handler
when :unreserve
unreserve_handler
when :add
add_static_handler
end
end
def list_handler
if @args[2].nil?
@list = get("/servers")
return @list
end
self.options[:type] = @args[2]
@list = case @args[2]
when "chef"
get("/servers/chef").map {|l| {"chef_node_name" => l}}
when "ec2", "openstack", "static"
get("/servers/#{@args[2]}")
else
@options_parser.invalid_list_command
abort("Invlid argument '#{@args[2]}'")
end
end
def create_handler
r = inspect_parameters @options_parser.create_params, @args[2], @args[3]
unless r.nil?
@options_parser.invalid_create_command
abort(r)
end
q = {
:project => @args[2],
:deploy_env => @args[3]
}
[:key, :without_bootstrap, :name, :groups, :force, :private_ip].each do |k|
q[k] = self.options[k] unless self.options[k].nil?
end
post_chunk "/server", q
end
def delete_handler
@args[2..-1].each do |name|
r = inspect_parameters @options_parser.delete_params, name
unless r.nil?
@options_parser.invalid_delete_command
abort(r)
end
if question(I18n.t("handler.server.question.delete", :name => name))
puts "Server '#{name}', deleting..."
puts delete("/server/#{name}", options)
end
end
""
end
def show_handler
r = inspect_parameters @options_parser.show_params, @args[2]
unless r.nil?
@options_parser.invalid_show_command
abort r
end
@show = get("/server/#{@args[2]}")
end
def bootstrap_handler
r = inspect_parameters @options_parser.bootstrap_params, @args[2]
unless r.nil?
@options_parser.invalid_bootstrap_command
abort(r)
end
q = {
:instance_id => @args[2]
}
[:name, :bootstrap_template, :run_list].each do |k|
q[k] = self.options[k] unless self.options[k].nil?
end
if q.has_key?(:run_list)
abort unless Project.validate_run_list(q[:run_list])
end
post_chunk "/server/bootstrap", q
end
def add_static_handler # add <project> <env> <private_ip> <ssh_username> --public-ip <public_ip> -k <keyname>
r = inspect_parameters @options_parser.add_params, @args[2], @args[3], @args[4], @args[5], @args[6]
unless r.nil?
@options_parser.invalid_add_command
abort(r)
end
q = {
:project => @args[2],
:deploy_env => @args[3],
:private_ip => @args[4],
:remote_user => @args[5],
:key => @args[6]
}
q[:public_ip] = self.options[:public_ip] unless self.options[:public_ip].nil?
post "/server/add", q
end
def pause_handler
r = inspect_parameters @options_parser.pause_params, @args[2]
unless r.nil?
@options_parser.invalid_pause_command
abort(r)
end
post "/server/#{@args[2]}/pause", options
end
def unpause_handler
r = inspect_parameters @options_parser.unpause_params, @args[2]
unless r.nil?
@options_parser.invalid_unpause_command
abort(r)
end
post "/server/#{@args[2]}/unpause", options
end
def reserve_handler
r = inspect_parameters @options_parser.reserve_params, @args[2]
unless r.nil?
@options_parser.invalid_reserve_command
abort(r)
end
post "/server/#{@args[2]}/reserve", options
end
def unreserve_handler
r = inspect_parameters @options_parser.unreserve_params, @args[2]
unless r.nil?
@options_parser.invalid_unreserve_command
abort(r)
end
post "/server/#{@args[2]}/unreserve", options
end
end