fog 0.0.73 → 0.0.74

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/Gemfile +1 -0
  2. data/Gemfile.lock +69 -0
  3. data/Rakefile +121 -74
  4. data/fog.gemspec +421 -490
  5. data/lib/fog.rb +4 -0
  6. data/lib/fog/aws/requests/s3/get_object.rb +12 -2
  7. data/lib/fog/aws/s3.rb +16 -10
  8. data/lib/fog/ssh.rb +42 -0
  9. data/lib/fog/terremark.rb +53 -2
  10. data/lib/fog/terremark/bin.rb +15 -4
  11. data/lib/fog/terremark/models/server.rb +54 -0
  12. data/lib/fog/terremark/models/servers.rb +53 -0
  13. data/lib/fog/terremark/models/task.rb +41 -0
  14. data/lib/fog/terremark/models/tasks.rb +56 -0
  15. data/lib/fog/terremark/parsers/get_internet_services.rb +58 -0
  16. data/lib/fog/terremark/parsers/get_node_services.rb +34 -0
  17. data/lib/fog/terremark/parsers/{get_public_Ips.rb → get_public_ips.rb} +0 -0
  18. data/lib/fog/terremark/parsers/get_vdc.rb +4 -4
  19. data/lib/fog/terremark/parsers/public_ip.rb +24 -0
  20. data/lib/fog/terremark/parsers/vapp.rb +16 -16
  21. data/lib/fog/terremark/requests/add_node_service.rb +1 -1
  22. data/lib/fog/terremark/requests/get_catalog.rb +1 -1
  23. data/lib/fog/terremark/requests/get_internet_services.rb +41 -0
  24. data/lib/fog/terremark/requests/get_node_services.rb +36 -0
  25. data/lib/fog/terremark/requests/get_organization.rb +1 -1
  26. data/lib/fog/terremark/requests/get_organizations.rb +3 -1
  27. data/lib/fog/terremark/requests/get_public_ip.rb +35 -0
  28. data/lib/fog/terremark/requests/get_tasks_list.rb +1 -1
  29. data/lib/fog/terremark/requests/get_vdc.rb +1 -1
  30. data/lib/fog/terremark/requests/instantiate_vapp_template.rb +9 -4
  31. data/lib/fog/terremark/requests/shutdown.rb +1 -1
  32. data/spec/aws/models/s3/files_spec.rb +1 -0
  33. metadata +57 -99
  34. data/.document +0 -3
  35. data/.gitignore +0 -7
  36. data/VERSION.yml +0 -5
data/lib/fog.rb CHANGED
@@ -8,6 +8,7 @@ require 'hmac-sha1'
8
8
  require 'hmac-sha2'
9
9
  require 'json'
10
10
  require 'mime/types'
11
+ require 'net/ssh'
11
12
  require 'nokogiri'
12
13
  require 'time'
13
14
 
@@ -21,6 +22,7 @@ require 'fog/collection'
21
22
  require 'fog/connection'
22
23
  require 'fog/model'
23
24
  require 'fog/parser'
25
+ require 'fog/ssh'
24
26
  require 'fog/aws'
25
27
  require 'fog/rackspace'
26
28
  require 'fog/slicehost'
@@ -28,6 +30,8 @@ require 'fog/terremark'
28
30
 
29
31
  module Fog
30
32
 
33
+ VERSION = '0.0.74'
34
+
31
35
  module Mock
32
36
  @delay = 1
33
37
  def self.delay
@@ -108,9 +108,19 @@ module Fog
108
108
  end
109
109
 
110
110
  def get_object_url(bucket_name, object_name, expires)
111
- raise MockNotImplemented.new("Contributions welcome!")
111
+ unless bucket_name
112
+ raise ArgumentError.new('bucket_name is required')
113
+ end
114
+ unless object_name
115
+ raise ArgumentError.new('object_name is required')
116
+ end
117
+ url({
118
+ :headers => {},
119
+ :host => "#{bucket_name}.#{@host}",
120
+ :method => 'GET',
121
+ :path => CGI.escape(object_name)
122
+ }, expires)
112
123
  end
113
-
114
124
  end
115
125
  end
116
126
  end
@@ -61,7 +61,19 @@ module Fog
61
61
  Mock.reset_data(keys)
62
62
  end
63
63
 
64
+ module Utils
65
+ def url(params, expires)
66
+ params[:headers]['Date'] = expires.to_i
67
+ query = [params[:query]].compact
68
+ query << "AWSAccessKeyId=#{@aws_access_key_id}"
69
+ query << "Signature=#{CGI.escape(signature(params))}"
70
+ query << "Expires=#{params[:headers]['Date']}"
71
+ "http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
72
+ end
73
+ end
74
+
64
75
  class Mock
76
+ include Utils
65
77
 
66
78
  def self.data
67
79
  @data ||= Hash.new do |hash, key|
@@ -82,9 +94,13 @@ module Fog
82
94
  @data = self.class.data[@aws_access_key_id]
83
95
  end
84
96
 
97
+ def signature(params)
98
+ "foo"
99
+ end
85
100
  end
86
101
 
87
102
  class Real
103
+ include Utils
88
104
 
89
105
  # Initialize connection to S3
90
106
  #
@@ -174,16 +190,6 @@ DATA
174
190
  hmac = @hmac.update(string_to_sign)
175
191
  signature = Base64.encode64(hmac.digest).chomp!
176
192
  end
177
-
178
- def url(params, expires)
179
- params[:headers]['Date'] = expires.to_i
180
- query = [params[:query]].compact
181
- query << "AWSAccessKeyId=#{@aws_access_key_id}"
182
- query << "Signature=#{CGI.escape(signature(params))}"
183
- query << "Expires=#{params[:headers]['Date']}"
184
- "http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
185
- end
186
-
187
193
  end
188
194
  end
189
195
  end
@@ -0,0 +1,42 @@
1
+ module Fog
2
+
3
+ class SSH
4
+
5
+ def initialize(address, username, options = {})
6
+ unless options[:keys] || options[:password]
7
+ raise ArgumentError.new(':keys or :password are required to initialize SSH')
8
+ end
9
+ @address = address
10
+ @username = username
11
+ @options = options.merge!(:paranoid => false)
12
+ end
13
+
14
+ def run(commands)
15
+ commands = [*commands]
16
+ results = []
17
+ Net::SSH.start(@address, @username, @options) do |ssh|
18
+ commands.each do |command|
19
+ ssh.open_channel do |channel|
20
+ channel.request_pty
21
+ result = { :command => command }
22
+ channel.exec(command.sub(/^sudo/, %q{sudo -p 'fog sudo password:'})) do |channel, success|
23
+ channel.on_data do |channel, data|
24
+ if data.strip == 'fog sudo password:'
25
+ channel.send_data("#{@options[:password]}\n")
26
+ else
27
+ result[:data] ||= ''
28
+ result[:data] << data
29
+ end
30
+ end
31
+ end
32
+ results << result
33
+ end
34
+ ssh.loop
35
+ end
36
+ end
37
+ results
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,11 +1,30 @@
1
1
  module Fog
2
2
  module Terremark
3
3
 
4
+ module Parser
5
+
6
+ def parse(data)
7
+ case data['type']
8
+ when 'application/vnd.vmware.vcloud.vApp+xml'
9
+ servers.new(data.merge!(:connection => self))
10
+ else
11
+ data
12
+ end
13
+ end
14
+
15
+ end
16
+
4
17
  def self.new(options={})
5
18
 
6
19
  unless @required
20
+ require 'fog/terremark/models/server'
21
+ require 'fog/terremark/models/servers'
22
+ require 'fog/terremark/models/task'
23
+ require 'fog/terremark/models/tasks'
7
24
  require 'fog/terremark/parsers/get_catalog'
8
25
  require 'fog/terremark/parsers/get_catalog_item'
26
+ require 'fog/terremark/parsers/get_internet_services'
27
+ require 'fog/terremark/parsers/get_node_services'
9
28
  require 'fog/terremark/parsers/get_organization'
10
29
  require 'fog/terremark/parsers/get_organizations'
11
30
  require 'fog/terremark/parsers/get_public_ips'
@@ -15,6 +34,7 @@ module Fog
15
34
  require 'fog/terremark/parsers/instantiate_vapp_template'
16
35
  require 'fog/terremark/parsers/internet_service'
17
36
  require 'fog/terremark/parsers/node_service'
37
+ require 'fog/terremark/parsers/public_ip'
18
38
  require 'fog/terremark/parsers/task'
19
39
  require 'fog/terremark/parsers/vapp'
20
40
  require 'fog/terremark/requests/add_internet_service'
@@ -26,8 +46,11 @@ module Fog
26
46
  require 'fog/terremark/requests/deploy_vapp'
27
47
  require 'fog/terremark/requests/get_catalog'
28
48
  require 'fog/terremark/requests/get_catalog_item'
49
+ require 'fog/terremark/requests/get_internet_services'
50
+ require 'fog/terremark/requests/get_node_services'
29
51
  require 'fog/terremark/requests/get_organization'
30
52
  require 'fog/terremark/requests/get_organizations'
53
+ require 'fog/terremark/requests/get_public_ip'
31
54
  require 'fog/terremark/requests/get_public_ips'
32
55
  require 'fog/terremark/requests/get_task'
33
56
  require 'fog/terremark/requests/get_tasks_list'
@@ -56,6 +79,7 @@ module Fog
56
79
  end
57
80
 
58
81
  class Mock
82
+ include Fog::Terremark::Parser
59
83
 
60
84
  def self.data
61
85
  @data ||= Hash.new do |hash, key|
@@ -77,12 +101,22 @@ module Fog
77
101
  end
78
102
 
79
103
  class Real
104
+ include Fog::Terremark::Parser
80
105
 
81
106
  def initialize(options={})
82
107
  @terremark_password = options[:terremark_password]
83
108
  @terremark_username = options[:terremark_username]
84
- @host = options[:host] || "services.vcloudexpress.terremark.com"
85
- @path = options[:path] || "/api/v0.8"
109
+ @terremark_service = options[:terremark_service] || :vcloud
110
+ case options[:terremark_service]
111
+ when :ecloud
112
+ @host = options[:host] || "services.enterprisecloud.terremark.com"
113
+ @path = options[:path] || "/api/v0.8a-ext2.0"
114
+ when :vcloud
115
+ @host = options[:host] || "services.vcloudexpress.terremark.com"
116
+ @path = options[:path] || "/api/v0.8"
117
+ else
118
+ raise "Unsupported Terremark Service"
119
+ end
86
120
  @port = options[:port] || 443
87
121
  @scheme = options[:scheme] || 'https'
88
122
  @cookie = get_organizations.headers['Set-Cookie']
@@ -147,6 +181,23 @@ module Fog
147
181
  end
148
182
  end
149
183
 
184
+ def default_tasks_list_id
185
+ if default_organization_id
186
+ @default_tasks_list_id ||= begin
187
+ task_lists = get_organization(default_organization_id).body['Links'].select {|link|
188
+ link['type'] == 'application/vnd.vmware.vcloud.tasksList+xml'
189
+ }
190
+ if task_lists.length == 1
191
+ task_lists.first['href'].split('/').last.to_i
192
+ else
193
+ nil
194
+ end
195
+ end
196
+ else
197
+ nil
198
+ end
199
+ end
200
+
150
201
  private
151
202
 
152
203
  def request(params)
@@ -6,19 +6,30 @@ module Terremark
6
6
  true
7
7
  end
8
8
 
9
+ def terremark_service
10
+ @terremark_service ||= begin
11
+ Fog.credentials[:terremark_service] || :vcloud
12
+ end
13
+ end
14
+
9
15
  def [](service)
10
16
  @@connections ||= Hash.new do |hash, key|
11
17
  credentials = Fog.credentials.reject do |k,v|
12
18
  ![:terremark_username, :terremark_password].include?(k)
13
19
  end
14
- hash[key] = case key
15
- when :vcloud
16
- Fog::Terremark.new(credentials)
17
- end
20
+ hash[key] = Fog::Terremark.new(credentials.merge(:terremark_service => terremark_service))
18
21
  end
19
22
  @@connections[service]
20
23
  end
21
24
 
25
+ def servers
26
+ self[terremark_service].servers
27
+ end
28
+
29
+ def tasks
30
+ self[terremark_service].tasks
31
+ end
32
+
22
33
  else
23
34
 
24
35
  def initialized?
@@ -0,0 +1,54 @@
1
+ require 'fog/model'
2
+
3
+ module Fog
4
+ module Terremark
5
+
6
+ class Server < Fog::Model
7
+
8
+ identity :id
9
+
10
+ attribute :name
11
+ attribute :status
12
+
13
+ def destroy
14
+ requires :id
15
+ task = connection.tasks.new(connection.power_off(@id).body)
16
+ task.wait_for { ready? }
17
+ connection.delete_vapp(@id)
18
+ true
19
+ end
20
+
21
+ # { '0' => 'Being created', '2' => 'Powered Off', '4' => 'Powered On'}
22
+ def ready?
23
+ @status == '2'
24
+ end
25
+
26
+ def reboot
27
+ requires :id
28
+ connection.reset(@id)
29
+ true
30
+ end
31
+
32
+ def save
33
+ requires :name
34
+ data = connection.instantiate_vapp(@name)
35
+ merge_attributes(data.body)
36
+ deploy_task = connection.tasks.new(connection.deploy_vapp(@id).body)
37
+ deploy_task.wait_for { ready? }
38
+ power_on_task = connection.tasks.new(connection.power_on(@id).body)
39
+ power_on_task.wait_for { ready? }
40
+ true
41
+ end
42
+
43
+ private
44
+
45
+ def href=(new_href)
46
+ @id = new_href.split('/').last.to_i
47
+ end
48
+
49
+ def type=(new_type); end
50
+
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,53 @@
1
+ require 'fog/collection'
2
+ require 'fog/terremark/models/server'
3
+
4
+ module Fog
5
+ module Terremark
6
+
7
+ class Mock
8
+ def servers
9
+ Fog::Terremark::Servers.new(:connection => self)
10
+ end
11
+ end
12
+
13
+ class Real
14
+ def servers
15
+ Fog::Terremark::Servers.new(:connection => self)
16
+ end
17
+ end
18
+
19
+ class Servers < Fog::Collection
20
+
21
+ model Fog::Terremark::Server
22
+
23
+ def all
24
+ data = connection.get_vdc(vdc_id).body['ResourceEntities'].select do |entity|
25
+ entity['type'] == 'application/vnd.vmware.vcloud.vApp+xml'
26
+ end
27
+ load(data)
28
+ end
29
+
30
+ def get(server_id)
31
+ if server_id && server = connection.get_vapp(server_id).body
32
+ new(server)
33
+ elsif !server_id
34
+ nil
35
+ end
36
+ rescue Excon::Errors::Forbidden
37
+ nil
38
+ end
39
+
40
+ def vdc_id
41
+ @vdc_id ||= connection.default_vdc_id
42
+ end
43
+
44
+ private
45
+
46
+ def vdc_id=(new_vdc_id)
47
+ @vdc_id = new_vdc_id
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,41 @@
1
+ require 'fog/model'
2
+
3
+ module Fog
4
+ module Terremark
5
+
6
+ class Task < Fog::Model
7
+
8
+ identity :id
9
+
10
+ attribute :end_time, 'endTime'
11
+ attribute :owner, 'Owner'
12
+ attribute :result, 'Result'
13
+ attribute :start_time, 'startTime'
14
+ attribute :status
15
+
16
+ def initialize(attributes = {})
17
+ new_owner = attributes.delete('Owner')
18
+ new_result = attributes.delete('Result')
19
+ super
20
+ @owner = connection.parse(new_owner)
21
+ if new_result
22
+ @result = connection.parse(new_result)
23
+ end
24
+ end
25
+
26
+ def ready?
27
+ @status == 'success'
28
+ end
29
+
30
+ private
31
+
32
+ def href=(new_href)
33
+ @id = new_href.split('/').last.to_i
34
+ end
35
+
36
+ def type=(new_type); end
37
+
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,56 @@
1
+ require 'fog/collection'
2
+ require 'fog/terremark/models/server'
3
+
4
+ module Fog
5
+ module Terremark
6
+
7
+ class Mock
8
+ def tasks
9
+ Fog::Terremark::Tasks.new(:connection => self)
10
+ end
11
+ end
12
+
13
+ class Real
14
+ def tasks
15
+ Fog::Terremark::Tasks.new(:connection => self)
16
+ end
17
+ end
18
+
19
+ class Tasks < Fog::Collection
20
+
21
+ model Fog::Terremark::Task
22
+
23
+ def all
24
+ data = connection.get_tasks_list(task_list_id).body['Tasks']
25
+ load(data)
26
+ end
27
+
28
+ def get(task_id)
29
+ if task_id && task = connection.get_task(task_id).body
30
+ new(task)
31
+ elsif !task_id
32
+ nil
33
+ end
34
+ rescue Excon::Errors::Forbidden
35
+ nil
36
+ end
37
+
38
+ def task_list_id
39
+ @task_list_id ||=
40
+ if connection.default_organization_id && organization = connection.get_organization(connection.default_organization_id).body
41
+ organization['Links'].detect {|link| link['type'] == 'application/vnd.vmware.vcloud.tasksList+xml'}['href'].split('/').last.to_i
42
+ else
43
+ nil
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def task_list_id=(new_task_list_id)
50
+ @task_list_id = new_task_list_id
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end