Class: Souffle::Provisioner::System

Inherits:
Object
  • Object
show all
Defined in:
lib/souffle/provisioner/system.rb

Overview

The system provisioning statemachine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system, provider, max_failures = 3, timeout = 600) ⇒ System

Creates a new system using a specific provider.

Parameters:

  • system (Souffle::System)

    The system to provision.

  • provider (Souffle::Provider::Base)

    The provider to use.

  • max_failures (Fixnum) (defaults to: 3)

    the maximum number of failures.

  • timeout (Fixnum) (defaults to: 600)

    The maximum time to wait for node creation.



55
56
57
58
59
60
61
62
63
64
# File 'lib/souffle/provisioner/system.rb', line 55

def initialize(system, provider, max_failures=3, timeout=600)
  @failures = 0
  @system = system
  @provider = provider
  @time_used = 0
  @timeout = timeout
  @max_failures = max_failures
  @nodes_completed = 0
  super() # NOTE: This is here to initialize state_machine.
end

Instance Attribute Details

#max_failuresObject (readonly)

Returns the value of attribute max_failures.



9
10
11
# File 'lib/souffle/provisioner/system.rb', line 9

def max_failures
  @max_failures
end

#providerObject

Returns the value of attribute provider.



7
8
9
# File 'lib/souffle/provisioner/system.rb', line 7

def provider
  @provider
end

#time_usedObject

Returns the value of attribute time_used.



7
8
9
# File 'lib/souffle/provisioner/system.rb', line 7

def time_used
  @time_used
end

Instance Method Details

#createObject

Creates the system from an api or command.



67
68
69
70
71
72
73
74
# File 'lib/souffle/provisioner/system.rb', line 67

def create
  Souffle::Log.info "[#{system_tag}] Creating a new system..."
  @system.nodes.each do |node|
    node.provisioner = Souffle::Provisioner::Node.new(node)
    node.provisioner.initialized
  end
  wait_until_created
end

#error_handlerObject

Handles the error state and recreates the system.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/souffle/provisioner/system.rb', line 135

def error_handler
  @failures += 1
  if @failures >= @max_failures
    Souffle::Log.error "[#{system_tag}] Complete failure. Halting Creation."
    creation_halted
  else
    err_msg =  "[#{system_tag}] Error creating system. "
    err_msg << "Killing and recreating..."
    Souffle::Log.error(err_msg)
    kill_system
    reclaimed
  end
end

#kill_systemObject

Kills the system.



130
131
132
# File 'lib/souffle/provisioner/system.rb', line 130

def kill_system
  # @provider.kill(@system.nodes)
end

#node_provisionedObject

Updates the number of nodes provisioned for the system provisioner.



124
125
126
127
# File 'lib/souffle/provisioner/system.rb', line 124

def node_provisioned
  @nodes_completed += 1
  provisioned if @nodes_completed == @system.nodes.size
end

#provisionObject

TODO:

We should really have these provisioned with fibers.

Provisioning the system.



79
80
81
82
83
84
85
86
# File 'lib/souffle/provisioner/system.rb', line 79

def provision
  Souffle::Log.info "[#{system_tag}] Provisioning the system..."
  @system.rebalance_nodes
  @system.nodes.each do |node|
    when_parents_are_complete(node) { node.provisioner.begin_provision }
  end
  wait_until_complete
end

#system_provisionedObject

System has completed provisioning.



119
120
121
# File 'lib/souffle/provisioner/system.rb', line 119

def system_provisioned
  Souffle::Log.info "[#{system_tag}] System provisioned."
end

#when_parents_are_complete(node) ⇒ Object

Wait until all of the parent nodes are in a completed state and yield.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/souffle/provisioner/system.rb', line 89

def when_parents_are_complete(node)
  total_nodes = node.parents.size
  if total_nodes == 0
    yield if block_given?
    all_complete = true
  else
    all_complete = false
  end
  timer = EM::PeriodicTimer.new(2) do
    nodes_complete = node.parents.select do |n|
      n.provisioner.state == "complete"
    end.size

    if nodes_complete == total_nodes
      all_complete = true
      timer.cancel
      yield if block_given?
    end
  end

  EM::Timer.new(@timeout) do
    unless all_complete
      Souffle::Log.error "[#{system_tag}] Parent creation timeout reached."
      timer.cancel
      error_occurred
    end
  end
end