Browse Source

updated pdm scripts to no longer use shflags

feature/startup-from-labels
Mar Alegre 5 years ago
parent
commit
d815804821
  1. 143
      .install/bin/pdm-build
  2. 104
      .install/bin/pdm-launch
  3. 50
      .install/bin/pdm-shell
  4. 1216
      .install/shflags

143
.install/bin/pdm-build

@ -1,98 +1,121 @@
#!/bin/bash
################################################################################
# Functions/variables
quit() {
if [[ $1 == 0 || $FLAGS_debug == $FLAGS_FALSE ]]; then
podman rm -i -f tmp-$epoch 2>&1 > /dev/null
fi
exit $1
}
set -eEuo pipefail
err=0
trap 'err=$?' ERR
trap 'cleanup' EXIT
epoch=$(date +%s.%3N)
today=$(date +%Y-%m-%d-T%H%M)
# Handle flags
source shflags
DEFINE_boolean 'squash' false 'squash newly built layers into a single new layer' 's'
DEFINE_boolean 'debug' false "Don't delete temporary container on build fail" 'd'
DEFINE_string 'tag' 'latest' 'Tag (other than date) to assign to the image' 't'
badarg() {
echo -n "$(basename $0): "
echo "$1"
echo "Try '$(basename $0) -h' for more information."
exit 2
}
cleanup() {
if [[ $err -eq 0 || $opt_debug -eq 0 ]]; then
podman rm -i -f tmp-$epoch 2>&1 > /dev/null
fi
}
help="Usage: $(basename $0) [-sdh] [-t tag] [directory] [name]
Builds an image from files in a directory, and assigns it a name.
Files used are 'Containerfile' and optionally 'Systemdfile'. If first argument
is omitted, script assumes files can be found in the current working directory.
If second argument is omitted, the directory where the files were found is used
as the image name.
FLAGS_HELP="Usage: $(basename $0) [-sd] [-t tag] [directory] [name]
Options:
-s Squash all newly built layers in the image into a single layer
-d Debug mode: don't delete the temporary container created by the script
when encountering an error
-t [tag] Tag the image with the given string. Can be used multiple times to assign
the image multiple tags
-h Display this help and exit"
Builds an image from the Containerfile and (optionally) Systemdfile in the
specified directory, and tags the image with the given name. If no directory
argument is given, the current working directory is used. If no name argument
is given, the image is named after the directory.
"
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# Handle options
opt_squash=0
opt_debug=0
opt_tags=()
while getopts ':sdt:h' arg; do
case $arg in
s) opt_squash=1;;
d) opt_debug=1;;
t) opt_tags+=("${OPTARG}");;
h) echo "$help"; exit 0;;
:) badarg "Argument missing for option '-$OPTARG'";;
?) badarg "Invalid option '-$OPTARG'";;
esac
done
# Handle errors/arguments/cases
echo "${opt_tags[*]}"
exit 0
# Handle non-option arguments
if [[ $# -gt 2 ]]; then
echo "Error: too many arguments"
echo ""
flags_help
exit 1
badarg "Too many arguments"
fi
if [[ -n $1 ]]; then
directory=$1
directory="$1"
else
directory=$(pwd)
directory=$(pwd)
fi
if [[ ! -d $directory ]]; then
echo "Error: directory \"$directory\" not found"
echo ""
flags_help
exit 1
if [[ -n $2 ]]; then
name="$2"
else
cd $directory
name=$(basename "$directory")
fi
if [[ -n $2 ]]; then
name=$2
# Main
if [[ ! -d "$directory" ]]; then
echo "Error: directory '$directory' not found"
exit 1
else
name=$(basename $(pwd))
cd "$directory"
fi
# build options
buildopts=""
if [[ $FLAGS_squash == $FLAGS_TRUE ]]; then
buildopts="$buildopts --squash"
if [[ $opt_squash -eq 1 ]]; then
buildopts="$buildopts --squash"
fi
# Main
# tell buildah to build images in docker format instead of the default OCI format
# because only docker-format images can use the SHELL directive in Containerfiles
export BUILDAH_FORMAT=docker
# build image
echo "Building image ..."
podman build -f Containerfile -t tmp-$epoch $buildopts || quit $?
podman build -f Containerfile -t tmp-$epoch $buildopts
# Systemdfile is for commands that need systemd to execute
if [[ -f Systemdfile ]]; then
echo "Running build steps that require systemd ..."
echo "Creating container ..."
podman create --name tmp-$epoch tmp-$epoch || quit $?
podman start tmp-$epoch || quit $?
echo "Copying script to container ..."
podman cp Systemdfile tmp-$epoch:/root/
echo "Running script ..."
podman exec tmp-$epoch bash -c "chmod +x /root/Systemdfile && /root/Systemdfile" || quit $?
echo "Committing container to image ..."
podman commit tmp-$epoch $name:$today || quit $?
echo "Running build steps that require systemd ..."
echo "Creating temporary container ..."
podman create --name tmp-$epoch tmp-$epoch
podman start tmp-$epoch
echo "Copying script to container ..."
podman cp Systemdfile tmp-$epoch:/root/
echo "Running script ..."
podman exec tmp-$epoch bash -c "chmod +x /root/Systemdfile && /root/Systemdfile"
echo "Committing container to image ..."
podman commit tmp-$epoch "$name:$today"
else
echo "Systemdfile not found, skipping container creation ..."
# tag image we already built with appropriate tag, and untag with tmp
podman tag tmp-$epoch $name:$today
podman rmi tmp-$epoch
echo "Systemdfile not found, skipping temporary container step ..."
# tag image we already built with appropriate tag, and untag with tmp
podman tag tmp-$epoch "$name:$today"
podman rmi tmp-$epoch
fi
# tag with latest tag
podman tag $name:$today $name:$FLAGS_tag
echo "Done!"
# assign any extra tags
for tag in "${opt_tags[@]}"; do
podman tag "$name:$today" "$name:$tag"
done
quit 0
echo "Done!"

104
.install/bin/pdm-launch

@ -1,59 +1,79 @@
#!/bin/bash
################################################################################
# Handle flags
source shflags
DEFINE_boolean 'overwrite' false 'Overwrite container if one with same name already exists.' 'o'
DEFINE_boolean 'config' false 'Automatically configure container with deploy options stored in image metadata.' 'c'
DEFINE_string 'label' 'deployopts' 'Image metadata label from which to get the deploy options.' 'l'
FLAGS_HELP="Usage: $(basename $0) [-oc] [-d label] [image] [name]
Creates and starts a container from the specified image, and assigns it the
specified name. If no image argument is given, uses the current working
directory as the name of the image. If no name argument is given, the container
is given the same name as the image.
"
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# Handle errors/arguments/cases
if [[ $# -gt 2 ]]; then
echo "Error: too many arguments"
echo ""
flags_help
exit 1
fi
set -eEuo pipefail
badarg() {
echo -n "$(basename $0): "
echo "$1"
echo "Try '$(basename $0) -h' for more information."
exit 2
}
help="Usage: $(basename $0) [-fch] [-l label] [-e options] image [container]
Create and start a container from a local image, and assign it the given name.
If second argument is omitted, defaults to assigning the container the same name
as the image.
if [[ -n $1 ]]; then
image=$1
Options:
-f Force overwrite if a container with the given name already exists
-c Configure the container with deploy options stored in the image
metadata. By default, uses the metadata tag labeled 'config_default'
-l [label] Label from which to get the deploy options. Automatically sets -c
-e [options] Extra deploy options to assign to the container. If -e and -c are
both used, options from image metadata and command line are combined
-h Display this help and exit"
# Handle options
opt_force=0
opt_config=0
opt_label='config_default'
opt_extras=''
while getopts ':fcl:e:h' arg; do
case $arg in
f) opt_force=1;;
c) opt_config=1;;
l) opt_config=1; opt_label="${OPTARG}";;
e) opt_extras="${OPTARG}";;
h) echo "$help"; exit 0;;
:) badarg "Argument missing for option '-$OPTARG'";;
?) badarg "Invalid option '-$OPTARG'";;
esac
done
# Handle non-option arguments
if [[ $# -lt 1 ]]; then
badarg "Missing image name"
else
echo "Warning: No image name given. Assuming image name from current working directory."
image=$(basename $(pwd))
image="$1"
fi
if [[ $# -gt 2 ]]; then
badarg "Too many arguments"
fi
if [[ -n $2 ]]; then
name=$2
container="$2"
else
name=$image
container="$image"
fi
# Main
set -e
if [[ $FLAGS_config -eq $FLAGS_TRUE ]]; then
echo "Getting deploy options from image metadata label \"$FLAGS_label\" ..."
deployopts=$(podman image inspect -f "{{ .Config.Labels.${FLAGS_label} }}" $image)
if [[ $deployopts == "<no value>" ]]; then
echo "Error: image metadata label \"$FLAGS_label\" is empty or nonexistent."
exit 2
fi
if [[ $opt_config -eq 1 ]]; then
echo "Getting deploy options from image metadata label '$opt_label' ..."
deployconf=$(podman image inspect -f "{{ .Config.Labels.${opt_label} }}" "$image")
if [[ $config == "<no value>" ]]; then
echo "Error: could not find image metadata label '$opt_label'"
exit 1
fi
else
deployopts=""
deployconf=""
fi
deployconf="$deployconf $opts_extras"
if [[ $FLAGS_overwrite -eq $FLAGS_TRUE ]]; then
podman rm -i -f $name
if [[ $opt_force -eq 1 ]]; then
podman rm -i -f "$container"
fi
podman run -itd --name $name --hostname $name $deployopts $image
podman run -itd --name "$container" --hostname "$container" $deployconf "$image"
echo "Done!"

50
.install/bin/pdm-shell

@ -1,18 +1,48 @@
#!/bin/bash
################################################################################
set -eEuo pipefail
if [[ -z $1 || $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: $(basename $0) container [command]
badarg() {
echo -n "$(basename $0): "
echo "$1"
echo "Try '$(basename $0) -h' for more information."
exit 2
}
Runs a bash shell on the given container. If second argument is
omitted, an interactive login shell is launched. If second argument
is given, the string is interpreted as a command and executed directly.
To ensure that any shell syntax is evaluated in the container shell
instead of the local shell, wrap your command in single quotes."
exit 1
help="Usage: $(basename $0) [-h] container [command]
Runs a bash shell on the given container.
If the second argument is omitted, an interactive login shell is launched. If the
second argument is present, the string is interpreted as a command and executed
directly. To ensure that any shell syntax is evaluated in the container shell
instead of the host shell, make sure to wrap your string in single quotes.
Options:
-h Display this help and exit"
# Handle options
while getopts ':h' arg; do
case $arg in
h) echo "$help"; exit 0;;
:) badarg "Argument missing for option '-$OPTARG'";;
?) badarg "Invalid option '-$OPTARG'";;
esac
done
# Handle non-option arguments
if [[ $# -lt 1 ]]; then
badarg "Missing container name"
else
container="$1"
fi
if [[ $# -gt 2 ]]; then
badarg "Too many arguments"
fi
# Main
if [[ -z $2 ]]; then
podman exec -it $1 bash -l
podman exec -it "$container" bash -l
else
podman exec -it $1 bash -c "${*:2}"
podman exec -it "$container" bash -c "${@:2}"
fi

1216
.install/shflags

File diff suppressed because it is too large
Loading…
Cancel
Save