Class: LinodeCluster::NodeFactory

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

Overview

node factory class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ NodeFactory

Returns a new instance of NodeFactory.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/linode_cluster/node_factory.rb', line 8

def client
  @client
end

Instance Method Details

#create(attributes) ⇒ Object



38
39
40
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
# File 'lib/linode_cluster/node_factory.rb', line 38

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

  image_name = attributes[:image_name] || 'Ubuntu 14.04 LTS'.freeze

  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(image_name),
      label: 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



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

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

#find_default_kernel(*args) ⇒ Object



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

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

#find_distribution_id_by_name(*args) ⇒ Object



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

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

#find_node_by_id(*args) ⇒ Object



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

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

#find_plan_id(*args) ⇒ Object



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

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

#refresh_nodes(*args) ⇒ Object



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

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