Module: Kontena::Machine::Packet::PacketCommon

Included in:
MasterProvisioner, NodeDestroyer, NodeProvisioner, NodeRestarter
Defined in:
lib/kontena/machine/packet/packet_common.rb

Instance Method Summary collapse

Instance Method Details

#api_retry(message, times = 5, &block) ⇒ Object

Retry API requests to recover from random tls errors



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kontena/machine/packet/packet_common.rb', line 62

def api_retry(message, times=5, &block)
  attempt = 1
  begin
    yield
  rescue => error
    ENV['DEBUG'] && puts("Packet API error: #{error}: #{error.message} - attempt #{attempt}")
    attempt += 1
    if attempt < times
      sleep 5 and retry
    else
      abort(message)
    end
  end
end

#check_or_create_ssh_key(keyfile_path) ⇒ Object



26
27
28
29
30
31
# File 'lib/kontena/machine/packet/packet_common.rb', line 26

def check_or_create_ssh_key(keyfile_path)
  abort('Ssh key file not found') unless File.exist?(keyfile_path)
  abort('Ssh key file not readable') unless File.readable?(keyfile_path)
  ssh_key = File.read(keyfile_path).strip
  create_ssh_key(ssh_key) unless ssh_key_exist?(ssh_key)
end

#create_ssh_key(ssh_key) ⇒ Object



12
13
14
# File 'lib/kontena/machine/packet/packet_common.rb', line 12

def create_ssh_key(ssh_key)
  client.create_ssh_key(ssh_key_label(ssh_key))
end

#device_public_ip(device) ⇒ Object



53
54
55
56
57
# File 'lib/kontena/machine/packet/packet_common.rb', line 53

def device_public_ip(device)
  api_retry "Packet API did not find a public ip address for the device" do
    device.ip_addresses.find{|ip| ip['public'] && ip['address_family'] == 4}
  end
end

#erb(template, vars) ⇒ Object



82
83
84
# File 'lib/kontena/machine/packet/packet_common.rb', line 82

def erb(template, vars)
  ERB.new(template, nil, '%<>-').result(OpenStruct.new(vars).instance_eval { binding })
end

#find_device(project_id, device_hostname) ⇒ Object



37
38
39
# File 'lib/kontena/machine/packet/packet_common.rb', line 37

def find_device(project_id, device_hostname)
  client.list_devices(project_id).find{|device| device.hostname == device_hostname}
end

#find_facility(facility_code) ⇒ Object



41
42
43
# File 'lib/kontena/machine/packet/packet_common.rb', line 41

def find_facility(facility_code)
  client.list_facilities.find{|f| f.code == facility_code}
end

#find_os(os_code) ⇒ Object



45
46
47
# File 'lib/kontena/machine/packet/packet_common.rb', line 45

def find_os(os_code)
  client.list_operating_systems.find{|os| os.slug == os_code}
end

#find_plan(plan_code) ⇒ Object



49
50
51
# File 'lib/kontena/machine/packet/packet_common.rb', line 49

def find_plan(plan_code)
  client.list_plans.find{|plan| plan.slug == plan_code}
end

#find_project(project_id) ⇒ Object



33
34
35
# File 'lib/kontena/machine/packet/packet_common.rb', line 33

def find_project(project_id)
  client.list_projects.find{|project| project.id == project_id}
end

#login(token) ⇒ Object



8
9
10
# File 'lib/kontena/machine/packet/packet_common.rb', line 8

def (token)
  ::Packet::Client.new(token)
end

#ssh_key_exist?(ssh_key) ⇒ Boolean



16
17
18
# File 'lib/kontena/machine/packet/packet_common.rb', line 16

def ssh_key_exist?(ssh_key)
  client.list_ssh_keys.any?{|key| key.key == ssh_key}
end

#ssh_key_label(ssh_key) ⇒ Object



20
21
22
23
# File 'lib/kontena/machine/packet/packet_common.rb', line 20

def ssh_key_label(ssh_key)
  label = ssh_key[/^ssh.+?\s+\S+\s+(.*)$/, 1].to_s.strip
  label.empty? ? "kontena-ssh-key-#{rand(1..9)}" : label
end

#user_data(vars, template_filename) ⇒ Object



77
78
79
80
# File 'lib/kontena/machine/packet/packet_common.rb', line 77

def user_data(vars, template_filename)
  cloudinit_template = File.join(__dir__ , template_filename)
  erb(File.read(cloudinit_template), vars)
end