wakame-vdc-agents 11.06.0 → 11.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/Rakefile +19 -31
  2. data/bin/hva +15 -5
  3. data/bin/nsa +15 -5
  4. data/bin/sta +9 -222
  5. data/config/db/migrations/0001_v1110_origin.rb +446 -0
  6. data/config/hva.conf.example +19 -11
  7. data/config/nsa.conf.example +1 -1
  8. data/lib/dcmgr.rb +99 -22
  9. data/lib/dcmgr/cli/base.rb +34 -1
  10. data/lib/dcmgr/cli/host.rb +24 -20
  11. data/lib/dcmgr/cli/image.rb +38 -19
  12. data/lib/dcmgr/cli/keypair.rb +16 -12
  13. data/lib/dcmgr/cli/network.rb +189 -81
  14. data/lib/dcmgr/cli/quota.rb +2 -2
  15. data/lib/dcmgr/cli/security_group.rb +106 -0
  16. data/lib/dcmgr/cli/spec.rb +144 -39
  17. data/lib/dcmgr/cli/storage.rb +16 -15
  18. data/lib/dcmgr/cli/tag.rb +20 -14
  19. data/lib/dcmgr/cli/vlan.rb +5 -5
  20. data/lib/dcmgr/drivers/backing_store.rb +32 -0
  21. data/lib/dcmgr/drivers/comstar.rb +81 -0
  22. data/lib/dcmgr/drivers/iijgio_storage.rb +9 -19
  23. data/lib/dcmgr/drivers/iscsi_target.rb +41 -0
  24. data/lib/dcmgr/drivers/kvm.rb +161 -28
  25. data/lib/dcmgr/drivers/linux_iscsi.rb +60 -0
  26. data/lib/dcmgr/drivers/local_storage.rb +24 -0
  27. data/lib/dcmgr/drivers/lxc.rb +167 -125
  28. data/lib/dcmgr/drivers/raw.rb +74 -0
  29. data/lib/dcmgr/drivers/s3_storage.rb +7 -19
  30. data/lib/dcmgr/drivers/snapshot_storage.rb +18 -28
  31. data/lib/dcmgr/drivers/storage_initiator.rb +28 -0
  32. data/lib/dcmgr/drivers/sun_iscsi.rb +32 -0
  33. data/lib/dcmgr/drivers/zfs.rb +77 -0
  34. data/lib/dcmgr/endpoints/core_api.rb +315 -263
  35. data/lib/dcmgr/endpoints/errors.rb +21 -10
  36. data/lib/dcmgr/endpoints/metadata.rb +360 -23
  37. data/lib/dcmgr/helpers/cli_helper.rb +6 -3
  38. data/lib/dcmgr/helpers/ec2_metadata_helper.rb +9 -0
  39. data/lib/dcmgr/helpers/nic_helper.rb +11 -0
  40. data/lib/dcmgr/helpers/snapshot_storage_helper.rb +34 -0
  41. data/lib/dcmgr/models/account.rb +0 -6
  42. data/lib/dcmgr/models/account_resource.rb +0 -4
  43. data/lib/dcmgr/models/base_new.rb +14 -2
  44. data/lib/dcmgr/models/dhcp_range.rb +38 -0
  45. data/lib/dcmgr/models/frontend_system.rb +0 -6
  46. data/lib/dcmgr/models/history.rb +0 -11
  47. data/lib/dcmgr/models/host_node.rb +131 -0
  48. data/lib/dcmgr/models/hostname_lease.rb +0 -8
  49. data/lib/dcmgr/models/image.rb +31 -18
  50. data/lib/dcmgr/models/instance.rb +137 -143
  51. data/lib/dcmgr/models/instance_nic.rb +52 -29
  52. data/lib/dcmgr/models/instance_security_group.rb +9 -0
  53. data/lib/dcmgr/models/instance_spec.rb +163 -31
  54. data/lib/dcmgr/models/ip_lease.rb +10 -21
  55. data/lib/dcmgr/models/mac_lease.rb +30 -11
  56. data/lib/dcmgr/models/network.rb +148 -27
  57. data/lib/dcmgr/models/physical_network.rb +18 -0
  58. data/lib/dcmgr/models/quota.rb +0 -10
  59. data/lib/dcmgr/models/request_log.rb +3 -18
  60. data/lib/dcmgr/models/security_group.rb +66 -0
  61. data/lib/dcmgr/models/security_group_rule.rb +145 -0
  62. data/lib/dcmgr/models/ssh_key_pair.rb +16 -19
  63. data/lib/dcmgr/models/{storage_pool.rb → storage_node.rb} +35 -25
  64. data/lib/dcmgr/models/tag.rb +0 -14
  65. data/lib/dcmgr/models/tag_mapping.rb +1 -7
  66. data/lib/dcmgr/models/vlan_lease.rb +2 -8
  67. data/lib/dcmgr/models/volume.rb +49 -37
  68. data/lib/dcmgr/models/volume_snapshot.rb +15 -17
  69. data/lib/dcmgr/node_modules/hva_collector.rb +69 -28
  70. data/lib/dcmgr/node_modules/instance_ha.rb +23 -12
  71. data/lib/dcmgr/node_modules/instance_monitor.rb +16 -2
  72. data/lib/dcmgr/node_modules/openflow_controller.rb +784 -0
  73. data/lib/dcmgr/node_modules/scheduler.rb +189 -0
  74. data/lib/dcmgr/node_modules/service_netfilter.rb +452 -227
  75. data/lib/dcmgr/node_modules/service_openflow.rb +731 -0
  76. data/lib/dcmgr/node_modules/sta_collector.rb +20 -0
  77. data/lib/dcmgr/node_modules/sta_tgt_initializer.rb +35 -0
  78. data/lib/dcmgr/rack/request_logger.rb +11 -6
  79. data/lib/dcmgr/rpc/hva_handler.rb +256 -110
  80. data/lib/dcmgr/rpc/sta_handler.rb +244 -0
  81. data/lib/dcmgr/scheduler.rb +122 -8
  82. data/lib/dcmgr/scheduler/host_node/exclude_same.rb +24 -0
  83. data/lib/dcmgr/scheduler/host_node/find_first.rb +12 -0
  84. data/lib/dcmgr/scheduler/host_node/least_usage.rb +28 -0
  85. data/lib/dcmgr/scheduler/host_node/per_instance.rb +18 -0
  86. data/lib/dcmgr/scheduler/host_node/specify_node.rb +26 -0
  87. data/lib/dcmgr/scheduler/network/flat_single.rb +23 -0
  88. data/lib/dcmgr/scheduler/network/nat_one_to_one.rb +23 -0
  89. data/lib/dcmgr/scheduler/network/per_instance.rb +39 -0
  90. data/lib/dcmgr/scheduler/network/vif_template.rb +19 -0
  91. data/lib/dcmgr/scheduler/storage_node/find_first.rb +13 -0
  92. data/lib/dcmgr/scheduler/storage_node/least_usage.rb +23 -0
  93. data/lib/dcmgr/storage_service.rb +39 -40
  94. data/lib/dcmgr/tags.rb +3 -3
  95. data/lib/dcmgr/version.rb +1 -1
  96. data/lib/dcmgr/vnet.rb +105 -0
  97. data/lib/dcmgr/vnet/factories.rb +141 -0
  98. data/lib/dcmgr/vnet/isolators/by_securitygroup.rb +21 -0
  99. data/lib/dcmgr/vnet/isolators/dummy.rb +17 -0
  100. data/lib/dcmgr/vnet/netfilter/cache.rb +51 -0
  101. data/lib/dcmgr/vnet/netfilter/chain.rb +66 -0
  102. data/lib/dcmgr/vnet/netfilter/controller.rb +193 -0
  103. data/lib/dcmgr/vnet/netfilter/ebtables_rule.rb +53 -0
  104. data/lib/dcmgr/vnet/netfilter/iptables_rule.rb +45 -0
  105. data/lib/dcmgr/vnet/netfilter/task_manager.rb +459 -0
  106. data/lib/dcmgr/vnet/tasks/accept_all_dns.rb +19 -0
  107. data/lib/dcmgr/vnet/tasks/accept_arp_broadcast.rb +24 -0
  108. data/lib/dcmgr/vnet/tasks/accept_arp_from_friends.rb +34 -0
  109. data/lib/dcmgr/vnet/tasks/accept_arp_from_gateway.rb +21 -0
  110. data/lib/dcmgr/vnet/tasks/accept_arp_to_host.rb +30 -0
  111. data/lib/dcmgr/vnet/tasks/accept_ip_from_friends.rb +26 -0
  112. data/lib/dcmgr/vnet/tasks/accept_ip_from_gateway.rb +23 -0
  113. data/lib/dcmgr/vnet/tasks/accept_ip_to_anywhere.rb +18 -0
  114. data/lib/dcmgr/vnet/tasks/accept_related_established.rb +45 -0
  115. data/lib/dcmgr/vnet/tasks/accept_wakame_dhcp_only.rb +33 -0
  116. data/lib/dcmgr/vnet/tasks/accept_wakame_dns_only.rb +33 -0
  117. data/lib/dcmgr/vnet/tasks/debug_iptables.rb +21 -0
  118. data/lib/dcmgr/vnet/tasks/drop_arp_forwarding.rb +27 -0
  119. data/lib/dcmgr/vnet/tasks/drop_arp_to_host.rb +24 -0
  120. data/lib/dcmgr/vnet/tasks/drop_ip_from_anywhere.rb +18 -0
  121. data/lib/dcmgr/vnet/tasks/drop_ip_spoofing.rb +34 -0
  122. data/lib/dcmgr/vnet/tasks/drop_mac_spoofing.rb +33 -0
  123. data/lib/dcmgr/vnet/tasks/exclude_from_nat.rb +47 -0
  124. data/lib/dcmgr/vnet/tasks/security_group.rb +37 -0
  125. data/lib/dcmgr/vnet/tasks/static_nat.rb +54 -0
  126. data/lib/dcmgr/vnet/tasks/translate_metadata_address.rb +32 -0
  127. metadata +105 -68
  128. data/lib/dcmgr/cli/group.rb +0 -101
  129. data/lib/dcmgr/endpoints/core_api_mock.rb +0 -865
  130. data/lib/dcmgr/models/host_pool.rb +0 -122
  131. data/lib/dcmgr/models/instance_netfilter_group.rb +0 -16
  132. data/lib/dcmgr/models/netfilter_group.rb +0 -89
  133. data/lib/dcmgr/models/netfilter_rule.rb +0 -21
  134. data/lib/dcmgr/scheduler/find_last.rb +0 -16
  135. data/lib/dcmgr/scheduler/find_random.rb +0 -16
  136. data/lib/dcmgr/stm/instance.rb +0 -25
  137. data/lib/dcmgr/stm/snapshot_context.rb +0 -33
  138. data/lib/dcmgr/stm/volume_context.rb +0 -65
@@ -11,6 +11,15 @@ module Dcmgr
11
11
  self.const_set(class_name.to_sym, c)
12
12
  end
13
13
 
14
+ def self.deprecated_error(class_name, status_code, error_code, &blk)
15
+ c = Class.new(DeprecatedAPIError)
16
+ c.status_code(status_code)
17
+ c.error_code(error_code)
18
+ c.instance_eval(&blk) if blk
19
+ self.set_error_code_type(error_code, c)
20
+ self.const_set(class_name.to_sym, c)
21
+ end
22
+
14
23
  @error_code_map = {}
15
24
  def self.set_error_code_type(error_code, klass)
16
25
  raise TypeError unless klass < APIError
@@ -51,6 +60,9 @@ module Dcmgr
51
60
  end
52
61
  end
53
62
 
63
+ class DeprecatedAPIError < APIError
64
+ end
65
+
54
66
  define_error(:UnknownUUIDResource, 404, '100')
55
67
  define_error(:UnknownMember, 400, '101')
56
68
  define_error(:InvalidCredentialHeaders, 400, '102')
@@ -58,14 +70,14 @@ module Dcmgr
58
70
  define_error(:DisabledAccount, 403, '104')
59
71
  define_error(:OperationNotPermitted, 403, '105')
60
72
  define_error(:UndefinedVolumeSize, 400, '106')
61
- define_error(:StoragePoolNotPermitted, 403, '107')
62
- define_error(:UnknownStoragePool, 404, '108')
73
+ define_error(:StorageNodeNotPermitted, 403, '107')
74
+ define_error(:UnknownStorageNode, 404, '108')
63
75
  define_error(:OutOfDiskSpace, 400, '109')
64
76
  define_error(:DatabaseError, 400, '110')
65
77
  define_error(:UndefinedVolumeID, 400, '111')
66
78
  define_error(:InvalidDeleteRequest, 400, '112')
67
79
  define_error(:UnknownVolume, 404, '113')
68
- define_error(:UnknownHostPool, 404, '114')
80
+ define_error(:UnknownHostNode, 404, '114')
69
81
  define_error(:UnknownInstance, 404, '115')
70
82
  define_error(:UndefindVolumeSnapshotID, 400, '116')
71
83
  define_error(:UnknownVolumeSnapshot, 404, '117')
@@ -73,7 +85,7 @@ module Dcmgr
73
85
  define_error(:InvalidVolumeSize, 400, '119')
74
86
  define_error(:OutOfHostCapacity, 400, '120')
75
87
  define_error(:UnknownSshKeyPair, 404, '121')
76
- define_error(:UndefinedStoragePoolID, 400, '122')
88
+ define_error(:UndefinedStorageNodeID, 400, '122')
77
89
  define_error(:DetachVolumeFailure, 400, '123')
78
90
  define_error(:AttachVolumeFailure, 400, '124')
79
91
  define_error(:InvalidInstanceState, 400, '125')
@@ -84,17 +96,16 @@ module Dcmgr
84
96
  define_error(:OutOfNetworkCapacity, 400, '130')
85
97
  define_error(:InvalidVolumeSnapshotState, 400, '131')
86
98
 
87
-
88
- # netfilter_group
89
- define_error(:UndefinedNetfilterGroup, 400, '132')
90
- define_error(:UnknownNetfilterGroup, 400, '133')
91
- define_error(:NetfilterGroupNotPermitted, 400, '134')
92
- define_error(:DuplicatedNetfilterGroup, 400, '135')
99
+ define_error(:UndefinedSecurityGroup, 400, '132')
100
+ define_error(:UnknownSecurityGroup, 400, '133')
101
+ define_error(:SecurityGroupNotPermitted, 400, '134')
102
+ deprecated_error(:DuplicatedSecurityGroup, 400, '135')
93
103
 
94
104
  define_error(:DuplicateSshKeyName, 400, '136')
95
105
  define_error(:InvalidImageID, 400, '137')
96
106
  define_error(:InvalidInstanceSpec, 400, '138')
97
107
  define_error(:UndefinedInstanceID, 404, '139')
98
108
  define_error(:InvalidVolumeState, 400, '140')
109
+ define_error(:InvalidHostNodeID, 400, '141')
99
110
  end
100
111
  end
@@ -5,19 +5,24 @@ require 'sinatra/base'
5
5
  require 'sinatra/sequel_transaction'
6
6
  require 'yaml'
7
7
  require 'json'
8
+ require 'ipaddress'
8
9
 
9
10
  require 'dcmgr'
10
11
 
11
12
  # Metadata service endpoint for running VMs.
12
13
  # The running VM can not identify itself that who or where i am. The service supplies these information from somewhere
13
- # out of the VM. It publishes some very crucial information to each VM so that the access control to this service is
14
+ # out of the VM. It publishes some very crucial information to each VM so that the access control to this service is
14
15
  # mandated at both levels, the network and the application itself.
15
- #
16
- # The concept of the service is similar with Amazon EC2's Metadata service given via http://169.254.169.254/. The
16
+ #
17
+ # The concept of the service is similar with Amazon EC2's Metadata service given via http://169.254.169.254/. The
17
18
  # difference is the URI structure. This gives the single point URI as per below:
18
19
  # http://metadata.server/[version]/meatadata.[format]
19
- # It will return a document which results in a syntax specified in the last extension field. The document contains
20
+ # It will return a document which results in a syntax specified in the last extension field. The document contains
20
21
  # over all information that the VM needs for self recoginition.
22
+ #
23
+ # see also
24
+ # http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instancedata-data-categories.html
25
+
21
26
  module Dcmgr
22
27
  module Endpoints
23
28
  class Metadata < Sinatra::Base
@@ -28,7 +33,7 @@ module Dcmgr
28
33
  disable :show_exceptions
29
34
 
30
35
  LATEST_PROVIDER_VER_ID='2011-05-19'
31
-
36
+
32
37
  get '/' do
33
38
  ''
34
39
  end
@@ -41,7 +46,7 @@ module Dcmgr
41
46
  #get %r!\A/(\d{4}-\d{2}-\d{2})/metadata.(\w+)\Z! do
42
47
  v = parse_version params[:version]
43
48
  ext = params[:splat][0]
44
-
49
+
45
50
  hash_doc = begin
46
51
  self.class.find_const("Provider_#{v}").new.document(request.ip)
47
52
  rescue NameError => e
@@ -68,9 +73,9 @@ module Dcmgr
68
73
  private
69
74
  def get_data(params)
70
75
  v = parse_version params[:version]
71
-
76
+
72
77
  get_method = params[:data].gsub(/-/,'_')
73
-
78
+
74
79
  provider = begin
75
80
  self.class.find_const("Provider_#{v}").new
76
81
  rescue NameError => e
@@ -93,7 +98,7 @@ module Dcmgr
93
98
 
94
99
  result
95
100
  end
96
-
101
+
97
102
  def parse_version(v)
98
103
  ret = case v
99
104
  when 'latest'
@@ -101,11 +106,11 @@ module Dcmgr
101
106
  when /\A\d{4}-\d{2}-\d{2}\Z/
102
107
  v
103
108
  else
104
- raise "Invalid syntax in the version"
109
+ raise "Invalid syntax in the version"
105
110
  end
106
111
  ret.gsub(/-/, '')
107
112
  end
108
-
113
+
109
114
  def shell_dump(hash)
110
115
  # TODO: values to be shell escaped
111
116
  hash.map {|k,v|
@@ -155,7 +160,7 @@ module Dcmgr
155
160
  }
156
161
  ret
157
162
  end
158
-
163
+
159
164
  def get_instance_from_ip(src_ip)
160
165
  ip = Models::IpLease.find(:ipv4=>src_ip)
161
166
  if ip.nil? || ip.instance_nic.nil?
@@ -208,13 +213,13 @@ module Dcmgr
208
213
  get_instance_from_ip(src_ip).image.cuuid
209
214
  end
210
215
  alias ami_id wmi_id
211
-
216
+
212
217
  def mac(src_ip)
213
218
  get_instance_from_ip(src_ip).nic.map { |nic|
214
219
  nic.pretty_mac_addr
215
220
  }.join("\n")
216
221
  end
217
-
222
+
218
223
  def network(src_ip)
219
224
  get_instance_from_ip(src_ip).nic.map { |nic|
220
225
  nic.ip.map { |ip|
@@ -222,15 +227,15 @@ module Dcmgr
222
227
  }
223
228
  }.join("\n")
224
229
  end
225
-
230
+
226
231
  def instance_id(src_ip)
227
232
  get_instance_from_ip(src_ip).cuuid
228
233
  end
229
-
234
+
230
235
  def local_hostname(src_ip)
231
236
  get_instance_from_ip(src_ip).hostname
232
237
  end
233
-
238
+
234
239
  def local_ipv4(src_ip)
235
240
  get_instance_from_ip(src_ip).nic.map { |nic|
236
241
  nic.ip.map { |ip|
@@ -242,7 +247,7 @@ module Dcmgr
242
247
  }.compact
243
248
  }.join("\n")
244
249
  end
245
-
250
+
246
251
  def public_ipv4(src_ip)
247
252
  get_instance_from_ip(src_ip).nic.map { |nic|
248
253
  nic.ip.map { |ip|
@@ -254,23 +259,355 @@ module Dcmgr
254
259
  }.compact
255
260
  }.join("\n")
256
261
  end
257
-
262
+
258
263
  def public_keys(src_ip)
259
264
  i = get_instance_from_ip(src_ip)
260
265
  # ssh_key_data is possible to be nil.
261
266
  i.ssh_key_data.nil? ? '' : i.ssh_key_data[:public_key]
262
267
  end
263
-
268
+
264
269
  def security_groups(src_ip)
265
- get_instance_from_ip(src_ip).netfilter_groups.map { |grp|
266
- grp.name
270
+ get_instance_from_ip(src_ip).security_groups.map { |grp|
271
+ grp.canonical_uuid
267
272
  }.join("\n")
268
273
  end
269
-
274
+
270
275
  def user_data(src_ip)
271
276
  get_instance_from_ip(src_ip).user_data
272
277
  end
273
278
  end
274
279
  end
280
+
281
+ class Ec2Metadata < Sinatra::Base
282
+ include Dcmgr::Logger
283
+ register Sinatra::SequelTransaction
284
+
285
+ disable :sessions
286
+ disable :show_exceptions
287
+
288
+ API_VERSIONS = ['latest', '2011-01-01']
289
+ TOP_LEVEL_ITEMS = ['meta-data', 'user-data' ]
290
+ TOP_LEVEL_METADATA_ITEMS = [
291
+ 'ami-id',
292
+ 'ami-launch-index',
293
+ 'ami-manifest-path',
294
+ 'ancestor-ami-ids',
295
+ 'block-device-mapping/',
296
+ 'hostname',
297
+ 'instance-action',
298
+ 'instance-id',
299
+ 'instance-type',
300
+ 'kernel-id',
301
+ 'local-hostname',
302
+ 'local-ipv4',
303
+ 'mac',
304
+ 'network/',
305
+ 'placement/',
306
+ 'product-codes',
307
+ 'public-hostname',
308
+ 'public-ipv4',
309
+ 'public-keys/',
310
+ 'ramdisk-id',
311
+ 'reservation-id',
312
+ 'security-groups',
313
+ ]
314
+
315
+ get '/' do
316
+ API_VERSIONS.join("\n")
317
+ end
318
+
319
+ get '/:version' do
320
+ ''
321
+ end
322
+
323
+ get '/:version/' do
324
+ TOP_LEVEL_ITEMS.join("\n")
325
+ end
326
+
327
+ get '/:version/user-data' do
328
+ instance[:user_data]
329
+ end
330
+
331
+ get '/:version/meta-data/' do
332
+ TOP_LEVEL_METADATA_ITEMS.join("\n")
333
+ end
334
+
335
+ get '/:version/meta-data/ami-id' do
336
+ instance[:image][:uuid]
337
+ end
338
+
339
+ get '/:version/meta-data/ami-launch-index' do
340
+ # TODO
341
+ '0'
342
+ end
343
+
344
+ get '/:version/meta-data/ami-manifest-path' do
345
+ # TODO
346
+ ''
347
+ end
348
+
349
+ get '/:version/meta-data/ancestor-ami-ids' do
350
+ # TODO
351
+ ''
352
+ end
353
+
354
+ get '/:version/meta-data/block-device-mapping/' do
355
+ # TODO
356
+ 'root'
357
+ end
358
+
359
+ get '/:version/meta-data/block-device-mapping/root' do
360
+ # TODO
361
+ '/dev/sda'
362
+ end
363
+
364
+ get '/:version/meta-data/hostname' do
365
+ instance[:hostname]
366
+ end
367
+
368
+ get '/:version/meta-data/instance-action' do
369
+ instance[:state]
370
+ end
371
+
372
+ get '/:version/meta-data/instance-id' do
373
+ instance[:uuid]
374
+ end
375
+
376
+ get '/:version/meta-data/instance-type' do
377
+ instance[:instance_spec][:uuid]
378
+ end
379
+
380
+ get '/:version/meta-data/kernel-id' do
381
+ # TODO
382
+ ''
383
+ end
384
+
385
+ get '/:version/meta-data/local-hostname' do
386
+ instance[:hostname]
387
+ end
388
+
389
+ get '/:version/meta-data/local-ipv4' do
390
+ instance[:ips].first
391
+ end
392
+
393
+ get '/:version/meta-data/mac' do
394
+ vnic = instance[:instance_nics].first || {}
395
+ vnic[:mac_addr].unpack('A2'*6).join(':')
396
+ end
397
+
398
+ get '/:version/meta-data/network/' do
399
+ 'interfaces/'
400
+ end
401
+
402
+ get '/:version/meta-data/network/interfaces/' do
403
+ 'macs/'
404
+ end
405
+
406
+ get '/:version/meta-data/network/interfaces/macs/' do
407
+ instance[:vif].map { |vnic|
408
+ "#{vnic[:mac_addr].unpack('A2'*6).join(':')}/"
409
+ }.join("\n")
410
+ end
411
+
412
+ get '/:version/meta-data/network/interfaces/macs/:mac/' do
413
+ if vnic_mac?(params[:mac])
414
+ [
415
+ 'local-hostname',
416
+ 'local-ipv4s',
417
+ 'mac',
418
+ 'public-hostname',
419
+ 'public-ipv4s',
420
+ 'security-groups',
421
+ # wakame-vdc extention items.
422
+ 'x-gateway',
423
+ 'x-netmask',
424
+ 'x-network',
425
+ 'x-broadcast',
426
+ 'x-metric',
427
+ ].join("\n")
428
+ else
429
+ # TODO
430
+ ''
431
+ end
432
+ end
433
+
434
+ get '/:version/meta-data/network/interfaces/macs/:mac/local-hostname' do
435
+ if vnic_mac?(params[:mac])
436
+ instance[:hostname]
437
+ else
438
+ ''
439
+ end
440
+ end
441
+
442
+ get '/:version/meta-data/network/interfaces/macs/:mac/local-ipv4s' do
443
+ if vnic_mac?(params[:mac])
444
+ vnic = vnic(params[:mac])
445
+ vnic[:ipv4][:address]
446
+ else
447
+ ''
448
+ end
449
+ end
450
+
451
+ get '/:version/meta-data/network/interfaces/macs/:mac/mac' do
452
+ if vnic_mac?(params[:mac])
453
+ params[:mac]
454
+ else
455
+ ''
456
+ end
457
+ end
458
+
459
+ get '/:version/meta-data/network/interfaces/macs/:mac/public-hostname' do
460
+ if vnic_mac?(params[:mac])
461
+ instance[:hostname]
462
+ else
463
+ # TODO
464
+ ''
465
+ end
466
+ end
467
+
468
+ get '/:version/meta-data/network/interfaces/macs/:mac/public-ipv4s' do
469
+ if vnic_mac?(params[:mac])
470
+ vnic = vnic(params[:mac])
471
+ vnic[:ipv4][:nat_address]
472
+ else
473
+ ''
474
+ end
475
+ end
476
+
477
+ get '/:version/meta-data/network/interfaces/macs/:mac/security-groups' do
478
+ if vnic_mac?(params[:mac])
479
+ instance[:security_groups].join("\n")
480
+ else
481
+ # TODO
482
+ ''
483
+ end
484
+ end
485
+
486
+ get '/:version/meta-data/network/interfaces/macs/:mac/x-gateway' do
487
+ if vnic_mac?(params[:mac])
488
+ vnic(params[:mac])[:ipv4][:network][:ipv4_gw]
489
+ else
490
+ ''
491
+ end
492
+ end
493
+
494
+ get '/:version/meta-data/network/interfaces/macs/:mac/x-netmask' do
495
+ if vnic_mac?(params[:mac])
496
+ vnic = vnic(params[:mac])
497
+ netaddr = IPAddress::IPv4.new("#{vnic[:ipv4][:network][:ipv4_network]}/#{vnic[:ipv4][:network][:prefix]}")
498
+ netaddr.prefix.to_ip
499
+ else
500
+ ''
501
+ end
502
+ end
503
+
504
+ get '/:version/meta-data/network/interfaces/macs/:mac/x-network' do
505
+ if vnic_mac?(params[:mac])
506
+ vnic = vnic(params[:mac])
507
+ vnic[:ipv4][:network][:ipv4_network]
508
+ else
509
+ ''
510
+ end
511
+ end
512
+
513
+ get '/:version/meta-data/network/interfaces/macs/:mac/x-broadcast' do
514
+ if vnic_mac?(params[:mac])
515
+ vnic = vnic(params[:mac])
516
+ netaddr = IPAddress::IPv4.new("#{vnic[:ipv4][:network][:ipv4_network]}/#{vnic[:ipv4][:network][:prefix]}")
517
+ netaddr.broadcast.to_s
518
+ else
519
+ ''
520
+ end
521
+ end
522
+
523
+ get '/:version/meta-data/network/interfaces/macs/:mac/x-metric' do
524
+ if vnic_mac?(params[:mac])
525
+ vnic = vnic(params[:mac])
526
+ vnic[:ipv4][:network][:metric].to_s
527
+ else
528
+ ''
529
+ end
530
+ end
531
+
532
+ get '/:version/meta-data/placement/' do
533
+ 'availability-zone'
534
+ end
535
+
536
+ get '/:version/meta-data/placement/availability-zone' do
537
+ # TODO
538
+ ''
539
+ end
540
+
541
+ get '/:version/meta-data/product-codes' do
542
+ # TODO
543
+ ''
544
+ end
545
+
546
+ get '/:version/meta-data/public-hostname' do
547
+ # TODO
548
+ instance[:hostname]
549
+ end
550
+
551
+ get '/:version/meta-data/public-ipv4' do
552
+ instance[:nat_ips]
553
+ end
554
+
555
+ get '/:version/meta-data/public-keys/' do
556
+ ssh_key_data = instance[:ssh_key_data]
557
+ ssh_key_data.nil? ? '' : [0, ssh_key_data[:uuid]].join("=")
558
+ end
559
+
560
+ get '/:version/meta-data/public-keys/0/' do
561
+ ssh_key_data = instance[:ssh_key_data]
562
+ ssh_key_data.nil? ? '' : 'openssh-key'
563
+ end
564
+
565
+ get '/:version/meta-data/public-keys/0/openssh-key' do
566
+ ssh_key_data = instance[:ssh_key_data]
567
+ # ssh_key_data is possible to be nil.
568
+ ssh_key_data.nil? ? '' : ssh_key_data[:public_key]
569
+ end
570
+
571
+ get '/:version/meta-data/ramdisk-id' do
572
+ # TODO
573
+ ''
574
+ end
575
+
576
+ get '/:version/meta-data/reservation-id' do
577
+ # TODO
578
+ ''
579
+ end
580
+
581
+ get '/:version/meta-data/security-groups' do
582
+ instance[:security_groups].join("\n")
583
+ end
584
+
585
+ private
586
+ def instance
587
+ ip = Models::IpLease.find(:ipv4 => request.ip)
588
+ if ip.nil? || ip.instance_nic.nil?
589
+ raise UnknownSourceIpError, request.ip
590
+ end
591
+ ip.instance_nic.instance.to_hash
592
+ end
593
+
594
+ def vnic_mac?(mac)
595
+ if vnic(mac).size > 0
596
+ true
597
+ else
598
+ false
599
+ end
600
+ end
601
+
602
+ def vnic(mac)
603
+ instance[:vif].map { |vnic|
604
+ vnic if mac == vnic[:mac_addr].unpack('A2'*6).join(':')
605
+ }.compact.first
606
+ end
607
+
608
+ class UnknownSourceIpError < StandardError; end
609
+
610
+ end
611
+
275
612
  end
276
613
  end