Class: LinodeCluster::NodeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/linode_cluster/node_factory.rb

Overview

node factory class

Constant Summary collapse

DEFAULT_IMAGE_NAME =

DEFAULT_IMAGE_NAME = ‘Ubuntu 16.04 LTS’.freeze

'Ubuntu 14.04 LTS'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ NodeFactory

Returns a new instance of NodeFactory.



13
14
15
# File 'lib/linode_cluster/node_factory.rb', line 13

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/linode_cluster/node_factory.rb', line 11

def client
  @client
end

Instance Method Details

#create(attributes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/linode_cluster/node_factory.rb', line 41

def create(attributes)
  linode_attributes = { datacenterid: find_datacenter_id(attributes[:region]),
                        planid: find_plan_id(attributes[:size]) }

  result = client.linode.create(linode_attributes)

  begin
    client.linode.update(linodeid: result.linodeid,
                         label: attributes[:name],
                         lpm_displaygroup: attributes[:group_name])

    swap_disk = client.linode.disk.create(
      linodeid: result.linodeid,
      type: 'swap',
      label: 'Swap',
      size: 512
    )

    # need to fetch data about the new node to calculate the size of the new
    # disk
    refresh_nodes
    new_node = find_node_by_id(result.linodeid)

    os_disk = client.linode.disk.createfromdistribution(
      linodeid: result.linodeid,
      distributionid: find_distribution_id_by_name(DEFAULT_IMAGE_NAME),
      label: DEFAULT_IMAGE_NAME,
      rootpass: SecureRandom.hex,
      size: new_node.totalhd - 512,
      rootsshkey: File.read("#{ENV.fetch('HOME')}/.ssh/id_rsa.pub")
    )

    client.linode.config.create(
      linodeid: result.linodeid,
      label: 'default',
      kernelid: find_default_kernel.kernelid,
      disklist: [os_disk.diskid, swap_disk.diskid].join(',')
    )

    client.linode.boot(linodeid: result.linodeid)
    new_node
  rescue StandardError => e
    client.linode.delete(linodeid: result.linodeid, skipchecks: true)
    raise e
  end
end

#find_datacenter_id(*args) ⇒ Object



17
18
19
# File 'lib/linode_cluster/node_factory.rb', line 17

def find_datacenter_id(*args)
  client.find_datacenter_id(*args)
end

#find_default_kernel(*args) ⇒ Object



37
38
39
# File 'lib/linode_cluster/node_factory.rb', line 37

def find_default_kernel(*args)
  client.find_default_kernel(*args)
end

#find_distribution_id_by_name(*args) ⇒ Object



33
34
35
# File 'lib/linode_cluster/node_factory.rb', line 33

def find_distribution_id_by_name(*args)
  client.find_distribution_id_by_name(*args)
end

#find_node_by_id(*args) ⇒ Object



25
26
27
# File 'lib/linode_cluster/node_factory.rb', line 25

def find_node_by_id(*args)
  client.find_node_by_id(*args)
end

#find_plan_id(*args) ⇒ Object



21
22
23
# File 'lib/linode_cluster/node_factory.rb', line 21

def find_plan_id(*args)
  client.find_plan_id(*args)
end

#refresh_nodes(*args) ⇒ Object



29
30
31
# File 'lib/linode_cluster/node_factory.rb', line 29

def refresh_nodes(*args)
  client.refresh_nodes(*args)
end