Class: GTAPI::GaptoolServer

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/gaptool-api.rb

Constant Summary collapse

DEFAULT_AWS_AZ =
'us-west-2c'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, apikey, uri, default_aws_az = DEFAULT_AWS_AZ) ⇒ GaptoolServer

Returns a new instance of GaptoolServer.



11
12
13
14
15
16
17
18
# File 'lib/gaptool-api.rb', line 11

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

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/gaptool-api.rb', line 9

def version
  @version
end

Instance Method Details

#addnode(zone, itype, role, environment, mirror = nil, security_group = nil, ami = nil, chef_repo = nil, chef_branch = nil, chef_runlist = nil, no_terminate = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gaptool-api.rb', line 58

def addnode(zone, itype, role, environment, mirror=nil, security_group=nil,
            ami=nil, chef_repo=nil, chef_branch=nil, chef_runlist=nil,
            no_terminate=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
  unless chef_runlist.nil? || chef_runlist.empty?
    body['chef_runlist'] = [*chef_runlist]
  end
  if no_terminate
    body['terminate'] = false
  end
  post('/init', {body: body.to_json})
end

#api_versionObject



106
107
108
# File 'lib/gaptool-api.rb', line 106

def api_version()
  get("/version")
end

#call(verb, url, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gaptool-api.rb', line 20

def call(verb, url, opts={})
  options = opts || {}
  options[:headers] ||= {}
  options[:headers].merge!(@auth)
  options[:headers]['Accept'] = 'application/json'
  options[:headers]['Content-Type'] = 'application/json'
  options[:headers]['X-GAPTOOL-VERSION'] = @version
  case verb
  when :post
    options[:body] ||= ''
  end
  JSON.parse(self.class.send(verb, url, options).body)
end

#get(url, opts = {}) ⇒ Object



34
35
36
# File 'lib/gaptool-api.rb', line 34

def get(url, opts={})
  call(:get, url, opts)
end

#getallnodes(params) ⇒ Object



102
103
104
# File 'lib/gaptool-api.rb', line 102

def getallnodes(params)
  get("/hosts", {query: params})
end

#getappnodes(app, environment, params = {}) ⇒ Object



90
91
92
# File 'lib/gaptool-api.rb', line 90

def getappnodes(app, environment, params={})
  get("/app/#{app}/#{environment}/hosts", {query: params})
end

#getenvnodes(environment, params = {}) ⇒ Object



98
99
100
# File 'lib/gaptool-api.rb', line 98

def getenvnodes(environment, params={})
  get("/hosts/ALL/#{environment}", {query: params})
end

#getenvroles(role, environment, params = {}) ⇒ Object



54
55
56
# File 'lib/gaptool-api.rb', line 54

def getenvroles(role, environment, params={})
  get("/hosts/#{role}/#{environment}", {query: params})
end

#getonenode(id) ⇒ Object



50
51
52
# File 'lib/gaptool-api.rb', line 50

def getonenode(id)
  get("/instance/#{id}")
end

#getrolenodes(role, params = {}) ⇒ Object



94
95
96
# File 'lib/gaptool-api.rb', line 94

def getrolenodes(role, params={})
  get("/hosts/#{role}", {query: params})
end

#patch(url, opts = {}) ⇒ Object



42
43
44
# File 'lib/gaptool-api.rb', line 42

def patch(url, opts={})
  call(:patch, url, opts)
end

#post(url, opts = {}) ⇒ Object



38
39
40
# File 'lib/gaptool-api.rb', line 38

def post(url, opts={})
  call(:post, url, opts)
end

#rehashObject



46
47
48
# File 'lib/gaptool-api.rb', line 46

def rehash()
  post("/rehash")
end

#setparameters(instance, params = {}) ⇒ Object



86
87
88
# File 'lib/gaptool-api.rb', line 86

def setparameters(instance, params={})
  patch("/instance/#{instance}", {body: params.to_json})
end

#terminatenode(id, zone) ⇒ Object



82
83
84
# File 'lib/gaptool-api.rb', line 82

def terminatenode(id, zone)
  post('/terminate', {body: {'id' => id, 'zone' => zone || @default_aws_region}.to_json})
end