Class: LinodeCluster::Cluster

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

Overview

Cluster class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, app_name, stage, options) ⇒ Cluster

Returns a new instance of Cluster.



11
12
13
14
15
16
17
18
19
20
# File 'lib/linode_cluster/cluster.rb', line 11

def initialize(api_key, app_name, stage, options)
  @node_groups = {}
  @app_name = app_name || ''
  @stage = stage || ''
  @client = ClientWrapper.new(Linode.new(api_key: api_key))
  @ansible_ssh_user = options[:ansible_ssh_user] || 'root'

  raise 'app name cannot be blank' if app_name.empty?
  raise 'stage cannot be blank' if stage.empty?
end

Instance Attribute Details

#ansible_ssh_userObject

Returns the value of attribute ansible_ssh_user.



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

def ansible_ssh_user
  @ansible_ssh_user
end

#app_nameObject

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#stageObject

Returns the value of attribute stage.



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

def stage
  @stage
end

Instance Method Details

#add_node_group(name, region, size, count, options = {}) ⇒ Object



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

def add_node_group(name, region, size, count, options = {})
  raise "Group with name '#{name}' already exists" if @node_groups[name]
  @node_groups[name] = NodeGroup.new(name, name_prefix, region, size, count, self, default_options(options))
end

#as_ansible_inventoryObject



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

def as_ansible_inventory
  @node_groups.map { |_, group| group.as_ansible_inventory }.join
end

#cost_per_monthObject



43
44
45
# File 'lib/linode_cluster/cluster.rb', line 43

def cost_per_month
  nodes.map(&:cost_per_month).sum
end

#create!Object



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

def create!
  @node_groups.each_value(&:create!)
end

#create_node(attributes) ⇒ Object



47
48
49
50
51
# File 'lib/linode_cluster/cluster.rb', line 47

def create_node(attributes)
  result = node_factory.create(attributes)
  @nodes = nil
  result
end

#name_prefixObject



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

def name_prefix
  "#{app_name}-#{stage}-"
end

#nodesObject



39
40
41
# File 'lib/linode_cluster/cluster.rb', line 39

def nodes
  @node_groups.values.flat_map(&:nodes)
end