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

Parameters:

  • message (String)

    Message to output when giving up

  • times (Fixnum) (defaults to: 5)

    Default: 5



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kontena/machine/packet/packet_common.rb', line 73

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(ssh_key) ⇒ Object

Parameters:

  • ssh_key (String)

    SSH public key



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

def check_or_create_ssh_key(ssh_key)
  return nil if ssh_key.nil?
  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(label: ssh_key_label(ssh_key), key: ssh_key)
end

#device_public_ip(device) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kontena/machine/packet/packet_common.rb', line 51

def device_public_ip(device)
  start_time = Time.now.to_i
  api_retry "Packet API did not find a public ip address for the device" do
    loop do
      ip = refresh(device).ip_addresses.find{|ip| ip['public'] && ip['address_family'] == 4}
      return ip if ip
      sleep 0.5
      raise 'Timeout while looking for device public ip' if (Time.now.to_i - start_time) > 300
    end
  end
end

#erb(template, vars) ⇒ Object



93
94
95
# File 'lib/kontena/machine/packet/packet_common.rb', line 93

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

#find_device(project_id, device_hostname) ⇒ Object



35
36
37
# File 'lib/kontena/machine/packet/packet_common.rb', line 35

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

#find_facility(facility_code) ⇒ Object



39
40
41
# File 'lib/kontena/machine/packet/packet_common.rb', line 39

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

#find_os(os_code) ⇒ Object



43
44
45
# File 'lib/kontena/machine/packet/packet_common.rb', line 43

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

#find_plan(plan_code) ⇒ Object



47
48
49
# File 'lib/kontena/machine/packet/packet_common.rb', line 47

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

#find_project(project_id) ⇒ Object



31
32
33
# File 'lib/kontena/machine/packet/packet_common.rb', line 31

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

#login(token) ⇒ Object

Parameters:

  • token (String)

    Packet token



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

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

#refresh(device) ⇒ Object

Reloads the device data from Packet API

Parameters:

  • device (Packet::Device)


66
67
68
# File 'lib/kontena/machine/packet/packet_common.rb', line 66

def refresh(device)
  client.get_device(device.id)
end

#ssh_key_exist?(ssh_key) ⇒ Boolean

Returns:

  • (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



88
89
90
91
# File 'lib/kontena/machine/packet/packet_common.rb', line 88

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