Created
July 23, 2020 20:55
-
-
Save mdelillo/47c5bd4a88fd230bc2f403101509ad21 to your computer and use it in GitHub Desktop.
Bash function for creating `fly exec` scripts from concourse task config files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkexec() { | |
if [[ $# != 1 ]]; then | |
echo "Usage: ${FUNCNAME[0]} </path/to/task/config.yml>" | |
return 1 | |
fi | |
task_config=$1 | |
if ! [[ -f "${task_config}" ]]; then | |
echo "Could not find file ${task_config}" | |
return 1 | |
fi | |
task_config_abs_path="$(cd "$(dirname "${task_config}")" && pwd)/$(basename "${task_config}")" | |
task_config_json="$(yj -yj < "${task_config_abs_path}")" | |
task_name="$(basename "$(dirname "${task_config_abs_path}")")" | |
params="" | |
for param in $(echo "${task_config_json}" | jq -r '.params // [] | keys | .[]'); do | |
params+="${param}='' \\"$'\n' | |
done | |
inputs="" | |
for input in $(echo "${task_config_json}" | jq -r '.inputs // [] | .[].name'); do | |
if [[ -d "${HOME}/workspace/${input}" ]]; then | |
inputs+="-i ${input}=\"${HOME}/workspace/${input}\" \\"$'\n' | |
else | |
inputs+="-i ${input}=\"\" \\"$'\n' | |
fi | |
done | |
outputs="" | |
for output in $(echo "${task_config_json}" | jq -r '.outputs // [] | .[].name'); do | |
outputs+="-o ${output}=\"\${outputs_dir}/${output}\" \\"$'\n' | |
done | |
outputs_dir="$(mktemp -d)" | |
rmdir "${outputs_dir}" | |
fly_exec_command=$(cat <<-EOF | |
${params} | |
fly -t <YOUR-TARGET-HERE> e \\ | |
-c "${task_config_abs_path}" \\ | |
${inputs} | |
${outputs} | |
# --include-ignored | |
# --privileged | |
EOF | |
) | |
exec_script="exec-${task_name}" | |
cat >"${exec_script}" <<-EOF | |
#!/usr/bin/env bash | |
set -euo pipefail | |
outputs_dir="${outputs_dir}" | |
rm -rf "\$outputs_dir" | |
mkdir -p "\$outputs_dir" | |
$(echo "${fly_exec_command% \\}" | grep -v '^$' | sed 's/^-/ -/') | |
EOF | |
chmod +x "${exec_script}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment