Skip to content

Instantly share code, notes, and snippets.

@codeword
Last active August 7, 2018 17:59
Show Gist options
  • Save codeword/e3e65317bc7d58d4afa4 to your computer and use it in GitHub Desktop.
Save codeword/e3e65317bc7d58d4afa4 to your computer and use it in GitHub Desktop.
script to generate fly execute shell scripts with values from the pipeline/task/secrets files
#!/usr/bin/env ruby
require 'yaml'
require 'optparse'
options = {
target: 'local'
}
optparse = OptionParser.new do |opt|
opt.banner = "Usage: /path/to/generate_fly_exec.rb [options]"
opt.on('-p', '--pipeline=PIPELINE', 'the pipeline.yml to read') { |o| options[:pipeline_file] = o }
opt.on('-v', '--vars-from=SECRETS', 'the secrets.yml to pull secrets from') { |o| options[:secrets_file] = o }
opt.on('-j', '--job-name=job_name', 'the name of the job entry in the pipline') { |o| options[:job_name] = o }
opt.on('-t', '--task-name=task_name', 'the name of the task entry in the pipeline') { |o| options[:task_name] = o }
opt.on('-r', '--project-root=/path', 'path to project containing the task.yml') { |o| options[:project_root] = o }
opt.on('--target=target', 'the concourse server to target') { |o| options[:target] = o }
end
begin
optparse.parse!
mandatory = [:pipeline_file, :secrets_file, :job_name, :task_name]
missing = mandatory.select{ |param| options[param].nil? }
unless missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts optparse
exit
end
raise OptionParser::InvalidOption.new("#{options[:pipeline_file]} does not exist from #{Dir.pwd}") unless File.exists?(options[:pipeline_file])
raise OptionParser::InvalidOption.new("#{options[:secrets_file]} does not exist from #{Dir.pwd}") unless File.exists?(options[:secrets_file])
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end
#./generate_fly_exec.rb path/to/pipeline.yml path/to/secrets.yml bats-ubuntu-manual test
pipeline_file = options[:pipeline_file]
secrets_file = options[:secrets_file]
job_name = options[:job_name]
task_name = options[:task_name]
raise "#{pipeline_file} does not exist from #{Dir.pwd}" unless File.exists?(pipeline_file)
raise "#{secrets_file} does not exist from #{Dir.pwd}" unless File.exists?(secrets_file)
pipeline = YAML.load_file(pipeline_file)
secrets = YAML.load_file(secrets_file)
job = pipeline['jobs'].find {|entry| entry['name'] == job_name }
raise "Cannot find job with name '#{job_name}' in #{pipeline['jobs'].map{|x| x['name']}.inspect}" unless job
task = job['plan'].find {|entry| entry['task'] == task_name }
raise "Cannot find task with name '#{task_name}' in #{job['plan'].map{|x| x['task']}.compact.inspect}" unless task
relative_task_file_name = task['file'].split('/')[1..-1].join('/')
relative_task_file_name = File.join(options[:project_root], relative_task_file_name) if options[:project_root]
raise "#{relative_task_file_name} does not exist from #{Dir.pwd}" unless File.exists?(relative_task_file_name)
task_file = YAML.load_file(relative_task_file_name)
params = []
if task['config'] && task['config']['params']
params = task['config']['params']
params.each do |k, v|
if v.is_a?(Hash)
secrets_key = v.keys.first.keys.first
new_value = secrets[secrets_key]
params[k] = new_value
end
if v=~/{{(\w+)}}/
params[k] = secrets[match]
end
end
end
puts "#!/usr/bin/env bash"
puts
puts "set -x -e"
puts
params.each do |k, v|
if v.is_a?(String) && v.match(/\n/)
v = "$(cat <<-EOF\n#{v}EOF\n)"
end
puts "#{k}=\"#{v}\" \\"
end
puts "fly -t #{options[:target]} execute -x -c #{relative_task_file_name} \\"
task_file['inputs'].each_with_index do |input, index|
string = "-i #{input['name']}=replace-me"
string += " \\" if index < (task_file['inputs'].size - 1)
puts string
end
@codeword
Copy link
Author

initial attempt at a fly exe script generator that is based on your pipeline/secrets/tasks configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment