Class: Ftl::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ftl/client.rb

Constant Summary collapse

SINGLE_COMMANDS =
%w{ edit sample }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil, opts = {}) ⇒ Client

Returns a new instance of Client.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ftl/client.rb', line 43

def initialize(args=nil, opts={})
  if args && args.length > 0
    arg = args.reverse.pop
    @args = [arg, args - [arg]]
    load_config(opts)
    if (!SINGLE_COMMANDS.include?(arg))
      @con = Fog::Compute.new(:provider => 'AWS', :aws_secret_access_key => options['SECRET_ACCESS_KEY'], :aws_access_key_id => options['ACCESS_KEY_ID'])
    end
    send(*@args)
  else
    Ftl.help
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)

def remote(cmd)

%x|#{ssh_command(server)} #{cmd}|

end



386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/ftl/client.rb', line 386

def method_missing(*args)
  method = args.first.to_sym
  if con.respond_to? method
    results = con.send(method)
    display results.table(_headers_for(method))
  elsif options[:actions][method]
    eval_action(options[:actions][method], args)
  elsif options[:scripts][method]
    eval_script(options[:scripts][method], args)
  else
    Ftl.help
  end
end

Instance Attribute Details

#conObject (readonly)

Returns the value of attribute con.



40
41
42
# File 'lib/ftl/client.rb', line 40

def con
  @con
end

#optionsObject

Returns the value of attribute options.



41
42
43
# File 'lib/ftl/client.rb', line 41

def options
  @options
end

Instance Method Details

#cancel(args = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/ftl/client.rb', line 114

def cancel(args={})
  guard(args.first, "Please provide the id for the spot request to cancel.") 
  spot = con.spot_requests.get(args.first)
  if spot && spot.destroy
    display "Canceled Spot Instance #{spot.id}."
  else
    display "Whups, spot instance not found!"
  end
end

#commandObject



255
256
257
# File 'lib/ftl/client.rb', line 255

def command
  @args[0]
end

#connect(args = {}) ⇒ Object Also known as: x, ssh



133
134
135
136
137
138
139
140
# File 'lib/ftl/client.rb', line 133

def connect(args={})
  if server = running_instance(args.first)
    # puts(ssh_command(server))
    exec(ssh_command(server))
  else
    display "Typo alert! No server found!"
  end
end

#destroy(args = {}) ⇒ Object Also known as: d, delete, kill, down, shutdown



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ftl/client.rb', line 163

def destroy(args={})
  guard(on_what, "Please provide the name (or partial name for the instance(s) you want to delete. For instance, like: ftl destroy ninja")
  display "Spinning down FTL..."
  instances = find_instances(on_what).select{|i| i.state == 'running' }
  if !instances.empty?
    instances.map(&:destroy)
    display "Destroyed [bold]\[#{instances.map(&:id).join(', ')}\][/]"
  else
    display "No instances found"
  end
end

#edit(args = {}) ⇒ Object



246
247
248
249
# File 'lib/ftl/client.rb', line 246

def edit(args={})
  display "You need to set [bold]$EDITOR[/] environment variable" if ENV['EDITOR'].nil?
  `$EDITOR ~/.ftl/ftl.yml`
end

#execute(args = {}) ⇒ Object Also known as: ex



273
274
275
276
277
278
# File 'lib/ftl/client.rb', line 273

def execute(args={})
  arg = args.is_a?(String) ? args : args[1]
  server = find_instances(on_what).select{|i| i.state == 'running' }.first
  puts %|#{ssh_command(server)} #{arg}|
  system(%|#{ssh_command(server)} #{arg}|)
end

#headers(args = {}) ⇒ Object



236
237
238
239
240
# File 'lib/ftl/client.rb', line 236

def headers(args={})
  what = args.first || :servers
  display "Showing header options for #{what}"
  display con.send(what).first.attributes.keys
end

#image(args = {}) ⇒ Object Also known as: img



205
206
207
# File 'lib/ftl/client.rb', line 205

def image(args={})
  Formatador.display_table(con.images.find(:id => args.first))
end

#images(args = {}) ⇒ Object Also known as: describe_images

TODO: Make this better by including more details of block devices



211
212
213
214
215
216
217
218
219
220
# File 'lib/ftl/client.rb', line 211

def images(args={})
  hashes = con.describe_images('Owner' => 'self').body['imagesSet'].collect do |hash|
    h = {}
    h[:image_id]  = hash['imageId']
    h[:name]  = hash['name']
    h[:rootDeviceType] = hash['rootDeviceType']
    h
  end 
  puts Formatador.display_table(hashes)
end

#info(args = {}) ⇒ Object Also known as: i



193
194
195
# File 'lib/ftl/client.rb', line 193

def info(args={})
  display find_instance(args.first)
end

#ip_addressesObject



223
224
225
# File 'lib/ftl/client.rb', line 223

def ip_addresses
  con.addresses
end

#ips(args = {}) ⇒ Object Also known as: addresses



227
228
229
230
231
232
233
# File 'lib/ftl/client.rb', line 227

def ips(args={})
  # TODO add server "name"
  addrs = con.describe_addresses.body['addressesSet'] #.collect do |addr|
    # addr
  # end 
  Formatador.display_table addrs
end

#launch(args = {}) ⇒ Object Also known as: up, spinup, create, new



88
89
90
91
92
# File 'lib/ftl/client.rb', line 88

def launch(args={})
  guard(args.first, "Please provide a short name for instance\n\t[bold]ftl[/] launch <name>")
  options.merge(options[:templates][args.first.to_sym]) if !options[:templates][args.first.to_sym].nil?
  server = launch_instance(args)
end

#launch_instance(args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ftl/client.rb', line 57

def launch_instance(args)
  display "Spinning up FTL..."
  opts = options
  opts = options.merge(options[:templates][args.first.to_sym]) if !options[:templates][args.first.to_sym].nil?

  opts[:group_ids] = (opts[:group_ids] || []) + opts[:groups].select { | group | group.to_s =~ /^sg-[0-9a-f]{8}$/ }
  opts[:groups] = opts[:groups].reject { | group | group.to_s =~ /^sg-[0-9a-f]{8}$/ }

  launcher = options.delete(:launcher) || :servers
  server = con.send(launcher).create(
                              :user_data          => opts[:user_data],
                              :key_name           => opts[:keypair],
                              :groups             => opts[:groups],
                              :security_group_ids => opts[:group_ids],
                              :image_id           => opts[:ami],
                              :availability_zone  => opts[:availability_zone],
                              :flavor_id          => opts[:instance_type],
                              :username           => opts[:username],
                              :tags               => opts[:tags].merge(:Name => args.first),
                              :subnet_id          => opts[:subnet_id],
                              :private_ip_address => opts[:ip_private],
                              :ip_address         => opts[:ip_address],
                              :price              => opts[:price],
                              :instance_count     => opts[:count]
                             )

  display server
  display "Executing :post_script..." if opts[:post_script]
  eval(opts[:post_script]) if opts[:post_script]
end

#list(opts) ⇒ Object Also known as: l, ls



198
199
200
201
# File 'lib/ftl/client.rb', line 198

def list(opts)
  opts = [:servers] if opts.empty?
  con.send(opts.first).table(_headers_for(opts.first))
end

#on_whatObject



263
264
265
# File 'lib/ftl/client.rb', line 263

def on_what 
  secondary_arguments.first
end

#running_instance(name) ⇒ Object



128
129
130
131
# File 'lib/ftl/client.rb', line 128

def running_instance(name)
  instances = running_instances(name)
  instances.first if instances
end

#running_instances(name) ⇒ Object



124
125
126
# File 'lib/ftl/client.rb', line 124

def running_instances(name)
  find_instances(name).select{|i| i.state == "running" }
end

#sample(args = {}) ⇒ Object



251
252
253
# File 'lib/ftl/client.rb', line 251

def sample(args={})
  puts File.open(File.dirname(__FILE__) + "/../resources/ftl.yml").read
end

#secondary_argumentsObject



259
260
261
# File 'lib/ftl/client.rb', line 259

def secondary_arguments
  @args[1] 
end

#server_instances(args = {}) ⇒ Object



242
243
244
# File 'lib/ftl/client.rb', line 242

def server_instances(args={})
  @servers ||= (con.servers.all||[])
end

#spot(args = {}) ⇒ Object Also known as: request



98
99
100
101
102
103
104
105
106
# File 'lib/ftl/client.rb', line 98

def spot(args={})
  guard(args[0], "Please provide a short name for instance\n\t[bold]ftl[/] spot <name> <price>")
  guard(args[1], "Please provide a price for spot request\n\t[bold]ftl[/] spot <name> <price>")
  display "Spinning up FTL..."
  options.merge(options[:templates][args.first.to_sym]) if !options[:templates][args.first.to_sym].nil?
  options[:price] = args[1] || options[:templates][args.first.to_sym][:price] || options[:price]
  options.merge!(:launcher => :spot_requests)
  server = launch_instance(args)
end

#spots(args = {}) ⇒ Object Also known as: spot_requests



109
110
111
# File 'lib/ftl/client.rb', line 109

def spots(args={})
  con.spot_requests.table(_headers_for(:spot_requests))
end

#start(args = {}) ⇒ Object Also known as: run



151
152
153
154
# File 'lib/ftl/client.rb', line 151

def start(args={})
  display "Bringing \"#{args.first}\" server back to life."
  find_instance(args.first).start
end

#status(args = {}) ⇒ Object Also known as: st



144
145
146
147
148
# File 'lib/ftl/client.rb', line 144

def status(args={})
  guard(args, :message => "Please provide the name/id of a server (ftl status <server>)")
  server = find_instance(args.first)
  display server
end

#stop(args = {}) ⇒ Object Also known as: pause



157
158
159
160
# File 'lib/ftl/client.rb', line 157

def stop(args={})
  display "Stopping \"#{args.first}\" server."
  find_instance(args.first).stop
end

#terminate(args = {}) ⇒ Object Also known as: t



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ftl/client.rb', line 180

def terminate(args={})
  guard(on_what, "Please provide the name (or partial name for the instance(s) you want to terminate. For instance, like: ftl destroy ninja")
  display "Spinning down FTL..."
  instances = find_instances(on_what)
  if !instances.empty?
    instances.map(&:destroy)
    display "Destroyed [bold]\[#{instances.map(&:id).join(', ')}\][/]"
  else
    display "No instances found"
  end
end

#volume(args = {}) ⇒ Object



267
268
269
270
271
# File 'lib/ftl/client.rb', line 267

def volume(args={})
  arg = args.is_a?(String) ? args : on_what
  display vol = con.volumes.select{|v| v.tags['Name'] == arg || v.id == arg }.first
  vol
end