Class: OrchestratorClient

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrator_client.rb,
lib/orchestrator_client/version.rb

Defined Under Namespace

Classes: ApiError, BadResponse, Command, Config, ConfigError, Job, Jobs

Constant Summary collapse

VERSION =
'0.2.2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides, load_files = false) ⇒ OrchestratorClient

Returns a new instance of OrchestratorClient.



15
16
17
18
# File 'lib/orchestrator_client.rb', line 15

def initialize(overrides, load_files=false)
  @config = Config.new(overrides, load_files)
  @config.validate
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/orchestrator_client.rb', line 13

def config
  @config
end

Instance Method Details

#commandObject



65
66
67
# File 'lib/orchestrator_client.rb', line 65

def command
  @command ||= OrchestratorClient::Command.new(self)
end

#create_http(uri) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/orchestrator_client.rb', line 24

def create_http(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.ssl_version = :TLSv1
  http.ca_file = config['cacert']
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http
end

#get(location) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/orchestrator_client.rb', line 33

def get(location)
  uri = make_uri(location)
  https = create_http(uri)
  req = Net::HTTP::Get.new(uri)
  req['Content-Type'] = "application/json"
  req.add_field('X-Authentication', @config.token)
  res = https.request(req)

  if res.code != "200"
    raise OrchestratorClient::ApiError.make_error_from_response(res)
  end

  JSON.parse(res.body)
end

#jobsObject



69
70
71
# File 'lib/orchestrator_client.rb', line 69

def jobs
  @jobs ||= OrchestratorClient::Jobs.new(self)
end

#make_uri(path) ⇒ Object



20
21
22
# File 'lib/orchestrator_client.rb', line 20

def make_uri(path)
  URI.parse("#{config.root_url}#{path}")
end

#new_job(options, type = :deploy) ⇒ Object



73
74
75
# File 'lib/orchestrator_client.rb', line 73

def new_job(options, type = :deploy)
  OrchestratorClient::Job.new(self, options, type)
end

#post(location, body) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/orchestrator_client.rb', line 48

def post(location, body)
  uri = make_uri(location)
  https = create_http(uri)

  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = "application/json"
  req.add_field('X-Authentication', @config.token)
  req.body = body.to_json
  res = https.request(req)

  if res.code != "202"
    raise OrchestratorClient::ApiError.make_error_from_response(res)
  end

  JSON.parse(res.body)
end

#rootObject



84
85
86
# File 'lib/orchestrator_client.rb', line 84

def root
  get(url)
end

#run_task(options) ⇒ Object



77
78
79
80
81
82
# File 'lib/orchestrator_client.rb', line 77

def run_task(options)
  job = OrchestratorClient::Job.new(self, options, :task)
  job.start
  job.wait
  job.nodes['items']
end