Class: Blimpy::Fleet

Inherits:
Object
  • Object
show all
Includes:
Helpers::State
Defined in:
lib/blimpy/fleet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::State

#ensure_state_folder, #state_folder

Constructor Details

#initializeFleet

Returns a new instance of Fleet.



11
12
13
14
15
# File 'lib/blimpy/fleet.rb', line 11

def initialize
  @ships = []
  @id = Time.now.utc.to_i
  @airborn = false
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/blimpy/fleet.rb', line 9

def id
  @id
end

#shipsObject (readonly)

Returns the value of attribute ships.



9
10
11
# File 'lib/blimpy/fleet.rb', line 9

def ships
  @ships
end

Instance Method Details

#add(box_type, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blimpy/fleet.rb', line 21

def add(box_type, &block)
  unless valid_types.include? box_type
    raise Blimpy::InvalidShipException
  end
  if block.nil?
    return false
  end

  box = nil
  if box_type == :aws
    box = Blimpy::Boxes::AWS.new
  end
  if box_type == :openstack
    box = Blimpy::Boxes::OpenStack.new
  end

  if box.nil?
    return false
  end
  box.fleet_id = @id
  @ships << box
  block.call(box)
end

#animateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/blimpy/fleet.rb', line 71

def animate
  buffer ="""
        _..--=--..._
      .-'            '-.  .-.
    /.'              '.\\/  /
    |=-  B L I M P Y   -=| (
    \\'.              .'/\\  \\
      '-.,_____ _____.-'  '-'
          [_____]=+ ~ ~"""
  frames = [
    'x~   ',
    'x ~  ',
    '+~ ~ ',
    '+ ~ ~',
    '+  ~ ',
    'x   ~',
  ]

  print buffer
  $stdout.flush
  until @airborn do
    frames.each do |frame|
      # Reset every frame
      5.times { print "\b" }
      print frame
      $stdout.flush
      sleep 0.2
    end
  end
end

#destroyObject



164
165
166
167
168
169
170
171
172
173
# File 'lib/blimpy/fleet.rb', line 164

def destroy
  members.each do |instance_id, instance_data|
    box = Blimpy::Box.from_instance_id(instance_id, instance_data)
    box.destroy
  end

  if File.exists? state_file
    File.unlink(state_file)
  end
end

#membersObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/blimpy/fleet.rb', line 136

def members
  instances = []
  Dir["#{Dir.pwd}/.blimpy.d/*.blimp"].each do |d|
    filename = File.basename(d)
    instance_id = filename.split('.blimp').first
    instance_data = YAML.load_file(d)
    instances << [instance_id, instance_data]
  end
  instances
end

#resume(instances) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/blimpy/fleet.rb', line 55

def resume(instances)
  boxes = []
  print '>> Resuming: '
  instances.each do |instance_id, instance_data|
    print "#{instance_data[:name]},"
    box = Blimpy::Box.from_instance_id(instance_id, instance_data)
    box.resume
    boxes << box
  end

  boxes.each do |box|
    box.wait_for_state('running') { print '.' }
  end
  puts
end

#save!Object



49
50
51
52
53
# File 'lib/blimpy/fleet.rb', line 49

def save!
  File.open(state_file, 'w') do |f|
    f.write("id=#{id}\n")
  end
end

#startObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/blimpy/fleet.rb', line 102

def start
  instances = members
  unless instances.empty?
    return resume(instances)
  end

  # Make sure all our ships are valid first!
  @ships.each do |host|
    host.validate!
  end

  Thread.new do
    animate
  end

  @ships.each do |host|
    host.start
  end

  @ships.each do |host|
    host.wait_for_state('running') {  }
    @airborn = true
    print "\n"
    puts ">> #{host.name} online at: #{host.dns}"
    host.online!
    if host.provision_on_start
      host.bootstrap
      puts
    end
  end

  save!
end

#state_fileObject



45
46
47
# File 'lib/blimpy/fleet.rb', line 45

def state_file
  File.join(state_folder, 'manifest')
end

#stopObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/blimpy/fleet.rb', line 147

def stop
  print '>> Stopping: '
  boxes = []

  members.each do |instance_id, instance_data|
    box = Blimpy::Box.from_instance_id(instance_id, instance_data)
    print "#{instance_data[:name]},"
    box.stop
    boxes << box
  end

  boxes.each do |box|
    box.wait_for_state('stopped')  { print '.' }
  end
  puts
end

#valid_typesObject



17
18
19
# File 'lib/blimpy/fleet.rb', line 17

def valid_types
  [:aws, :openstack]
end