From 7751b2955948664076409abaff4d4e4161a31c6f Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sat, 8 Feb 2020 19:12:28 +0100 Subject: [PATCH] LXC: add LXC tooling box (initial, WIP) Signed-off-by: Markus Heiser --- Makefile | 1 + utils/lxc.sh | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100755 utils/lxc.sh diff --git a/Makefile b/Makefile index 500910d8..0e1816eb 100644 --- a/Makefile +++ b/Makefile @@ -75,6 +75,7 @@ test: test.pep8 test.unit test.sh test.robot - make pylint test.sh: + shellcheck -x utils/lxc.sh shellcheck -x utils/lib.sh shellcheck -x utils/filtron.sh shellcheck -x utils/searx.sh diff --git a/utils/lxc.sh b/utils/lxc.sh new file mode 100755 index 00000000..d7926253 --- /dev/null +++ b/utils/lxc.sh @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*- +# SPDX-License-Identifier: AGPL-3.0-or-later + +# shellcheck source=utils/lib.sh +source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +source_dot_config + +# ---------------------------------------------------------------------------- +# config +# ---------------------------------------------------------------------------- + +# name of https://images.linuxcontainers.org +LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}" +HOST_PREFIX="${HOST_PREFIX:-searx}" + +TEST_IMAGES=( + "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804" + "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904" + "$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux" + #"$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31" + #"ubuntu-minimal:18.04" "ubu1804" + #"ubuntu-minimal:19.10" "ubu1910" +) + +REMOTE_IMAGES=() +LOCAL_IMAGES=() + +for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do + REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}") + LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${TEST_IMAGES[i+1]}") +done + +# ---------------------------------------------------------------------------- +usage() { +# ---------------------------------------------------------------------------- + + cat </dev/null; then + info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}" + else + info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}" + lxc image copy "${TEST_IMAGES[i]}" local: \ + --alias "${TEST_IMAGES[i+1]}" prefix_stdout + fi + done + #lxc image list local: +} + +lxc_delete_images_localy() { + echo + for i in "${LOCAL_IMAGES[@]}"; do + info_msg "delete image 'local:$i'" + lxc image delete "local:$i" + done + #lxc image list local: +} + +lxc_init_containers() { + echo + for i in "${LOCAL_IMAGES[@]}"; do + if lxc info "$HOST_PREFIX-$i" &>/dev/null; then + info_msg "conatiner '$HOST_PREFIX-$i' already exists" + else + info_msg "create conatiner instance: $HOST_PREFIX-$i" + lxc init "local:$i" "$HOST_PREFIX-$i" + fi + done + #lxc list "$HOST_PREFIX" +} + +lxc_delete_containers() { + echo + for i in "${LOCAL_IMAGES[@]}"; do + if lxc info "$HOST_PREFIX-$i" &>/dev/null; then + info_msg "stop & delete instance '$HOST_PREFIX-$i'" + lxc stop "$HOST_PREFIX-$i" &>/dev/null + lxc delete "$HOST_PREFIX-$i" | prefix_stdout + else + warn_msg "instance '$HOST_PREFIX-$i' does not exist / can't delete :o" + fi + done + #lxc list "$HOST_PREFIX" +} + + +# ---------------------------------------------------------------------------- +main "$@" +# ----------------------------------------------------------------------------