File: ssh-setup-lxd

package info (click to toggle)
autopkgtest 5.53
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,600 kB
  • sloc: python: 15,484; sh: 2,317; makefile: 116; perl: 19
file content (125 lines) | stat: -rwxr-xr-x 3,310 bytes parent folder | download | duplicates (5)
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
#!/bin/sh
# This testsuite is part of autopkgtest
# autopkgtest is a tool for testing Debian binary packages
# autopkgtest is Copyright (C) 2006-2014 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
#
# autopkgtest-virt-ssh setup script that configures a container; this is only used for
# testing autopkgtest itself, autopkgtest-virt-lxd is much better for actual test runs

set -e

# add testbed capabilities here (possibly dynamically), see
# doc/README.virtualisation-server.rst
CAPABILITIES='isolation-container revert revert-full-system'
USER=autopkgtest
SUDO_PASSWORD=autopkgtest

CONTAINER=""
IMAGE=""
INSTALL_KEY=
ENABLE_SUDO=

# create a testbed (if necessary), configure ssh, copy ssh key into it,
# configure sudo, etc.; print a list of "key=value" parameters to stdout on
# success
# required: login, hostname, and one of identity or password
# optional: port, options, capabilities
open() {
    [ -z "$2" ] || IMAGE="$2"
    if [ -z "${IMAGE}" ]; then
        echo "ERROR: $0 needs to be called with image name" >&1
        exit 1
    fi

    [ -n "$CONTAINER" ] || CONTAINER=$(mktemp -u autopkgtest-test-XXX)

    lxc launch --ephemeral "$IMAGE" "$CONTAINER" >/dev/null

    # wait for and parse IPv4
    while ! OUT=$(lxc info "$CONTAINER"|grep 'eth0:.*inet[^6]'); do
        sleep 1
    done
    IP=$(echo "$OUT" | grep -o '10\.[0-9]\+\.[0-9]\+\.[0-9]\+')

    # create user
    # password: python3 -c 'from crypt import *; print(crypt("autopkgtest", mksalt(METHOD_CRYPT)))'
    lxc exec "$CONTAINER" -- useradd --password FJfXYBhFnX6xA --create-home "$USER"

    # install SSH
    lxc exec "$CONTAINER" -- eatmydata apt-get install -y openssh-server >/dev/null 2>&1

    if [ -n "$INSTALL_KEY" ]; then
        key=$(cat "$HOME/.ssh/id_rsa.pub")
        lxc exec "$CONTAINER" -- su -c "mkdir ~/.ssh; echo '$key' > ~/.ssh/authorized_keys" "$USER"
        echo "identity=$HOME/.ssh/id_rsa"
    fi

    if [ -n "$ENABLE_SUDO" ]; then
        lxc exec "$CONTAINER" -- sh -ec "echo '$USER ALL=(ALL) $ENABLE_SUDO' > /etc/sudoers.d/autopkgtest"
    fi

    cat<<EOF
login=$USER
hostname=$IP
capabilities=$CAPABILITIES
password=$SUDO_PASSWORD
extraopts=-n $CONTAINER -I $IMAGE
EOF
}

revert() {
    if [ -z "$CONTAINER" ]; then
        echo "Needs to be called with -n <container name>" >&2
        exit 1
    fi
    cleanup
    open
}

cleanup() {
    if [ -z "$CONTAINER" ]; then
        echo "Needs to be called with -n <container name>" >&2
        exit 1
    fi
    lxc delete --force "$CONTAINER"
}

# parse options
eval "set -- $(getopt -o "ksSn:I:c" -- "$@")"
while true; do
    case "$1" in
        -k)
            INSTALL_KEY=1; shift ;;
        -s)
            ENABLE_SUDO="ALL"; shift ;;
        -S)
            ENABLE_SUDO="NOPASSWD: ALL"; shift ;;
        -n)
            CONTAINER="$2"; shift 2 ;;
        -I)
            IMAGE="$2"; shift 2 ;;
        --)
            shift; break ;;
        *)
            echo "$0: unsupported option $1" >&2
            exit 1;;
    esac
done

case "$1" in
    open)
        open "$@";;
    cleanup)
        cleanup "$@";;
    revert)
        revert "$@";;
    '')
        echo "Needs to be called with command as first argument" >&2
        exit 1
        ;;
    *)
        echo "invalid command $1" >&2
        exit 1
        ;;
esac