mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[new_servers] first pass at basic functionality
This commit is contained in:
parent
53ce447574
commit
abdc151081
14 changed files with 396 additions and 12 deletions
12
bin/fog
12
bin/fog
|
@ -3,20 +3,12 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'fog')
|
|||
require 'irb'
|
||||
require 'yaml'
|
||||
require File.join('fog', 'credentials')
|
||||
require File.join('fog', 'bin')
|
||||
|
||||
Fog.credential = ARGV.first ? :"#{ARGV.first}" : :default
|
||||
Fog.credential = ARGV.first ? :"#{ARGV.first}" : :default
|
||||
unless Fog.credentials
|
||||
exit
|
||||
end
|
||||
|
||||
require File.join('fog', 'aws', 'bin')
|
||||
require File.join('fog', 'local', 'bin')
|
||||
require File.join('fog', 'rackspace', 'bin')
|
||||
require File.join('fog', 'slicehost', 'bin')
|
||||
require File.join('fog', 'terremark', 'bin')
|
||||
require File.join('fog', 'vcloud', 'bin')
|
||||
require File.join('fog', 'bluebox', 'bin')
|
||||
require 'fog/bin'
|
||||
|
||||
if ARGV.length > 1
|
||||
print(instance_eval(ARGV[1..-1].join(' ')).to_json)
|
||||
|
|
|
@ -30,6 +30,7 @@ require 'fog/ssh'
|
|||
require 'fog/aws'
|
||||
require 'fog/bluebox'
|
||||
require 'fog/local'
|
||||
require 'fog/new_servers'
|
||||
require 'fog/rackspace'
|
||||
require 'fog/slicehost'
|
||||
require 'fog/terremark'
|
||||
|
|
|
@ -1,11 +1,18 @@
|
|||
require File.join(File.dirname(__FILE__), 'credentials')
|
||||
require 'fog/aws/bin'
|
||||
require 'fog/local/bin'
|
||||
require 'fog/new_servers/bin'
|
||||
require 'fog/rackspace/bin'
|
||||
require 'fog/slicehost/bin'
|
||||
require 'fog/terremark/bin'
|
||||
require 'fog/vcloud/bin'
|
||||
require 'fog/bluebox/bin'
|
||||
|
||||
module Fog
|
||||
class << self
|
||||
|
||||
def services
|
||||
services = []
|
||||
[::AWS, ::Local, ::Rackspace, ::Slicehost, ::Terremark, ::Vcloud, ::Bluebox].each do |service|
|
||||
[::AWS, ::Local, ::NewServers, ::Rackspace, ::Slicehost, ::Terremark, ::Vcloud, ::Bluebox].each do |service|
|
||||
if service.initialized?
|
||||
services << service
|
||||
end
|
||||
|
|
|
@ -35,6 +35,8 @@ module Fog
|
|||
:bluebox_api_key: INTENTIONALLY_LEFT_BLANK
|
||||
:bluebox_customer_id: INTENTIONALLY_LEFT_BLANK
|
||||
:local_root: INTENTIONALLY_LEFT_BLANK
|
||||
:new_servers_password: INTENTIONALLY_LEFT_BLANK
|
||||
:new_servers_username: INTENTIONALLY_LEFT_BLANK
|
||||
:public_key_path: INTENTIONALLY_LEFT_BLANK
|
||||
:private_key_path: INTENTIONALLY_LEFT_BLANK
|
||||
:rackspace_api_key: INTENTIONALLY_LEFT_BLANK
|
||||
|
|
89
lib/fog/new_servers.rb
Normal file
89
lib/fog/new_servers.rb
Normal file
|
@ -0,0 +1,89 @@
|
|||
require 'fog/parser'
|
||||
|
||||
module Fog
|
||||
module NewServers
|
||||
extend Fog::Service
|
||||
|
||||
requires :new_servers_password
|
||||
requires :new_servers_username
|
||||
|
||||
model_path 'fog/new_servers/models'
|
||||
|
||||
request_path 'fog/new_servers/requests'
|
||||
request 'add_server'
|
||||
request 'cancel_server'
|
||||
request 'get_server'
|
||||
request 'list_images'
|
||||
request 'list_plans'
|
||||
request 'list_servers'
|
||||
request 'reboot_server'
|
||||
|
||||
class Mock
|
||||
include Collections
|
||||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, key|
|
||||
hash[key] = {}
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset_data(keys=data.keys)
|
||||
for key in [*keys]
|
||||
data.delete(key)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(options={})
|
||||
@new_server_username = options[:new_servers_username]
|
||||
@data = self.class.data[@new_server_username]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Real
|
||||
include Collections
|
||||
|
||||
def initialize(options={})
|
||||
@new_servers_password = options[:new_servers_password]
|
||||
@new_servers_username = options[:new_servers_username]
|
||||
@host = options[:host] || "noc.newservers.com"
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
||||
end
|
||||
|
||||
def reload
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
def request(params)
|
||||
params[:query] ||= {}
|
||||
params[:query].merge!({
|
||||
:password => @new_servers_password,
|
||||
:username => @new_servers_username
|
||||
})
|
||||
params[:headers] ||= {}
|
||||
case params[:method]
|
||||
when 'DELETE', 'GET', 'HEAD'
|
||||
params[:headers]['Accept'] = 'application/xml'
|
||||
when 'POST', 'PUT'
|
||||
params[:headers]['Content-Type'] = 'application/xml'
|
||||
end
|
||||
|
||||
begin
|
||||
response = @connection.request(params.merge!({:host => @host}))
|
||||
rescue Excon::Errors::Error => error
|
||||
raise case error
|
||||
when Excon::Errors::NotFound
|
||||
Fog::NewServers::NotFound.slurp(error)
|
||||
else
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/new_servers/bin.rb
Normal file
30
lib/fog/new_servers/bin.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module NewServers
|
||||
class << self
|
||||
if Fog.credentials[:new_servers_password] && Fog.credentials[:new_servers_username]
|
||||
|
||||
def initialized?
|
||||
true
|
||||
end
|
||||
|
||||
def [](service)
|
||||
@@connections ||= Hash.new do |hash, key|
|
||||
credentials = Fog.credentials.reject do |k,v|
|
||||
![:new_servers_password, :new_servers_username].include?(k)
|
||||
end
|
||||
hash[key] = case key
|
||||
when :new_servers
|
||||
Fog::NewServers.new(credentials)
|
||||
end
|
||||
end
|
||||
@@connections[service]
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
def initialized?
|
||||
false
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
39
lib/fog/new_servers/requests/add_server.rb
Normal file
39
lib/fog/new_servers/requests/add_server.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# Boot a new server
|
||||
#
|
||||
# ==== Parameters
|
||||
# * planId<~String> - The id of the plan to boot the server with
|
||||
# * options<~Hash>: optional extra arguments
|
||||
# * imageId<~String> - Optional image to boot server from
|
||||
# * name<~String> - Name to boot new server with
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'server'<~Hash>:
|
||||
# * 'id'<~String> - Id of the image
|
||||
#
|
||||
def add_server(plan_id, options = {})
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/addServer',
|
||||
:query => {'planId' => plan_id}.merge!(options)
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def add_server(server_id)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
36
lib/fog/new_servers/requests/cancel_server.rb
Normal file
36
lib/fog/new_servers/requests/cancel_server.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# Shutdown a running server
|
||||
#
|
||||
# ==== Parameters
|
||||
# * serverId<~String> - The id of the server to shutdown
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'server'<~Hash>:
|
||||
# * 'id'<~String> - Id of the image
|
||||
#
|
||||
def cancel_server(server_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/cancelServer',
|
||||
:query => {'serverId' => server_id}
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def cancel_server(server_id)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/new_servers/requests/get_server.rb
Normal file
42
lib/fog/new_servers/requests/get_server.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# List servers
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * server<~Hash>:
|
||||
# * 'id'<~String> - Id of the server
|
||||
# * 'ip'<~Hash>:
|
||||
# * 'address'<~String> - Address of the ip
|
||||
# * 'name'<~String> - Name of the ip
|
||||
# * 'login'<~Hash>:
|
||||
# * 'name'<~String> - Name of the login
|
||||
# * 'password'<~String> - Password of the login
|
||||
# * 'username'<~String> - Username of the login
|
||||
# * 'name'<~String> - Name of the server
|
||||
# * 'notes'<~String> - Notes about the server
|
||||
# * 'state'<~String> - State of the server
|
||||
#
|
||||
def get_server(server_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/getServer',
|
||||
:query => {'serverId' => server_id}
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def get_server(server_id)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
33
lib/fog/new_servers/requests/list_images.rb
Normal file
33
lib/fog/new_servers/requests/list_images.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# List images
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Array>:
|
||||
# * 'id'<~String> - Id of the image
|
||||
# * 'name'<~String> - Name of the image
|
||||
# * 'size'<~String> - Size of the image
|
||||
#
|
||||
def list_images
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/listImages'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def list_images
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
36
lib/fog/new_servers/requests/list_plans.rb
Normal file
36
lib/fog/new_servers/requests/list_plans.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# List available plans
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Array>:
|
||||
# * 'description'<~String> - Description of the plan
|
||||
# * 'id'<~String> - Id of the plan
|
||||
# * 'name'<~String> - Name of the plan
|
||||
# * 'rate'<~String> - Cost per hour of the plan
|
||||
# * 'os'<~String> - Operating system of the plan
|
||||
# * 'config'<~String> - Configuration of the plan
|
||||
#
|
||||
def list_plans
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/listPlans'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def list_plans
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
43
lib/fog/new_servers/requests/list_servers.rb
Normal file
43
lib/fog/new_servers/requests/list_servers.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# List servers
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Array>:
|
||||
# * server<~Hash>:
|
||||
# * 'id'<~String> - Id of the server
|
||||
# * 'ip'<~Hash>:
|
||||
# * 'address'<~String> - Address of the ip
|
||||
# * 'name'<~String> - Name of the ip
|
||||
# * 'login'<~Hash>:
|
||||
# * 'name'<~String> - Name of the login
|
||||
# * 'password'<~String> - Password of the login
|
||||
# * 'username'<~String> - Username of the login
|
||||
# * 'name'<~String> - Name of the server
|
||||
# * 'notes'<~String> - Notes about the server
|
||||
# * 'state'<~String> - State of the server
|
||||
#
|
||||
def list_servers
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/listServers'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def list_servers
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
30
lib/fog/new_servers/requests/reboot_server.rb
Normal file
30
lib/fog/new_servers/requests/reboot_server.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module NewServers
|
||||
class Real
|
||||
|
||||
# Reboot a running server
|
||||
#
|
||||
# ==== Parameters
|
||||
# * serverId<~String> - The id of the server to reboot
|
||||
#
|
||||
def reboot_server(server_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => 'api/rebootServer',
|
||||
:query => {'serverId' => server_id}
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def reboot_server(server_id)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -51,6 +51,10 @@ module Fog
|
|||
@stack.first
|
||||
end
|
||||
|
||||
def response
|
||||
body
|
||||
end
|
||||
|
||||
def start_element(name, attributes = [])
|
||||
@value = ''
|
||||
parsed_attributes = {}
|
||||
|
|
Loading…
Reference in a new issue