class LinodeCluster::NodeFactory

node factory class

Attributes

client[RW]

Public Class Methods

new(client) click to toggle source
# File lib/linode_cluster/node_factory.rb, line 10
def initialize(client)
  @client = client
end

Public Instance Methods

create(attributes) click to toggle source
# 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) click to toggle source
# File lib/linode_cluster/node_factory.rb, line 14
def find_datacenter_id(*args)
  client.find_datacenter_id(*args)
end
find_default_kernel(*args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/linode_cluster/node_factory.rb, line 18
def find_plan_id(*args)
  client.find_plan_id(*args)
end
refresh_nodes(*args) click to toggle source
# File lib/linode_cluster/node_factory.rb, line 26
def refresh_nodes(*args)
  client.refresh_nodes(*args)
end