#!/usr/bin/env ruby # vim: filetype=ruby require 'conoha' require 'conoha/util' require 'pp' subcommand = ARGV.shift Conoha.init! def server_id(server_id_or_index) if server_id_or_index.length == '01234567-89ab-cdef-0123-456789abcdef'.length server_id_or_index else Conoha.vps_list[server_id_or_index.to_i] end end case subcommand when 'authenticate' Conoha.authenticate! puts 'Succeeded!' when 'vpslist' pp Conoha.vps_list when 'ipaddress' exit 1 if ARGV.size != 1 pp Conoha.ip_address_of server_id(ARGV.first) when 'create' pp Conoha.create *ARGV when 'delete' exit 1 if ARGV.size != 1 puts Conoha.delete server_id(ARGV.first) when 'boot' exit 1 if ARGV.size != 1 puts Conoha.boot server_id(ARGV.first) when 'shutdown' exit 1 if ARGV.size != 1 puts Conoha.shutdown server_id(ARGV.first) when 'imagelist' pp Conoha.images when 'imagecreate' exit 1 if ARGV.size != 2 name = ARGV[1] puts Conoha.create_image server_id(ARGV[0]), name when 'imagedelete' exit 1 if ARGV.size != 1 image_ref = ARGV.first puts Conoha.delete_image image_ref when 'createfromimage' exit 1 if ARGV.size != 2 image_ref = ARGV[0] ram = ARGV[1] puts Conoha.create_from_image image_ref, ram when 'ssh' exit 1 if ARGV.size < 1 || 2 < ARGV.size ipaddress = ipv4(Conoha.ip_address_of(server_id(ARGV.first))) user = ARGV[1].nil? ? '' : "#{ARGV[1]}@" command = "ssh -oStrictHostKeyChecking=no #{user}#{ipaddress}" puts command system command when 'mosh' exit 1 if ARGV.size != 1 ipaddress = ipv4(Conoha.ip_address_of(server_id(ARGV.first))) command = "mosh #{ipaddress}" puts command system command when 'browse' exit 1 if ARGV.size < 1 || 2 < ARGV.size ipaddress = ipv4(Conoha.ip_address_of(server_id(ARGV.first))) port = ARGV[1].nil? ? '' : ":#{ARGV[1]}" command = "xdg-open http://#{ipaddress}#{port}" puts command system command else STDERR.puts 'Error' end