Top Level Namespace

Defined Under Namespace

Modules: EC2

Instance Method Summary collapse

Instance Method Details

#evaluate_command(driver, cmd_arg) ⇒ Object



55
56
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ec2cli/cli/functions.rb', line 55

def evaluate_command(driver, cmd_arg)
  cmd, arg = cmd_arg.split(/\s+/, 2).map {|i| i.strip }
  arg = nil if (arg || '').strip.empty?

  r = /\A#{Regexp.compile(cmd)}/i

  commands = {
    'help' => lambda {
      print_help
    },

    ['exit', 'quit'] => lambda {
      exit 0
    },

    'debug' => lambda {
      if arg
        r_arg = /\A#{Regexp.compile(arg)}/i

        if r_arg =~ 'true'
          driver.debug = true
        elsif r_arg =~ 'false'
          driver.debug = false
        else
          print_error('Invalid argument')
        end
      else
        puts driver.debug
      end
    },

    'version' => lambda {
      print_version
    }
  }

  cmd_name, cmd_proc = commands.find do |name, proc|
    if name.kind_of?(Array)
      name.any? {|i| r =~ i }
    else
      r =~ name
    end
  end

  if cmd_proc
    cmd_proc.call
  else
    print_error('Unknown command')
  end
end

#evaluate_query(driver, src, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ec2cli/cli/evaluate.rb', line 3

def evaluate_query(driver, src, opts = {})
  ss = StringScanner.new(src.dup)
  buf = ''

  until ss.eos?
    if (tok = ss.scan %r{[^`'";\\/#]+}) #'
      buf << tok
    elsif (tok = ss.scan /`(?:[^`]|``)*`/)
      buf << tok
    elsif (tok = ss.scan /'(?:[^']|'')*'/) #'
      buf << tok
    elsif (tok = ss.scan /"(?:[^"]|"")*"/) #"
      buf << tok
    elsif (tok = ss.scan %r{/\*/?(?:\n|[^/]|[^*]/)*\*/})
      # nothing to do
    elsif (tok = ss.scan /--[^\r\n]*(?:\r\n|\r|\n|\Z)/)
      # nothing to do
    elsif (tok = ss.scan /#[^\r\n]*(?:\r\n|\r|\n|\Z)/)
      # nothing to do
    elsif (tok = ss.scan /(?:\\;)/)
      buf << ';' # escape of ';'
    elsif (tok = ss.scan /(?:;|\\G)/)
      src.replace(ss.rest)
      query = buf
      buf = ''

      if query.strip.empty?
        print_error('No query specified')
        next
      end

      start_time = Time.new
      out = driver.execute(query)
      elapsed = Time.now - start_time

      if out.kind_of?(EC2::Driver::Rownum)
        print_rownum(out, :time => elapsed)
      elsif out.kind_of?(String)
        puts out
      elsif out
        opts = opts.merge(:inline => (tok != '\G'), :time => elapsed)
        print_json(out, opts)
      end
    elsif (tok = ss.scan /./)
      buf << tok # 落ち穂拾い
    end
  end

  src.replace(buf.strip)
  buf
end

#parse_optionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ec2cli/cli/options.rb', line 4

def parse_options
  options = OpenStruct.new
  options.access_key_id     = ENV['AWS_ACCESS_KEY_ID']
  options.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
  options.ec2_endpoint_or_region = ENV['EC2_ENDPOINT'] || ENV['EC2_REGION'] || 'ec2.us-east-1.amazonaws.com'

  # default value
  options.debug       = false

  ARGV.options do |opt|
    opt.on('-k', '--access-key=ACCESS_KEY')          {|v| options.access_key_id          = v      }
    opt.on('-s', '--secret-key=SECRET_KEY')          {|v| options.secret_access_key      = v      }
    opt.on('-r', '--region=REGION_OR_ENDPOINT')      {|v| options.ddb_endpoint_or_region = v      }
    opt.on('',   '--debug')                          {    options.debug                  = true   }

    opt.on('-h', '--help') {
      puts opt.help
      puts
      print_help
      exit
    }

    opt.parse!

    unless options.access_key_id and options.secret_access_key and options.ec2_endpoint_or_region
      puts opt.help
      exit 1
    end
  end

  options
end


3
4
5
6
7
8
# File 'lib/ec2cli/cli/functions.rb', line 3

def print_error(errmsg, opts = {})
  errmsg = errmsg.join("\n") if errmsg.kind_of?(Array)
  errmsg = errmsg.strip.split("\n").map {|i| "// #{i.strip}" }.join("\n")
  errmsg += "\n\n" unless opts[:strip]
  $stderr.puts errmsg
end


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ec2cli/cli/help.rb', line 1

def print_help
  puts <<EOS
##### Query #####

DESC[RIBE] INSTANCES [WHERE name = '...' AND ...]
  Describes one or more of your instances.

DESC[RIBE] IMAGES [WHERE name = '...' AND ...]
DESC[RIBE] ALL IMAGES [WHERE [Owner = {amazon|aws-marketplace|self} AND] name = '...' AND ...]
  Describes the images.

RUN INSTANCES image_id
  Launches instances

START INSTANCES id_or_name
START INSTANCES (id_or_name, ...)
  Starts an Amazon EBS-backed AMI that you've previously stopped.

STOP INSTANCES id_or_name
STOP INSTANCES (id_or_name, ...)
  Stops an Amazon EBS-backed instance

SET TAGS name = value, ...  [WHERE name = '...' AND ...]
  Adds or overwrites one or more tags for the specified EC2 resource or resources.

USE region_or_endpoint
  changes an endpoint

SHOW REGIONS
  displays a region list

##### Command #####

.help                           displays this message
.quit | .exit                   exits sdbcli
.debug           (true|false)?  displays a debug status or changes it
.version                        displays a version

EOS
end


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ec2cli/cli/functions.rb', line 18

def print_json(data, opts = {})
  str = nil

  if data.kind_of?(Array) and opts[:inline]
    str = "[\n"

    data.each_with_index do |item, i|
      str << "  #{item.to_json}"
      str << ',' if i < (data.length - 1)
      str << "\n"
    end

    str << "]"
  else
    if data.kind_of?(Array) or data.kind_of?(Hash)
      str = JSON.pretty_generate(data)
    else
      str = data.to_json
    end
  end

  str.sub!(/(?:\r\n|\r|\n)*\Z/, "\n")

  if opts[:show_rows] and data.kind_of?(Array)
    str << "// #{data.length} #{data.length > 1 ? 'rows' : 'row'} in set"
    str << " (%.2f sec)" % opts[:time] if opts[:time]
    str << "\n"
  end

  str << "\n"
  puts str
end


10
11
12
13
14
15
16
# File 'lib/ec2cli/cli/functions.rb', line 10

def print_rownum(data, opts = {})
  rownum = data.to_i
  msg = "// #{rownum} #{rownum > 1 ? 'rows' : 'row'} changed"
  msg << " (%.2f sec)" % opts[:time] if opts[:time]
  msg << "\n\n"
  puts msg
end


51
52
53
# File 'lib/ec2cli/cli/functions.rb', line 51

def print_version
  puts "ec2cli #{Version}"
end