Add lxc/lxd to /usr/local/bin so that it works nicely with sudo

This commit is contained in:
Alexandre Aubin 2019-10-08 23:45:39 +02:00
parent 360c25c747
commit ea22e9409d
2 changed files with 30 additions and 23 deletions

View file

@ -123,8 +123,11 @@ be able to install `snapd` using the system package manager (or even
apt install git snapd
sudo snap install lxd
# You need to add /snap/bin to your PATH variable ... maybe add this to your .bashrc!
PATH=$PATH:/snap/bin
# Adding lxc/lxd to /usr/local/bin to make sure we can use them easily even
# with sudo for which the PATH is defined in /etc/sudoers and probably doesn't
# include /snap/bin
sudo ln -s /snap/bin/lxc /usr/local/bin/lxc
sudo ln -s /snap/bin/lxd /usr/local/bin/lxd
```
Then you shall initialize LXD which will ask you a bunch of question. Usually

46
ynh-dev
View file

@ -1,7 +1,5 @@
#!/bin/bash
lxc="sudo /snap/bin/lxc"
function show_usage() {
cat <<EOF
@ -115,10 +113,16 @@ function create_sym_link() {
function check_lxd_setup()
{
local LXD_VERSION=$(lxd --version)
# Check lxd is installed somehow
[[ -e /snap/bin/lxd ]] || which lxd 2>/dev/null \
|| critical "You need to have LXD installed for ynh-dev to be usable from the host machine. Refer to the README to know how to install it."
[[ -n "$LXD_VERSION" ]] \
|| critical "You need to have LXD install for ynh-dev to be usable from the host machine. From a debian-like system, you can install it with 'apt install snap' then 'snap install lxd'. (Don't forget to add /snap/bin to your \$PATH somehow. Then you can run 'lxd init' (keeping all the default option is usally okay!)"
# Check that we'll be able to use lxc/lxd using sudo (for which the PATH is defined in /etc/sudoers and probably doesn't include /snap/bin)
if [[ ! -e /usr/bin/lxc ]] && [[ ! -e /usr/bin/lxd ]]
then
[[ -e /usr/local/bin/lxc ]] && [[ -e /usr/local/bin/lxd ]] \
|| critical "You might want to add lxc and lxd inside /usr/local/bin so that there's no tricky PATH issue with sudo. If you installed lxd/lxc with snapd, this should do the trick: sudo ln -s /snap/bin/lxc /usr/local/bin/lxc && sudo ln -s /snap/bin/lxd /usr/local/bin/lxd"
fi
ip a | grep -q lxdbr0 \
|| critical "There is no 'lxdbr0' interface... Did you ran 'lxd init' ?"
@ -130,12 +134,12 @@ function start_ynhdev()
local BOX=${1:-ynh-dev}
$lxc info $BOX &>/dev/null && critical "The container already exist. Use 'attach' to enter the LXC, or 'destroy' if you aim to recreate it."
$lxc image info $BOX-base &>/dev/null || critical "You should first build the base YunoHost LXC using ./ynh-dev rebuild"
sudo lxc info $BOX &>/dev/null && critical "The container already exist. Use 'attach' to enter the LXC, or 'destroy' if you aim to recreate it."
sudo lxc image info $BOX-base &>/dev/null || critical "You should first build the base YunoHost LXC using ./ynh-dev rebuild"
set -eu
set -x
$lxc launch $BOX-base $BOX
$lxc config device add $BOX ynhdev-shared-folder disk path=/ynh-dev source="$PWD"
sudo lxc launch $BOX-base $BOX
sudo lxc config device add $BOX ynhdev-shared-folder disk path=/ynh-dev source="$PWD"
set +x
attach_ynhdev $BOX
@ -145,16 +149,16 @@ function attach_ynhdev()
{
check_lxd_setup
local BOX=${1:-ynh-dev}
$lxc start $BOX 2>/dev/null || true
$lxc exec $BOX -- /bin/bash
sudo lxc start $BOX 2>/dev/null || true
sudo lxc exec $BOX -- /bin/bash
}
function destroy_ynhdev()
{
check_lxd_setup
local BOX=${1:-ynh-dev}
$lxc stop $BOX
$lxc delete $BOX
sudo lxc stop $BOX
sudo lxc delete $BOX
}
function rebuild_ynhdev()
@ -164,14 +168,14 @@ function rebuild_ynhdev()
local BOX=${1:-ynh-dev}
set -x
$lxc info $BOX-rebuild >/dev/null && $lxc delete $BOX-rebuild --force
$lxc launch images:debian/stretch/amd64 $BOX-rebuild
$lxc config set $BOX-rebuild security.privileged true
$lxc restart $BOX-rebuild
$lxc exec $BOX-rebuild -- apt install curl -y
$lxc exec $BOX-rebuild -- /bin/bash -c "curl https://install.yunohost.org | bash -s -- -a -d unstable"
$lxc stop $BOX-rebuild
$lxc publish $BOX-rebuild --alias $BOX-base
sudo lxc info $BOX-rebuild >/dev/null && sudo lxc delete $BOX-rebuild --force
sudo lxc launch images:debian/stretch/amd64 $BOX-rebuild
sudo lxc config set $BOX-rebuild security.privileged true
sudo lxc restart $BOX-rebuild
sudo lxc exec $BOX-rebuild -- apt install curl -y
sudo lxc exec $BOX-rebuild -- /bin/bash -c "curl https://install.yunohost.org | bash -s -- -a -d unstable"
sudo lxc stop $BOX-rebuild
sudo lxc publish $BOX-rebuild --alias $BOX-base
set +x
}