wakame-adapters-tengine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'json'
4
+ gem 'right_aws', '2.1.0'
5
+ gem 'rspec'
6
+
7
+ group :development do
8
+ gem "jeweler", "~> 1.6.4"
9
+ end
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ json (1.6.1)
11
+ rake (0.9.2.2)
12
+ right_aws (2.1.0)
13
+ right_http_connection (>= 1.2.5)
14
+ right_http_connection (1.3.0)
15
+ rspec (2.5.0)
16
+ rspec-core (~> 2.5.0)
17
+ rspec-expectations (~> 2.5.0)
18
+ rspec-mocks (~> 2.5.0)
19
+ rspec-core (2.5.2)
20
+ rspec-expectations (2.5.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.5.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ jeweler (~> 1.6.4)
29
+ json
30
+ right_aws (= 2.1.0)
31
+ rspec
data/README ADDED
@@ -0,0 +1,74 @@
1
+ Tama
2
+ ====
3
+ TamaはアマゾンEC2とWakame-vdcを呼び出すソフトウェアです。Tamaでは、一つのAPIからどちも呼び出すことが出来るようになります。
4
+
5
+ 必要なRuby Gem
6
+ =============
7
+ rubygems
8
+ json
9
+ right_aws
10
+
11
+ セットアップ
12
+ ==========
13
+ 必要なgemをインストールすために、バンドルを使って下さい。
14
+
15
+ gem install bundler
16
+
17
+ そのあとで、Tamaのダイレクトリーでこのコマンドをして下さい。
18
+
19
+ bundle install
20
+
21
+ これでTamaを使えるようになります。
22
+
23
+ Tamaの使い方
24
+ ==========
25
+ Tamaを使うのだったら、Tamaのオブジェクトが作らなければなりません。
26
+
27
+ t = Tama::Tama.new(Account,Ec2_host,Ec2_port,Ec2_protocol,Wakame_host,Wakame_port,Wakame_protocol)
28
+
29
+ パラメータの説明:
30
+ *Account: Wakame-vdcで使うaccount-idです。例えば:'a-shpoolxx'
31
+ *Ec2_host:Ec2のWebAPIのIPアドレス。例えば:"192.168.2.22"
32
+ *Ec2_port:Ec2のWebAPIのポート。例えば:9005
33
+ *Ec2_protocol:Ec2が使うプロトコル。例えば:"http"や"https"
34
+ *Wakame_host:Wakame-vdcのWebAPIのIPアドレス。例えば:"192.168.2.22"
35
+ *Wakame_port:Wakame-vdcのWebAPIのポート。例えば:9001
36
+ *Wakame_protocol:Wakame-vdcが使うプロトコル。例えば:"http"や"https"
37
+
38
+ そのあとでこのメソッドが呼び出せます。
39
+
40
+ t.run_instances(Image_id,Min,Max,Security_Groups,SSH_Key, User_Data, Addressing_Type, Instance_Type)
41
+ t.describe_instances([])
42
+ t.terminate_instances([])
43
+ t.describe_images([])
44
+ t.describe_host_nodes
45
+
46
+ run_instancesのパラメーターの説明
47
+ *Image_id:マシンイメージのIDです。このイメージのインスタンスを立ち上がります。例えば:'wmi-lucid5'
48
+ *Min:立ち上がるインスタンスのミニマムです。例えば3だったら、インスタンスを3台立ち上がります。
49
+ *Max:立ち上がるインスタンスのマクシマムです。例えば5だったら、出来れば5台を立ち上がります。
50
+ *Security_Groups:セキュリティーグループの名前が入っているArrayです。例えば:['group1','group2']
51
+ *SSH_Key:立ち上がるインスタンスで使いたいSSHキーペアの名前です。例えば:"demokey"
52
+ *User_Data:ユーザーが何でも入れても良いのデータです。例えば:"ワカメが大好きです"
53
+ *Addressing_Type:Wakame-vdcのアダプターで使うと、これは何もしませんが、Tamaはright_awsを使っているので、必要です。何でも入れても良いです。
54
+ *Instance_Type:どんなInstance-specでインスタンスを立ち上がるのはここで決めます。例えば:"is-demospec"
55
+
56
+ describe_instancesとterminate_instancesとdescribe_imagesのパラメーターはみんなインスタンスやイメージIDが入っているArrayです。describeだったら、そのIDのデータを返すとterminateだったら、そのIDのインスタンスをターミネートします。例えば:['i-9slepd2d','i-yfhvndlp','i-74mcdk49']
57
+
58
+ Tamaのテストの使い方
59
+ =================
60
+ TamaはWakameやEc2を呼び出せるだけではなく、見せ物のデータでテストをすることも出来ます。そうする場合は、こうやってTamaのオブジェクトを作って下さい。
61
+
62
+ t = Tama::Tama.new(:test)
63
+
64
+ メソッドはふつうのように呼び出せますが、Wakame-vdcやEc2を呼び出さない。その代わりにデータをファイルから読みます。そのファイルはJSONで書いてあるとディフォルトではTama_directory/test/test_filesにあるます。
65
+
66
+ *本当にインスタンスを立ち上がりませんので、t.run_instancesはパラメーターが必要がありません。
67
+
68
+ 欲しければ、JSONのファイルのパースも決められます。
69
+
70
+ t.api[0].describe_instances_file = "/path/to/file"
71
+ t.api[0].describe_images_file = "/path/to/file"
72
+ t.api[0].run_instances_file = "/path/to/file"
73
+ t.api[0].terminate_instances_file = "/path/to/file"
74
+ t.api[1].host_nodes_file = "/path/to/file"
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "wakame-adapters-tengine"
18
+ gem.homepage = "https://github.com/axsh-dev/wakame-adapters-tengine"
19
+ # gem.license = "MIT"
20
+ gem.summary = %Q{wakame-adapters-tengine is ...}
21
+ gem.description = %Q{wakame-adapters-tengine is ...}
22
+ gem.email = "axsh"
23
+ gem.authors = ["axsh"]
24
+ # gem.bindir = 'bin'
25
+ # gem.executables = []
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,145 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'json'
5
+
6
+ module Tama
7
+ module Apis
8
+
9
+ # Reads JSON files and returns Hash maps similar to right_aws
10
+ class Ec2ApiTest #< ec2Api
11
+ attr_accessor :describe_instances_file
12
+ attr_accessor :describe_images_file
13
+ attr_accessor :run_instances_file
14
+ attr_accessor :terminate_instances_file
15
+ attr_accessor :account
16
+
17
+ def initialize
18
+ self.terminate_instances_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/terminate_instances.json"
19
+ self.describe_images_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/describe_images.json"
20
+ self.describe_instances_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/describe_instances.json"
21
+ self.run_instances_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/run_instances.json"
22
+ self.account = "a-shpoolxx"
23
+ end
24
+
25
+ def describe_instances(list=[])
26
+ File.open(self.describe_instances_file) { |file|
27
+ res = JSON.parse(file.readlines.join.to_s).first["results"]
28
+ res.delete_if {|item| not list.member?(item["id"])} unless list.empty?
29
+ res.map { |inst_map|
30
+ dns_name = inst_map["network"].first["nat_dns_name"] unless inst_map["network"].nil? || inst_map["network"].empty?
31
+ private_dns_name = inst_map["network"].first["dns_name"] unless inst_map["network"].nil? || inst_map["network"].empty?
32
+ ip_address = inst_map["vif"].first["ipv4"]["nat_address"] unless inst_map["vif"].nil? || inst_map["vif"].empty? || inst_map["vif"].first["ipv4"].nil?
33
+ private_ip_address = inst_map["vif"].first["ipv4"]["address"] unless inst_map["vif"].nil? || inst_map["vif"].empty? || inst_map["vif"].first["ipv4"].nil?
34
+ {:aws_kernel_id=>"",
35
+ :aws_launch_time=>inst_map["created_at"],
36
+ :tags=>{},
37
+ :aws_reservation_id=>"",
38
+ :aws_owner=>self.account,
39
+ :instance_lifecycle=>"",
40
+ :block_device_mappings=>[{:ebs_volume_id=>"", :ebs_status=>"", :ebs_attach_time=>"", :ebs_delete_on_termination=>false, :device_name=>""}],
41
+ :ami_launch_index=>"",
42
+ :root_device_name=>"",
43
+ :aws_ramdisk_id=>"",
44
+ :aws_availability_zone=>inst_map["host_node"],
45
+ :aws_groups=>inst_map["netfilter_group_id"],
46
+ :spot_instance_request_id=>"",
47
+ :ssh_key_name=>"",
48
+ :virtualization_type=>"",
49
+ :placement_group_name=>"",
50
+ :requester_id=>"",
51
+ :aws_instance_id=>inst_map["id"],
52
+ :aws_product_codes=>[],
53
+ :client_token=>"",
54
+ :private_ip_address=>private_ip_address,
55
+ :architecture=>inst_map["arch"],
56
+ :aws_state_code=>0,
57
+ :aws_image_id=>inst_map["image_id"],
58
+ :root_device_type=>"",
59
+ :ip_address=>ip_address,
60
+ :dns_name=>dns_name,
61
+ :monitoring_state=>"",
62
+ :aws_instance_type=>inst_map["instance_spec_id"],
63
+ :aws_state=>inst_map["state"],
64
+ :private_dns_name=>private_dns_name,
65
+ :aws_reason=>""
66
+ }
67
+ }
68
+ }
69
+ end
70
+
71
+ def describe_images(list=[], image_type=nil)
72
+ File.open(self.describe_images_file) { |file|
73
+ res = JSON.parse(file.readlines.join.to_s).first["results"]
74
+ res.delete_if {|item| not list.member?(item["id"])} unless list.empty?
75
+ res.map { |img_map|
76
+ {:root_device_name=>"",
77
+ :aws_ramdisk_id=>"",
78
+ :block_device_mappings=>[{:ebs_snapshot_id=>"",
79
+ :ebs_volume_size=>0,
80
+ :ebs_delete_on_termination=>false,
81
+ :device_name=>""}],
82
+ :aws_is_public=>img_map["is_public"],
83
+ :virtualization_type=>"",
84
+ :image_owner_alias=>"",
85
+ :aws_id=>img_map["uuid"],
86
+ :aws_architecture=>img_map["arch"],
87
+ :root_device_type=>"",
88
+ :aws_location=>img_map["source"],
89
+ :aws_image_type=>"",
90
+ :name=>"",
91
+ :aws_state=>img_map["state"],
92
+ :description=>img_map["description"],
93
+ :aws_kernel_id=>"",
94
+ :tags=>{},
95
+ :aws_owner=>img_map["account_id"]
96
+ }
97
+ }
98
+ }
99
+ end
100
+
101
+ # The arguments here are just placeholders to match the real
102
+ # run_instances method. The output is read from a file as usual.
103
+ def run_instances(image_id=nil, min_count=nil, max_count=nil, group_ids=nil, key_name=nil, user_data='', addressing_type = nil, instance_type = nil, kernel_id = nil, ramdisk_id = nil, availability_zone = nil, block_device_mappings = nil)
104
+ File.open(self.run_instances_file) { |file|
105
+ JSON.parse(file.readlines.join.to_s).map { |inst_map|
106
+ dns_name = inst_map["network"].first["nat_dns_name"] unless inst_map["network"].nil? || inst_map["network"].empty?
107
+ private_dns_name = inst_map["network"].first["dns_name"] unless inst_map["network"].nil? || inst_map["network"].empty?
108
+ {:aws_launch_time=>inst_map["created_at"],
109
+ :tags=>{},
110
+ :aws_reservation_id=>"",
111
+ :aws_owner=>self.account,
112
+ :ami_launch_index=>"",
113
+ :aws_availability_zone=>"",
114
+ :aws_groups=>inst_map["netfilter_group_id"],
115
+ :ssh_key_name=>inst_map["ssh_key_pair"],
116
+ :virtualization_type=>"",
117
+ :placement_group_name=>"",
118
+ :aws_instance_id=>inst_map["id"],
119
+ :aws_product_codes=>[],
120
+ :client_token=>"",
121
+ :aws_state_code=>0,
122
+ :aws_image_id=>inst_map["image_id"],
123
+ :dns_name=>dns_name,
124
+ :aws_instance_type=>inst_map["instance_spec_id"],
125
+ :aws_state=>inst_map["state"],
126
+ :private_dns_name=>private_dns_name,
127
+ :aws_reason=>""}
128
+ }
129
+ }
130
+ end
131
+
132
+ def terminate_instances(list=[])
133
+ File.open(self.terminate_instances_file) { |file|
134
+ JSON.parse(file.readlines.join.to_s).map { |instance_id|
135
+ {:aws_current_state_name=>"",
136
+ :aws_prev_state_name=>"",
137
+ :aws_prev_state_code=>0,
138
+ :aws_current_state_code=>0,
139
+ :aws_instance_id=>instance_id}
140
+ }
141
+ }
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,206 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+
4
+ module Tama
5
+ module Apis
6
+ module InstanceHashBuilder
7
+ # Takes the response from wakame-vdc's /api/instances
8
+ # and builds a hash similar to right_aws' describe_instances
9
+ # but changes the layout of IP addresses
10
+ def build_instances_hash(w_response,account_id)
11
+ w_response.first["results"].map { |inst_map|
12
+ # Build the final hash to return
13
+ # This hash is based on right_aws' describe_instances but
14
+ # changes the ip_address format
15
+ {:aws_kernel_id=>"",
16
+ :aws_launch_time=>inst_map["created_at"],
17
+ :tags=>{},
18
+ :aws_reservation_id=>"",
19
+ :aws_owner=>account_id,
20
+ :instance_lifecycle=>"",
21
+ :block_device_mappings=>[{:ebs_volume_id=>"", :ebs_status=>"", :ebs_attach_time=>"", :ebs_delete_on_termination=>false, :device_name=>""}],
22
+ :ami_launch_index=>"",
23
+ :root_device_name=>"",
24
+ :aws_ramdisk_id=>"",
25
+ :aws_availability_zone=>inst_map["host_node"],
26
+ :aws_groups=>inst_map["security_groups"],
27
+ :spot_instance_request_id=>"",
28
+ :ssh_key_name=>inst_map["ssh_key_pair"],
29
+ :virtualization_type=>"",
30
+ :placement_group_name=>"",
31
+ :requester_id=>"",
32
+ :aws_instance_id=>inst_map["id"],
33
+ :aws_product_codes=>[],
34
+ :client_token=>"",
35
+ :private_ip_address=>private_ip(inst_map),
36
+ :architecture=>inst_map["arch"],
37
+ :aws_state_code=>0,
38
+ :aws_image_id=>inst_map["image_id"],
39
+ :root_device_type=>"",
40
+ :ip_address=>ip_list(inst_map),
41
+ :dns_name=>dns_list(inst_map),
42
+ :monitoring_state=>"",
43
+ :aws_instance_type=>inst_map["instance_spec_id"],
44
+ :aws_state=>inst_map["state"],
45
+ :private_dns_name=>private_dns(inst_map),
46
+ :aws_reason=>""
47
+ }
48
+ }
49
+ end
50
+
51
+ def dns_list(inst_map, separator = ",")
52
+ ip_address = []
53
+ inst_map["network"].each { |network|
54
+ ip_address << "#{network["network_name"]}=#{network["dns_name"]}" unless network["network_name"].nil? || network["dns_name"].nil?
55
+ ip_address << "#{network["nat_network_name"]}=#{network["nat_#{"dns_name"}"]}" unless network["nat_network_name"].nil? || network["nat_dns_name"].nil?
56
+ }
57
+ ip_address.uniq.join(separator)
58
+ end
59
+
60
+ def ip_list(inst_map, separator = ",")
61
+ ip_address = []
62
+ inst_map["network"].each { |network|
63
+ ip_address << "#{network["network_name"]}=#{network["ipaddr"].first}" unless network["network_name"].nil? || network["ipaddr"].nil?
64
+ ip_address << "#{network["nat_network_name"]}=#{network["nat_ipaddr"].first}" unless network["nat_network_name"].nil? || network["nat_ipaddr"].nil?
65
+ }
66
+ ip_address.uniq.join(separator)
67
+ end
68
+
69
+ def private_dns(inst_map, private_network = self.private_network_name)
70
+ output = inst_map["network"].find(lambda { {"dns_name" => ""} }) { |network|
71
+ network["network_name"] == private_network
72
+ }
73
+ output["dns_name"]
74
+ end
75
+
76
+ def private_ip(inst_map, private_network = self.private_network_name)
77
+ output = inst_map["network"].find(lambda { {"ipaddr" => [""]} }) { |network|
78
+ network["network_name"] == private_network
79
+ }
80
+ output["ipaddr"].first
81
+ end
82
+ end
83
+
84
+ class WakameApi
85
+ attr_accessor :host
86
+ attr_accessor :port
87
+ attr_accessor :protocol
88
+ attr_accessor :account
89
+ attr_accessor :private_network_name
90
+
91
+ Protocols = ["http","https"]
92
+
93
+ include InstanceHashBuilder
94
+
95
+ def initialize(account = nil,host = nil, port = nil, protocol = "http",private_network_name = "nw-data")
96
+ raise ArgumentError, "Unknown protocol: #{protocol}. Can be either of the following: #{Protocols.join(",")}" unless Protocols.member?(protocol)
97
+
98
+ self.host = host
99
+ self.port = port
100
+ self.protocol = protocol
101
+ self.account = account
102
+
103
+ self.private_network_name = private_network_name
104
+ end
105
+
106
+ def show_host_nodes(list = [], account = self.account)
107
+ make_request("#{self.web_api}/api/host_nodes",Net::HTTP::Get,account, list).first["results"]
108
+ end
109
+ alias :describe_host_nodes :show_host_nodes
110
+
111
+ def show_instance_specs(list = [], account = self.account)
112
+ make_request("#{self.web_api}/api/instance_specs",Net::HTTP::Get,account, list).first["results"]
113
+ end
114
+ alias :describe_instance_specs :show_instance_specs
115
+
116
+ # This method could be taken care of by the EC2 adapter but since we
117
+ # want to show ip addresses differently from EC2, we add it to the
118
+ # Wakame part of Tama
119
+ def show_instances(list = [])
120
+ instances = make_request("#{self.web_api}/api/instances",Net::HTTP::Get,account, list)
121
+ build_instances_hash(instances,account)
122
+ end
123
+ alias :describe_instances :show_instances
124
+
125
+ def make_request(uri,type,accesskey,list = [])
126
+ api = URI.parse(uri)
127
+
128
+ req = type.new(api.path)
129
+ req.add_field("X_VDC_ACCOUNT_UUID", accesskey)
130
+
131
+ req.body = ""
132
+ #req.set_form_data(form_data) unless form_data.nil?
133
+
134
+ session = Net::HTTP.new(api.host, api.port)
135
+ session.use_ssl = (self.protocol == "https")
136
+
137
+ res = session.start do |http|
138
+ http.request(req)
139
+ end
140
+
141
+ body = JSON.parse(res.body)
142
+ body.first["results"].delete_if {|item| not list.member?(item["id"])} unless list.empty?
143
+
144
+ body
145
+ end
146
+
147
+ def web_api
148
+ protocol.to_s + "://" + host.to_s + ":" + port.to_s
149
+ end
150
+ end
151
+
152
+ class WakameApiTest
153
+ attr_accessor :host_nodes_file
154
+ attr_accessor :show_instance_specs_file
155
+ attr_accessor :show_instances_file
156
+ attr_accessor :private_network_name
157
+ attr_accessor :account
158
+
159
+ alias :describe_instance_specs_file :show_instance_specs_file
160
+ alias :describe_instance_specs_file= :show_instance_specs_file=
161
+ alias :describe_instances_file :show_instances_file
162
+ alias :describe_instances_file= :show_instances_file=
163
+ alias :describe_host_nodes_file :host_nodes_file
164
+ alias :describe_host_nodes_file= :host_nodes_file=
165
+
166
+ include InstanceHashBuilder
167
+
168
+ def initialize
169
+ self.host_nodes_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/host_nodes.json"
170
+ self.show_instance_specs_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/show_instance_specs.json"
171
+ self.show_instances_file = "#{File.expand_path(File.dirname(__FILE__))}/../../test/test_files/describe_instances.json"
172
+
173
+ self.account = "a-shpoolxx"
174
+ self.private_network_name = "nw-data"
175
+ end
176
+
177
+ def show_host_nodes(list = [])
178
+ read_file(self.host_nodes_file, list).first["results"]
179
+ end
180
+ alias :describe_host_nodes :show_host_nodes
181
+
182
+ def show_instance_specs(list = [])
183
+ read_file(self.show_instance_specs_file, list).first["results"]
184
+ end
185
+ alias :describe_instance_specs :show_instance_specs
186
+
187
+ # This method could be taken care of by the EC2 adapter but since we
188
+ # want to show ip addresses differently from EC2, we add it to the
189
+ # Wakame part of Tama
190
+ def show_instances(list = [])
191
+ instances = read_file(self.show_instances_file, list)
192
+ build_instances_hash(instances,self.account)
193
+ end
194
+ alias :describe_instances :show_instances
195
+
196
+ private
197
+ def read_file(file,list = [])
198
+ File.open(file) { |file|
199
+ res = JSON.parse(file.readlines.join.to_s)
200
+ res.first["results"].delete_if {|item| not list.member?(item["id"])} unless list.empty?
201
+ res
202
+ }
203
+ end
204
+ end
205
+ end
206
+ end