befog 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/befog/cli.rb CHANGED
@@ -7,7 +7,10 @@ require "befog/commands/list"
7
7
  require "befog/commands/configure"
8
8
 
9
9
  module Befog
10
+
10
11
  module CLI
12
+
13
+ class Error < RuntimeError ; end
11
14
 
12
15
  COMMANDS = {
13
16
  "add" => Befog::Commands::Add,
@@ -30,8 +33,12 @@ module Befog
30
33
  # differentiate between expected exceptions
31
34
  # (just display the error message) and un-
32
35
  # expected (display the backtrace)
33
- rescue => e
36
+ rescue Befog::CLI::Error => e
34
37
  $stderr.puts "befog: #{e.message}"
38
+ exit(-1)
39
+ rescue => e # uh-oh
40
+ $stderr.puts "Unexpected error"
41
+ $stderr.puts "#{e.class}: #{e.message}"
35
42
  $stderr.puts e.backtrace
36
43
  exit(-1)
37
44
  end
@@ -50,11 +57,16 @@ module Befog
50
57
 
51
58
  Usage: befog <subcommand> [<bank>] [<options>]
52
59
 
53
- The befog command allows you to manage your cloud servers from the command line.
60
+ The befog command allows you to manage your cloud servers from the command line.
61
+ You reference these sets of servers as banks. A bank can have one or many servers.
62
+
63
+ Example: befog add web-servers --count 3
64
+
65
+ Adds 3 servers to the bank "web-servers"
54
66
 
55
67
  Valid commands:
56
68
 
57
- configure, Configure a bank of servers
69
+ configure Configure a bank of servers
58
70
  config
59
71
  add Provision new servers for a bank of servers
60
72
  remove De-provision servers for a bank of servers
@@ -69,4 +81,4 @@ eos
69
81
 
70
82
  end
71
83
  end
72
- end
84
+ end
@@ -24,9 +24,7 @@ module Befog
24
24
  :long => "--count COUNT",
25
25
  :required => true,
26
26
  :description => "The number of machines to provision"
27
-
28
- # TODO: Add support for machine type
29
-
27
+
30
28
  def run
31
29
  count = options[:count].to_i
32
30
  if count <= 0
@@ -35,10 +33,14 @@ module Befog
35
33
  end
36
34
  servers = []
37
35
  count.times do |i|
38
- $stdout.puts "Provisioning server #{i+1} ..."
36
+ $stdout.puts "Provisioning server #{i+1} for bank '#{options[:bank]}'..."
39
37
  # TODO: Figure out how to give the server a name
40
- servers << compute.servers.create(:image_id => bank["image"],
41
- :key_name => bank["keypair"], :region => bank["region"])
38
+ # TODO: Check for values for all crucial configuration properties
39
+ servers << compute.servers.create(
40
+ :tags => {"Name" => options[:bank]}, :region => bank["region"],
41
+ :flavor_id => bank["type"], :image_id => bank["image"],
42
+ :security_group_ids => [options[:group]||"default"],
43
+ :key_name => bank["keypair"])
42
44
  end
43
45
  $stdout.puts "This may take a few minutes ..."
44
46
  servers.each do |server|
@@ -53,6 +53,11 @@ module Befog
53
53
  :short => "-g GROUP",
54
54
  :long => "--group GROUP",
55
55
  :description => "The security group to use for new instances"
56
+
57
+ option :type,
58
+ :short => "-t TYPE",
59
+ :long => "--type TYPE",
60
+ :description => "The number of machines to provision"
56
61
 
57
62
  def self.run(args)
58
63
  self.new(args).run
@@ -67,11 +72,9 @@ module Befog
67
72
  _key = key.to_sym
68
73
  provider[key] = options[_key] if options[_key]
69
74
  end
70
- if options[:bank]
71
- %w( provider region image keypair group ).each do |key|
72
- _key = key.to_sym
73
- bank[key] = options[_key] if options[_key]
74
- end
75
+ %w( provider region image keypair group type ).each do |key|
76
+ _key = key.to_sym
77
+ bank[key] = options[_key] if options[_key]
75
78
  end
76
79
  save
77
80
  end
@@ -66,7 +66,8 @@ module Befog
66
66
  out << "instances:"
67
67
  banks[name]["servers"].each do |id|
68
68
  c = compute(configuration["provider"])
69
- out << "- #{c.servers.get(id).public_ip_address}"
69
+ s = c.servers.get(id)
70
+ out << "- #{s.flavor_id} #{s.public_ip_address} #{s.state}"
70
71
  end
71
72
  $stdout.puts out.join("\n#{indent}")
72
73
  end
@@ -8,7 +8,7 @@ module Befog
8
8
  end
9
9
 
10
10
  def _bank
11
- raise "You must specify a bank to do this" unless options[:bank]
11
+ raise Befog::CLI::Error.new("You must specify a bank to do this") unless options[:bank]
12
12
  banks[options[:bank]] ||= {}
13
13
  end
14
14
 
@@ -22,13 +22,13 @@ module Befog
22
22
 
23
23
  def process_arguments(arguments)
24
24
  _bank,*rest = arguments
25
- if _bank =~ /^\-/ or rest.empty?
25
+ if _bank.nil? or _bank =~ /^\-/
26
26
  super
27
27
  else
28
28
  super(rest)
29
29
  options[:bank] = _bank
30
30
  bank.each do |key,value|
31
- options[key.to_sym] = value
31
+ options[key.to_sym] ||= value
32
32
  end
33
33
  end
34
34
  end
@@ -36,6 +36,7 @@ module Befog
36
36
 
37
37
  def save
38
38
  File.open(File.expand_path(configuration_path),"w") { |f| YAML.dump(_configuration,f) }
39
+ $stdout.puts "Configuration written to: #{configuration_path}"
39
40
  end
40
41
 
41
42
 
@@ -14,7 +14,7 @@ module Befog
14
14
  end
15
15
 
16
16
  def provider
17
- raise "You must specify a provider (with --provider or -q) to do this" unless options[:provider]
17
+ raise Befog::CLI::Error.new("You must specify a provider (with --provider or -q) to do this") unless options[:provider]
18
18
  providers[options[:provider]] ||= {}
19
19
  end
20
20
 
@@ -23,7 +23,7 @@ module Befog
23
23
  end
24
24
 
25
25
  def region
26
- raise "Please set the region using -r or --region." unless options[:region]
26
+ raise Befog::CLI::Error.new("Please set the region using -r or --region.") unless options[:region]
27
27
  regions[options[:region]] ||= {}
28
28
  end
29
29
 
@@ -33,11 +33,16 @@ module Befog
33
33
  end
34
34
 
35
35
  def run_for_server(id)
36
- address = get_server(id).public_ip_address
37
- $stdout.puts "Running command '#{options[:command]}' for #{address} ..."
38
- result = Fog::SSH.new(address, "root").run(options[:command]).first
39
- $stdout.puts "[#{address}: STDOUT] #{result.stdout}"
40
- $stderr.puts "[#{address}: STDERR] #{result.stderr}"
36
+ server = get_server(id)
37
+ if server.state == "running"
38
+ address = server.public_ip_address
39
+ $stdout.puts "Running command '#{options[:command]}' for #{address} ..."
40
+ result = Fog::SSH.new(address, "root").run(options[:command]).first
41
+ $stdout.puts "[#{address}: STDOUT] #{result.stdout}"
42
+ $stderr.puts "[#{address}: STDERR] #{result.stderr}"
43
+ else
44
+ $stdout.puts "Server #{id} isn't running - skipping"
45
+ end
41
46
  end
42
47
 
43
48
  end
@@ -19,13 +19,20 @@ module Befog
19
19
  include Mixins::Help
20
20
 
21
21
  command "befog start <bank>",
22
- :default_to_help => true
22
+ :default_to_help => false
23
23
 
24
24
  def run
25
25
  run_for_each_server
26
26
  end
27
27
 
28
28
  def run_for_server(id)
29
+ server = get_server(id)
30
+ if server.state == "stopped"
31
+ $stdout.puts "Starting server #{id} ..."
32
+ server.start
33
+ else
34
+ $stdout.puts "Server #{id} is already (or still) running"
35
+ end
29
36
  end
30
37
 
31
38
  end
@@ -20,13 +20,20 @@ module Befog
20
20
  include Mixins::Help
21
21
 
22
22
  command "befog stop <bank>",
23
- :default_to_help => true
23
+ :default_to_help => false
24
24
 
25
25
  def run
26
26
  run_for_each_server
27
27
  end
28
28
 
29
29
  def run_for_server(id)
30
+ server = get_server(id)
31
+ if server.state == "running"
32
+ $stdout.puts "Stopping server #{id} ..."
33
+ server.stop
34
+ else
35
+ $stdout.puts "Server #{id} is not (yet) running"
36
+ end
30
37
  end
31
38
 
32
39
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: befog
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Carlo Flores carlo@spire.io