Project

General

Profile

« Previous | Next » 

Revision 04bb710e

Added by Thomas McKay over 8 years ago

job-templates

+ export job-templates
+ import job-templates
+ other odds&bits

View differences:

lib/hammer_cli_csv/compute_resources.rb
'name' => name,
'url' => line[URL]
}
})['compute_resource']['id']
})['id']
end
# Update associated resources
associate_organizations(id, line[ORGANIZATIONS], 'compute_resource')
associate_locations(id, line[LOCATIONS], 'compute_resource')
# TODO: this doesn't work "Environments you cannot remove environments that are used by hosts or inherited."
#associate_organizations(id, line[ORGANIZATIONS], 'compute_resource')
#associate_locations(id, line[LOCATIONS], 'compute_resource')
print "done\n" if option_verbose?
end
lib/hammer_cli_csv/hosts.rb
ORGANIZATION = 'Organization'
LOCATION = 'Location'
ENVIRONMENT = 'Environment'
ENVIRONMENT = 'Puppet Environment'
OPERATINGSYSTEM = 'Operating System'
ARCHITECTURE = 'Architecture'
MACADDRESS = 'MAC Address'
......
raise "Host 'id=#{host['id']}' not found" if !host || host.empty?
name = host['name']
organization = foreman_organization(:id => host['organization_id'])
location = foreman_location(:id => host['location_id'])
environment = foreman_environment(:id => host['environment_id'])
operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id'])
architecture = foreman_architecture(:id => host['architecture_id'])
organization = host['organization_name']
location = host['location_name']
environment = host['environment_name']
operatingsystem = host['operatingsystem_name']
architecture = host['architecture_name']
mac = host['mac']
domain = foreman_domain(:id => host['domain_id'])
ptable = foreman_partitiontable(:id => host['ptable_id'])
domain = host['domain_name']
ptable = host['ptable_name']
csv << [name, organization, location, environment, operatingsystem, architecture, mac, domain, ptable]
end
lib/hammer_cli_csv/import.rb
domains architectures partition_tables lifecycle_environments host_collections
provisioning_templates
subscriptions products content_views content_view_filters activation_keys
hosts content_hosts smart_proxies reports roles users )
hosts content_hosts smart_proxies compute_resources reports roles users )
RESOURCES.each do |resource|
dashed = resource.gsub('_', '-')
option "--#{dashed}", 'FILE', "csv file for #{dashed}"
lib/hammer_cli_csv/job_templates.rb
ORGANIZATIONS = 'Organizations'
LOCATIONS = 'Locations'
DESCRIPTION = 'Description'
JOB = 'Job Category'
PROVIDER = 'Provider'
SNIPPET = 'Snippet'
TEMPLATE = 'Template'
INPUT_NAME = 'Input Name'
INPUT_NAME = 'Input:Name'
INPUT_DESCRIPTION = 'Input:Description'
INPUT_REQUIRED = 'Input:Required'
INPUT_TYPE = 'Input:Type'
INPUT_PARAMETERS = 'Input:Parameters'
def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
csv << [NAME, ORGANIZATIONS, LOCATIONS, JOB, PROVIDER, SNIPPET, TEMPLATE, INPUT_NAME]
csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, JOB, PROVIDER, SNIPPET, TEMPLATE,
INPUT_NAME, INPUT_DESCRIPTION, INPUT_REQUIRED, INPUT_TYPE, INPUT_PARAMETERS]
@api.resource(:job_templates).call(:index, {
:per_page => 999999
})['results'].each do |template_id|
......
next if template['locked']
next unless option_organization.nil? || template['organizations'].detect { |org| org['name'] == option_organization }
name = template['name']
job = template['job_name']
description = template['description_format']
job = template['job_category']
snippet = template['snippet'] ? 'Yes' : 'No'
provider = template['provider_type']
organizations = export_column(template, 'organizations', 'name')
locations = export_column(template, 'locations', 'name')
csv << [name, organizations, locations, job, provider, snippet, template['template']]
csv << [name, organizations, locations, description, job, provider, snippet, template['template']]
template['template_inputs'].each do |input_id|
input = @api.resource(:templates).call(:template_inputs, {:template_id => template['id'], :id => input_id['id']})
x = input
template_columns = [name] + Array.new(7)
@api.resource(:template_inputs).call(:index, {
:template_id => template['id']
})['results'].each do|input|
input_field = nil
input_options = nil
case input['input_type']
when /user/
input_name = export_column(input, 'options') do |value|
value
end
when /fact/
input_name = input['fact_name']
when /variable/
input_name = input['variable_name']
when /puppet_parameter/
input_name = "#{input['puppet_class_name']}|#{input['puppet_parameter_name']}"
else
raise _("Unknown job template input type '%{type}'") % {:type => input['input_type']}
end
required = input['required'] ? 'Yes' : 'No'
csv << template_columns + [input['name'], input['description'], required, input['input_type'], input_name]
end
end
end
......
end
count(line[COUNT]).times do |number|
name = namify(line[NAME], number)
job_name = namify(line[JOB], number)
options = {
'job_template' => {
'name' => name,
'job_name' => job_name,
'snippet' => line[SNIPPET] == 'Yes' ? true : false,
'provider_type' => line[PROVIDER],
'organization_ids' => organizations,
'location_ids' => locations,
'template' => line[TEMPLATE]
}
}
template_id = @existing[name]
if !template_id
print _("Creating job template '%{name}'...") % {:name => name } if option_verbose?
template_id = @api.resource(:job_templates).call(:create, options)['id']
@existing[name] = template_id
if line[INPUT_NAME].nil? || line[INPUT_NAME].empty?
create_template(line, number)
else
print _("Updating job template '%{name}'...") % {:name => name} if option_verbose?
options['id'] = template_id
template_id = @api.resource(:job_templates).call(:update, options)['id']
create_template_input(line, number)
end
# ????
# Update associated resources
# @template_organizations ||= {}
# organizations.each do |organization_id|
......
# end
# end
puts _('done') if option_verbose?
end
rescue RuntimeError => e
raise "#{e}\n #{line[NAME]}"
end
def create_template(line, number)
name = namify(line[NAME], number)
job_category = namify(line[JOB], number)
options = {
'job_template' => {
'name' => name,
'description_format' => line[DESCRIPTION],
'job_category' => job_category,
'snippet' => line[SNIPPET] == 'Yes' ? true : false,
'provider_type' => line[PROVIDER],
#'organization_ids' => organizations,
#'location_ids' => locations,
'template' => line[TEMPLATE]
}
}
template_id = @existing[name]
if !template_id
print _("Creating job template '%{name}'...") % {:name => name } if option_verbose?
template_id = @api.resource(:job_templates).call(:create, options)['id']
@existing[name] = template_id
else
print _("Updating job template '%{name}'...") % {:name => name} if option_verbose?
options['id'] = template_id
template_id = @api.resource(:job_templates).call(:update, options)['id']
end
puts _('done') if option_verbose?
end
def create_template_input(line, number)
name = namify(line[NAME], number)
template_id = @existing[name]
raise "Job template '#{name}' must exist before setting inputs" unless template_id
options = {
'template_id' => template_id,
'template_input' => {
'name' => line[INPUT_NAME],
'description' => line[INPUT_DESCRIPTION],
'input_type' => line[INPUT_TYPE],
'required' => line[INPUT_REQUIRED] == 'Yes' ? true : false
}
}
case line[INPUT_TYPE]
when /user/
options['template_input']['options'] = line[INPUT_PARAMETERS]
when /fact/
options['template_input']['fact_name'] = line[INPUT_PARAMETERS]
when /variable/
options['template_input']['variable_name'] = line[INPUT_PARAMETERS]
when /puppet_parameter/
options['template_input']['puppet_class_name'], options['template_input']['puppet_parameter_name'] = line[INPUT_PARAMETERS].split('|')
else
raise _("Unknown job template input type '%{type}'") % {:type => line[INPUT_TYPE]}
end
template_input = @api.resource(:template_inputs).call(:index, {
:template_id => template_id,
:search => "name = \"#{line[INPUT_NAME]}\""
})['results']
if template_input.empty?
print _("Creating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
@api.resource(:template_inputs).call(:create, options)
else
print _("Updating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
options['id'] = template_input[0]['id']
@api.resource(:template_inputs).call(:update, options)
end
puts _('done') if option_verbose?
end
def export_associations(template)
return '' unless template['template_combinations']
values = CSV.generate do |column|
lib/hammer_cli_csv/lifecycle_environments.rb
count(line[COUNT]).times do |number|
name = namify(line[NAME], number)
prior = namify(line[PRIORENVIRONMENT], number)
prior = line[PRIORENVIRONMENT] == 'Library' ? 'Library' :
namify(line[PRIORENVIRONMENT], number)
raise "Organization '#{line[ORGANIZATION]}' does not exist" if !@existing.include? line[ORGANIZATION]
if !@existing[line[ORGANIZATION]].include? name
print "Creating environment '#{name}'..." if option_verbose?
test/data/hosts.csv
Name,Count,Organization,Location,Environment,Operating System,Architecture,MAC Address,Domain,Partition Table
Name,Count,Organization,Location,Puppet Environment,Operating System,Architecture,MAC Address,Domain,Partition Table
dhcp130-%03d.megacorp.com,255,Mega Corporation,Asia Pacific,Development,RHEL 6.4,x86_64,"02:FE:B5:E0:70:%02x",megacorp.com,RedHat default
dhcp129-%03d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E0:70:%02x",megacorp.com,RedHat default
# dhcp129-1%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E1:70:%02x",megacorp.com,RedHat default
test/job_templates_test.rb
require File.join(File.dirname(__FILE__), 'csv_test_helper')
describe 'job-templates' do
extend CommandTestHelper
context "import" do
it "create job template with input" do
set_user 'admin'
name = "jobtemplate#{rand(10000)}"
file = Tempfile.new('job_templates_test')
# rubocop:disable LineLength
file.write <<-FILE
"Name","Organizations","Locations","Description","Job Category","Provider","Snippet","Template","Input:Name","Input:Description","Input:Required","Input:Type","Input:Parameters"
"#{name}","","","","TEST","SSH","No","<%= input(""command"") %>"
"#{name}","","","","","","","","command","command to run","Yes","user",""
FILE
# rubocop:enable LineLength
file.rewind
stdout,stderr = capture {
hammer.run(%W{csv job-templates --verbose --file #{file.path}})
}
stderr.must_equal ''
lines = stdout.split("\n")
lines[0].must_equal "Creating job template '#{name}'...done"
lines[1].must_equal "Creating job template input 'command' on '#{name}'...done"
file.unlink
end
end
end

Also available in: Unified diff