concourse-ssh-resource/common

120 lines
2.7 KiB
Text
Raw Normal View History

2022-05-06 02:20:44 +02:00
#!/usr/bin/env sh
INPUT_PATH=
CONFIG_PATH=
HOSTNAME=
PORT=
USER=
IDENTITY=
HOSTKEY=
save_input() {
input_file="$(mktemp -tp /tmp)"
cat > "$input_file" <&0
echo "$input_file"
return 0
}
read_config() {
HOSTNAME="$(jq -r .source.hostname < "$1")"
PORT="$(jq -r '.source.port // "22"' < "$1")"
USER="$(jq -r .source.user < "$1")"
IDENTITY="$(jq -r .source.identity < "$1")"
HOSTKEY="$(jq -r .source.hostkey < "$1")"
return 0
}
write_config() {
directory="$(mktemp -dp /tmp)"
config="$directory/config"
identity_file="$directory/identity"
knownhosts_file="$directory/knownhosts"
{
echo "host $HOSTNAME"
echo " HostName $HOSTNAME"
echo " Port $PORT"
echo " User $USER"
echo " IdentityFile $identity_file"
echo " UserKnownHostsFile $knownhosts_file"
} > "$config"
echo "$IDENTITY" > "$identity_file"
echo "$HOSTKEY" > "$knownhosts_file"
chmod 600 "$identity_file" "$knownhosts_file" "$config"
echo "$config"
return 0
}
INPUT_PATH="$(save_input)"
read_config "$INPUT_PATH"
CONFIG_PATH="$(write_config)"
export INPUT_PATH
export CONFIG_PATH
get_version() {
jq -r '.version // []' < "$INPUT_PATH"
}
get_files() {
jq -r '.source.files?[]' < "$INPUT_PATH"
}
2022-05-06 03:10:47 +02:00
get_command() {
jq -r '.params.command?' < "$INPUT_PATH"
}
get_directory() {
jq -r '.params.directory // "/"' < "$INPUT_PATH"
}
get_workdir() {
jq -r '.params.workdir // "$HOME"' < "$INPUT_PATH"
}
fetch_file_infos() {
files_file="$(mktemp -tp /tmp)"
ssh -F "$CONFIG_PATH" "$HOSTNAME" sh -s << EOF > "$files_file"
files="$("$1")"
echo "\$files" | while read -r file; do
echo "\$file"
[ -e "\$file" ] && echo true || echo false
[ -e "\$file" ] && echo "\$(stat --format=%Y "\$file")" || echo
done
EOF
echo "$files_file"
return 0
}
_compute_version() {
echo "["
last_line=
line_file=
line_exists=
line_modify=
while read -r line; do
if [ -z "$line_file" ]; then
line_file="$line"
elif [ -z "$line_exists" ]; then
line_exists="$line"
elif [ -z "$line_modify" ]; then
line_modify="$line"
else
if "$line_exists"; then
echo '{"path":' "\"$line_file\"," '"modify":' "$line_modify" '}',
fi
line_file="$line"
line_exists=
line_modify=
fi
done < "$1"
if [ -n "$line_file" ] && [ -n "$line_exists" ] && [ -n "$line_modify" ] && "$line_exists"; then
echo '{"path":' "\"$line_file\"," '"modify":' "$line_modify" '}'
fi
echo "]"
}
compute_version() {
_compute_version "$@" | jq -r tostring
}