Class: CLI

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

Instance Method Summary collapse

Instance Method Details

#get(os_list) ⇒ Object



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/vmfloaty/cli.rb', line 8

def get(os_list)
  # HTTP POST -d os_list vmpooler.company.com/vm

  uri = URI.parse("#{$vmpooler_url}/vm/#{os_list.gsub(",","+")}")
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  response = http.request(request)

  if response.code.to_i == 200
    hosts = JSON.parse(response.body)

    # puts hosts

    save_hosts = {}
    hosts.each do |k,v|
      unless k == 'ok' || k == 'domain'
        save_hosts[k] = v['hostname']
      end
    end

    puts 'New Hosts:'
    puts save_hosts

    #hosts.add_host save_hosts
  end

  # parse host names/os's and save
end

#list(pattern = nil) ⇒ Object



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

def list(pattern=nil)
  # HTTP GET vmpooler.company.com/vm
  uri = URI.parse("#{$vmpooler_url}/vm")
  response = Net::HTTP.get_response(uri)
  host_res = JSON.parse(response.body)

  if pattern
    # Filtering VMs based on pattern
    hosts = host_res.select { |i| i[/#{pattern}/] }
  else
    hosts = host_res
  end

  puts hosts
end

#modify(hostname) ⇒ Object



38
39
40
# File 'lib/vmfloaty/cli.rb', line 38

def modify(hostname)
  say 'Modify a vm'
end

#release(hostname_list = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vmfloaty/cli.rb', line 66

def release(hostname_list=nil)
  # HTTP DELETE vmpooler.company.com/vm/#{hostname}
  # { "ok": true }

  if options[:all]
    # release all hosts managed by vmfloaty
  else
    hostname_arr = hostname_list.split(',')

    hostname_arr.each do |hostname|
      say "Releasing host #{hostname}..."
      uri = URI.parse("#{$vmpooler_url}/vm/#{hostname}")
      http = Net::HTTP.new(uri.host, uri.port)
      request = Net::HTTP::Delete.new(uri.request_uri)
      response = http.request(request)
      res = JSON.parse(response.body)

      puts res
    end
  end
end

#statusObject



43
44
45
# File 'lib/vmfloaty/cli.rb', line 43

def status
  #$hosts.print_host_list
end