Class: Inf::Fleet

Inherits:
Thor
  • Object
show all
Defined in:
lib/fleet.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



2
3
4
# File 'lib/fleet.rb', line 2

def method_missing(m, *args, &block)
  Inf.send(m, *args, &block)
end

Class Method Details

.each_fleet(&block) ⇒ Object



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

def self.each_fleet(&block)
  list_state("apps/#{app_name}/fleets") do |key|
    key.match(/fleets\/([^\/]+)/)[1]
  end.uniq.each { |name| block.call name }
end

.ips(fleet_name = 'web') ⇒ Object



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

def self.ips(fleet_name='web')
  sfr = new.get_sfr fleet_name

  unless sfr
    puts "No sfr for #{app_name}/#{fleet_name}".red
    puts "Is a fleet launched?"
    exit 1
  end

  instance_ids = ec2.describe_spot_fleet_instances(
    spot_fleet_request_id: sfr
  ).active_instances.map(&:instance_id)

  if instance_ids.empty?
    []
  else
    ec2.describe_instances(
      instance_ids: instance_ids
    ).reservations.map do |it|
      it.instances.first.public_ip_address
    end
  end
end

.method_missing(m, *args, &block) ⇒ Object



6
7
8
# File 'lib/fleet.rb', line 6

def self.method_missing(m, *args, &block)
  Inf.send(m, *args, &block)
end

Instance Method Details

#kill(fleet_name = 'web') ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fleet.rb', line 65

def kill(fleet_name='web')
  sfr = get_sfr fleet_name

  if sfr
    ec2.cancel_spot_fleet_requests(
      spot_fleet_request_ids: [sfr],
      terminate_instances: true
    )
    puts "Killed #{sfr}".blue
  else
    puts "Did not find a sfr".yellow
  end

  delete_state "apps/#{app_name}/fleets/#{fleet_name}/sfr"
end

#launch(fleet_name = 'web') ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fleet.rb', line 48

def launch(fleet_name='web')
  sfr = get_sfr fleet_name

  if sfr
    puts "Kill existing sfr first".red
    exit 1
  end

  Inf::AWS.request_spot_fleet fleet_name

  script = File.expand_path "../../bootstrap-scripts/default-app.sh", __FILE__

  put_state "apps/#{app_name}/bootstrap-instance.sh", File.read(script)
  puts "Don't forget to deploy something".yellow
end

#listObject



158
159
160
# File 'lib/fleet.rb', line 158

def list
  Inf::Fleet.each_fleet { |name| puts name }
end

#nodes(fleet_name = 'web') ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/fleet.rb', line 104

def nodes(fleet_name='web')
  ips = Inf::Fleet.ips fleet_name

  if ips.any?
    puts ips.join("\n")
  else
    puts "No active instances in fleet"
  end
end

#rename(name, new_name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fleet.rb', line 82

def rename(name, new_name)
  sfr = get_sfr new_name

  if sfr
    puts "Fleet #{new_name} already exists".red
    exit 1
  end

  list_state("apps/#{app_name}/fleets/#{name}") do |key|
    leaf = key.split('/').last

    s3.copy_object(
      bucket: state_bucket,
      key: "apps/#{app_name}/fleets/#{new_name}/#{leaf}",
      copy_source: "#{state_bucket}/#{key}"
    )

    delete_state key
  end
end

#scale(fleet_name, count) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/fleet.rb', line 129

def scale(fleet_name, count)
  sfr = get_sfr fleet_name

  ec2.modify_spot_fleet_request({
    spot_fleet_request_id: sfr,
    target_capacity: count.to_i,
    excess_capacity_termination_policy: "default"
  })
end

#ssh(fleet_name = 'web') ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fleet.rb', line 115

def ssh(fleet_name='web')
  ips = Inf::Fleet.ips fleet_name

  if ips.empty?
    puts "No instances in fleet #{fleet_name}".red
  else
    cmd = "#{SSH_CMD} ubuntu@#{ips.first}"

    puts cmd.blue
    Kernel.exec cmd
  end
end

#status(fleet_name = 'web', indent = '') ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fleet.rb', line 140

def status(fleet_name='web', indent='')
  nodes = Inf::Fleet.ips fleet_name
  sfr = get_state("apps/#{app_name}/fleets/#{fleet_name}/sfr")

  sfr_status = ec2.describe_spot_fleet_requests(
    spot_fleet_request_ids: [sfr]
  ).spot_fleet_request_configs[0].spot_fleet_request_state

  if nodes.any?
    puts "Fleet #{fleet_name} has #{nodes.count} nodes (#{nodes.join ', '})".blue
  elsif sfr
    puts "Fleet #{fleet_name} (#{sfr}: #{sfr_status}) has no nodes!".red
  else
    puts "No fleet #{fleet_name} is launched".yelow
  end
end

#status_allObject



163
164
165
# File 'lib/fleet.rb', line 163

def status_all
  Inf::Fleet.each_fleet { |name| status name }
end