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.5'.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



67
68
69
# File 'lib/orchestrator_client.rb', line 67

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
47
# 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['User-Agent'] = @config['User-Agent']
  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



71
72
73
# File 'lib/orchestrator_client.rb', line 71

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



75
76
77
# File 'lib/orchestrator_client.rb', line 75

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

#post(location, body) ⇒ Object



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

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

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

  unless Set.new(["202", "200"]).include? res.code
    raise OrchestratorClient::ApiError.make_error_from_response(res)
  end

  JSON.parse(res.body)
end

#rootObject



91
92
93
# File 'lib/orchestrator_client.rb', line 91

def root
  get(url)
end

#run_task(options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/orchestrator_client.rb', line 79

def run_task(options)
  if options[:plan_job] || options['plan_job']
    type = :plan_task
  else
    type = :task
  end
  job = OrchestratorClient::Job.new(self, options, type)
  job.start
  job.wait
  job.nodes['items']
end