Class: Skewer::CLI::Parser

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

Overview

Parses the CLI input and makes sure that it’s clean.

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, options = {}) ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/parser.rb', line 10

def initialize(type = nil, options = {})
  # base case tests that we have input that we accept.
  Fog.mock! if options[:mock]
  validate_options(options, type)

  if type == 'delete'
    if options[:region]
      SkewerConfig.set 'region', options[:region]
    end
    case options[:kind]
      when :ec2
        node = AWS::Node.find_by_name(options[:host])
      when :rackspace
        node = Rackspace::Node.find_by_ip(options[:host])
      else
        raise("#{options[:kind]} not found")
    end
    destroy_node(node, options)
  else
    Skewer::CLI.bootstrap_and_go(options)
  end
end

Instance Method Details

#delete_usageObject



87
88
89
90
91
92
# File 'lib/parser.rb', line 87

def delete_usage
  out = <<EOF
Usage: skewer delete --cloud <which cloud> --host <host>
EOF
  out.strip
end

#destroy_node(node, options) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/parser.rb', line 33

def destroy_node(node, options)
  if node
    node.destroy
    Skewer.logger.info("#{options[:host]} deleted.")
  else
    abort("#{options[:host]} not found.")
  end
end

#provision_usageObject



73
74
75
76
77
78
# File 'lib/parser.rb', line 73

def provision_usage
  out = <<EOF
Usage: skewer provision --cloud <which cloud>  --image <AWS image> --role <puppet role class>
EOF
  out.strip
end

#update_usageObject



80
81
82
83
84
85
# File 'lib/parser.rb', line 80

def update_usage
  out = <<EOF
Usage: skewer update --host <host> --user <user with sudo rights> --role <puppet role class>
EOF
  out.strip
end

#usageObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parser.rb', line 61

def usage
  out = <<EOF
Usage: skewer COMMAND [options]

The available skewer commands are:
   provision  spawn a new VM via a cloud system and provision it with puppet code
   update     update the puppet code on a machine that you've already provisioned
   delete     deletes the given host from the provided cloud provider
EOF
  out.strip
end

#validate_options(options, type) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/parser.rb', line 42

def validate_options(options, type)
  abort(usage) if type.nil? and options.empty?
  abort(usage) unless ['provision', 'update', 'delete'].include? type
  abort("A key (--key KEY) must be provided if using EC2") if options[:kind] == :ec2 && !options[:key_name]
  if type == 'provision'
    unless options[:kind] && options[:image] && options[:role] && !options[:help]
      abort(provision_usage)
    end
  elsif type == 'update'
    unless options[:host] && options[:user] && !options[:help]
      abort(update_usage)
    end
  elsif type == 'delete'
    unless options[:kind] && options[:host] && !options[:help]
      abort(delete_usage)
    end
  end
end