Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Added Job URL to Task after launching
Browse files Browse the repository at this point in the history
  • Loading branch information
slemrmartin committed Apr 15, 2020
1 parent b17633d commit 01a1bde
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ def job_status_to_task_status(job_status)
self.class.job_status_to_task_status(job_status)
end

# Ansible Tower's URL to Job/Workflow
def self.job_external_url(job, tower_base_url)
path = job.type == 'workflow_job' ? 'workflows' : 'jobs/playbook'
File.join(tower_url(tower_base_url), "/#/#{path}", job.id.to_s)
end

def job_external_url(job)
tower_host = [default_endpoint.scheme, default_endpoint.host].join('://')
self.class.job_external_url(job, tower_host)
end

def self.tower_url(hostname)
if hostname.to_s.index('http').nil?
File.join('https://', hostname)
else
hostname
end
end

private

attr_accessor :identity, :task_id, :source_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ def order
job = client.order_service(job_type, service_offering.source_ref, order_params)
logger.info("ServiceOffering#order: Task(id: #{task_id}): Ordering ServiceOffering(id: #{service_offering.id}, source_ref: #{service_offering.source_ref}): Job(:id #{job.id}) has launched.")

context = {
:service_instance => {
:job_status => job.status,
:url => client.job_external_url(job)
}
}
update_task(task_id,
:context => context,
:state => "running",
:status => client.job_status_to_task_status(job.status),
:source_id => source_id.to_s,
Expand All @@ -38,7 +45,9 @@ def order
logger.info("ServiceOffering#order: Task(id: #{task_id}): Ordering ServiceOffering(id: #{service_offering.id}, source_ref: #{service_offering.source_ref})...Task updated")
rescue StandardError => err
logger.error("ServiceOffering#order: Task(id: #{task_id}), ServiceOffering(id: #{service_offering&.id} source_ref: #{service_offering&.source_ref}): Ordering error: #{err.cause} #{err}\n#{err.backtrace.join("\n")}")
update_task(task_id, :state => "completed", :status => "error", :context => {:error => err.to_s})
err_context = {:error => err.to_s}
err_context = err_context.merge(context) if context.present?
update_task(task_id, :state => "completed", :status => "error", :context => err_context)
end

def ansible_tower_client(source_id, task_id, identity)
Expand Down
12 changes: 7 additions & 5 deletions lib/topological_inventory/ansible_tower/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "topological_inventory/ansible_tower/operations/core/ansible_tower_client"

module TopologicalInventory::AnsibleTower
class Parser < TopologicalInventory::Providers::Common::Collector::Parser
require "topological_inventory/ansible_tower/parser/service_credential"
Expand All @@ -20,11 +22,7 @@ class Parser < TopologicalInventory::Providers::Common::Collector::Parser

def initialize(tower_url:)
super()
self.tower_url = if tower_url.to_s.index('http').nil?
File.join('https://', tower_url)
else
tower_url
end
self.tower_url = tower_client_class.tower_url(tower_url)
end

def parse_base_item(entity)
Expand All @@ -37,5 +35,9 @@ def parse_base_item(entity)
protected

attr_accessor :tower_url

def tower_client_class
TopologicalInventory::AnsibleTower::Operations::Core::AnsibleTowerClient
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "topological_inventory/ansible_tower/operations/core/ansible_tower_client"

module TopologicalInventory::AnsibleTower
class Parser
module ServiceInstance
Expand All @@ -12,14 +10,13 @@ def parse_service_instance(job_hash)
service_inventory = lazy_find(:service_inventories, :source_ref => job.inventory_id.to_s) if job.respond_to?(:inventory_id)

# Set to tower UI url
path = job.type == 'workflow_job' ? 'workflows' : 'jobs/playbook'
external_url = File.join(self.tower_url, "/#/#{path}", job.id.to_s)
external_url = tower_client_class.job_external_url(job, self.tower_url)

extra = {
:started => job.started,
:finished => job.finished,
:status => job.status,
:task_status => TopologicalInventory::AnsibleTower::Operations::Core::AnsibleTowerClient.job_status_to_task_status(job.status)
:task_status => tower_client_class.job_status_to_task_status(job.status)
}
# launch variables set either manually, by survey values or artifacts from previous job in workflow
extra_vars = job.extra_vars_hash
Expand Down

0 comments on commit 01a1bde

Please sign in to comment.