For a while there I was completely flummoxed trying to come up with an
ultra simple way to deal with dotless LXD hostnames without requiring an
otherwise useless out of band host-to-fqdn mapping system. This strategy
works for me and might be useful for someone else. It does rely on DNS or
/etc/hosts to resolve a hostname to a FQDN but that tool is most likely
required anyway and not some arbitrary file or db mapping system.

$1 is EITHER hostname or fqdn, $2 is name of ssh key (or ~/.ssh/id_rsa)

# exit script if $1 does not resolve

host -t a $1 || exit 1

# if $1 does not contain a dot then resolve $VHOST to FQDN

[[ $1 =~ \. ]] && VHOST=$1 || VHOST=$(host -t a $1 | cut -d ' ' -f1)

if [[ -d /var/lib/lxd/containers/$1 ]]; then
    if ! lxc list | grep " $1 " | grep -q RUNNING; then
        lxc start $1
    fi

    LEX="lxc exec $1 --"
    $LEX bash -c 'test ! -d ~/.ssh && mkdir ~/.ssh && chmod 0700 ~/.ssh'

    $LEX bash -c 'test -f ~/.ssh/authorized_keys'
    if [[ $? -eq 1 ]]; then
        lxc file push --uid=0 --gid=0 --mode=0600 ~/.ssh/$S_KEY.pub 
$1/root/.ssh/authorized_keys
    fi

    $LEX bash -c 'which sshd' > /dev/null
    if [[ $? -gt 0 ]]; then
        $LEX apt-get update > /dev/null 2>&1
        $LEX apt-get install -qq -y --no-install-recommends openssh-server dnsutils > 
/dev/null 2>&1
    fi
else
    # $1 is a fqdn so setup some other non-LXD remote server
fi

ssh -o StrictHostKeyChecking=no root@$VHOST 'bash -s' << EOS
# continue system setup inside container (or remote host)
EOS

Now we have a "normalized" container or remote server ready to continue
working on.
_______________________________________________
lxc-users mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-users

Reply via email to