wakame-vdc-dcmgr 10.12.0 → 11.06.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE +164 -201
  2. data/Rakefile +6 -11
  3. data/bin/collector +10 -24
  4. data/config/dcmgr.conf.example +18 -6
  5. data/config/initializers/isono.rb +7 -23
  6. data/config/initializers/sequel.rb +11 -2
  7. data/lib/dcmgr.rb +70 -11
  8. data/lib/dcmgr/cli/base.rb +74 -0
  9. data/lib/dcmgr/cli/errors.rb +59 -0
  10. data/lib/dcmgr/cli/group.rb +101 -0
  11. data/lib/dcmgr/cli/host.rb +101 -0
  12. data/lib/dcmgr/cli/image.rb +108 -0
  13. data/lib/dcmgr/cli/keypair.rb +72 -0
  14. data/lib/dcmgr/cli/network.rb +198 -0
  15. data/lib/dcmgr/cli/quota.rb +28 -0
  16. data/lib/dcmgr/cli/spec.rb +82 -0
  17. data/lib/dcmgr/cli/storage.rb +88 -0
  18. data/lib/dcmgr/cli/tag.rb +81 -0
  19. data/lib/dcmgr/cli/vlan.rb +53 -0
  20. data/lib/dcmgr/drivers/hypervisor.rb +33 -0
  21. data/lib/dcmgr/drivers/iijgio_storage.rb +37 -0
  22. data/lib/dcmgr/drivers/kvm.rb +118 -0
  23. data/lib/dcmgr/drivers/lxc.rb +167 -0
  24. data/lib/dcmgr/drivers/s3_storage.rb +39 -0
  25. data/lib/dcmgr/drivers/snapshot_storage.rb +51 -0
  26. data/lib/dcmgr/endpoints/core_api.rb +188 -324
  27. data/lib/dcmgr/endpoints/core_api_mock.rb +52 -3
  28. data/lib/dcmgr/endpoints/errors.rb +73 -32
  29. data/lib/dcmgr/endpoints/metadata.rb +163 -16
  30. data/lib/dcmgr/helpers/cli_helper.rb +1 -1
  31. data/lib/dcmgr/helpers/nic_helper.rb +35 -0
  32. data/lib/dcmgr/logger.rb +5 -1
  33. data/lib/dcmgr/messaging_client.rb +117 -0
  34. data/lib/dcmgr/models/account.rb +27 -3
  35. data/lib/dcmgr/models/base_new.rb +21 -7
  36. data/lib/dcmgr/models/host_pool.rb +27 -7
  37. data/lib/dcmgr/models/image.rb +31 -3
  38. data/lib/dcmgr/models/instance.rb +72 -23
  39. data/lib/dcmgr/models/instance_nic.rb +12 -2
  40. data/lib/dcmgr/models/instance_spec.rb +16 -0
  41. data/lib/dcmgr/models/ip_lease.rb +37 -1
  42. data/lib/dcmgr/models/netfilter_group.rb +7 -7
  43. data/lib/dcmgr/models/network.rb +42 -3
  44. data/lib/dcmgr/models/quota.rb +25 -0
  45. data/lib/dcmgr/models/request_log.rb +26 -11
  46. data/lib/dcmgr/models/ssh_key_pair.rb +14 -1
  47. data/lib/dcmgr/models/storage_pool.rb +19 -72
  48. data/lib/dcmgr/models/tag.rb +5 -0
  49. data/lib/dcmgr/models/vlan_lease.rb +8 -0
  50. data/lib/dcmgr/models/volume.rb +26 -8
  51. data/lib/dcmgr/models/volume_snapshot.rb +37 -0
  52. data/lib/dcmgr/node_modules/hva_collector.rb +56 -36
  53. data/lib/dcmgr/node_modules/instance_ha.rb +1 -1
  54. data/lib/dcmgr/node_modules/instance_monitor.rb +70 -0
  55. data/lib/dcmgr/node_modules/service_netfilter.rb +914 -0
  56. data/lib/dcmgr/node_modules/sta_collector.rb +7 -30
  57. data/lib/dcmgr/rack/request_logger.rb +60 -0
  58. data/lib/dcmgr/rack/run_initializer.rb +42 -0
  59. data/lib/dcmgr/rpc/hva_handler.rb +388 -0
  60. data/lib/dcmgr/rubygems.rb +7 -0
  61. data/lib/dcmgr/storage_service.rb +98 -0
  62. data/lib/dcmgr/tags.rb +2 -2
  63. data/lib/dcmgr/version.rb +8 -0
  64. data/lib/ext/time.rb +8 -0
  65. data/lib/sinatra/respond_to.rb +3 -0
  66. data/lib/sinatra/sequel_transaction.rb +20 -5
  67. data/web/api/config.ru +9 -13
  68. data/web/metadata/config.ru +10 -13
  69. metadata +162 -120
  70. data/lib/dcmgr/models/physical_host.rb +0 -67
  71. data/lib/dcmgr/web/base.rb +0 -21
@@ -0,0 +1,101 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'thor'
4
+ require 'isono'
5
+
6
+ module Dcmgr::Cli
7
+ class Host < Base
8
+ namespace :host
9
+ include Dcmgr::Models
10
+
11
+ desc "add NODE_ID [options]", "Register a new host node"
12
+ method_option :uuid, :type => :string, :aliases => "-u", :desc => "The UUID for the new host pool"
13
+ #method_option :force, :type => :boolean, :aliases => "-f", :default=>false, :desc => "Force to create new entry"
14
+ method_option :cpu_cores, :type => :numeric, :aliases => "-c", :default=>1, :desc => "Number of cpu cores to be offered"
15
+ method_option :memory_size, :type => :numeric, :aliases => "-m", :default=>1024, :desc => "Amount of memory to be offered (in MB)"
16
+ method_option :hypervisor, :type => :string, :aliases => "-p", :default=>'kvm', :desc => "The hypervisor name. [#{HostPool::SUPPORTED_HYPERVISOR.join(', ')}]"
17
+ method_option :arch, :type => :string, :aliases => "-r", :default=>'x86_64', :desc => "The CPU architecture type. [#{HostPool::SUPPORTED_ARCH.join(', ')}]"
18
+ method_option :account_id, :type => :string, :default=>'a-shpool', :aliases => "-a", :desc => "The account ID to own this"
19
+ def add(node_id)
20
+ UnknownUUIDError.raise(options[:account_id]) if Account[options[:account_id]].nil?
21
+ UnsupportedArchError.raise(options[:arch]) unless HostPool::SUPPORTED_ARCH.member?(options[:arch])
22
+ UnsupportedHypervisorError.raise(options[:hypervisor]) unless HostPool::SUPPORTED_HYPERVISOR.member?(options[:hypervisor])
23
+
24
+ if (options[:force] == false && Isono::Models::NodeState.filter(:node_id=>node_id).first == nil)
25
+ abort("Node ID is not registered yet: #{node_id}")
26
+ end
27
+
28
+ fields = {
29
+ :node_id=>node_id,
30
+ :offering_cpu_cores=>options[:cpu_cores],
31
+ :offering_memory_size=>options[:memory_size],
32
+ :hypervisor=>options[:hypervisor],
33
+ :arch=>options[:arch],
34
+ :account_id=>options[:account_id],
35
+ }
36
+ fields.merge!({:uuid => options[:uuid]}) unless options[:uuid].nil?
37
+ puts super(HostPool,fields)
38
+ end
39
+
40
+ desc "modify UUID [options]", "Modify a registered host node"
41
+ method_option :cpu_cores, :type => :numeric, :aliases => "-c", :desc => "Number of cpu cores to be offered"
42
+ method_option :account_id, :type => :string, :aliases => "-a", :desc => "The account ID to own this"
43
+ method_option :memory_size, :type => :numeric, :aliases => "-m", :desc => "Amount of memory to be offered (in MB)"
44
+ method_option :hypervisor, :type => :string, :aliases => "-p", :desc => "The hypervisor name. [#{HostPool::SUPPORTED_HYPERVISOR.join(', ')}]"
45
+ def modify(uuid)
46
+ UnknownUUIDError.raise(options[:account_id]) if options[:account_id] && Account[options[:account_id]].nil?
47
+ UnsupportedHypervisorError.raise(options[:hypervisor]) unless options[:hypervisor].nil? || HostPool::SUPPORTED_HYPERVISOR.member?(options[:hypervisor])
48
+ fields = {
49
+ :offering_memory_size=>options[:memory_size],
50
+ :offering_cpu_cores=>options[:cpu_cores],
51
+ :account_id=>options[:account_id],
52
+ :hypervisor=>options[:hypervisor]
53
+ }
54
+ super(HostPool,uuid,fields)
55
+ end
56
+
57
+ desc "del UUID", "Deregister a host node"
58
+ def del(uuid)
59
+ super(HostPool,uuid)
60
+ end
61
+
62
+ desc "show [UUID]", "Show list of host nodes and details"
63
+ def show(uuid=nil)
64
+ if uuid
65
+ host = HostPool[uuid]
66
+ puts ERB.new(<<__END, nil, '-').result(binding)
67
+ Host UUID:
68
+ <%= host.canonical_uuid %>
69
+ Node ID:
70
+ <%= host.node_id %>
71
+ CPU Cores (offerring):
72
+ <%= host.offering_cpu_cores %>
73
+ Memory (offerring):
74
+ <%= host.offering_memory_size %>MB
75
+ Hypervisor:
76
+ <%= host.hypervisor %>
77
+ __END
78
+ else
79
+ cond = {}
80
+ all = HostPool.filter(cond).all
81
+ puts ERB.new(<<__END, nil, '-').result(binding)
82
+ <%- all.each { |row| -%>
83
+ <%= "%-15s %-20s %-10s" % [row.canonical_uuid, row.node_id, row.status] %>
84
+ <%- } -%>
85
+ __END
86
+ end
87
+ end
88
+
89
+ desc "shownodes", "Show node (agents)"
90
+ def shownodes
91
+ nodes = Isono::Models::NodeState.filter.all
92
+
93
+ puts ERB.new(<<__END, nil, '-').result(binding)
94
+ Node ID State
95
+ <%- nodes.each { |row| -%>
96
+ <%= "%-20s %-10s" % [row.node_id, row.state] %>
97
+ <%- } -%>
98
+ __END
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,108 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Dcmgr::Cli
4
+ class Image < Base
5
+ namespace :image
6
+ M = Dcmgr::Models
7
+
8
+
9
+ class AddOperation < Base
10
+ namespace :add
11
+
12
+ desc "local IMAGE_LOCATION [options]", "Register local store machine image"
13
+ method_option :uuid, :type => :string, :aliases => "-u", :desc => "The UUID for the new machine image"
14
+ method_option :account_id, :type => :string, :aliases => "-a", :required => true, :desc => "The UUID of the account that this machine image belongs to"
15
+ method_option :arch, :type => :string, :default => 'x86_64', :aliases => "-r", :desc => "The architecture for the new machine image. [#{M::HostPool::SUPPORTED_ARCH.join(', ')}]"
16
+ method_option :is_public, :type => :boolean, :aliases => "-p", :default => false, :desc => "A flag that determines whether the new machine image is public or not"
17
+ method_option :description, :type => :string, :aliases => "-d", :desc => "An arbitrary description of the new machine image"
18
+ method_option :state, :type => :string, :aliases => "-s", :default => "init", :desc => "The state for the new machine image"
19
+ def local(location)
20
+ UnknownUUIDError.raise(options[:account_id]) if M::Account[options[:account_id]].nil?
21
+ UnsupportedArchError.raise(options[:arch]) unless M::HostPool::SUPPORTED_ARCH.member?(options[:arch])
22
+
23
+ full_path = File.expand_path(location)
24
+ File.exists?(full_path) || Error.raise("File not found: #{full_path}",100)
25
+
26
+ #TODO: Check if :state is a valid state
27
+ fields = options.dup
28
+ fields[:boot_dev_type]=M::Image::BOOT_DEV_LOCAL
29
+ fields[:source] = {
30
+ :uri => "file://#{full_path}",
31
+ }
32
+ puts add(M::Image, fields)
33
+ end
34
+
35
+ desc "volume snapshot_id [options]", "Register volume store machine image."
36
+ method_option :uuid, :type => :string, :aliases => "-u", :desc => "The UUID for the new machine image."
37
+ method_option :account_id, :type => :string, :aliases => "-a", :required => true, :desc => "The UUID of the account that this machine image belongs to."
38
+ method_option :arch, :type => :string, :default => 'x86_64', :aliases => "-r", :desc => "The architecture for the new machine image. [#{M::HostPool::SUPPORTED_ARCH.join(', ')}]"
39
+ method_option :is_public, :type => :boolean, :aliases => "-p", :default => false, :desc => "A flag that determines whether the new machine image is public or not."
40
+ method_option :description, :type => :string, :aliases => "-d", :desc => "An arbitrary description of the new machine image"
41
+ method_option :state, :type => :string, :aliases => "-s", :default => "init", :desc => "The state for the new machine image"
42
+ def volume(snapshot_id)
43
+ UnknownUUIDError.raise(options[:account_id]) if M::Account[options[:account_id]].nil?
44
+ UnsupportedArchError.raise(options[:arch]) unless M::HostPool::SUPPORTED_ARCH.member?(options[:arch])
45
+ UnknownUUIDError.raise(snapshot_id) if M::VolumeSnapshot[snapshot_id].nil?
46
+ #TODO: Check if :state is a valid state
47
+ fields = options.dup
48
+ fields[:boot_dev_type]=M::Image::BOOT_DEV_SAN
49
+ fields[:source] = {
50
+ :snapshot_id => snapshot_id,
51
+ }
52
+ puts add(M::Image, fields)
53
+ end
54
+
55
+ protected
56
+ def self.basename
57
+ "vdc-manage #{Image.namespace} #{self.namespace}"
58
+ end
59
+ end
60
+
61
+ register AddOperation, 'add', "add IMAGE_TYPE [options]", "Add image metadata [#{AddOperation.tasks.keys.join(', ')}]"
62
+
63
+ desc "modify UUID [options]", "Modify a registered machine image"
64
+ method_option :description, :type => :string, :aliases => "-d", :desc => "An arbitrary description of the machine image"
65
+ method_option :state, :type => :string, :aliases => "-s", :default => "init", :desc => "The state for the machine image"
66
+ def modify(uuid)
67
+ #TODO: Check if state is valid here too
68
+ super(M::Image,uuid,options)
69
+ end
70
+
71
+ desc "del IMAGE_ID", "Delete registered machine image"
72
+ def del(image_id)
73
+ UnknownUUIDError.raise(image_id) if M::Image[image_id].nil?
74
+ super(M::Image, image_id)
75
+ end
76
+
77
+ desc "show [IMAGE_ID]", "Show list of machine image and details"
78
+ def show(uuid=nil)
79
+ if uuid
80
+ img = M::Image[uuid]
81
+ print ERB.new(<<__END, nil, '-').result(binding)
82
+ UUID:
83
+ <%= img.canonical_uuid %>
84
+ Boot Type:
85
+ <%= img.boot_dev_type %>
86
+ Arch:
87
+ <%= img.arch %>
88
+ <%- if img.description -%>
89
+ Description:
90
+ <%= img.description %>
91
+ <%- end -%>
92
+ Is Public:
93
+ <%= img.is_public %>
94
+ State:
95
+ <%= img.state %>
96
+ __END
97
+ else
98
+ cond = {}
99
+ imgs = M::Image.filter(cond).all
100
+ print ERB.new(<<__END, nil, '-').result(binding)
101
+ <%- imgs.each { |row| -%>
102
+ <%= "%-20s %-15s %-15s" % [row.canonical_uuid, row.boot_dev_type, row.arch] %>
103
+ <%- } -%>
104
+ __END
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,72 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Dcmgr::Cli
4
+ class KeyPair < Base
5
+ namespace :keypair
6
+ M = Dcmgr::Models
7
+
8
+ desc "add [options]", "Register a new key pair."
9
+ method_option :uuid, :type => :string, :aliases => "-u", :desc => "The UUID for the new key pair"
10
+ method_option :account_id, :type => :string, :aliases => "-a", :desc => "The UUID of the account this key pair belongs to", :required => true
11
+ method_option :name, :type => :string, :aliases => "-n", :desc => "The name for this key pair", :required => true
12
+ method_option :public_key, :type => :string, :aliases => "-p", :desc => "The path to the public key", :required => true
13
+ method_option :private_key, :type => :string, :aliases => "-r", :desc => "The path to the private key", :required => true
14
+ def add
15
+ UnknownUUIDError.raise(options[:account_id]) if M::Account[options[:account_id]].nil?
16
+ private_key_path = File.expand_path(options[:private_key])
17
+ public_key_path = File.expand_path(options[:public_key])
18
+ Error.raise "Private key file doesn't exist",100 unless File.exists?(private_key_path)
19
+ Error.raise "Public key file doesn't exist",100 unless File.exists?(public_key_path)
20
+
21
+ fields = options.dup
22
+
23
+ #Get the keys from their respective files.
24
+ fields[:public_key] = File.open(public_key_path) {|f| f.readline}
25
+ fields[:private_key] = File.open(private_key_path) {|f| f.readlines.map.join}
26
+
27
+ #Generate the fingerprint from the public key file
28
+ fields[:finger_print] = %x{ssh-keygen -lf #{options[:public_key]} | cut -d ' ' -f2}.chomp
29
+
30
+ puts super(M::SshKeyPair,fields)
31
+ end
32
+
33
+ desc "modify UUID [options]", "Modify an existing key pair"
34
+ method_option :account_id, :type => :string, :aliases => "-a", :desc => "The UUID of the account this key pair belongs to"
35
+ method_option :name, :type => :string, :aliases => "-n", :desc => "The name for this key pair"
36
+ def modify(uuid)
37
+ UnknownUUIDError.raise(options[:account_id]) if options[:account_id] && M::Account[options[:account_id]].nil?
38
+ super(M::SshKeyPair,uuid,options)
39
+ end
40
+
41
+ desc "del UUID", "Delete an existing keypair"
42
+ def del(uuid)
43
+ super(M::SshKeyPair,uuid)
44
+ end
45
+
46
+ desc "show [UUID] [options]", "Show network(s)"
47
+ def show(uuid=nil)
48
+ if uuid
49
+ keypair = M::SshKeyPair[uuid] || UnknownUUIDError.raise(uuid)
50
+ puts ERB.new(<<__END, nil, '-').result(binding)
51
+ Keypair UUID:
52
+ <%= keypair.canonical_uuid %>
53
+ Account id:
54
+ <%= keypair.account_id %>
55
+ Name:
56
+ <%= keypair.name%>
57
+ Finger print:
58
+ <%= keypair.finger_print %>
59
+ Public Key:
60
+ <%= keypair.public_key%>
61
+ __END
62
+ else
63
+ puts ERB.new(<<__END, nil, '-').result(binding)
64
+ <%- M::SshKeyPair.each { |row| -%>
65
+ <%= row.canonical_uuid %>\t<%= row.account_id %>\t<%= row.name %>\t<%= row.finger_print %>
66
+ <%- } -%>
67
+ __END
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,198 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'ipaddress'
4
+
5
+ module Dcmgr::Cli
6
+ class Network < Base
7
+ namespace :network
8
+ M=Dcmgr::Models
9
+
10
+ desc "add [options]", "Register a new network entry"
11
+ method_option :uuid, :type => :string, :aliases => "-u", :desc => "UUID of the network"
12
+ method_option :ipv4_gw, :type => :string, :aliases => "-g", :required => true, :desc => "Gateway address for IPv4 network"
13
+ method_option :prefix, :type => :numeric, :default=>24, :aliases => "-p", :desc => "IP network mask size (1 < prefix < 32)"
14
+ method_option :domain, :type => :string, :aliases => "-m", :desc => "DNS domain name of the network"
15
+ method_option :dns, :type => :string, :aliases => "-n", :desc => "IP address for DNS server of the network"
16
+ method_option :dhcp, :type => :string, :aliases => "-c", :desc => "IP address for DHCP server of the network"
17
+ method_option :metadata, :type => :string, :aliases => "-t", :desc => "IP address for metadata server of the network"
18
+ method_option :metadata_port, :type => :string, :aliases => "--tp", :desc => "Port for the metadata server of the network"
19
+ method_option :bandwidth, :type => :numeric, :aliases => "-b", :desc => "The maximum bandwidth for the network in Mbit/s"
20
+ method_option :vlan_id, :type => :numeric, :default=>0, :aliases => "-l", :desc => "Tag VLAN (802.1Q) ID of the network. 0 is for no VLAN network"
21
+ method_option :description, :type => :string, :aliases => "-d", :desc => "Description for the network"
22
+ method_option :account_id, :type => :string, :default=>'a-shpool', :aliases => "-a", :desc => "The account ID to own this"
23
+ def add
24
+ vlan_pk = if options[:vlan_id].to_i > 0
25
+ vlan = M::VlanLease.find(:tag_id=>options[:vlan_id]) || Error.raise("Invalid or Unknown VLAN ID: #{options[:vlan_id]}", 100)
26
+ vlan.id
27
+ else
28
+ 0
29
+ end
30
+
31
+ fields = {
32
+ :ipv4_gw => options[:ipv4_gw],
33
+ :prefix => options[:prefix],
34
+ :dns_server => options[:dns],
35
+ :domain_name => options[:domain],
36
+ :dhcp_server => options[:dhcp],
37
+ :metadata_server => options[:metadata],
38
+ :metadata_server_port => options[:metadata_port],
39
+ :description => options[:description],
40
+ :account_id => options[:account_id],
41
+ :bandwidth => options[:bandwidth],
42
+ :vlan_lease_id => vlan_pk,
43
+ }
44
+ fields.merge!({:uuid => options[:uuid]}) unless options[:uuid].nil?
45
+
46
+ puts super(M::Network,fields)
47
+ end
48
+
49
+ desc "del UUID", "Deregister a network entry"
50
+ def del(uuid)
51
+ super(M::Network,uuid)
52
+ end
53
+
54
+ desc "modify UUID [options]", "Update network information"
55
+ method_option :ipv4_gw, :type => :string, :aliases => "-g", :desc => "Gateway address for IPv4 network"
56
+ method_option :prefix, :type => :numeric, :aliases => "-p", :desc => "IP network mask size (1 < prefix < 32)"
57
+ method_option :domain, :type => :string, :aliases => "-m", :desc => "DNS domain name of the network"
58
+ method_option :dns, :type => :string, :aliases => "-n", :desc => "IP address for DNS server of the network"
59
+ method_option :dhcp, :type => :string, :aliases => "-c", :desc => "IP address for DHCP server of the network"
60
+ method_option :metadata, :type => :string, :aliases => "-t", :desc => "IP address for metadata server of the network"
61
+ method_option :metadata_port, :type => :string, :aliases => "--tp", :desc => "Port for the metadata server of the network"
62
+ method_option :vlan_id, :type => :numeric, :aliases => "-l", :desc => "Tag VLAN (802.1Q) ID of the network. 0 is for no VLAN network"
63
+ method_option :bandwidth, :type => :numeric, :aliases => "-b", :desc => "The maximum bandwidth for the network in Mbit/s"
64
+ method_option :description, :type => :string, :aliases => "-d", :desc => "Description for the network"
65
+ method_option :account_id, :type => :string, :aliases => "-a", :desc => "The account ID to own this"
66
+ def modify(uuid)
67
+ vlan_pk = if options[:vlan_id].to_i > 0
68
+ vlan = M::VlanLease.find(:tag_id=>options[:vlan_id]) || Error.raise("Invalid or Unknown VLAN ID: #{options[:vlan_id]}", 100)
69
+ vlan.id
70
+ else
71
+ 0
72
+ end
73
+
74
+ fields = {
75
+ :ipv4_gw => options[:ipv4_gw],
76
+ :prefix => options[:prefix],
77
+ :dns_server => options[:dns],
78
+ :domain_name => options[:domain],
79
+ :dhcp_server => options[:dhcp],
80
+ :metadata_server => options[:metadata],
81
+ :metadata_server_port => options[:metadata_port],
82
+ :description => options[:description],
83
+ :account_id => options[:account_id],
84
+ :bandwidth => options[:bandwidth],
85
+ :vlan_lease_id => vlan_pk,
86
+ }
87
+ super(M::Network,uuid,fields)
88
+ end
89
+
90
+ desc "nat UUID [options]", "Set or clear nat mapping for a network"
91
+ method_option :outside_network_id, :type => :string, :aliases => "-o", :desc => "The network that this network will be natted to"
92
+ method_option :clear, :type => :boolean, :aliases => "-c", :desc => "Clears a previously natted network"
93
+ def nat(uuid)
94
+ in_nw = M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}", 100)
95
+ ex_nw = M::Network[options[:outside_network_id]] || Error.raise("Unknown network UUID: #{uuid}", 100) unless options[:outside_network_id].nil?
96
+
97
+ if options[:clear] then
98
+ in_nw.set_only({:nat_network_id => nil},:nat_network_id)
99
+ in_nw.save_changes
100
+ else
101
+ in_nw.set_only({:nat_network_id => ex_nw.id},:nat_network_id)
102
+ in_nw.save_changes
103
+ end
104
+ end
105
+
106
+ desc "show [UUID] [options]", "Show network(s)"
107
+ method_option :vlan_id, :type => :numeric, :aliases => "-l", :desc => "Show networks in the VLAN ID"
108
+ method_option :account_id, :type => :string, :aliases => "-a", :desc => "Show networks with the account"
109
+ def show(uuid=nil)
110
+ if uuid
111
+ nw = M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}", 100)
112
+ puts ERB.new(<<__END, nil, '-').result(binding)
113
+ Network UUID:
114
+ <%= nw.canonical_uuid %>
115
+ Tag VLAN:
116
+ <%= nw.vlan_lease_id == 0 ? 'none' : nw.vlan_lease.tag_id %>
117
+ IPv4:
118
+ Network address: <%= nw.ipaddress.network %>/<%= nw.prefix %>
119
+ Gateway address: <%= nw.ipv4_gw %>
120
+ <%- if nw.nat_network_id -%>
121
+ Outside NAT network address: <%= nw.nat_network.ipaddress.network %>/<%= nw.nat_network.prefix %> (<%= nw.nat_network.canonical_uuid %>)
122
+ <%- end -%>
123
+ DHCP Information:
124
+ DHCP Server: <%= nw.dhcp_server %>
125
+ DNS Server: <%= nw.dns_server %>
126
+ <%- if nw.metadata_server -%>
127
+ Metadata Server: <%= nw.metadata_server %>
128
+ <%- end -%>
129
+ Bandwidth:
130
+ <%- if nw.bandwidth.nil? -%>
131
+ unlimited
132
+ <%- else -%>
133
+ <%= nw.bandwidth %> Mbit/s
134
+ <%- end -%>
135
+ <%- if nw.description -%>
136
+ Description:
137
+ <%= nw.description %>
138
+ <%- end -%>
139
+ __END
140
+ else
141
+ cond = {}
142
+ cond[:account_id]= options[:account_id] if options[:account_id]
143
+ if options[:vlan_id]
144
+ vlan = M::VlanLease.find(:tag_id=>options[:vlan_id]) || abort("Unknown Tag VLAN ID: #{options[:vlan_id]}")
145
+ cond[:vlan_lease_id] = vlan.id
146
+ end
147
+
148
+ nw = M::Network.filter(cond).all
149
+ puts ERB.new(<<__END, nil, '-').result(binding)
150
+ <%- nw.each { |row| -%>
151
+ <%= row.canonical_uuid %>\t<%= row.ipaddress.network %>/<%= row.prefix %>\t<%= (row.vlan_lease && row.vlan_lease.tag_id) %>
152
+ <%- } -%>
153
+ __END
154
+ end
155
+ end
156
+
157
+ desc "leases UUID", "Show IPs used in the network"
158
+ def leases(uuid)
159
+ nw = M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}", 100)
160
+
161
+ print ERB.new(<<__END, nil, '-').result(binding)
162
+ <%- nw.ip_lease_dataset.order(:ipv4).all.each { |l| -%>
163
+ <%= "%-20s %-15s" % [l.ipv4, M::IpLease::TYPE_MESSAGES[l.alloc_type]] %>
164
+ <%- } -%>
165
+ __END
166
+ end
167
+
168
+ desc "reserve UUID", "Add reserved IP to the network"
169
+ method_option :ipv4, :type => :string, :aliases => "-i", :required => true, :desc => "The ip address to reserve"
170
+ def reserve(uuid)
171
+ nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)
172
+
173
+ if nw.ipaddress.include?(IPAddress(options[:ipv4]))
174
+ nw.ip_lease_dataset.add_reserved(options[:ipv4])
175
+ else
176
+ Error.raise("IP address is out of range: #{options[:ipv4]} => #{nw.ipaddress.network}/#{nw.ipaddress.prefix}",100)
177
+ end
178
+ end
179
+
180
+ desc "release UUID", "Release a reserved IP from the network"
181
+ method_option :ipv4, :type => :string, :aliases => "-i", :required => true, :desc => "The ip address to release"
182
+ def release(uuid)
183
+ nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)
184
+
185
+ if nw.ip_lease_dataset.filter(:ipv4=>options[:ipv4]).delete == 0
186
+ Error.raise("The IP is not reserved in network #{uuid}: #{options[:ipv4]}", 100)
187
+ end
188
+ end
189
+
190
+ no_tasks {
191
+ private
192
+ def find_network(uuid)
193
+ M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}")
194
+ end
195
+ }
196
+
197
+ end
198
+ end