wakame-vdc-dcmgr 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 (136) hide show
  1. data/Rakefile +19 -31
  2. data/bin/collector +6 -1
  3. data/config/db/migrations/0001_v1110_origin.rb +446 -0
  4. data/config/dcmgr.conf.example +51 -0
  5. data/lib/dcmgr.rb +99 -22
  6. data/lib/dcmgr/cli/base.rb +34 -1
  7. data/lib/dcmgr/cli/host.rb +24 -20
  8. data/lib/dcmgr/cli/image.rb +38 -19
  9. data/lib/dcmgr/cli/keypair.rb +16 -12
  10. data/lib/dcmgr/cli/network.rb +189 -81
  11. data/lib/dcmgr/cli/quota.rb +2 -2
  12. data/lib/dcmgr/cli/security_group.rb +106 -0
  13. data/lib/dcmgr/cli/spec.rb +144 -39
  14. data/lib/dcmgr/cli/storage.rb +16 -15
  15. data/lib/dcmgr/cli/tag.rb +20 -14
  16. data/lib/dcmgr/cli/vlan.rb +5 -5
  17. data/lib/dcmgr/drivers/backing_store.rb +32 -0
  18. data/lib/dcmgr/drivers/comstar.rb +81 -0
  19. data/lib/dcmgr/drivers/iijgio_storage.rb +9 -19
  20. data/lib/dcmgr/drivers/iscsi_target.rb +41 -0
  21. data/lib/dcmgr/drivers/kvm.rb +161 -28
  22. data/lib/dcmgr/drivers/linux_iscsi.rb +60 -0
  23. data/lib/dcmgr/drivers/local_storage.rb +24 -0
  24. data/lib/dcmgr/drivers/lxc.rb +167 -125
  25. data/lib/dcmgr/drivers/raw.rb +74 -0
  26. data/lib/dcmgr/drivers/s3_storage.rb +7 -19
  27. data/lib/dcmgr/drivers/snapshot_storage.rb +18 -28
  28. data/lib/dcmgr/drivers/storage_initiator.rb +28 -0
  29. data/lib/dcmgr/drivers/sun_iscsi.rb +32 -0
  30. data/lib/dcmgr/drivers/zfs.rb +77 -0
  31. data/lib/dcmgr/endpoints/core_api.rb +315 -263
  32. data/lib/dcmgr/endpoints/errors.rb +21 -10
  33. data/lib/dcmgr/endpoints/metadata.rb +360 -23
  34. data/lib/dcmgr/helpers/cli_helper.rb +6 -3
  35. data/lib/dcmgr/helpers/ec2_metadata_helper.rb +9 -0
  36. data/lib/dcmgr/helpers/nic_helper.rb +11 -0
  37. data/lib/dcmgr/helpers/snapshot_storage_helper.rb +34 -0
  38. data/lib/dcmgr/models/account.rb +0 -6
  39. data/lib/dcmgr/models/account_resource.rb +0 -4
  40. data/lib/dcmgr/models/base_new.rb +14 -2
  41. data/lib/dcmgr/models/dhcp_range.rb +38 -0
  42. data/lib/dcmgr/models/frontend_system.rb +0 -6
  43. data/lib/dcmgr/models/history.rb +0 -11
  44. data/lib/dcmgr/models/host_node.rb +131 -0
  45. data/lib/dcmgr/models/hostname_lease.rb +0 -8
  46. data/lib/dcmgr/models/image.rb +31 -18
  47. data/lib/dcmgr/models/instance.rb +137 -143
  48. data/lib/dcmgr/models/instance_nic.rb +52 -29
  49. data/lib/dcmgr/models/instance_security_group.rb +9 -0
  50. data/lib/dcmgr/models/instance_spec.rb +163 -31
  51. data/lib/dcmgr/models/ip_lease.rb +10 -21
  52. data/lib/dcmgr/models/mac_lease.rb +30 -11
  53. data/lib/dcmgr/models/network.rb +148 -27
  54. data/lib/dcmgr/models/physical_network.rb +18 -0
  55. data/lib/dcmgr/models/quota.rb +0 -10
  56. data/lib/dcmgr/models/request_log.rb +3 -18
  57. data/lib/dcmgr/models/security_group.rb +66 -0
  58. data/lib/dcmgr/models/security_group_rule.rb +145 -0
  59. data/lib/dcmgr/models/ssh_key_pair.rb +16 -19
  60. data/lib/dcmgr/models/{storage_pool.rb → storage_node.rb} +35 -25
  61. data/lib/dcmgr/models/tag.rb +0 -14
  62. data/lib/dcmgr/models/tag_mapping.rb +1 -7
  63. data/lib/dcmgr/models/vlan_lease.rb +2 -8
  64. data/lib/dcmgr/models/volume.rb +49 -37
  65. data/lib/dcmgr/models/volume_snapshot.rb +15 -17
  66. data/lib/dcmgr/node_modules/hva_collector.rb +69 -28
  67. data/lib/dcmgr/node_modules/instance_ha.rb +23 -12
  68. data/lib/dcmgr/node_modules/instance_monitor.rb +16 -2
  69. data/lib/dcmgr/node_modules/openflow_controller.rb +784 -0
  70. data/lib/dcmgr/node_modules/scheduler.rb +189 -0
  71. data/lib/dcmgr/node_modules/service_netfilter.rb +452 -227
  72. data/lib/dcmgr/node_modules/service_openflow.rb +731 -0
  73. data/lib/dcmgr/node_modules/sta_collector.rb +20 -0
  74. data/lib/dcmgr/node_modules/sta_tgt_initializer.rb +35 -0
  75. data/lib/dcmgr/rack/request_logger.rb +11 -6
  76. data/lib/dcmgr/rpc/hva_handler.rb +256 -110
  77. data/lib/dcmgr/rpc/sta_handler.rb +244 -0
  78. data/lib/dcmgr/scheduler.rb +122 -8
  79. data/lib/dcmgr/scheduler/host_node/exclude_same.rb +24 -0
  80. data/lib/dcmgr/scheduler/host_node/find_first.rb +12 -0
  81. data/lib/dcmgr/scheduler/host_node/least_usage.rb +28 -0
  82. data/lib/dcmgr/scheduler/host_node/per_instance.rb +18 -0
  83. data/lib/dcmgr/scheduler/host_node/specify_node.rb +26 -0
  84. data/lib/dcmgr/scheduler/network/flat_single.rb +23 -0
  85. data/lib/dcmgr/scheduler/network/nat_one_to_one.rb +23 -0
  86. data/lib/dcmgr/scheduler/network/per_instance.rb +39 -0
  87. data/lib/dcmgr/scheduler/network/vif_template.rb +19 -0
  88. data/lib/dcmgr/scheduler/storage_node/find_first.rb +13 -0
  89. data/lib/dcmgr/scheduler/storage_node/least_usage.rb +23 -0
  90. data/lib/dcmgr/storage_service.rb +39 -40
  91. data/lib/dcmgr/tags.rb +3 -3
  92. data/lib/dcmgr/version.rb +1 -1
  93. data/lib/dcmgr/vnet.rb +105 -0
  94. data/lib/dcmgr/vnet/factories.rb +141 -0
  95. data/lib/dcmgr/vnet/isolators/by_securitygroup.rb +21 -0
  96. data/lib/dcmgr/vnet/isolators/dummy.rb +17 -0
  97. data/lib/dcmgr/vnet/netfilter/cache.rb +51 -0
  98. data/lib/dcmgr/vnet/netfilter/chain.rb +66 -0
  99. data/lib/dcmgr/vnet/netfilter/controller.rb +193 -0
  100. data/lib/dcmgr/vnet/netfilter/ebtables_rule.rb +53 -0
  101. data/lib/dcmgr/vnet/netfilter/iptables_rule.rb +45 -0
  102. data/lib/dcmgr/vnet/netfilter/task_manager.rb +459 -0
  103. data/lib/dcmgr/vnet/tasks/accept_all_dns.rb +19 -0
  104. data/lib/dcmgr/vnet/tasks/accept_arp_broadcast.rb +24 -0
  105. data/lib/dcmgr/vnet/tasks/accept_arp_from_friends.rb +34 -0
  106. data/lib/dcmgr/vnet/tasks/accept_arp_from_gateway.rb +21 -0
  107. data/lib/dcmgr/vnet/tasks/accept_arp_to_host.rb +30 -0
  108. data/lib/dcmgr/vnet/tasks/accept_ip_from_friends.rb +26 -0
  109. data/lib/dcmgr/vnet/tasks/accept_ip_from_gateway.rb +23 -0
  110. data/lib/dcmgr/vnet/tasks/accept_ip_to_anywhere.rb +18 -0
  111. data/lib/dcmgr/vnet/tasks/accept_related_established.rb +45 -0
  112. data/lib/dcmgr/vnet/tasks/accept_wakame_dhcp_only.rb +33 -0
  113. data/lib/dcmgr/vnet/tasks/accept_wakame_dns_only.rb +33 -0
  114. data/lib/dcmgr/vnet/tasks/debug_iptables.rb +21 -0
  115. data/lib/dcmgr/vnet/tasks/drop_arp_forwarding.rb +27 -0
  116. data/lib/dcmgr/vnet/tasks/drop_arp_to_host.rb +24 -0
  117. data/lib/dcmgr/vnet/tasks/drop_ip_from_anywhere.rb +18 -0
  118. data/lib/dcmgr/vnet/tasks/drop_ip_spoofing.rb +34 -0
  119. data/lib/dcmgr/vnet/tasks/drop_mac_spoofing.rb +33 -0
  120. data/lib/dcmgr/vnet/tasks/exclude_from_nat.rb +47 -0
  121. data/lib/dcmgr/vnet/tasks/security_group.rb +37 -0
  122. data/lib/dcmgr/vnet/tasks/static_nat.rb +54 -0
  123. data/lib/dcmgr/vnet/tasks/translate_metadata_address.rb +32 -0
  124. data/web/metadata/config.ru +1 -1
  125. metadata +174 -89
  126. data/lib/dcmgr/cli/group.rb +0 -101
  127. data/lib/dcmgr/endpoints/core_api_mock.rb +0 -865
  128. data/lib/dcmgr/models/host_pool.rb +0 -122
  129. data/lib/dcmgr/models/instance_netfilter_group.rb +0 -16
  130. data/lib/dcmgr/models/netfilter_group.rb +0 -89
  131. data/lib/dcmgr/models/netfilter_rule.rb +0 -21
  132. data/lib/dcmgr/scheduler/find_last.rb +0 -16
  133. data/lib/dcmgr/scheduler/find_random.rb +0 -16
  134. data/lib/dcmgr/stm/instance.rb +0 -25
  135. data/lib/dcmgr/stm/snapshot_context.rb +0 -33
  136. data/lib/dcmgr/stm/volume_context.rb +0 -65
@@ -1,101 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Dcmgr::Cli
4
- class Group < Base
5
- namespace :group
6
- M = Dcmgr::Models
7
-
8
- desc "add [options]", "Add a new security group"
9
- method_option :uuid, :type => :string, :aliases => "-u", :desc => "The UUID for the new security group."
10
- method_option :account_id, :type => :string, :aliases => "-a", :desc => "The UUID of the account this security group belongs to.", :required => true
11
- method_option :name, :type => :string, :aliases => "-n", :desc => "The name for this security group.", :required => true
12
- method_option :description, :type => :string, :aliases => "-d", :desc => "The description for this new security group."
13
- def add
14
- UnknownUUIDError.raise(options[:account_id]) if M::Account[options[:account_id]].nil?
15
-
16
- puts super(M::NetfilterGroup,options)
17
- end
18
-
19
- desc "del UUID", "Delete a security group"
20
- def del(uuid)
21
- super(M::NetfilterGroup,uuid)
22
- end
23
-
24
- desc "show [UUID]", "Show security group(s)"
25
- def show(uuid=nil)
26
- if uuid
27
- group = M::NetfilterGroup[uuid] || UnknownUUIDError.raise(uuid)
28
- puts ERB.new(<<__END, nil, '-').result(binding)
29
- Group UUID:
30
- <%= group.canonical_uuid %>
31
- Account id:
32
- <%= group.account_id %>
33
- <%- if group.description -%>
34
- Description:
35
- <%= group.description %>
36
- <%- end -%>
37
- <%- unless group.netfilter_rules.empty? -%>
38
- Rules:
39
- <%- group.netfilter_rules.each { |rule| -%>
40
- <%= rule.permission %>
41
- <%- } -%>
42
- <%- end -%>
43
- __END
44
- else
45
- puts ERB.new(<<__END, nil, '-').result(binding)
46
- <%- M::NetfilterGroup.all { |row| -%>
47
- <%= row.canonical_uuid %>\t<%= row.account_id %>\t<%= row.name %>
48
- <%- } -%>
49
- __END
50
- end
51
- end
52
-
53
- desc "modify UUID [options]", "Modify an existing security group"
54
- method_option :account_id, :type => :string, :aliases => "-a", :desc => "The UUID of the account this security group belongs to."
55
- method_option :name, :type => :string, :aliases => "-n", :desc => "The name for this security group."
56
- method_option :description, :type => :string, :aliases => "-d", :desc => "The description for this new security group."
57
- def modify(uuid)
58
- UnknownUUIDError.raise(options[:account_id]) if options[:account_id] && M::Account[options[:account_id]].nil?
59
- super(M::NetfilterGroup,uuid,options)
60
- end
61
-
62
- desc "apply UUID [options]", "Apply a security group to an instance"
63
- method_option :instance, :type => :string, :aliases => "-i", :required => :true, :desc => "The instance to apply the group to"
64
- def apply(uuid)
65
- group = M::NetfilterGroup[uuid] || UnknownUUIDError.raise(uuid)
66
- instance = M::Instance[options[:instance]] || UnknownUUIDError.raise(options[:instance])
67
- Error.raise("Group #{uuid} is already applied to instance #{options[:instance]}.",100) if group.instances.member?(instance)
68
- group.add_instance(instance)
69
- end
70
-
71
- desc "remove UUID [options]", "Remove a security group from an instance"
72
- method_option :instance, :type => :string, :aliases => "-i", :required => :true, :desc => "The instance to remove the group from"
73
- def remove(uuid)
74
- group = M::NetfilterGroup[uuid] || UnknownUUIDError.raise(uuid)
75
- instance = M::Instance[options[:instance]] || UnknownUUIDError.raise(options[:instance])
76
- Error.raise("Group #{uuid} is not applied to instance #{options[:instance]}.",100) unless group.instances.member?(instance)
77
- group.remove_instance(instance)
78
- end
79
-
80
- desc "addrule UUID [options]", "Add a rule to a security group"
81
- method_option :rule, :type => :string, :aliases => "-r", :desc => "The new rule to be added"
82
- def addrule(g_uuid)
83
- UnknownUUIDError.raise(g_uuid) if M::NetfilterGroup[g_uuid].nil?
84
-
85
- #TODO: check rule syntax
86
- new_rule = M::NetfilterRule.new(:permission => options[:rule])
87
- new_rule.netfilter_group = M::NetfilterGroup[g_uuid]
88
- new_rule.save
89
- end
90
-
91
- desc "delrule UUID [options]", "Delete a rule from a security group"
92
- method_option :rule, :type => :string, :aliases => "-r", :desc => "The rule to be deleted."
93
- def delrule(g_uuid)
94
- UnknownUUIDError.raise(g_uuid) if M::NetfilterGroup[g_uuid].nil?
95
- rule = M::NetfilterRule.find(:netfilter_group_id => M::NetfilterGroup[g_uuid].id,:permission => options[:rule])
96
- Error.raise("Group '#{g_uuid}' does not contain rule '#{options[:rule]}'.",100) if rule.nil?
97
-
98
- rule.destroy
99
- end
100
- end
101
- end
@@ -1,865 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require 'sinatra/base'
4
- require 'sinatra/rabbit'
5
-
6
- require 'json'
7
- require 'extlib/hash'
8
-
9
- require 'dcmgr/endpoints/errors'
10
-
11
- module Dcmgr
12
- module Endpoints
13
- class Mock
14
- def self.loadfile(path, ext='json')
15
- root_path = File.expand_path('../../')
16
- controller,action = path.split('/')
17
- file = action + '.' + ext
18
- dir_path = File.join(root_path,'fixtures','mock',controller)
19
- readfile = File.join(dir_path,file)
20
- data = ''
21
- open(readfile) {|f| data = f.read }
22
- data
23
- end
24
- end
25
-
26
- class CoreAPI < Sinatra::Base
27
- register Sinatra::Rabbit
28
-
29
- disable :sessions
30
- disable :show_exceptions
31
-
32
- before do
33
- @params = parsed_request_body if request.post?
34
- request.env['dcmgr.frotend_system.id'] = 1
35
- request.env['HTTP_X_VDC_REQUESTER_TOKEN']='u-xxxxxx'
36
- request.env['HTTP_X_VDC_ACCOUNT_UUID']='a-00000000'
37
- end
38
-
39
- before do
40
- @account = Models::Account[request.env['HTTP_X_VDC_ACCOUNT_UUID']]
41
- @requester_token = request.env['HTTP_X_VDC_REQUESTER_TOKEN']
42
- #@frontend = Models::FrontendSystem[request.env['dcmgr.frotend_system.id']]
43
-
44
- #raise InvalidRequestCredentials if !(@account && @frontend)
45
- raise DisabledAccount if @account.disable?
46
- end
47
-
48
- def find_by_uuid(model_class, uuid)
49
- if model_class.is_a?(Symbol)
50
- model_class = Models.const_get(model_class)
51
- end
52
- model_class[uuid] || raise(UnknownUUIDResource, uuid.to_s)
53
- end
54
-
55
- def find_account(account_uuid)
56
- find_by_uuid(:Account, account_uuid)
57
- end
58
-
59
- def find_user(user_uuid)
60
- find_by_uuid(:User, user_uuid)
61
- end
62
-
63
- # Returns deserialized hash from HTTP body. Serialization fromat
64
- # is guessed from content type header. The query string params
65
- # is returned if none of content type header is in HTTP headers.
66
- # This method is called only when the request method is POST.
67
- def parsed_request_body
68
- # @mime_types should be defined by sinatra/respond_to.rb plugin.
69
- if @mime_types.nil?
70
- # use query string as requested params if Content-Type
71
- # header was not sent.
72
- # ActiveResource library tells the one level nested hash which has
73
- # {'something key'=>real_params} so that dummy key is assinged here.
74
- hash = {:dummy=>@params}
75
- else
76
- mime = @mime_types.first
77
- case mime.to_s
78
- when 'application/json', 'text/json'
79
- require 'json'
80
- hash = JSON.load(request.body)
81
- hash = hash.to_mash
82
- when 'application/yaml', 'text/yaml'
83
- require 'yaml'
84
- hash = YAML.load(request.body)
85
- hash = hash.to_mash
86
- else
87
- raise "Unsupported body document type: #{mime.to_s}"
88
- end
89
- end
90
- return hash.values.first
91
- end
92
-
93
- # I am not going to use error(ex, &blk) hook since it works only
94
- # when matches the Exception class exactly. I expect to match
95
- # whole subclasses of APIError so that override handle_exception!().
96
- def handle_exception!(boom)
97
- if boom.kind_of?(APIError)
98
- @env['sinatra.error'] = boom
99
- error(boom.status_code, boom.class.to_s)
100
- else
101
- super
102
- end
103
- end
104
-
105
- def pagenate(data,start,limit)
106
- return data unless data.kind_of?(Array)
107
- if !start.nil? && !limit.nil?
108
- start = start.to_i
109
- limit = limit.to_i
110
- from = start
111
- to = (from + limit -1)
112
- data = data[from..to]
113
- end
114
- data
115
- end
116
-
117
- collection :accounts do
118
- operation :index do
119
- control do
120
- end
121
- end
122
-
123
- operation :show do
124
- control do
125
- a = find_account(params[:id])
126
- respond_to { |f|
127
- f.json { a.to_hash_document.to_json }
128
- }
129
- end
130
- end
131
-
132
- operation :create do
133
- description 'Register a new account'
134
- control do
135
- a = Models::Account.create()
136
- respond_to { |f|
137
- f.json { a.to_hash_document.to_json }
138
- }
139
- end
140
- end
141
-
142
- operation :destroy do
143
- description 'Unregister the account.'
144
- # Associated resources all have to be destroied prior to
145
- # removing the account.
146
- #param :id, :string, :required
147
- control do
148
- a = find_account(params[:id])
149
- a.destroy
150
-
151
- respond_to { |f|
152
- f.json { {} }
153
- }
154
- end
155
- end
156
-
157
- operation :enable, :method=>:get, :member=>true do
158
- description 'Enable the account for all operations'
159
- control do
160
- a = find_account(params[:id])
161
- a.enabled = Models::Account::ENABLED
162
- a.save
163
-
164
- respond_to { |f|
165
- f.json { {} }
166
- }
167
- end
168
- end
169
-
170
- operation :disable, :method=>:get, :member=>true do
171
- description 'Disable the account for all operations'
172
- control do
173
- a = find_account(params[:id])
174
- a.enabled = Models::Account::DISABLED
175
- a.save
176
-
177
- respond_to { |f|
178
- f.json { {} }
179
- }
180
- end
181
- end
182
-
183
- operation :add_tag, :method=>:get, :member=>true do
184
- description 'Add a tag belongs to the account'
185
- #param :tag_name, :string, :required
186
- control do
187
- a = find_account(params[:id])
188
-
189
- tag_class = Models::Tags.find_tag_class(params[:tag_name])
190
- raise "UnknownTagClass: #{params[:tag_name]}" if tag_class.nil?
191
-
192
- a.add_tag(tag_class.new(:name=>params[:name]))
193
- end
194
- end
195
-
196
- operation :remove_tag, :method=>:get, :member=>true do
197
- description 'Unlink the associated tag of the account'
198
- #param :tag_id, :string, :required
199
- control do
200
- a = find_account(params[:id])
201
- t = a.tags_dataset.filter(:uuid=>params[:tag_id]).first
202
- if t
203
- a.remove_tag(t)
204
- else
205
- raise "Unknown or disassociated tag for #{a.cuuid}: #{params[:tag_id]}"
206
- end
207
- end
208
- end
209
- end
210
-
211
- collection :tags do
212
- operation :create do
213
- description 'Register new tag to the account'
214
- #param :tag_name, :string, :required
215
- #param :type_id, :fixnum, :optional
216
- #param :account_id, :string, :optional
217
- control do
218
- tag_class = Models::Tag.find_tag_class(params[:tag_name])
219
-
220
- tag_class.create
221
-
222
- end
223
- end
224
-
225
- operation :show do
226
- #param :account_id, :string, :optional
227
- control do
228
- end
229
- end
230
-
231
- operation :destroy do
232
- description 'Create a new user'
233
- control do
234
- end
235
- end
236
-
237
- operation :update do
238
- control do
239
- end
240
- end
241
- end
242
-
243
- collection :instances do
244
- operation :index do
245
- description 'Show list of instances'
246
- control do
247
- start = params[:start].to_i
248
- start = start < 1 ? 0 : start
249
- limit = params[:limit].to_i
250
- limit = limit < 1 ? 10 : limit
251
-
252
- partial_ds = (1..30).collect { |i| {
253
- :id => "i-123456" + ('%02d' % i).to_s,
254
- :image_id => 'wmi-12345678',
255
- :cpu_cores => 1,
256
- :memory_size => 256,
257
- :network => [{
258
- :ipaddr => '192.168.0.%d' % i,
259
- }],
260
- :state => 'running',
261
- :status => 'running'
262
- }
263
- }
264
-
265
- total = partial_ds.count
266
- partial_ds = pagenate(partial_ds,params[:start],params[:limit])
267
-
268
- res = [{
269
- :owner_total => total,
270
- :start => start,
271
- :limit => limit,
272
- :results=> partial_ds
273
- }]
274
-
275
- respond_to { |f|
276
- f.json {res.to_json}
277
- }
278
- end
279
- end
280
-
281
- operation :create do
282
- description 'Runs a new instance'
283
- # param :image_id, :required
284
- # param :host_pool_id :required
285
- # param :instance_spec_id, :required
286
- control do
287
- i = {
288
- :memory_size => 256,
289
- :image_id => "wmi-640cbf3r",
290
- :created_at => "Wed Oct 27 16:58:24 +0900 2010",
291
- :network => {"ipaddr"=>"192.168.1.241"},
292
- :id => "i-umwcbev3",
293
- :volume => {},
294
- :host_pool_id => "hp-hb4f6f84",
295
- :cpu_cores => 1,
296
- :status => "init",
297
- :state => "init"
298
- }
299
-
300
- respond_to { |f|
301
- f.json { i.to_json }
302
- }
303
- end
304
- end
305
-
306
- operation :show do
307
- control do
308
- i = {
309
- :id => params[:id],
310
- :image_id => 'wmi-12345678',
311
- :cpu_cores => 1,
312
- :memory_size => 256,
313
- :network => [{
314
- :ipaddr => '192.168.0.1',
315
- }],
316
- :state => 'running',
317
- :status => 'running',
318
- :ssh_key_pair => nil,
319
- :netfilter_group => {},
320
- :created_at => "Wed Oct 27 16:58:24 +0900 2010",
321
- }
322
- respond_to { |f|
323
- f.json { i.to_json }
324
- }
325
- end
326
- end
327
-
328
- operation :destroy do
329
- description 'Shutdown the instance'
330
- control do
331
- i = find_by_uuid(:Instance, params[:id])
332
-
333
- respond_to { |f|
334
- f.json { i.to_hash_document.to_json }
335
- }
336
- end
337
- end
338
-
339
- operation :update do
340
- description 'Change vcpu cores or memory size on the instance'
341
- control do
342
- i = find_by_uuid(:Instance, params[:id])
343
- end
344
- end
345
-
346
- operation :reboot, :method=>:get, :member=>true do
347
- description 'Reboots the instance'
348
- control do
349
- i = find_by_uuid(:Instance, params[:id])
350
- end
351
- end
352
-
353
- operation :resume, :method=>:get, :member=>true do
354
- description 'Resume the suspending instance'
355
- control do
356
- i = find_by_uuid(:Instance, params[:id])
357
- end
358
- end
359
-
360
- operation :suspend, :method=>:get, :member=>true do
361
- description 'Suspend the instance'
362
- control do
363
- i = find_by_uuid(:Instance, params[:id])
364
- end
365
- end
366
- end
367
-
368
- collection :host_pools do
369
- operation :create do
370
- description 'Register a new physical host'
371
- # param :
372
- control do
373
- raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
374
- input = parsed_request_body
375
-
376
- hp = Models::HostPool.create(:cpu_cores=>input[:cpu_cores],
377
- :memory_size=>input[:memory_size],
378
- :arch=>input[:arch],
379
- :hypervisor=>input[:hypervisor]
380
- )
381
- respond_to { |f|
382
- f.json{ hp.to_hash_document.to_json }
383
- }
384
- end
385
- end
386
-
387
- operation :show do
388
- description 'Show status of the host'
389
- #param :account_id, :string, :optional
390
- control do
391
- raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
392
-
393
- hp = find_by_uuid(:HostPool, params[:id])
394
- respond_to { |f|
395
- f.json { hp.to_hash_document.to_json }
396
- }
397
- end
398
- end
399
-
400
- operation :destroy do
401
- description 'Unregister the existing host'
402
- control do
403
- raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
404
-
405
- hp = find_by_uuid(:HostPool, params[:id])
406
- if hp.depend_resources?
407
- raise ""
408
- end
409
- hp.destroy
410
- end
411
- end
412
-
413
- operation :update do
414
- description 'Update parameters for the host'
415
- # param :cpu_cores, :optional
416
- # param :memory_size, :optional
417
- control do
418
- raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
419
-
420
- hp = find_by_uuid(:HostPool, params[:id])
421
- if params[:cpu_cores]
422
- hp.offering_cpu_cores = params[:cpu_cores].to_i
423
- end
424
- if params[:memory_size]
425
- hp.offering_memory_size = params[:memory_size].to_i
426
- end
427
-
428
- hp.save
429
- end
430
- end
431
-
432
- end
433
-
434
- collection :images do
435
- operation :index do
436
- description 'Show list of machine images'
437
- control do
438
- start = params[:start].to_i
439
- start = start < 1 ? 0 : start
440
- limit = params[:limit].to_i
441
- limit = limit < 1 ? 10 : limit
442
-
443
- partial_ds = (1..30).collect { |i| {
444
- :created_at => "Mon Oct 18 18:33:58 +0900 2010",
445
- :updated_at => "Mon Oct 18 18:33:58 +0900 2010",
446
- :uuid => "wmi-640cbf"+sprintf('%02d',i),
447
- :arch => "x86",
448
- :account_id => "a-00000000",
449
- :id => "wmi-640cbf"+sprintf('%02d',i),
450
- :boot_dev_type => 2,
451
- :description => "",
452
- :source => {
453
- :uri => "http://localhost/vdc/tmpmz0N86.qcow2",
454
- :type => "http"
455
- },
456
- :state => "init"
457
- }}
458
-
459
- total = partial_ds.count
460
- partial_ds = pagenate(partial_ds,params[:start],params[:limit])
461
-
462
- res = [{
463
- :owner_total => total,
464
- :start => start,
465
- :limit => limit,
466
- :results=> partial_ds
467
- }]
468
-
469
- respond_to { |f|
470
- f.json {res.to_json}
471
- }
472
- end
473
- end
474
- end
475
-
476
- collection :volumes do
477
- operation :index do
478
- description 'Show lists of the volume'
479
- # param start, fixnum, optional
480
- # param limit, fixnum, optional
481
- control do
482
- start = params[:start].to_i
483
- start = start < 1 ? 0 : start
484
- limit = params[:limit].to_i
485
- limit = limit < 1 ? 10 : limit
486
-
487
- json = Mock.loadfile('volumes/list')
488
- vl = JSON.load(json)
489
- total = vl.count
490
- vl = pagenate(vl,start,limit)
491
-
492
- res = [{
493
- :owner_total => total,
494
- :start => start,
495
- :limit => limit,
496
- :results => vl
497
- }]
498
- respond_to { |f|
499
- f.json {res.to_json}
500
- }
501
- end
502
- end
503
-
504
- operation :show do
505
- description 'Show the volume status'
506
- # param id, string, required
507
- control do
508
- volume_id = params[:id]
509
- raise UndefinedVolumeID if volume_id.nil?
510
- json = Mock.loadfile('volumes/details')
511
- vl = JSON.load(json)
512
- vl = vl[volume_id]
513
- respond_to { |f|
514
- f.json { vl.to_json}
515
- }
516
- end
517
- end
518
-
519
- operation :create do
520
- description 'Create the new volume'
521
- # param volume_size, string, required
522
- # param snapshot_id, string, optional
523
- # param private_pool_id, string, optional
524
- control do
525
- vl = { :status => 'creating', :messages => 'creating the new volume vol-xxxxxxx'}
526
- # vl = Models::Volume.create(:size=> params[:volume_size])
527
- # vl.state_machine.on_create
528
- respond_to { |f|
529
- f.json { vl.to_json}
530
- }
531
- end
532
- end
533
-
534
- operation :destroy do
535
- description 'Delete the volume'
536
- # param volume_id, string, required
537
- control do
538
- vl = { :status => 'deleting', :messages => 'deleting the volume vol-xxxxxxx'}
539
- respond_to { |f|
540
- f.json { vl.to_json}
541
- }
542
- end
543
- end
544
-
545
- operation :attach, :method =>:put, :member =>true do
546
- description 'Attachd the volume'
547
- # param volume_id, string, required
548
- # param instance_id, string, required
549
- control do
550
- vl = { :status => 'attaching', :message => 'attaching the volume of vol-xxxxxx to instance_id'}
551
- respond_to { |f|
552
- f.json { vl.to_json}
553
- }
554
- end
555
- end
556
-
557
- operation :detach, :method =>:put, :member =>true do
558
- description 'Detachd the volume'
559
- # param volume_id, string, required
560
- control do
561
- vl = { :status => 'detaching', :message => 'detaching the volume of instance_id to vol-xxxxxx'}
562
- respond_to { |f|
563
- f.json { vl.to_json}
564
- }
565
- end
566
- end
567
-
568
- operation :status, :method =>:get, :member =>true do
569
- description 'Show the status'
570
- control do
571
- vl = [{ :id => 1, :uuid => 'vol-xxxxxxx', :status => 1 },
572
- { :id => 2, :uuid => 'vol-xxxxxxx', :status => 0 },
573
- { :id => 3, :uuid => 'vol-xxxxxxx', :status => 3 },
574
- { :id => 4, :uuid => 'vol-xxxxxxx', :status => 2 },
575
- { :id => 5, :uuid => 'vol-xxxxxxx', :status => 4 }]
576
- respond_to {|f|
577
- f.json { vl.to_json}
578
- }
579
- end
580
- end
581
- end
582
-
583
- collection :volume_snapshots do
584
- operation :index do
585
- description 'Show lists of the volume_snapshots'
586
- # param start, Fixnum, optional
587
- # param limit, Fixnum, optional
588
- control do
589
- start = params[:start].to_i
590
- start = start < 1 ? 0 : start
591
- limit = params[:limit].to_i
592
- limit = limit < 1 ? 10 : limit
593
-
594
- json = Mock.loadfile('volume_snapshots/list')
595
- vs = JSON.load(json)
596
- total = vs.count
597
- vs = pagenate(vs,start,limit)
598
-
599
- res = [{
600
- :owner_total => total,
601
- :start => start,
602
- :limit => limit,
603
- :results => vs
604
- }]
605
- respond_to { |f|
606
- f.json {res.to_json}
607
- }
608
- end
609
- end
610
-
611
- operation :show do
612
- description 'Show the volume status'
613
- # param id, string, required
614
- control do
615
- snapshot_id = params[:id]
616
- raise UndefinedVolumeSnapshotID if snapshot_id.nil?
617
- json = Mock.loadfile('volume_snapshots/details')
618
- vs = JSON.load(json)
619
- vs = vs[snapshot_id]
620
- respond_to { |f|
621
- f.json { vs.to_json}
622
- }
623
- end
624
- end
625
-
626
- operation :create do
627
- description 'Create a new volume snapshot'
628
- # param volume_id, string, required
629
- # param pool_id, string, optional
630
- control do
631
- vs = { :status => 'creating', :message => 'creating the new snapshot'}
632
- respond_to { |f|
633
- f.json { vs.to_json }
634
- }
635
- end
636
- end
637
-
638
- operation :destroy do
639
- description 'Delete the volume snapshot'
640
- # param snapshot_id, string, required
641
- control do
642
- vs = { :status => 'deleting', :message => 'deleting the snapshot'}
643
- respond_to { |f|
644
- f.json { vs.to_json }
645
- }
646
- end
647
- end
648
-
649
- operation :status, :method =>:get, :member =>true do
650
- description 'Show the status'
651
- control do
652
- vs = [{ :id => 1, :uuid => 'snap-xxxxxxx', :status => 1 },
653
- { :id => 2, :uuid => 'snap-xxxxxxx', :status => 0 },
654
- { :id => 3, :uuid => 'snap-xxxxxxx', :status => 3 },
655
- { :id => 4, :uuid => 'snap-xxxxxxx', :status => 2 },
656
- { :id => 5, :uuid => 'snap-xxxxxxx', :status => 4 }]
657
- respond_to {|f|
658
- f.json { vs.to_json}
659
- }
660
- end
661
- end
662
- end
663
-
664
- collection :netfilter_groups do
665
- operation :index do
666
- control do
667
- start = params[:start].to_i
668
- start = start < 1 ? 0 : start
669
- limit = params[:limit].to_i
670
- limit = limit < 1 ? 10 : limit
671
-
672
- g = (1..30).collect { |i|
673
- {
674
- :id => i,
675
- :name => "group_#{i}",
676
- :description => "desc_group_#{i}",
677
- :rule => "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n",
678
- :account_id => "a-00000000",
679
- :created_at => "Fri Oct 22 10:50:09 +0900 2010",
680
- :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
681
- }
682
- }
683
- total = g.count
684
- g = pagenate(g,params[:start],params[:limit])
685
-
686
- res = [{
687
- :owner_total => total,
688
- :start => start,
689
- :limit => limit,
690
- :results => g
691
- }]
692
-
693
- respond_to { |f|
694
- f.json {res.to_json}
695
- }
696
- end
697
- end
698
-
699
- operation :show do
700
- description 'Show lists of the netfilter_groups'
701
- control do
702
- @name = params[:id]
703
- g = {
704
- :id => 1,
705
- :name => @name,
706
- :description => "desc_#{@name}",
707
- :rule => "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n",
708
- :account_id => "a-00000000",
709
- :created_at => "Fri Oct 22 10:50:09 +0900 2010",
710
- :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
711
- }
712
- respond_to { |f|
713
- f.json { g.to_json }
714
- }
715
- end
716
- end
717
-
718
- operation :create do
719
- description 'Register a new netfilter_group'
720
- # params name, string
721
- # params description, string
722
- # params rule, string
723
- control do
724
- raise UndefinedNetfilterGroup if params[:name].nil?
725
-
726
- @name = params[:name]
727
- @description = if params[:description]
728
- params[:description]
729
- else
730
- "desc_#{@name}"
731
- end
732
- @rule = if params[:rule]
733
- params[:rule]
734
- else
735
- "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n"
736
- end
737
-
738
- g = {
739
- :id => 1,
740
- :name => @name,
741
- :description => @description,
742
- :rule => @rule,
743
- :account_id => "a-00000000",
744
- :created_at => "Fri Oct 22 10:50:09 +0900 2010",
745
- :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
746
- }
747
- respond_to { |f|
748
- f.json { g.to_json }
749
- }
750
- end
751
- end
752
-
753
- operation :update do
754
- description "Update parameters for the netfilter group"
755
- # params description, string
756
- # params rule, string
757
- control do
758
- @name = params[:id]
759
- @description = nil
760
- @rule = nil
761
-
762
- if params[:description]
763
- @description = params[:description]
764
- else
765
- @description = "desc_#{@name}"
766
- end
767
-
768
- if params[:rule]
769
- @rule = params[:rule]
770
- else
771
- @rule = "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n"
772
- end
773
-
774
- g = {
775
- :id => 1,
776
- :name => @name,
777
- :description => @description,
778
- :rule => @rule,
779
- :account_id => "a-00000000",
780
- :created_at => "Fri Oct 22 10:50:09 +0900 2010",
781
- :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
782
- }
783
- respond_to { |f|
784
- f.json { g.to_json }
785
- }
786
- end
787
- end
788
-
789
- operation :destroy do
790
- description "Delete the netfilter group"
791
-
792
- control do
793
- @name = params[:id]
794
- g = {
795
- :id => 1,
796
- :name => @name,
797
- :description => "desc_#{@name}",
798
- :rule => "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n",
799
- :account_id => "a-00000000",
800
- :created_at => "Fri Oct 22 10:50:09 +0900 2010",
801
- :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
802
- }
803
- respond_to { |f|
804
- f.json { g.to_json }
805
- }
806
- end
807
- end
808
-
809
- end
810
-
811
- collection :netfilter_rules do
812
- operation :index do
813
- control do
814
- end
815
- end
816
-
817
- operation :show do
818
- description 'Show lists of the netfilter_rules'
819
- control do
820
- g = [
821
- {:id => 1, :netfilter_group_id => 1, :permission => "tcp:22,22,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
822
- {:id => 2, :netfilter_group_id => 1, :permission => "tcp:80,80,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
823
- {:id => 3, :netfilter_group_id => 1, :permission => "udp:53,53,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
824
- {:id => 4, :netfilter_group_id => 1, :permission => "icmp:-1,-1,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
825
- ]
826
- respond_to { |f|
827
- f.json { g.to_json }
828
- }
829
- end
830
- end
831
- end
832
-
833
- collection :private_pools do
834
- operation :show do
835
- description 'Show lists of the private_pools'
836
- control do
837
- pp = [{
838
- :id => 1,
839
- :account_id => 'u-xxxxxxx',
840
- :storage_pool_id => 1,
841
- :created_at => 'Fri Sep 10 14:50:11 +0900 2010',
842
- :updated_at => 'Fri Sep 10 14:50:11 +0900 2010'
843
- },{
844
- :id => 2,
845
- :account_id => 'u-xxxxxxx',
846
- :storage_pool_id => 23,
847
- :created_at => 'Fri Sep 10 14:50:11 +0900 2010',
848
- :updated_at => 'Fri Sep 10 14:50:11 +0900 2010'
849
- },{
850
- :id => 2,
851
- :account_id => 'u-xxxxxxx',
852
- :storage_pool_id => 150,
853
- :created_at => 'Fri Sep 10 14:50:11 +0900 2010',
854
- :updated_at => 'Fri Sep 10 14:50:11 +0900 2010'
855
- }]
856
- respond_to { |f|
857
- f.json { pp.to_json}
858
- }
859
- end
860
- end
861
- end
862
-
863
- end
864
- end
865
- end