summaryrefslogtreecommitdiff
path: root/utils/lxc.sh
blob: d792625347f19f1bd1bfe66ac7f31432af52f9af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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 <<EOF

usage::

  $(basename "$0") build [hosts]
  $(basename "$0") delete [hosts]

build / delete
  build and/or delete all LXC hosts

all LXC hosts:
  ${LOCAL_IMAGES[@]}

EOF
    [ ! -z "${1+x}" ] &&  err_msg "$1"
}

main() {
    rst_title "LXC tooling box" part

    required_commands lxc || exit

    local _usage="unknown or missing $1 command $2"

    case $1 in
        --source-only)  ;;
        -h|--help) usage; exit 0;;

        build)
            sudo_or_exit
            case $2 in
                hosts) build_instances ;;
                *) usage "$_usage"; exit 42;;
            esac ;;
        delete)
            sudo_or_exit
            case $2 in
                hosts) delete_instances ;;
                *) usage "$_usage"; exit 42;;
            esac ;;
        *)
            usage "unknown or missing command $1"; exit 42;;
    esac
}

build_instances() {
    rst_title "Build LXC instances"
    lxc_copy_images_localy
    lxc_init_containers

    err_msg "WIP / sorry, not implemented yet :o"
}

delete_instances() {
    rst_title "Delete LXC instances"
    echo -en "\\nLXC hosts(s)::\\n\\n  ${LOCAL_IMAGES[*]}\\n" | $FMT
    if ask_yn "Do you really want to delete all images"; then
        lxc_delete_containers
    fi
}

lxc_copy_images_localy() {
    echo
    for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
        if lxc image info "local:${TEST_IMAGES[i+1]}" &>/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 "$@"
# ----------------------------------------------------------------------------