Class: VagrantPlugins::Vultr::Helpers::ApiClient
- Inherits:
-
Object
- Object
- VagrantPlugins::Vultr::Helpers::ApiClient
- Includes:
- Vagrant::Util::Retryable
- Defined in:
- lib/vagrant-vultr/helpers/client.rb
Constant Summary collapse
- TimeoutError =
Class.new(StandardError)
Instance Method Summary collapse
- #create_server(attributes) ⇒ Object
- #create_ssh_config(machine) ⇒ Object
- #create_ssh_key(name, key) ⇒ Object
- #destroy_server(sub_id) ⇒ Object
-
#initialize(token, timeout) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #os_id(os) ⇒ Object
- #reboot_server(sub_id) ⇒ Object
- #region_id(region) ⇒ Object
- #server(sub_id) ⇒ Object
- #servers ⇒ Object
- #ssh_key_id(ssh_key_name) ⇒ Object
- #start_server(sub_id) ⇒ Object
- #startup_script_id(startup_script_name) ⇒ Object
- #stop_server(sub_id) ⇒ Object
- #vps_plan_id(plan) ⇒ Object
- #wait_for_ssh(machine) ⇒ Object
- #wait_to_activate(sub_id) ⇒ Object
- #wait_to_destroy(sub_id) ⇒ Object
- #wait_to_power_on(sub_id) ⇒ Object
Constructor Details
Instance Method Details
#create_server(attributes) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 35 def create_server(attributes) params = { DCID: region_id(attributes[:region]), VPSPLANID: vps_plan_id(attributes[:plan]), SSHKEYID: ssh_key_id(attributes[:ssh_key_name]), enable_ipv6: attributes[:enable_ipv6], enable_private_network: attributes[:enable_private_network], label: attributes[:label], tag: attributes[:tag], hostname: attributes[:hostname] } if attributes[:snapshot] params.merge!(OSID: os_id('Snapshot'), SNAPSHOTID: attributes[:snapshot]) else params.merge!(OSID: os_id(attributes[:os]), SCRIPTID: startup_script_id(attributes[:startup_script_name])) end request { ::Vultr::Server.create(params) }['SUBID'] end |
#create_ssh_config(machine) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 132 def create_ssh_config(machine) server = server(machine.id) if server && machine.data_dir address = server['main_ip'] label = server['label'].gsub(' ', '') hostname = label.empty? ? "vultr-#{machine.id}" : label cfg_file = machine.data_dir.join("config.#{hostname}") cfg_file.open("w+") do |f| f.write("Host #{hostname}\n") f.write("\tUser root\n") f.write("\tHostName #{address}\n") f.write("\tIdentityFile ~/.ssh/vagrant_vultr\n") end end end |
#create_ssh_key(name, key) ⇒ Object
99 100 101 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 99 def create_ssh_key(name, key) request { ::Vultr::SSHKey.create(name: name, ssh_key: key) }['SSHKEYID'] end |
#destroy_server(sub_id) ⇒ Object
68 69 70 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 68 def destroy_server(sub_id) request { ::Vultr::Server.destroy(SUBID: sub_id) } end |
#os_id(os) ⇒ Object
72 73 74 75 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 72 def os_id(os) oses = request { ::Vultr::OS.list } oses.values.find { |o| o['name'] == os }['OSID'] end |
#reboot_server(sub_id) ⇒ Object
60 61 62 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 60 def reboot_server(sub_id) request { ::Vultr::Server.reboot(SUBID: sub_id) } end |
#region_id(region) ⇒ Object
77 78 79 80 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 77 def region_id(region) regions = request { ::Vultr::Regions.list } regions.values.find { |r| r['name'] == region }['DCID'] end |
#server(sub_id) ⇒ Object
31 32 33 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 31 def server(sub_id) servers.find { |server| server['SUBID'] == sub_id } end |
#servers ⇒ Object
24 25 26 27 28 29 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 24 def servers servers = request { ::Vultr::Server.list } servers = servers.values if servers.any? servers end |
#ssh_key_id(ssh_key_name) ⇒ Object
93 94 95 96 97 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 93 def ssh_key_id(ssh_key_name) ssh_keys = request { ::Vultr::SSHKey.list } ssh_key = ssh_keys.values.find { |s| s['name'] == ssh_key_name } ssh_key['SSHKEYID'] if ssh_key end |
#start_server(sub_id) ⇒ Object
56 57 58 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 56 def start_server(sub_id) request { ::Vultr::Server.start(SUBID: sub_id) } end |
#startup_script_id(startup_script_name) ⇒ Object
87 88 89 90 91 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 87 def startup_script_id(startup_script_name) scripts = request { ::Vultr::StartupScript.list } script = scripts.values.find { |s| s['name'] == startup_script_name } script['SCRIPTID'] if script end |
#stop_server(sub_id) ⇒ Object
64 65 66 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 64 def stop_server(sub_id) request { ::Vultr::Server.halt(SUBID: sub_id) } end |
#vps_plan_id(plan) ⇒ Object
82 83 84 85 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 82 def vps_plan_id(plan) plans = request { ::Vultr::Plans.list } plans.values.find { |p| p['name'] == plan }['VPSPLANID'] end |
#wait_for_ssh(machine) ⇒ Object
TODO:
Fix the case when SSH key is not ready so it asks for password
TODO:
Extract away from client?
125 126 127 128 129 130 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 125 def wait_for_ssh(machine) # SSH may be unreachable after server is started wait_until(Errno::ENETUNREACH) do machine.communicate.wait_for_ready(@timeout) end end |
#wait_to_activate(sub_id) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 103 def wait_to_activate(sub_id) wait_until do # it might be not shown in API for some reason server = server(sub_id) server && server['status'] == 'active' end end |
#wait_to_destroy(sub_id) ⇒ Object
119 120 121 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 119 def wait_to_destroy(sub_id) wait_until { !server(sub_id) } end |
#wait_to_power_on(sub_id) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/vagrant-vultr/helpers/client.rb', line 111 def wait_to_power_on(sub_id) wait_until do # it might be not shown in API for some reason server = server(sub_id) server && server['status'] == 'active' && server['power_status'] == 'running' && server['server_state'] == 'ok' end end |