require 'httparty' require 'json' module GTAPI class GaptoolServer include HTTParty default_timeout 600 DEFAULT_AWS_AZ = 'us-west-2c' attr_reader :version def initialize(user, apikey, uri, default_aws_az=DEFAULT_AWS_AZ) @auth = { 'X-GAPTOOL-USER' => user, 'X-GAPTOOL-KEY' => apikey} default_aws_az = default_aws_az || DEFAULT_AWS_AZ @default_aws_az = default_aws_az @default_aws_region = default_aws_az[0..-2] @version = File.read(File.realpath(File.join(File.dirname(__FILE__), "..", 'VERSION'))).strip GaptoolServer.base_uri uri end def rehash() options = {:body => '', :headers => @auth} JSON::parse self.class.post("/rehash", options) end def getonenode(id) options = {:headers => @auth} JSON::parse self.class.get("/instance/#{id}", options) end def getenvroles(role, environment) options = {:headers => @auth} JSON::parse self.class.get("/hosts/#{role}/#{environment}", options) end def addnode(zone, itype, role, environment, mirror=nil, security_group=nil, ami=nil, chef_repo=nil, chef_branch=nil) @body = { 'zone' => zone || @default_aws_az, 'itype' => itype, 'role' => role, 'environment' => environment, 'ami' => ami, 'chef_repo' => chef_repo, 'chef_branch' => chef_branch } unless security_group.nil? @body['security_group'] = security_group end @body = @body.to_json options = { :body => @body, :headers => @auth} JSON::parse self.class.post("/init", options) end def terminatenode(id, zone) @body = {'id' => id, 'zone' => zone || @default_aws_region}.to_json options = {:body => @body, :headers => @auth} JSON::parse self.class.post("/terminate", options) end def getappnodes(app, environment) options = {:headers => @auth} role = JSON::parse(self.class.get("/apps", options))["app:#{app}"]['role'] JSON::parse self.class.get("/hosts/#{role}/#{environment}", options) end def ssh(role, environment, id) options = { :headers => @auth} JSON::parse self.class.get("/ssh/#{role}/#{environment}/#{id}", options) end def getrolenodes(role) options = { :headers => @auth} JSON::parse self.class.get("/hosts/#{role}", options) end def getenvnodes(environment) options = { :headers => @auth} JSON::parse self.class.get("/hosts/ALL/#{environment}", options) end def getallnodes() options = { :headers => @auth} JSON::parse self.class.get("/hosts", options) end def api_version() options = { :headers => @auth } JSON::parse self.class.get("/version", options) end end end