cloudstack-cli 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,9 @@
1
- class Offering < Thor
2
- include CommandLineReporter
1
+ class Offering < CloudstackCli::Base
3
2
 
4
- desc 'offering list', 'list offerings by type [compute|network|storage]'
3
+ desc 'list', 'list offerings by type [compute|network|storage]'
5
4
  option :domain
6
5
  def list(type='compute')
7
- cs_cli = CloudstackCli::Helper.new(options[:config])
8
- offerings = cs_cli.server_offerings(options[:domain])
6
+ offerings = client.list_service_offerings(options[:domain])
9
7
 
10
8
  offerings.group_by{|o| o["domain"]}.each_value do |offers|
11
9
  offers.sort {
@@ -18,26 +16,20 @@ class Offering < Thor
18
16
  if offerings.size < 1
19
17
  puts "No offerings found"
20
18
  else
21
- table(border: true) do
22
- row do
23
- column 'Name', width: 20
24
- column 'Description', width: 30
25
- column 'ID', width: 30
26
- column 'Domain', width: 16
27
- end
28
- offerings.each do |offering|
29
- row do
30
- column offering["name"]
31
- column offering["id"]
32
- column offering["displaytext"]
33
- column offering["domain"]
34
- end
35
- end
19
+ table = [["Name", "Displaytext", "Domain", "ID"]]
20
+ offerings.each do |offering|
21
+ table << [
22
+ offering["name"],
23
+ offering["displaytext"],
24
+ offering["domain"],
25
+ offering["id"]
26
+ ]
36
27
  end
28
+ print_table table
37
29
  end
38
30
  end
39
31
 
40
- desc 'offering create NAME', 'create offering'
32
+ desc 'create NAME', 'create offering'
41
33
  option :cpunumber, required: true
42
34
  option :cpuspeed, required: true
43
35
  option :displaytext, required: true
@@ -47,28 +39,25 @@ class Offering < Thor
47
39
  option :tags
48
40
  def create(name)
49
41
  options[:name] = name
50
- cs_cli = CloudstackCli::Helper.new(options[:config])
51
- puts "OK" if cs_cli.create_offering(options)
42
+ puts "OK" if client.create_offering(options)
52
43
  end
53
44
 
54
45
  desc 'delete ID', 'delete offering'
55
46
  def delete(id)
56
- cs_cli = CloudstackCli::Helper.new(options[:config])
57
- puts "OK" if cs_cli.delete_offering(id)
47
+ puts "OK" if client.delete_offering(id)
58
48
  end
59
49
 
60
50
 
61
- desc 'offering sort', 'sort by cpu and memory grouped by domain'
51
+ desc 'sort', 'sort by cpu and memory grouped by domain'
62
52
  def sort
63
- cs_cli = CloudstackCli::Helper.new(options[:config])
64
- offerings = cs_cli.server_offerings(options[:domain])
53
+ offerings = client.list_service_offerings(options[:domain])
65
54
  sortkey = -1
66
55
  offerings.group_by{|o| o["domain"]}.each_value do |offers|
67
56
  offers.sort {
68
57
  |oa, ob| [oa["cpunumber"], oa["memory"]] <=> [ob["cpunumber"], ob["memory"]]
69
58
  }.each do |offer|
70
59
  puts "#{sortkey.abs} #{offer['domain']} - #{offer["displaytext"]}"
71
- cs_cli.update_offering({
60
+ client.update_offering({
72
61
  "id" => offer['id'],
73
62
  'sortkey' => sortkey
74
63
  })
@@ -1,15 +1,17 @@
1
- class Project < Thor
1
+ class Project < CloudstackCli::Base
2
2
 
3
- desc "project list", "list projects"
3
+ desc "list", "list projects"
4
4
  def list
5
- cs_cli = CloudstackCli::Helper.new(options[:config])
6
- projects = cs_cli.projects
5
+ projects = client.list_projects
7
6
  if projects.size < 1
8
7
  puts "No projects found"
9
8
  else
9
+ table = [["Name", "Displaytext", "Domain"]]
10
10
  projects.each do |project|
11
- puts "#{project['name']} - #{project['displaytext']} - #{project['domain']}"
11
+ table << [project['name'], project['displaytext'], project['domain']]
12
12
  end
13
+ print_table(table)
13
14
  end
14
15
  end
16
+
15
17
  end
@@ -1,9 +1,8 @@
1
- class Publicip < Thor
1
+ class Publicip < CloudstackCli::Base
2
2
 
3
- desc "publicip remove ID", "remove public IP address"
3
+ desc "remove ID", "remove public IP address"
4
4
  def remove(id)
5
- cs_cli = CloudstackCli::Helper.new(options[:config])
6
- puts "OK" if cs_cli.remove_publicip(id)
5
+ puts "OK" if client.disassociate_ip_address(id)
7
6
  end
8
7
 
9
8
  end
@@ -1,83 +1,68 @@
1
- class Router < Thor
2
- include Thor::Actions
3
- include CommandLineReporter
1
+ class Router < CloudstackCli::Base
4
2
 
5
- desc "router list", "list virtual routers"
3
+ desc "list", "list virtual routers"
6
4
  option :project
7
5
  option :account
8
6
  option :zone
9
7
  option :status, desc: "Running or Stopped"
10
- option :redundant_state, desc: "MASTER, BACKUP or UNKNOWN"
8
+ option :redundant_state, desc: "master, backup, failed or unknown"
11
9
  option :listall, type: :boolean
12
- option :text, type: :boolean, desc: "text output (only the instance name)"
10
+ option :showid, type: :boolean
13
11
  option :command, desc: "command to execute for each router: START or STOP"
14
12
  option :reverse, type: :boolean, default: false, desc: "reverse listing of routers"
15
13
  def list
16
- cs_cli = CloudstackCli::Helper.new(options[:config])
17
- if options[:project]
18
- project = cs_cli.projects.select { |p| p['name'] == options[:project] }.first
19
- unless project
20
- say "Error: Project '#{options[:project]}' not found", :red
21
- exit 1
22
- end
23
- projectid = project['id']
24
- end
25
-
26
- routers = cs_cli.list_routers(
14
+ projectid = find_project['id'] if options[:project]
15
+ routers = client.list_routers(
27
16
  {
28
17
  account: options[:account],
29
18
  projectid: projectid,
30
19
  status: options[:status],
31
20
  zone: options[:zone]
32
- },
33
- options[:redundant_state]
21
+ }
34
22
  )
35
23
 
36
-
37
24
  if options[:listall]
38
- projects = cs_cli.projects
25
+ projects = client.list_projects
39
26
  projects.each do |project|
40
- routers = routers + cs_cli.list_routers(
27
+ routers = routers + client.list_routers(
41
28
  {
42
29
  account: options[:account],
43
30
  projectid: project['id'],
44
31
  status: options[:status],
45
32
  zone: options[:zone]
46
- },
47
- options[:redundant_state]
33
+ }
48
34
  )
49
35
  end
50
36
  end
51
37
 
38
+ if options[:redundant_state]
39
+ routers = filter_by(routers, 'redundantstate', options[:redundant_state].downcase)
40
+ end
41
+
52
42
  routers.reverse! if options[:reverse]
53
- if options[:text]
54
- puts routers.map {|r| r['name']}.join(" ")
43
+ if routers.size < 1
44
+ say "No routers found."
55
45
  else
56
- puts "Total number of routers: #{routers.size}"
57
- table(border: true) do
58
- row do
59
- column 'ID', width: 40
60
- column 'Name'
61
- column 'Zone'
62
- column 'Account', width: 14 unless options[:project]
63
- column 'Project', width: 14 if options[:listall] || options[:project]
64
- column 'Redundant State'
65
- column 'Linklocal IP', width: 15
66
- column 'Status'
67
- end
68
- routers.each do |router|
69
- row do
70
- column router["id"]
71
- column router["name"]
72
- column router["zonename"]
73
- column router["account"] unless options[:project]
74
- column router["project"] if options[:listall] || options[:project]
75
- column router["redundantstate"]
76
- column router["linklocalip"]
77
- column router["state"]
78
- end
79
- end
80
- end
46
+ table = [[
47
+ 'Name', 'Zone', 'Account', 'Project', 'Redundant-State', 'Linklocal IP', 'Status', 'ID'
48
+ ]]
49
+ table[0].delete('ID') unless options[:showid]
50
+ routers.each do |router|
51
+ table << [
52
+ router["name"],
53
+ router["zonename"],
54
+ router["account"],
55
+ router["project"],
56
+ router["redundantstate"],
57
+ router["linklocalip"],
58
+ router["state"],
59
+ router["id"]
60
+ ]
61
+ table[-1].delete_at(-1) unless table[0].index "ID"
62
+ end
63
+ print_table table
64
+ puts
65
+ say "Number of routers: #{routers.size}"
81
66
  end
82
67
 
83
68
  if options[:command]
@@ -86,14 +71,14 @@ class Router < Thor
86
71
  exit unless yes?("Start the routers above? [y/N]:", :magenta)
87
72
  routers.each do |router|
88
73
  say "Start router #{router['name']}... "
89
- say "job started ", :green if job = cs_cli.start_router(router['id'])
74
+ say "job started ", :green if job = client.start_router(router['id'])
90
75
  say "(jobid: #{job['jobid']})"
91
76
  end
92
77
  when "stop"
93
78
  exit unless yes?("Stop the routers above? [y/N]:", :magenta)
94
79
  routers.each do |router|
95
80
  say "Stop router #{router['name']}... "
96
- say "job started ", :green if job = cs_cli.stop_router(router['id'])
81
+ say "job started ", :green if job = client.stop_router(router['id'])
97
82
  say "(jobid: #{job['jobid']})"
98
83
  end
99
84
  else
@@ -103,25 +88,22 @@ class Router < Thor
103
88
  end
104
89
  end
105
90
 
106
- desc "router stop ID", "stop virtual router"
91
+ desc "stop ID", "stop virtual router"
107
92
  def stop(id)
108
93
  exit unless yes?("Stop the router with ID #{id}?", :magenta)
109
- cs_cli = CloudstackCli::Helper.new(options[:config])
110
- cs_cli.stop_router id
94
+ client.stop_router id
111
95
  end
112
96
 
113
- desc "router start ID", "start virtual router"
97
+ desc "start ID", "start virtual router"
114
98
  def start(id)
115
99
  exit unless yes?("Start the router with ID #{id}?", :magenta)
116
- cs_cli = CloudstackCli::Helper.new(options[:config])
117
- cs_cli.start_router id
100
+ client.start_router id
118
101
  end
119
102
 
120
- desc "router destroy ID", "destroy virtual router"
103
+ desc "destroy ID", "destroy virtual router"
121
104
  def destroy(id)
122
105
  exit unless yes?("Destroy the router with ID #{id}?", :magenta)
123
- cs_cli = CloudstackCli::Helper.new(options[:config])
124
- say "OK", :green if cs_cli.destroy_router(id)
106
+ say "OK", :green if client.destroy_router(id)
125
107
  end
126
108
 
127
109
  end
@@ -1,33 +1,37 @@
1
- class Server < Thor
1
+ class Server < CloudstackCli::Base
2
2
 
3
- desc "server list", "list servers"
4
- option :listall, :type => :boolean
5
- option :text, :type => :boolean
3
+ desc "list", "list servers"
6
4
  option :project
7
5
  option :account
8
6
  def list
9
- cs_cli = CloudstackCli::Helper.new(options[:config])
10
7
  if options[:project]
11
- project = cs_cli.projects.select { |p| p['name'] == options[:project] }.first
12
- exit_now! "Project '#{options[:project]}' not found" unless project
13
- options[:project_id] = project['id']
14
- options[:account] = nil
8
+ if options[:project].downcase == "all"
9
+ options[:project_id] = -1
10
+ else
11
+ project = find_project
12
+ options[:project_id] = project['id']
13
+ end
15
14
  end
16
- servers = cs_cli.virtual_machines(options)
15
+ servers = client.list_servers(options)
17
16
  if servers.size < 1
18
17
  puts "No servers found"
19
18
  else
20
- if options[:text]
21
- servers.each do |server|
22
- puts "#{server['name']} - #{server['state']} - #{server['domain']}"
23
- end
24
- else
25
- cs_cli.virtual_machines_table(servers)
19
+ table = [["Name", "State", "Offering", "Zone", options[:project] ? "Project" : "Account", "IP's"]]
20
+ servers.each do |server|
21
+ table << [
22
+ server['name'],
23
+ server['state'],
24
+ server['serviceofferingname'],
25
+ server['zonename'],
26
+ options[:project] ? server['project'] : server['account'],
27
+ server['nic'].map { |nic| nic['ipaddress']}.join(' ')
28
+ ]
26
29
  end
30
+ print_table table
27
31
  end
28
32
  end
29
33
 
30
- desc "server create NAME", "create a server"
34
+ desc "create NAME", "create a server"
31
35
  option :zone, :required => true
32
36
  option :template, :required => true
33
37
  option :offering, :required => true
@@ -37,34 +41,37 @@ class Server < Thor
37
41
  option :interactive, :type => :boolean
38
42
  def create(name)
39
43
  CloudstackCli::Helper.new(options[:config]).bootstrap_server(
40
- name,
41
- options[:zone],
42
- options[:template],
43
- options[:offering],
44
- options[:networks],
45
- options[:port_forwarding],
46
- options[:project]
47
- )
44
+ name,
45
+ options[:zone],
46
+ options[:template],
47
+ options[:offering],
48
+ options[:networks],
49
+ options[:port_forwarding],
50
+ options[:project]
51
+ )
48
52
  end
49
53
 
50
- desc "server bootstrap", "interactive creation of a server with network access"
54
+ desc "bootstrap", "interactive creation of a server with network access"
51
55
  def bootstrap
52
56
  CloudstackCli::Helper.new(options[:config]).bootstrap_server_interactive()
53
57
  end
54
58
 
55
- desc "server stop NAME", "stop a server"
59
+ desc "stop NAME", "stop a server"
56
60
  def stop(name)
57
- CloudstackCli::Helper.new(options[:config]).stop_server(name)
61
+ client.stop_server(name)
62
+ puts
58
63
  end
59
64
 
60
- desc "server start NAME", "start a server"
65
+ desc "start NAME", "start a server"
61
66
  def start(name)
62
- CloudstackCli::Helper.new(options[:config]).start_server(name)
67
+ client.start_server(name)
68
+ puts
63
69
  end
64
70
 
65
- desc "server reboot NAME", "reboot a server"
71
+ desc "reboot NAME", "reboot a server"
66
72
  def restart(name)
67
- CloudstackCli::Helper.new(options[:config]).reboot_server(name)
73
+ client.reboot_server(name)
74
+ puts
68
75
  end
69
76
 
70
77
  end
@@ -1,16 +1,8 @@
1
- class Stack < Thor
1
+ class Stack < CloudstackCli::Base
2
2
 
3
- desc "stack create STACKFILE", "create a stack of servers"
3
+ desc "create STACKFILE", "create a stack of servers"
4
4
  def create(stackfile)
5
- CloudstackCli::Cli.new.bootstrap_server(
6
- name,
7
- options[:zone],
8
- options[:template],
9
- options[:offering],
10
- options[:networks],
11
- options[:port_forwarding],
12
- options[:project]
13
- )
5
+ # TODO
14
6
  end
15
7
 
16
8
  end
@@ -1,24 +1,22 @@
1
- class Template < Thor
1
+ class Template < CloudstackCli::Base
2
2
 
3
- desc 'template list', 'list templates by type [featured|self|self-executable|executable|community]'
3
+ desc 'list', 'list templates by type [featured|self|self-executable|executable|community]'
4
4
  option :project
5
5
  def list(type='featured')
6
- cs_cli = CloudstackCli::Helper.new(options[:config])
7
-
8
- if options[:project]
9
- project = cs_cli.projects.select { |p| p['name'] == options[:project] }.first
10
- exit_now! "Project '#{options[:project]}' not found" unless project
6
+ project = find_project if options[:project]
7
+ unless %w(featured self self-executable executable community).include? type
8
+ say "unsupported template type '#{type}'", :red
9
+ exit 1
11
10
  end
12
-
13
- exit_now! "unsupported template type '#{type}'" unless
14
- %w(featured self self-executable executable community).include? type
15
- templates = cs_cli.templates(type, project ? project['id'] : nil)
11
+ templates = client.list_templates(type, project ? project['id'] : nil)
16
12
  if templates.size < 1
17
13
  puts "No templates found"
18
14
  else
15
+ table = [["Name", "Zone"]]
19
16
  templates.each do |template|
20
- puts template['name']
17
+ table << [template['name'], template['zonename']]
21
18
  end
19
+ print_table(table)
22
20
  end
23
21
  end
24
22