44require 'json'
55require 'net/http'
66require 'yaml'
7- require 'puppet_litmus'
87require 'etc'
98require 'date'
109require_relative '../lib/task_helper'
10+ require_relative '../lib/inventory_helper'
1111
1212# Provision and teardown vms through ABS.
1313class ABSProvision
14- include PuppetLitmus ::InventoryManipulation
15-
1614 # Enforces a k8s.infracore.puppet.net domain, but allows selection of prod,
1715 # stage, etc hostname from the environment variable +ABS_SUBDOMAIN+ so that
1816 # CI can test vms from staging.
@@ -23,7 +21,7 @@ def abs_host
2321 "#{ subdomain } .k8s.infracore.puppet.net"
2422 end
2523
26- def provision ( platform , inventory_location , vars )
24+ def provision ( platform , inventory , vars )
2725 uri = URI . parse ( "https://#{ abs_host } /api/v2/request" )
2826 jenkins_build_url = if ENV [ 'CI' ] == 'true' && ENV [ 'TRAVIS' ] == 'true'
2927 ENV . fetch ( 'TRAVIS_JOB_WEB_URL' , nil )
@@ -85,7 +83,6 @@ def provision(platform, inventory_location, vars)
8583
8684 raise "Timeout: unable to get a 200 response in #{ poll_duration } seconds" if reply . code != '200'
8785
88- inventory_hash = get_inventory_hash ( inventory_location )
8986 data = JSON . parse ( reply . body )
9087 data . each do |host |
9188 if platform_uses_ssh ( host [ 'type' ] )
@@ -109,27 +106,26 @@ def provision(platform, inventory_location, vars)
109106 var_hash = YAML . safe_load ( vars )
110107 node [ 'vars' ] = var_hash
111108 end
112- add_node_to_group ( inventory_hash , node , group_name )
109+ inventory . add ( node , group_name )
113110 end
114111
115- File . open ( inventory_location , 'w' ) { | f | f . write inventory_hash . to_yaml }
112+ inventory . save
116113 { status : 'ok' , nodes : data . length }
117114 end
118115
119- def tear_down ( node_name , inventory_location )
120- if File . file? ( inventory_location )
121- inventory_hash = inventory_hash_from_inventory_file ( inventory_location )
122- facts = facts_from_node ( inventory_hash , node_name )
123- platform = facts [ 'platform' ]
124- job_id = facts [ 'job_id' ]
125- end
116+ def tear_down ( node_name , inventory )
117+ node = inventory . lookup ( name : node_name )
118+ facts = node [ 'facts' ]
119+ platform = facts [ 'platform' ]
120+ job_id = facts [ 'job_id' ]
126121
127122 targets_to_remove = [ ]
128- inventory_hash [ 'groups' ] . each do |group |
123+ inventory [ 'groups' ] . each do |group |
129124 group [ 'targets' ] . each do |node |
130- targets_to_remove . push ( node [ 'uri' ] ) if node [ 'facts' ] [ 'job_id' ] == job_id
125+ targets_to_remove . push ( node ) if node [ 'facts' ] [ 'job_id' ] == job_id
131126 end
132127 end
128+
133129 uri = URI . parse ( "https://#{ abs_host } /api/v2/return" )
134130 headers = { 'X-AUTH-TOKEN' => token_from_fogfile ( 'abs' ) , 'Content-Type' => 'application/json' }
135131 payload = { 'job_id' => job_id ,
@@ -143,16 +139,17 @@ def tear_down(node_name, inventory_location)
143139 raise "Error: #{ reply } : #{ reply . message } " unless reply . code == '200'
144140
145141 targets_to_remove . each do |target |
146- remove_node ( inventory_hash , target )
142+ inventory . remove ( target )
147143 end
148- File . open ( inventory_location , 'w' ) { |f | f . write inventory_hash . to_yaml }
149- { status : 'ok' , removed : targets_to_remove }
144+ inventory . save
145+
146+ { status : 'ok' , removed : targets_to_remove . map { |t | t [ 'name' ] } }
150147 end
151148
152149 def task ( action :, platform : nil , node_name : nil , inventory : nil , vars : nil , **_kwargs )
153- inventory_location = sanitise_inventory_location ( inventory )
154- result = provision ( platform , inventory_location , vars ) if action == 'provision'
155- result = tear_down ( node_name , inventory_location ) if action == 'tear_down'
150+ inventory = InventoryHelper . open ( inventory )
151+ result = provision ( platform , inventory , vars ) if action == 'provision'
152+ result = tear_down ( node_name , inventory ) if action == 'tear_down'
156153 result
157154 end
158155
0 commit comments