Vagrant is madness, LXD is much easy to setup and use

This commit is contained in:
Alexandre Aubin 2019-10-01 20:33:36 +02:00
parent d2ef67fd62
commit 0ab8729d70
4 changed files with 71 additions and 201 deletions

View file

@ -53,14 +53,14 @@ your capacities and resources when aiming to setup a development environment.
Yunohost can be developed on using a combination of the following technologies:
* Git (any version is sufficient)
* Vagrant (>= 2.x)
* LXC (>= 3.x)
* Vagrant-LXC (>= 1.4.x)
* LXD (>= 2.x) (though only tested with 3.x for now)
LXC is typically lightweight but you may find the initial setup complex
(in particular network configuration). Alternatively, you may be able
to setup a local environnement using Virtualbox which is kinda more
resource-hungry :
As LXC are containers, they are typically lightweight but you may find the
initial setup complex (in particular network configuration). LXD makes the
management of LXC much simpler.
Alternatively, you may be able to setup a local environnement using Vagrant and
Virtualbox which is kinda more resource-hungry because it is fully virtualized.
* Virtualbox (>= 6.x)
* Vagrant-virtualbox (>= ?.?.?)
@ -113,58 +113,29 @@ Here is the development flow:
First you need to install the system dependencies.
`ynh-dev` essentially requires Git, Vagrant, and and the LXC ecosystem. Please
`ynh-dev` essentially requires Git and the LXD/LXC ecosystem. Please
see the [local development path](#local-development-path) section for some idea
of the versions required.
Please consider using the [latest Vagrant version from their website](https://www.vagrantup.com/downloads.html), distribution versions can include weird bugs that have been fixed upstream. If you still prefer to do that, here are the instructions:
The following commands should work on **Linux Mint 19** (and possibly on any Debian Stretch?):
On a Debian-based system (regular Debian, Ubuntu, Mint ...), LXD can be
installed using `snapd`. On other systems like Archlinux, it's probably that you
will also be able to install `snapd` using the system package manager (or even
`lxd` directly).
```bash
$ sudo apt update
$ sudo apt install git vagrant lxc-templates lxctl lxc cgroup-lite redir bridge-utils libc6 debootstrap libvirt-dev
$ vagrant plugin install vagrant-lxc
$ echo "cgroup /sys/fs/cgroup cgroup defaults 0 0" | sudo tee -a /etc/fstab
$ sudo mount /sys/fs/cgroup
$ lxc-checkconfig
$ echo "veth" | sudo tee -a /etc/modules
```
If you have install libvirtd, you need to stop it and kill dnsmasq libvirtd process, to avoid conflict with dhcp. If you don't ynh-dev start will fail because the lxc container won't be able to get an ip.
apt install git snapd
snap install lxd
On **Debian Buster**, I had to re-patch the driver.rb of vagrant-lxc plugin with [this version](https://raw.githubusercontent.com/fgrehm/vagrant-lxc/2a5510b34cc59cd3cb8f2dcedc3073852d841101/lib/vagrant-lxc/driver.rb) (especially the `roofs_path` function). I also had to install `apparmor` then `systemctl restart apparmor` for `lxc-start` to work.
Also check instruction on https://feeding.cloud.geek.nz/posts/lxc-setup-on-debian-stretch/.
If you run **Archlinux**, this page should be quite useful to setup LXC: https://github.com/fgrehm/vagrant-lxc/wiki/Usage-on-Arch-Linux-hosts
On **both Debian and Archlinux**, typically `/etc/default/lxc-net` and `/etc/lxc/default.conf` should look like this :
```
$ cat /etc/default/lxc-net
USE_LXC_BRIDGE="true"
LXC_BRIDGE="lxcbr0"
LXC_ADDR="10.0.3.1"
LXC_NETMASK="255.255.255.0"
LXC_NETWORK="10.0.3.0/24"
LXC_DHCP_RANGE="10.0.3.2,10.0.3.254"
LXC_DHCP_MAX="253"
$ cat /etc/lxc/default.conf
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
# You need to add /snap/bin to your PATH variable ... maybe add this to your .bashrc!
PATH=$PATH:/snap/bin
```
On **Debian Buster**, for backup stuff to work correctly with apparmor, I also had to add:
Then you shall initialize LXD which will ask you a bunch of question. Usually
answering the default (just pressing enter) to all questions is fine.
```bash
lxd init
```
mount options=(ro, remount, bind, rbind)
mount options=(ro, remount, bind, relatime)
```
to `/etc/apparmor.d/lxc/lxc-default-cgns` and restart the apparmor service.
Then, go into your favorite development folder and deploy `ynh-dev` with:
@ -182,18 +153,26 @@ between the host and the LXC.
When ran on the host, the `./ynh-dev` command allows you to manage YunoHost's dev LXCs.
First, you might want to start a new LXC with:
First, you might want to build the base LXC with:
```bash
$ cd ynh-dev # if not already done
$ ./ynh-dev rebuild
# ... This will take some time, grab your favorite beverage ...
```
This should create a fresh Debian Stretch LXC, install Yunohost inside and save
the result as `ynh-dev-base` which can then be used to create your actual dev
LXC. (This base can then be used to recreate a fresh Yunohost LXC if you need to
destroy your work LXC)
Then start your actual dev LXC using :
```bash
$ ./ynh-dev start
```
This should download an already built LXC from `build.yunohost.org`. If this does not work (or the LXC is outdated), you might want to (re)build a fresh LXC locally with `./ynh-dev rebuild`.
After starting the LXC, you should be automatically SSH'ed inside. If you later disconnect from the LXC, you can go back in with `./ynh-dev ssh`.
Later, you might want to destroy the LXC. You can do so with `./ynh-dev destroy`.
After starting the LXC, your terminal will automatically be attached to it. If you later disconnect from the LXC, you can go back in with `./ynh-dev attach`. Later, you might want to destroy the LXC. You can do so with `./ynh-dev destroy`.
## 3. Development and container testing
@ -227,9 +206,7 @@ Note that `./ynh-dev use-git yunohost-admin` has a particular behavior: it start
## Advanced: using snapshots
Vagrant is not well integrated with LXC snapshots.
However, you may still use `lxc-snapshot` directly to manage snapshots.
You can check `lxc snapshot --help` to learn how to manage lxc snapshots.
## Alternative: Using Only Virtualbox

35
Vagrantfile vendored
View file

@ -1,35 +0,0 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
NETWORK = "10.0.3."
HOSTS = {
"ynh-dev" => ["83", "ynh-dev"],
"ynh-dev-buster" => ["84", "ynh-dev-buster"],
}
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Force guest type, because YunoHost /etc/issue can't be tuned
config.vm.guest = :debian
HOSTS.each do | (name, cfg) |
ipaddr, version = cfg
config.vm.define name do |machine|
machine.vm.box = "yunohost/" + version
# Force guest type, because YunoHost /etc/issue can't be tuned
machine.vm.guest = :debian
machine.vm.provider "lxc" do |lxc|
config.vm.box_url = "https://build.yunohost.org/" + version + "-lxc.box"
config.vm.synced_folder ".", "/ynh-dev", id: "vagrant-root"
config.vm.network :private_network, ip: NETWORK + ipaddr, lxc__bridge_name: 'lxcbr0'
end
end
end # HOSTS-each
end

58
prebuild/Vagrantfile vendored
View file

@ -1,58 +0,0 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
$script = <<SCRIPT
export DEBIAN_FRONTEND=noninteractive
# Configure guest hostname
sudo bash -c 'echo 127.0.1.1 yunohost.yunohost.org yunohost >> /etc/hosts'
sudo hostname yunohost.yunohost.org
sudo bash -c 'echo yunohost.yunohost.org > /etc/hostname'
# Define root password
echo -e "yunohost\nyunohost" | sudo passwd root
# Allow sudo removal (YunoHost use sudo-ldap)
export SUDO_FORCE_REMOVE=yes
# Upgrade guest (done in install script)
sudo apt-get update
sudo apt-get -y --force-yes upgrade
sudo apt-get -y --force-yes dist-upgrade
# Install YunoHost
wget https://raw.githubusercontent.com/YunoHost/install_script/stretch/install_yunohost -q -O /tmp/install_yunohost
sudo bash /tmp/install_yunohost -a -d unstable
# Cleanup
sudo apt-get clean -y
SCRIPT
NETWORK = "10.0.3."
HOSTS = {
"ynh-dev" => ["83", "stretch64"],
"ynh-dev-buster" => ["84", "testing64"],
}
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
HOSTS.each do | (name, cfg) |
ipaddr, box = cfg
config.vm.define name do |machine|
machine.vm.box = "debian/" + box
machine.vm.provision "shell" do |s|
s.inline = $script
s.args = ""
end
machine.vm.provider "lxc" do |lxc|
machine.vm.network :private_network, ip: NETWORK + ipaddr, lxc__bridge_name: 'lxcbr0'
end
end
end
end

86
ynh-dev
View file

@ -7,7 +7,7 @@ function show_usage() {
${BLUE}==============================${NORMAL}
start [NAME] (Create and) starts a LXC (ynh-dev by default)
ssh [NAME] SSH into an already started LXC (ynh-dev by default)
attach [NAME] Attach an already started LXC (ynh-dev by default)
destroy [NAME] Destroy the ynh-dev LXC (ynh-dev by default)
rebuild Rebuild a fresh, up-to-date box
@ -35,7 +35,7 @@ function main()
help|-h|--help) show_usage $ARGUMENTS ;;
start|--start) start_ynhdev $ARGUMENTS ;;
ssh|--ssh) ssh_ynhdev $ARGUMENTS ;;
attach|--attach) attach_ynhdev $ARGUMENTS ;;
destroy|--destroy) destroy_ynhdev $ARGUMENTS ;;
rebuild|--rebuild) rebuild_ynhdev $ARGUMENTS ;;
@ -111,80 +111,66 @@ function create_sym_link() {
# Actions #
##################################################################
function check_lxc_setup()
function check_lxd_setup()
{
local LXC_VERSION=$(lxc-info --version)
local LXD_VERSION=$(lxd --version)
[[ "${LXC_VERSION:0:1}" == 3 ]] \
|| critical "You need at least version 3 of LXC otherwise the box won't work"
[[ -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!)"
[[ $(systemctl is-active lxc-net) == "active" ]] \
|| critical "Service lxc-net should be running ... You probably need to fix your lxc network conf before being able to use lxc's."
ip a | grep -q lxcbr0 \
|| critical "There is no 'lxcbr0' interface. You probably need to fix your lxc network conf before being able to use lxc's."
ip a | grep -q lxdbr0 \
|| critical "There is no 'lxdbr0' interface... Did you ran 'lxd init' ?"
}
function start_ynhdev()
{
check_lxc_setup
check_lxd_setup
local NAME=${1:-ynh-dev}
local BOX_NAME="yunohost/$NAME"
local BOX_URL="https://build.yunohost.org/yunohost-$BOX_NAME-lxc.box"
local BOX=${1:-ynh-dev}
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
# Download box if not available
#if ! vagrant box list | grep -qc $BOX_NAME ; then
# info "Vagrant box '$BOX_NAME' is missing. Trying to download it"
# vagrant box add $BOX_NAME $BOX_URL
#fi
set -x
sudo lxc launch $BOX-base $BOX
sudo lxc config device add $BOX ynhdev-shared-folder disk path=/ynh-dev source="$PWD"
set +x
# Run VM
vagrant up $NAME
# Log into the VM
ssh_ynhdev $NAME
attach_ynhdev $BOX
}
function ssh_ynhdev()
function attach_ynhdev()
{
check_lxc_setup
local NAME=${1:-ynh-dev}
vagrant ssh $NAME -c "echo 'You are now inside the LXC !'; cd /ynh-dev; sudo su"
check_lxd_setup
local BOX=${1:-ynh-dev}
sudo lxc start $BOX 2>/dev/null || true
sudo lxc exec $BOX -- /bin/bash
}
function destroy_ynhdev()
{
check_lxc_setup
local NAME=${1:-ynh-dev}
vagrant destroy $NAME
check_lxd_setup
local BOX=${1:-ynh-dev}
sudo lxc stop $BOX
sudo lxc delete $BOX
}
function rebuild_ynhdev()
{
check_lxc_setup
check_lxd_setup
local BOX="ynh-dev"
local BOX=${1:-ynh-dev}
set -x
cd prebuild
# Create box
vagrant up $BOX --provider=lxc --provision || critical "Could not create the box ?"
# Package box
rm -f /tmp/ynh-dev.box
vagrant package $BOX --output /tmp/$BOX.box || critical "Could not package package the box ?"
# Destroy current box
vagrant destroy $BOX
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
# User message, and exit
info "The Vagrant box was packaged to /tmp/$BOX.box"
info "You might want to run : vagrant box add 'yunohost/ynh-dev' /tmp/$BOX.box --force"
}
function show_vm_ip()