class Nucleus::Adapters::V1::OpenshiftV2

@see access.redhat.com/documentation/en-US/OpenShift/2.0/html/REST_API_Guide The Openshift V2

API documentation

Public Class Methods

new(endpoint_url, endpoint_app_domain = nil, check_certificates = true) click to toggle source
Calls superclass method
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 21
def initialize(endpoint_url, endpoint_app_domain = nil, check_certificates = true)
  super(endpoint_url, endpoint_app_domain, check_certificates)
end

Public Instance Methods

handle_error(error_response) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 25
def handle_error(error_response)
  # some error messages do not have the proper error message format
  errors = openshift_errors(error_response)
  if error_response.status == 404 && errors.any? { |e| e[:text].include?('not found') }
    raise Errors::AdapterResourceNotFoundError, errors.collect { |e| e[:text] }.join(' ')
  elsif error_response.status == 422
    raise Errors::SemanticAdapterRequestError, errors.collect { |e| e[:text] }.join(' ')
  elsif error_response.status == 503
    raise Errors::PlatformUnavailableError, 'The Openshift API is currently not available'
  elsif error_response.status == 504
    raise Errors::PlatformTimeoutError, 'The Openshift API did not receive information from it\'s slaves. '\
      'Most likely the request is still being executed. Please make sure to analyse whether the request '\
      'was successful before invoking further actions.'
  end
  # error still unhandled, will result in a 500, server error
  log.warn "Openshift error still unhandled: #{error_response}"
end
openshift_errors(error_response) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 43
def openshift_errors(error_response)
  if error_response.body.is_a?(Hash) && error_response.body.key?(:messages)
    error_response.body[:messages].collect { |error| { field: error[:field], text: error[:text] } }
  else
    []
  end
end

Private Instance Methods

active_deployment(app, deployments = nil) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 93
def active_deployment(app, deployments = nil)
  deployments = load_deployments(app[:id]) unless deployments
  active = nil
  active_ts = nil
  deployments.each do |deployment|
    ts = Time.parse(last_activation(deployment[:activations])).to_i
    if active.nil? || ts > active_ts
      active = deployment
      active_ts = ts
    end
  end
  active
end
app_domain() click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 57
def app_domain
  # A user always has only 1 domain as described on:
  # https://access.redhat.com/documentation/en-US/OpenShift/2.0/html/REST_API_Guide/chap-API_Guide-Domains.html
  user_domains = get('/domains').body[:data]
  fail_with(:no_user_domain) if user_domains.empty?
  user_domains.first[:name]
end
headers() click to toggle source
Calls superclass method
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 53
def headers
  super.merge('Accept' => 'application/json; version=1.7', 'Content-Type' => 'application/json')
end
last_activation(activations) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 107
def last_activation(activations)
  latest = nil
  activations.each do |activation|
    latest = activation if latest.nil? || Time.parse(activation).to_i > Time.parse(latest).to_i
  end
  latest
end
latest_deployment(application_id, deployments = nil) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 79
def latest_deployment(application_id, deployments = nil)
  deployments = load_deployments(application_id) unless deployments
  latest = nil
  latest_ts = nil
  deployments.each do |deployment|
    ts = Time.parse(deployment[:created_at]).to_i
    if latest.nil? || ts > latest_ts
      latest = deployment
      latest_ts = ts
    end
  end
  latest
end
load_deployments(application_id) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 115
def load_deployments(application_id)
  get("/application/#{application_id}/deployments").body[:data]
end
load_gears(application_id) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 119
def load_gears(application_id)
  get("/application/#{application_id}/gear_groups").body[:data]
end
original_deployment(app, deployments = nil) click to toggle source
# File lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb, line 65
def original_deployment(app, deployments = nil)
  # TODO: this is actually quite scary, could easily fail with wrong timing
  # What are the alternatives?
  # 1) Clone git repo and lookup commits --> insanely slow
  # 2) Identify initial commits by sha1 key --> would require collection of allowed values, which may change!
  deployments = load_deployments(app[:id]) unless deployments
  deployments.find do |deployment|
    diff = (Time.parse(deployment[:created_at]).to_i - Time.parse(app[:creation_time]).to_i).abs
    log.debug "OS deployment time diff: #{diff}"
    diff < 20 && deployment[:force_clean_build] == false &&
      deployment[:hot_deploy] == false && deployment[:ref] == 'master'
  end
end