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.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#create(attributes) ⇒ Object



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

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('/home/rs06r/.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_idObject



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

def find_datacenter_id
  client.find_datacenter_id
end

#find_default_kernelObject



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

def find_default_kernel
  client.find_default_kernel
end

#find_distribution_id_by_nameObject



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

def find_distribution_id_by_name
  client.find_distribution_id_by_name
end

#find_node_by_idObject



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

def find_node_by_id
  client.find_node_by_id
end

#find_plan_idObject



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

def find_plan_id
  client.find_plan_id
end

#refresh_nodesObject



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

def refresh_nodes
  client.refresh_nodes
end