lib/kontena/machine/upcloud/node_provisioner.rb in kontena-plugin-upcloud-0.3.0 vs lib/kontena/machine/upcloud/node_provisioner.rb in kontena-plugin-upcloud-0.3.1.rc1
- old
+ new
@@ -5,44 +5,36 @@
module Kontena
module Machine
module Upcloud
class NodeProvisioner
include RandomName
- include UpcloudCommon
include Kontena::Cli::ShellSpinner
- attr_reader :api_client, :username, :password
+ attr_reader :api_client, :uc_client
# @param [Kontena::Client] api_client Kontena api client
# @param [String] upcloud_username Upcloud username
# @param [String] upcloud_password Upcloud password
def initialize(api_client, upcloud_username, upcloud_password)
@api_client = api_client
- @username = upcloud_username
- @password = upcloud_password
+ @uc_client = Kontena::Machine::Upcloud::Client.new(upcloud_username, upcloud_password)
end
def run!(opts)
- if File.readable?(File.expand_path(opts[:ssh_key]))
- ssh_key = File.read(File.expand_path(opts[:ssh_key])).strip
- end
+ abort('Invalid ssh key') unless opts[:ssh_key].to_s.start_with?('ssh-')
- abort('Invalid ssh key') unless ssh_key && ssh_key.start_with?('ssh-')
-
count = opts[:count].to_i
userdata_vars = {
version: opts[:version],
master_uri: opts[:master_uri],
grid_token: opts[:grid_token],
}
- abort_unless_api_access
+ abort('CoreOS template not found on Upcloud') unless coreos_template = uc_client.find_template('CoreOS Stable')
+ abort('Server plan not found on Upcloud') unless plan = uc_client.find_plan(opts[:plan])
+ abort('Zone not found on Upcloud') unless uc_client.zone_exist?(opts[:zone])
- abort('CoreOS template not found on Upcloud') unless coreos_template = find_template('CoreOS Stable')
- abort('Server plan not found on Upcloud') unless plan = find_plan(opts[:plan])
- abort('Zone not found on Upcloud') unless zone_exist?(opts[:zone])
-
count.times do |i|
if opts[:name]
hostname = count == 1 ? opts[:name] : "#{opts[:name]}-#{i + 1}"
else
hostname = generate_name
@@ -70,25 +62,25 @@
},
login_user: {
create_password: 'no',
username: 'root',
ssh_keys: {
- ssh_key: [ssh_key]
+ ssh_key: [opts[:ssh_key]]
}
}
}
}.to_json
spinner "Creating UpCloud node #{hostname.colorize(:cyan)} " do
- response = post('server', body: device_data)
+ response = uc_client.post('server', device_data)
if response.has_key?(:error)
abort("\nUpCloud server creation failed (#{response[:error].fetch(:error_message, '')})")
end
device_data = response[:server]
until device_data && device_data.fetch(:state, nil).to_s == 'maintenance'
- device_data = get("server/#{device[:uuid]}").fetch(:server, {}) rescue nil
+ device_data = uc_client.get("server/#{device[:uuid]}").fetch(:server, {}) rescue nil
sleep 5
end
end
node = nil