cloudstack-cli 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile.lock +1 -3
- data/cloudstack-cli.gemspec +0 -1
- data/lib/cloudstack-cli/base.rb +3 -0
- data/lib/cloudstack-cli/cli.rb +5 -0
- data/lib/cloudstack-cli/commands/server.rb +8 -7
- data/lib/cloudstack-cli/helper.rb +6 -2
- data/lib/cloudstack-cli/version.rb +1 -1
- data/lib/cloudstack-client/client.rb +7 -980
- data/lib/cloudstack-client/commands/account.rb +22 -0
- data/lib/cloudstack-client/commands/capacity.rb +19 -0
- data/lib/cloudstack-client/commands/command_template +8 -0
- data/lib/cloudstack-client/commands/disk_offering.rb +49 -0
- data/lib/cloudstack-client/commands/domain.rb +22 -0
- data/lib/cloudstack-client/commands/ip_address.rb +77 -0
- data/lib/cloudstack-client/commands/iso.rb +60 -0
- data/lib/cloudstack-client/commands/load_balancer_rule.rb +61 -0
- data/lib/cloudstack-client/commands/network.rb +101 -0
- data/lib/cloudstack-client/commands/port_forwarding_rule.rb +50 -0
- data/lib/cloudstack-client/commands/project.rb +32 -0
- data/lib/cloudstack-client/commands/router.rb +74 -0
- data/lib/cloudstack-client/commands/server.rb +291 -0
- data/lib/cloudstack-client/commands/service_offering.rb +98 -0
- data/lib/cloudstack-client/commands/template.rb +60 -0
- data/lib/cloudstack-client/commands/volumes.rb +20 -0
- data/lib/cloudstack-client/commands/zone.rb +57 -0
- data/lib/cloudstack-client/helper.rb +2 -2
- data/lib/cloudstack-client/version.rb +3 -0
- data/lib/cloudstack_cli.rb +0 -8
- data/lib/cloudstack_client.rb +2 -2
- metadata +26 -34
- data/config/cloudstack.example.yml +0 -3
- data/lib/cloudstack-client/ssh_command.rb +0 -15
@@ -0,0 +1,22 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module Account
|
4
|
+
|
5
|
+
##
|
6
|
+
# Lists accounts.
|
7
|
+
|
8
|
+
def list_accounts(args = { :name => nil })
|
9
|
+
params = {
|
10
|
+
'command' => 'listAccounts',
|
11
|
+
'listall' => 'true',
|
12
|
+
'isrecursive' => 'true'
|
13
|
+
}
|
14
|
+
params['name'] = args[:name] if args[:name]
|
15
|
+
|
16
|
+
json = send_request(params)
|
17
|
+
json['account'] || []
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module DiskOffering
|
4
|
+
|
5
|
+
##
|
6
|
+
# Lists all available disk offerings.
|
7
|
+
|
8
|
+
def list_disk_offerings(domain = nil)
|
9
|
+
params = {
|
10
|
+
'command' => 'listDiskOfferings'
|
11
|
+
}
|
12
|
+
|
13
|
+
if domain
|
14
|
+
params['domainid'] = list_domains(domain).first["id"]
|
15
|
+
end
|
16
|
+
|
17
|
+
json = send_request(params)
|
18
|
+
json['diskoffering'] || []
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Get disk offering by name.
|
23
|
+
|
24
|
+
def get_disk_offering(name)
|
25
|
+
|
26
|
+
# TODO: use name parameter
|
27
|
+
# listServiceOfferings in CloudStack 2.2 doesn't seem to work
|
28
|
+
# when the name parameter is specified. When this is fixed,
|
29
|
+
# the name parameter should be added to the request.
|
30
|
+
params = {
|
31
|
+
'command' => 'listDiskOfferings'
|
32
|
+
}
|
33
|
+
json = send_request(params)
|
34
|
+
|
35
|
+
services = json['diskoffering']
|
36
|
+
return nil unless services
|
37
|
+
|
38
|
+
services.each { |s|
|
39
|
+
if s['name'] == name then
|
40
|
+
return s
|
41
|
+
end
|
42
|
+
}
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module Domain
|
4
|
+
|
5
|
+
##
|
6
|
+
# List domains.
|
7
|
+
|
8
|
+
def list_domains(name = nil)
|
9
|
+
params = {
|
10
|
+
'command' => 'listDomains',
|
11
|
+
'listall' => 'true',
|
12
|
+
'isrecursive' => 'true'
|
13
|
+
}
|
14
|
+
params['name'] = name if name
|
15
|
+
|
16
|
+
json = send_request(params)
|
17
|
+
json['domain'] || []
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module IpAddress
|
4
|
+
|
5
|
+
##
|
6
|
+
# Lists the public ip addresses.
|
7
|
+
|
8
|
+
def list_public_ip_addresses(args = {})
|
9
|
+
params = {
|
10
|
+
'command' => 'listPublicIpAddresses',
|
11
|
+
'isrecursive' => true
|
12
|
+
}
|
13
|
+
if args[:project]
|
14
|
+
project = get_project(args[:project])
|
15
|
+
params['projectid'] = project['id']
|
16
|
+
end
|
17
|
+
if args[:account]
|
18
|
+
account = list_accounts({name: args[:account]}).first
|
19
|
+
unless account
|
20
|
+
puts "Error: Account #{args[:account]} not found."
|
21
|
+
exit 1
|
22
|
+
end
|
23
|
+
params['domainid'] = account["domainid"]
|
24
|
+
params['account'] = args[:account]
|
25
|
+
end
|
26
|
+
params['listall'] = args[:listall] if args[:listall]
|
27
|
+
|
28
|
+
json = send_request(params)
|
29
|
+
json['publicipaddress'] || []
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# Finds the public ip address for a given ip address string.
|
34
|
+
|
35
|
+
def get_public_ip_address(ip_address)
|
36
|
+
params = {
|
37
|
+
'command' => 'listPublicIpAddresses',
|
38
|
+
'ipaddress' => ip_address
|
39
|
+
}
|
40
|
+
json = send_request(params)
|
41
|
+
ip_address = json['publicipaddress']
|
42
|
+
|
43
|
+
return nil unless ip_address
|
44
|
+
ip_address.first
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
##
|
49
|
+
# Acquires and associates a public IP to an account.
|
50
|
+
|
51
|
+
def associate_ip_address(network_id)
|
52
|
+
params = {
|
53
|
+
'command' => 'associateIpAddress',
|
54
|
+
'networkid' => network_id
|
55
|
+
}
|
56
|
+
|
57
|
+
json = send_async_request(params)
|
58
|
+
json['ipaddress']
|
59
|
+
end
|
60
|
+
|
61
|
+
##
|
62
|
+
# Disassociates an ip address from the account.
|
63
|
+
#
|
64
|
+
# Returns true if successful, false otherwise.
|
65
|
+
|
66
|
+
def disassociate_ip_address(id)
|
67
|
+
params = {
|
68
|
+
'command' => 'disassociateIpAddress',
|
69
|
+
'id' => id
|
70
|
+
}
|
71
|
+
json = send_async_request(params)
|
72
|
+
json['success']
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module Iso
|
4
|
+
|
5
|
+
##
|
6
|
+
# Lists all isos that match the specified filter.
|
7
|
+
#
|
8
|
+
# Allowable filter values are:
|
9
|
+
#
|
10
|
+
# * featured - isos that are featured and are public
|
11
|
+
# * self - isos that have been registered/created by the owner
|
12
|
+
# * self-executable - isos that have been registered/created by the owner that can be used to deploy a new VM
|
13
|
+
# * executable - all isos that can be used to deploy a new VM
|
14
|
+
# * community - isos that are public
|
15
|
+
|
16
|
+
def list_isos(args = {})
|
17
|
+
filter = args[:filter] || 'featured'
|
18
|
+
params = {
|
19
|
+
'command' => 'listIsos',
|
20
|
+
'templateFilter' => filter
|
21
|
+
}
|
22
|
+
params['projectid'] = args[:project_id] if args[:project_id]
|
23
|
+
params['zoneid'] = args[:zone_id] if args[:zone_id]
|
24
|
+
|
25
|
+
json = send_request(params)
|
26
|
+
json['iso'] || []
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Finds the template with the specified name.
|
31
|
+
|
32
|
+
def get_iso(name)
|
33
|
+
|
34
|
+
# TODO: use name parameter
|
35
|
+
# listIsos in CloudStack 2.2 doesn't seem to work
|
36
|
+
# when the name parameter is specified. When this is fixed,
|
37
|
+
# the name parameter should be added to the request.
|
38
|
+
params = {
|
39
|
+
'command' => 'listIsos',
|
40
|
+
'templateFilter' => 'executable'
|
41
|
+
}
|
42
|
+
json = send_request(params)
|
43
|
+
|
44
|
+
isos = json['iso']
|
45
|
+
if !isos then
|
46
|
+
return nil
|
47
|
+
end
|
48
|
+
|
49
|
+
isos.each { |t|
|
50
|
+
if t['name'] == name then
|
51
|
+
return t
|
52
|
+
end
|
53
|
+
}
|
54
|
+
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module LoadBalancerRule
|
4
|
+
|
5
|
+
##
|
6
|
+
# List loadbalancer rules
|
7
|
+
|
8
|
+
def list_load_balancer_rules(options = {})
|
9
|
+
params = {
|
10
|
+
'command' => 'listLoadBalancerRules',
|
11
|
+
}
|
12
|
+
params['name'] = options[:name] if options[:name]
|
13
|
+
|
14
|
+
if options[:project_name]
|
15
|
+
project = get_project(options[:project_name])
|
16
|
+
params['projectid'] = project['id']
|
17
|
+
end
|
18
|
+
|
19
|
+
json = send_request(params)
|
20
|
+
json['loadbalancerrule'] || []
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Creates a load balancing rule.
|
25
|
+
|
26
|
+
def create_load_balancer_rule(name, publicip, private_port, public_port, options = {})
|
27
|
+
params = {
|
28
|
+
'command' => 'createLoadBalancerRule',
|
29
|
+
'name' => name,
|
30
|
+
'privateport' => private_port,
|
31
|
+
'publicport' => public_port,
|
32
|
+
'publicipid' => get_public_ip_address(publicip)['id']
|
33
|
+
}
|
34
|
+
params['algorithm'] = options[:algorithm] || 'roundrobin'
|
35
|
+
params['openfirewall'] = options[:openfirewall] || true
|
36
|
+
|
37
|
+
json = send_async_request(params)
|
38
|
+
json['LoadBalancerRule']
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Assigns virtual machine or a list of virtual machines to a load balancer rule.
|
43
|
+
|
44
|
+
def assign_to_load_balancer_rule(name, vm_names)
|
45
|
+
id = list_load_balancer_rules(name).first['id']
|
46
|
+
|
47
|
+
vm_ids = vm_names.map do |vm|
|
48
|
+
get_server(vm)['id']
|
49
|
+
end
|
50
|
+
|
51
|
+
params = {
|
52
|
+
'command' => 'assignToLoadBalancerRule',
|
53
|
+
'id' => id,
|
54
|
+
'virtualmachineids' => vm_ids.join(',')
|
55
|
+
}
|
56
|
+
json = send_async_request(params)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module Network
|
4
|
+
|
5
|
+
##
|
6
|
+
# Finds the network with the specified name.
|
7
|
+
|
8
|
+
def get_network(name, project_id = nil)
|
9
|
+
params = {
|
10
|
+
'command' => 'listNetworks',
|
11
|
+
'listall' => true
|
12
|
+
}
|
13
|
+
params['projectid'] = project_id if project_id
|
14
|
+
json = send_request(params)
|
15
|
+
|
16
|
+
networks = json['network']
|
17
|
+
return nil unless networks
|
18
|
+
|
19
|
+
networks.each { |n|
|
20
|
+
if n['name'] == name then
|
21
|
+
return n
|
22
|
+
end
|
23
|
+
}
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Finds the default network.
|
29
|
+
|
30
|
+
def get_default_network(zone = nil)
|
31
|
+
params = {
|
32
|
+
'command' => 'listNetworks',
|
33
|
+
'isDefault' => true
|
34
|
+
}
|
35
|
+
if zone
|
36
|
+
params['zoneid'] = get_zone(zone)['id']
|
37
|
+
end
|
38
|
+
json = send_request(params)
|
39
|
+
|
40
|
+
networks = json['network']
|
41
|
+
return nil if !networks || networks.empty?
|
42
|
+
return networks.first if networks.length == 1
|
43
|
+
|
44
|
+
networks.each { |n|
|
45
|
+
if n['type'] == 'Direct' then
|
46
|
+
return n
|
47
|
+
end
|
48
|
+
}
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Lists all available networks.
|
54
|
+
|
55
|
+
def list_networks(args = {})
|
56
|
+
params = {
|
57
|
+
'command' => 'listNetworks',
|
58
|
+
'listall' => true,
|
59
|
+
}
|
60
|
+
params['projectid'] = args[:project_id] if args[:project_id]
|
61
|
+
params['zoneid'] = args[:zone_id] if args[:zone_id]
|
62
|
+
params['isDefault'] = true if args[:isdefault]
|
63
|
+
if args[:account]
|
64
|
+
domain = list_accounts(name: args[:account])
|
65
|
+
if domain.size > 0
|
66
|
+
params['account'] = args[:account]
|
67
|
+
params['domainid'] = domain.first["domainid"]
|
68
|
+
else
|
69
|
+
puts "Account #{args[:account]} not found."
|
70
|
+
end
|
71
|
+
end
|
72
|
+
json = send_request(params)
|
73
|
+
json['network'] || []
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Delete network.
|
78
|
+
|
79
|
+
def delete_network(id)
|
80
|
+
params = {
|
81
|
+
'command' => 'deleteNetwork',
|
82
|
+
'id' => id,
|
83
|
+
}
|
84
|
+
p json = send_async_request(params)
|
85
|
+
json['network']
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Lists all physical networks.
|
90
|
+
|
91
|
+
def list_physical_networks
|
92
|
+
params = {
|
93
|
+
'command' => 'listPhysicalNetworks',
|
94
|
+
}
|
95
|
+
json = send_request(params)
|
96
|
+
json['physicalnetwork'] || []
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module PortForwardingRule
|
4
|
+
|
5
|
+
##
|
6
|
+
# Lists all port forwarding rules.
|
7
|
+
|
8
|
+
def list_port_forwarding_rules(ip_address_id=nil, project_id)
|
9
|
+
params = {
|
10
|
+
'command' => 'listPortForwardingRules',
|
11
|
+
'listall' => true,
|
12
|
+
'isrecursive' => true
|
13
|
+
}
|
14
|
+
params['ipAddressId'] = ip_address_id if ip_address_id
|
15
|
+
params['projectid'] = project_id if project_id
|
16
|
+
json = send_request(params)
|
17
|
+
json['portforwardingrule'] || []
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Gets the SSH port forwarding rule for the specified server.
|
22
|
+
|
23
|
+
def get_ssh_port_forwarding_rule(server, cached_rules=nil)
|
24
|
+
rules = cached_rules || list_port_forwarding_rules || []
|
25
|
+
rules.find_all { |r|
|
26
|
+
r['virtualmachineid'] == server['id'] &&
|
27
|
+
r['privateport'] == '22'&&
|
28
|
+
r['publicport'] == '22'
|
29
|
+
}.first
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# Creates a port forwarding rule.
|
34
|
+
|
35
|
+
def create_port_forwarding_rule(ip_address_id, private_port, protocol, public_port, virtual_machine_id)
|
36
|
+
params = {
|
37
|
+
'command' => 'createPortForwardingRule',
|
38
|
+
'ipAddressId' => ip_address_id,
|
39
|
+
'privatePort' => private_port,
|
40
|
+
'protocol' => protocol,
|
41
|
+
'publicPort' => public_port,
|
42
|
+
'virtualMachineId' => virtual_machine_id
|
43
|
+
}
|
44
|
+
json = send_async_request(params)
|
45
|
+
json['portforwardingrule']
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module Project
|
4
|
+
|
5
|
+
##
|
6
|
+
# Get project by name.
|
7
|
+
|
8
|
+
def get_project(name)
|
9
|
+
params = {
|
10
|
+
'command' => 'listProjects',
|
11
|
+
'name' => name,
|
12
|
+
'listall' => true,
|
13
|
+
}
|
14
|
+
json = send_request(params)
|
15
|
+
json['project'] ? json['project'].first : nil
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# Lists projects.
|
20
|
+
|
21
|
+
def list_projects
|
22
|
+
params = {
|
23
|
+
'command' => 'listProjects',
|
24
|
+
'listall' => true,
|
25
|
+
}
|
26
|
+
json = send_request(params)
|
27
|
+
json['project'] || []
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module CloudstackClient
|
2
|
+
|
3
|
+
module Router
|
4
|
+
|
5
|
+
##
|
6
|
+
# Lists all virtual routers.
|
7
|
+
|
8
|
+
def list_routers(args = {:account => nil, :zone => nil, :projectid => nil, :status => nil, :name => nil})
|
9
|
+
params = {
|
10
|
+
'command' => 'listRouters',
|
11
|
+
'listall' => 'true',
|
12
|
+
'isrecursive' => 'true'
|
13
|
+
}
|
14
|
+
if args[:zone]
|
15
|
+
zone = get_zone(args[:zone])
|
16
|
+
unless zone
|
17
|
+
puts "Error: Zone #{args[:zone]} not found"
|
18
|
+
exit 1
|
19
|
+
end
|
20
|
+
params['zoneid'] = zone['id']
|
21
|
+
end
|
22
|
+
params['projectid'] = args[:projectid] if args[:projectid]
|
23
|
+
params['state'] = args[:status] if args[:status]
|
24
|
+
params['name'] = args[:name] if args[:name]
|
25
|
+
if args[:account]
|
26
|
+
account = list_accounts({name: args[:account]}).first
|
27
|
+
unless account
|
28
|
+
puts "Error: Account #{args[:account]} not found."
|
29
|
+
exit 1
|
30
|
+
end
|
31
|
+
params['domainid'] = account["domainid"]
|
32
|
+
params['account'] = args[:account]
|
33
|
+
end
|
34
|
+
|
35
|
+
json = send_request(params)
|
36
|
+
json['router'] || []
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Destroy virtual router.
|
41
|
+
|
42
|
+
def destroy_router(id, async = false)
|
43
|
+
params = {
|
44
|
+
'command' => 'destroyRouter',
|
45
|
+
'id' => id
|
46
|
+
}
|
47
|
+
async ? send_async_request(params) : send_request(params)
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Start virtual router.
|
52
|
+
|
53
|
+
def start_router(id, async = false)
|
54
|
+
params = {
|
55
|
+
'command' => 'startRouter',
|
56
|
+
'id' => id
|
57
|
+
}
|
58
|
+
async ? send_async_request(params) : send_request(params)
|
59
|
+
end
|
60
|
+
|
61
|
+
##
|
62
|
+
# Stop virtual router.
|
63
|
+
|
64
|
+
def stop_router(id, async = false)
|
65
|
+
params = {
|
66
|
+
'command' => 'stopRouter',
|
67
|
+
'id' => id
|
68
|
+
}
|
69
|
+
async ? send_async_request(params) : send_request(params)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|