#!/usr/bin/env ruby unless File.exist? 'Conoharantfile' if ARGV[0] == 'init' require 'securerandom' File.open('Conoharantfile', 'w') do |f| f.write(< 'nothing', 'id' => nil, 'images' => [], 'latestnumber' => 0, 'image-tags' => [], } end require 'conoha/util' # Dump current status to ".conoharant/status.yml" # @params [Hash] status def dump_conoharant_status(status) File.open('.conoharant/status.yml', 'w') do |f| f.print(YAML.dump(status)) end end require 'conoha/version' arg = ARGV.first case arg when 'version', '--version', '-v' puts ConohaVersion::ITSELF exit 0 end require 'conoha' Conoha.init! begin Conoha.vps_list rescue => e puts "# Auth token is invalid." puts "# Reauthenticating..." begin Conoha.authenticate! rescue => e STDERR.puts "Failed to authenticate again." STDERR.puts "Retry after modifying \"~/.conoha-config.json\"." exit 1 end end puts "# Authentication OK." case arg when 'status' puts status['status'] when 'up' if status['status'] == 'running' puts "# Nothing to do." exit 0 end if status['id'].nil? begin server_id = Conoha.create config['os'], config['ram'] rescue => e STDERR.puts e.to_s exit 1 end puts server_id loop do sleep 10 result = Conoha.status_of server_id break if result == "ACTIVE" puts "# Current status is \"#{result}\", not \"ACTIVE\"." puts "# Re-check after 10 seconds..." end puts "# OK!" status['id'] = server_id else server_id = status['id'] puts "conoha boot #{server_id}" Conoha.boot server_id end status['status'] = 'running' dump_conoharant_status status when 'halt', 'shutdown' if status['status'] != 'running' STDERR.puts "A running VPS doesn't exist." exit 1 end server_id = status['id'] puts "conoha shutdown #{server_id}" Conoha.shutdown server_id status['status'] = 'shutdown' dump_conoharant_status status when 'destroy' if status['status'] == 'nothing' puts "# Nothing to do." exit 0 end server_id = status['id'] if status['status'] == 'running' puts "conoha shutdown #{server_id}" Conoha.shutdown server_id sleep 10 end loop do result = Conoha.status_of server_id break if result == "SHUTOFF" puts "# Current status is \"#{result}\", not \"SHUTOFF\"." puts "# Re-check after 10 seconds..." sleep 10 end puts "conoha delete #{server_id}" Conoha.delete server_id status['status'] = 'nothing' status['id'] = nil dump_conoharant_status status puts "# OK!" when 'ssh', 'mosh' connection = arg # 'ssh' or 'mosh' if status['status'] != 'running' STDERR.puts "VPS is not running." exit 1 end server_id = status['id'] ipaddress = ipv4(Conoha.ip_address_of(server_id)) user = ARGV[1].nil? ? '' : "#{ARGV[1]}@" case connection when 'ssh' command = "ssh -oStrictHostKeyChecking=no #{user}#{ipaddress}" when 'mosh' command = "mosh #{user}#{ipaddress}" end puts command system command when 'dump' if status['status'] != 'running' STDERR.puts "A running VPS doesn't exist." exit 1 end latest_number = status['latestnumber'] server_id = status['id'] name = "#{config['tagprefix']}#{latest_number + 1}" latest_number += 1 puts "conoha shutdown #{server_id}" Conoha.shutdown server_id loop do sleep 60 puts "conoha imagecreate #{server_id} #{name}" result = Conoha.create_image server_id, name break if result == 'OK' puts '# Error! Retry after 60 seconds...' end puts '# OK!' image_id = Conoha.images.find { |e| e[0] == name }[1] loop do sleep 60 puts "conoha delete #{server_id}" result = Conoha.delete server_id break if result == 'OK' puts '# Error! Retry after 60 seconds...' end puts '# OK!' status['status'] = 'nothing' status['id'] = nil status['images'] << image_id status['latestnumber'] = latest_number status['image-tags'] << name dump_conoharant_status status when 'restore' # TODO: Remove comparing to 'no'. if status['status'] != 'no' && status['status'] != 'nothing' STDERR.puts "A VPS already exists." exit 1 end image_id = status['images'][-1] ram = config['ram'] puts "conoha restore #{image_id} #{ram}" server_id = Conoha.create_from_image image_id, ram loop do sleep 10 result = Conoha.status_of server_id break if result == "ACTIVE" puts "# Current status is \"#{result}\", not \"ACTIVE\"." puts "# Re-check after 10 seconds..." end puts "# OK!" status['status'] = 'running' status['id'] = server_id dump_conoharant_status status when 'clean' images = status['images'] if images.length > 2 images[0...-2].each do |e| puts "conoha delete #{e}" Conoha.delete_image e end status['images'] = status['images'][-2..-1] status['image-tags'] = status['image-tags'][-2..-1] dump_conoharant_status status else puts "# Nothing to do." end when 'browse' server_id = status['id'] ipaddress = ipv4(Conoha.ip_address_of(server_id)) port = ARGV[1].nil? ? '' : ":#{ARGV[1]}" command = "xdg-open http://#{ipaddress}#{port}" puts command system command when 'sftp' server_id = status['id'] ipaddress = ipv4(Conoha.ip_address_of(server_id)) command = "sftp #{ipaddress}" puts command system command else STDERR.puts 'Invalid argument.' exit 1 end