[enh] Add vagrant-lxc support

This commit is contained in:
ljf 2018-08-10 00:45:37 +02:00
parent f2311c1326
commit 9b2dde7dd4

59
ynh-dev
View file

@ -1,5 +1,8 @@
#!/bin/bash #!/bin/bash
DEFAULT_VERSION="stretch-unstable"
DEFAULT_PROVIDER="lxc"
usage() { usage() {
cat <<EOF cat <<EOF
Usage : Usage :
@ -103,9 +106,9 @@ elif [ "$1" = "create-env" ]; then
cp ynh-dev-tools/ynh-dev ynh-dev cp ynh-dev-tools/ynh-dev ynh-dev
################################# ##########################################
## Run a vm and give a prompt ## ## Run a vm/container and give a prompt ##
################################# ##########################################
elif [ "$1" = "run" ]; then elif [ "$1" = "run" ]; then
if [[ $2 == *"-"* ]]; then if [[ $2 == *"-"* ]]; then
echo "ERROR: Vagrant virtual machine ($2) cannot contain any dash" echo "ERROR: Vagrant virtual machine ($2) cannot contain any dash"
@ -113,30 +116,31 @@ elif [ "$1" = "run" ]; then
fi fi
DOMAIN=$2 DOMAIN=$2
VERSION='stable' VERSION=$DEFAULT_VERSION
if [ "$#" = "3" ]; then if [ "$#" = "3" ]; then
VERSION=$3 VERSION=$3
if [ "$VERSION" = "unstable" ]; then
VERSION="jessie-unstable"
fi
if [ "$VERSION" = "unstable" ]; then
echo "ERROR: Incorrect version '$VERSION'. See '$(basename $0) --help' for usage."
exit 102
fi
fi fi
PROVIDER=$DEFAULT_PROVIDER
echo "Creating $DOMAIN virtual machine with YunoHost $VERSION version" echo "Creating $DOMAIN virtual machine with YunoHost $VERSION version"
echo "" echo ""
# Get vagrant box info from version BOX_NAME="yunohost/$VERSION"
if [ "$VERSION" = "unstable" ]; then BOX_URL="https://build.yunohost.org/yunohost-$BOX_NAME-$PROVIDER.box"
BOX_NAME="yunohost/jessie-unstable"
BOX_URL="https://build.yunohost.org/yunohost-jessie-unstable.box"
elif [ "$VERSION" = "stretch-unstable" ]; then
BOX_NAME="yunohost/stretch-unstable"
BOX_URL="https://build.yunohost.org/yunohost-stretch-unstable.box"
else
echo "ERROR: Incorrect version '$VERSION'. See '$(basename $0) --help' for usage."
exit 102
fi
# Download box if not available # Download box if not available
if ! vagrant box list | grep -qc $BOX_NAME ; then if ! vagrant box list | grep -qc $BOX_NAME ; then
echo "Vagrant box '$BOX_NAME' is missing. Trying to download it" echo "Vagrant box '$BOX_NAME' is missing. Trying to download it"
vagrant box add $BOX_NAME $BOX_URL --provider virtualbox vagrant box add $BOX_NAME $BOX_URL --provider $PROVIDER
echo "" echo ""
fi fi
@ -151,13 +155,13 @@ elif [ "$1" = "run" ]; then
for i in `seq 2 254`; for i in `seq 2 254`;
do do
grep "${IP_BASE//./\.}$i" Vagrantfile &> /dev/null || { grep "${IP_BASE//./\.}$i" Vagrantfile &> /dev/null || {
IP="${IP_BASE}$i" IP="$i"
break break
} }
done done
# Update Vagrantfile # Update Vagrantfile
grep "### END AUTOMATIC YNH-DEV ###" ./Vagrantfile &> /dev/null || { grep "### END AUTOMATIC YNH-DEV" ./Vagrantfile &> /dev/null || {
pushd ./vagrant &> /dev/null pushd ./vagrant &> /dev/null
git pull git pull
popd &> /dev/null popd &> /dev/null
@ -167,22 +171,19 @@ elif [ "$1" = "run" ]; then
} }
# Adapt vagrantfile # Adapt vagrantfile
perl -i -pe "s| (### END AUTOMATIC YNH-DEV ###)|\ perl -i -pe "s| (### END AUTOMATIC YNH-DEV ZONE ###)|\
config.vm.define \"${VMNAME}\" do \|${VMNAME}\| \ \"${VMNAME}\" => [\"$IP\", \"${VERSION}\"], \
\n ${VMNAME}.vm.box = \"${BOX_NAME}\" \ \n \1|" ./Vagrantfile
\n ${VMNAME}.vm.network :private_network, ip: \"${IP}\" \
\n end \
\n \1|" ./Vagrantfile
} }
# Run VM # Run VM
vagrant up $VMNAME --provider virtualbox vagrant up $VMNAME --provider $PROVIDER
# Warn user about hosts file # Warn user about hosts file
IP_LINE="[[:space:]]*${VMNAME}.vm.network[[:space:]]*:private_network,[[:space:]]*ip:[[:space:]]*\"" IP_LINE="[[:space:]]*\"${VMNAME}\"[[:space:]]*=>[[:space:]]*\[\"\K.*?(?=\", \"${VERSION}\"\],)"
IP=$(grep "$IP_LINE" Vagrantfile | sed "s/${IP_LINE}//" | tr -d '"') IP=$(grep -Po "$IP_LINE" ./Vagrantfile )
echo "/!\ Please add '$IP $DOMAIN' to your /etc/hosts file /!\\" echo "/!\ Please add '$IP_BASE$IP $DOMAIN' to your /etc/hosts file /!\\"
echo "sudo bash -c 'echo \"$IP $DOMAIN\" >> /etc/hosts'" echo "sudo bash -c 'echo \"$IP_BASE$IP $DOMAIN\" >> /etc/hosts'"
echo "" echo ""
# Log into the VM # Log into the VM