blob: de9149c2bbf07355bdc88ff19dc36f5fb2f328ea [file] [log] [blame]
#!/bin/bash
#
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Utility to manage COS extensions.
set -o errexit
set -o pipefail
set -o nounset
readonly PROG_NAME=$(basename "$0")
readonly DEFAULT_GPU_INSTALLER="gcr.io/cos-cloud/cos-gpu-installer:v2.0.22"
readonly COS_GPU_INSTALLER="${COS_GPU_INSTALLER:-${DEFAULT_GPU_INSTALLER}}"
readonly OS_RELEASE="/etc/os-release"
usage() {
cat <<EOF
${PROG_NAME}: Utility to manage COS extensions.
Usage:
${PROG_NAME} [OPTIONS] COMMAND [ARGS]...
Options:
-h, --help print help message
Commands:
list [-- --gpu-installer] list all available COS extensions and
their versions.
install <extension> [-- -<version>] install a COS extension. If no version
is given, then the default version
will be installed.
}
EOF
exit "${1}"
}
parse_args() {
local args
if ! args=$(getopt --options "h" --longoptions "help" -- "$@"); then
usage 1
fi
eval set -- "${args}"
while true; do
case "$1" in
-h|--help)
usage 0
;;
--)
shift
break
;;
*)
usage 1
;;
esac
done
if [[ "$#" -eq 0 ]]; then
usage 1
fi
case "$1" in
list)
shift
list "$@"
;;
install)
if [[ "$#" -eq 2 ]]; then
install "$2"
elif [[ "$#" -eq 3 ]]; then
install "$2" "$3"
else
usage 1
fi
;;
*)
usage 1
;;
esac
}
list() {
if [[ "$#" -eq 0 ]]; then
printf "Available extensions for COS version %s-%s:\n\n" \
"${VERSION_ID}" "${BUILD_ID}"
echo "[gpu]"
echo "gpu installer: ${DEFAULT_GPU_INSTALLER}"
run_gpu_installer list 2>/dev/null
elif [[ "$#" -eq 1 ]]; then
case "$1" in
--gpu-installer)
echo "${DEFAULT_GPU_INSTALLER}"
;;
*)
echo "Unsupported argument $1"
usage 1
;;
esac
else
usage 1
fi
}
install() {
case "$1" in
gpu)
shift
run_gpu_installer install "-host-dir=/var/lib/nvidia" "$@"
;;
*)
echo "Unsupported extension $1"
exit 1
;;
esac
}
run_gpu_installer() {
/usr/bin/docker run --rm \
--name="cos-gpu-installer" \
--privileged \
--net=host \
--pid=host \
--volume /dev:/dev \
--volume /:/root \
"${COS_GPU_INSTALLER}" "$@"
}
main() {
source "${OS_RELEASE}"
parse_args "$@"
}
main "$@"