mirror of
https://github.com/YunoHost/ynh-dev.git
synced 2024-09-03 20:05:59 +02:00
9b4408505f
* [fix] Remove stable and testing because the images do not exist anymore * [fix] Remove stable and testing because images do not exist anymore
379 lines
13 KiB
Bash
Executable file
379 lines
13 KiB
Bash
Executable file
#!/bin/bash
|
||
|
||
usage() {
|
||
cat <<EOF
|
||
Usage :
|
||
|
||
On the host
|
||
`basename $0` create-env PATH
|
||
Create a dev environment into PATH
|
||
`basename $0` run DOMAIN [VERSION]
|
||
Run a vagrant or virtualbox vm
|
||
# `basename $0` kill
|
||
# Kill all vagrant
|
||
|
||
Inside the vm
|
||
`basename $0` ip
|
||
Give the ip of the guest container
|
||
`basename $0` upgrade
|
||
Upgrade the container
|
||
`basename $0` use-git [PACKAGES [PACKAGES ...]]
|
||
Use Git repositories from dev environment path
|
||
`basename $0` test [PACKAGES [PACKAGES ...]]
|
||
Deploy, update and run tests for some packages
|
||
`basename $0` self-update
|
||
Update this script (`basename $0`)
|
||
|
||
PACKAGES :
|
||
moulinette
|
||
ssowat
|
||
yunohost
|
||
yunohost-admin
|
||
|
||
VERSION
|
||
unstable
|
||
EOF
|
||
}
|
||
|
||
check_yunohost_vm() {
|
||
if [ ! -d /etc/yunohost ]
|
||
then
|
||
echo "You need to install YunoHost first. Maybe you are not in a vm ?"
|
||
exit 100;
|
||
fi
|
||
}
|
||
|
||
create_sym_link() {
|
||
# Remove current sources if not a symlink
|
||
if [ ! -L '$2' ]; then
|
||
sudo rm -rf $2
|
||
fi
|
||
# Symlink from Git repository
|
||
sudo ln -sfn $1 $2
|
||
}
|
||
|
||
packages=${@:2}
|
||
if [ "$#" = "1" ]; then
|
||
packages=('moulinette' 'ssowat' 'yunohost' 'yunohost-admin')
|
||
fi
|
||
|
||
BASE_DIR=./
|
||
IP_BASE="192.168.33."
|
||
|
||
##################
|
||
## Help message ##
|
||
##################
|
||
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ]; then
|
||
usage
|
||
|
||
|
||
######################################
|
||
## Create a development environment ##
|
||
######################################
|
||
elif [ "$1" = "create-env" ]; then
|
||
set -x
|
||
|
||
pwd=`pwd`
|
||
|
||
if [ ! "$2" ]
|
||
then
|
||
echo "I need a destination folder to create the dev environement"
|
||
exit 1
|
||
fi
|
||
|
||
[ -e "$2" ] || mkdir -p $2
|
||
cd $2
|
||
|
||
# Create apps & backup folder
|
||
mkdir -p apps
|
||
mkdir -p backup
|
||
|
||
# Get YunoHost packages
|
||
git clone -b unstable https://github.com/YunoHost/SSOwat ssowat
|
||
git clone -b unstable https://github.com/YunoHost/yunohost-admin yunohost-admin
|
||
git clone -b unstable https://github.com/YunoHost/yunohost yunohost
|
||
git clone -b unstable https://github.com/YunoHost/moulinette moulinette
|
||
|
||
# Get YunoHost Vagrantfile
|
||
git clone -b master https://github.com/YunoHost/Vagrantfile vagrant
|
||
ln -s vagrant/Vagrantfile Vagrantfile
|
||
|
||
# Get YunoHost dev tools
|
||
git clone -b master https://github.com/YunoHost/ynh-dev ynh-dev-tools
|
||
cp ynh-dev-tools/ynh-dev ynh-dev
|
||
|
||
|
||
#################################
|
||
## Run a vm and give a prompt ##
|
||
#################################
|
||
elif [ "$1" = "run" ]; then
|
||
if [[ $2 == *"-"* ]]; then
|
||
echo "ERROR: Vagrant virtual machine ($2) cannot contain any dash"
|
||
exit 2
|
||
fi
|
||
|
||
DOMAIN=$2
|
||
VERSION='stable'
|
||
if [ "$#" = "3" ]; then
|
||
VERSION=$3
|
||
fi
|
||
|
||
echo "Creating $DOMAIN virtual machine with YunoHost $VERSION version"
|
||
echo ""
|
||
|
||
# Get vagrant box info from version
|
||
if [ "$VERSION" = "unstable" ]; then
|
||
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
|
||
if ! vagrant box list | grep -qc $BOX_NAME ; then
|
||
echo "Vagrant box '$BOX_NAME' is missing. Trying to download it"
|
||
vagrant box add $BOX_NAME $BOX_URL --provider virtualbox
|
||
echo ""
|
||
fi
|
||
|
||
# Deduce the vm name
|
||
VMNAME=${DOMAIN//./_}
|
||
|
||
|
||
# Add the vm vagrant config in Vagrantfile
|
||
vagrant status $VMNAME &> /dev/null || {
|
||
|
||
# Find an available ip
|
||
for i in `seq 2 254`;
|
||
do
|
||
grep "${IP_BASE//./\.}$i" Vagrantfile &> /dev/null || {
|
||
IP="${IP_BASE}$i"
|
||
break
|
||
}
|
||
done
|
||
|
||
# Update Vagrantfile
|
||
grep "### END AUTOMATIC YNH-DEV ###" ./Vagrantfile &> /dev/null || {
|
||
pushd ./vagrant &> /dev/null
|
||
git pull
|
||
popd &> /dev/null
|
||
rm ./Vagrantfile
|
||
ln -s vagrant/Vagrantfile Vagrantfile
|
||
|
||
}
|
||
|
||
# Adapt vagrantfile
|
||
perl -i -pe "s| (### END AUTOMATIC YNH-DEV ###)|\
|
||
config.vm.define \"${VMNAME}\" do \|${VMNAME}\| \
|
||
\n ${VMNAME}.vm.box = \"${BOX_NAME}\" \
|
||
\n ${VMNAME}.vm.network :private_network, ip: \"${IP}\" \
|
||
\n end \
|
||
\n \1|" ./Vagrantfile
|
||
}
|
||
|
||
# Run VM
|
||
vagrant up $VMNAME --provider virtualbox
|
||
|
||
# Warn user about hosts file
|
||
IP_LINE="[[:space:]]*${VMNAME}.vm.network[[:space:]]*:private_network,[[:space:]]*ip:[[:space:]]*\""
|
||
IP=$(grep "$IP_LINE" Vagrantfile | sed "s/${IP_LINE}//" | tr -d '"')
|
||
echo "/!\ Please add '$IP $DOMAIN' to your /etc/hosts file /!\\"
|
||
echo "sudo bash -c 'echo \"$IP $DOMAIN\" >> /etc/hosts'"
|
||
echo ""
|
||
|
||
# Log into the VM
|
||
vagrant ssh $VMNAME -c "sudo -i"
|
||
|
||
|
||
#####################
|
||
## Kill running VM ##
|
||
#####################
|
||
elif [ "$1" = "kill" ]; then
|
||
vagrant destroy
|
||
|
||
|
||
#######################
|
||
## Update current VM ##
|
||
#######################
|
||
elif [ "$1" = "upgrade" ]; then
|
||
check_yunohost_vm
|
||
sudo apt-get update
|
||
sudo apt-get -y upgrade
|
||
sudo apt-get -y dist-upgrade
|
||
|
||
|
||
#######################
|
||
## Get current VM IP ##
|
||
#######################
|
||
elif [ "$1" = "ip" ]; then
|
||
check_yunohost_vm
|
||
# Print IP
|
||
ip=$(/bin/ip a | grep 'inet 192.168' | awk -F " " '{print $2}' | awk -F "/" '{print $1}')
|
||
echo "IP: $ip"
|
||
|
||
|
||
###########################################
|
||
## Use Git version for YunoHost packages ##
|
||
###########################################
|
||
elif [ "$1" = "use-git" ]; then
|
||
check_yunohost_vm
|
||
VERSION=$2
|
||
|
||
for i in ${!packages[@]}; do
|
||
case ${packages[i]} in
|
||
ssowat)
|
||
echo "Using Git repository for SSOwat"
|
||
create_sym_link "/vagrant/ssowat" "/usr/share/ssowat"
|
||
echo "↳ Don't forget to do 'sudo yunohost app ssowatconf' when hacking SSOwat"
|
||
echo ""
|
||
;;
|
||
moulinette)
|
||
create_sym_link "/vagrant/moulinette/locales" "/usr/share/moulinette/locales"
|
||
create_sym_link "/vagrant/moulinette/moulinette/authenticators" "/usr/lib/python2.7/dist-packages/moulinette/authenticators"
|
||
create_sym_link "/vagrant/moulinette/moulinette/interfaces" "/usr/lib/python2.7/dist-packages/moulinette/interfaces"
|
||
create_sym_link "/vagrant/moulinette/moulinette/utils" "/usr/lib/python2.7/dist-packages/moulinette/utils"
|
||
create_sym_link "/vagrant/moulinette/moulinette/__init__.py" "/usr/lib/python2.7/dist-packages/moulinette/__init__.py"
|
||
create_sym_link "/vagrant/moulinette/moulinette/core.py" "/usr/lib/python2.7/dist-packages/moulinette/core.py"
|
||
create_sym_link "/vagrant/moulinette/moulinette/actionsmap.py" "/usr/lib/python2.7/dist-packages/moulinette/actionsmap.py"
|
||
create_sym_link "/vagrant/moulinette/moulinette/cache.py" "/usr/lib/python2.7/dist-packages/moulinette/cache.py"
|
||
create_sym_link "/vagrant/moulinette/moulinette/globals.py" "/usr/lib/python2.7/dist-packages/moulinette/globals.py"
|
||
echo "↳ If you add files at the root of this directory /vagrant/moulinette/moulinette/ you should adapt ynh-dev"
|
||
echo ""
|
||
;;
|
||
yunohost)
|
||
echo "Using Git repository for yunohost"
|
||
|
||
# bin
|
||
create_sym_link "/vagrant/yunohost/bin/yunohost" "/usr/bin/yunohost"
|
||
create_sym_link "/vagrant/yunohost/bin/yunohost-api" "/usr/bin/yunohost-api"
|
||
|
||
# data
|
||
create_sym_link "/vagrant/yunohost/data/bash-completion.d/yunohost" "/etc/bash_completion.d/yunohost"
|
||
create_sym_link "/vagrant/yunohost/data/actionsmap/yunohost.yml" "/usr/share/moulinette/actionsmap/yunohost.yml"
|
||
create_sym_link "/vagrant/yunohost/data/hooks" "/usr/share/yunohost/hooks"
|
||
create_sym_link "/vagrant/yunohost/data/templates" "/usr/share/yunohost/templates"
|
||
create_sym_link "/vagrant/yunohost/data/helpers" "/usr/share/yunohost/helpers"
|
||
create_sym_link "/vagrant/yunohost/data/helpers.d" "/usr/share/yunohost/helpers.d"
|
||
create_sym_link "/vagrant/yunohost/data/other" "/usr/share/yunohost/yunohost-config/moulinette"
|
||
|
||
# debian
|
||
create_sym_link "/vagrant/yunohost/debian/conf/pam/mkhomedir" "/usr/share/pam-configs/mkhomedir"
|
||
|
||
# lib
|
||
create_sym_link "/vagrant/yunohost/lib/metronome/modules/ldap.lib.lua" "/usr/lib/metronome/modules/ldap.lib.lua"
|
||
create_sym_link "/vagrant/yunohost/lib/metronome/modules/mod_auth_ldap2.lua" "/usr/lib/metronome/modules/mod_auth_ldap2.lua"
|
||
create_sym_link "/vagrant/yunohost/lib/metronome/modules/mod_legacyauth.lua" "/usr/lib/metronome/modules/mod_legacyauth.lua"
|
||
create_sym_link "/vagrant/yunohost/lib/metronome/modules/mod_storage_ldap.lua" "/usr/lib/metronome/modules/mod_storage_ldap.lua"
|
||
create_sym_link "/vagrant/yunohost/lib/metronome/modules/vcard.lib.lua" "/usr/lib/metronome/modules/vcard.lib.lua"
|
||
|
||
# src
|
||
create_sym_link "/vagrant/yunohost/src/yunohost" "/usr/lib/moulinette/yunohost"
|
||
|
||
# locales
|
||
create_sym_link "/vagrant/yunohost/locales" "/usr/lib/moulinette/yunohost/locales"
|
||
|
||
echo ""
|
||
;;
|
||
yunohost-admin)
|
||
|
||
# Trick to check vagrant user exists (for install on VPS..)
|
||
getent passwd vagrant > /dev/null
|
||
if [ $? -eq 2 ]; then
|
||
useradd vagrant
|
||
chown -R vagrant: /vagrant/yunohost-admin
|
||
fi
|
||
|
||
# Install npm dependencies if needed
|
||
which gulp > /dev/null
|
||
if [ $? -eq 1 ]
|
||
then
|
||
sudo apt-get update --fix-missing
|
||
sudo apt-get -y install nodejs-legacy npm
|
||
cd /vagrant/yunohost-admin/src
|
||
sudo npm install
|
||
sudo npm install -g bower
|
||
sudo npm install -g gulp
|
||
fi
|
||
cd /vagrant/yunohost-admin/src
|
||
sudo su -c "bower install" vagrant
|
||
sudo su -c "gulp build --dev" vagrant
|
||
|
||
echo "Using Git repository for yunohost-admin"
|
||
create_sym_link "/vagrant/yunohost-admin/src" "/usr/share/yunohost/admin"
|
||
|
||
echo "--------------------------------------------------------"
|
||
echo "Launching gulp ... "
|
||
echo "NB : This command will keep running and watch for changes"
|
||
echo " in the folder /vagrant/yunohost-admin/src, such that you"
|
||
echo "don't need to re-run npm yourself everytime you change"
|
||
echo "something !"
|
||
echo "--------------------------------------------------------"
|
||
sudo su -c "gulp watch --dev" vagrant
|
||
|
||
;;
|
||
esac
|
||
done
|
||
|
||
|
||
elif [ "$1" = "test" ]; then
|
||
check_yunohost_vm
|
||
VERSION=$2
|
||
|
||
for i in ${!packages[@]}; do
|
||
case ${packages[i]} in
|
||
yunohost)
|
||
# Pytest and tests dependencies
|
||
if ! type "pytest" > /dev/null; then
|
||
echo "======================="
|
||
echo "> Installing pytest ..."
|
||
echo "======================="
|
||
apt-get install python-pip
|
||
pip2 install pytest
|
||
fi
|
||
PIP_DEPENDENCIES="pytest-mock requests-mock mock"
|
||
for DEP in $PIP_DEPENDENCIES
|
||
do
|
||
if [ -z "$(pip show $DEP)" ]; then
|
||
echo "======================="
|
||
echo "Installing $DEP with pip"
|
||
echo "======================="
|
||
pip2 install $DEP
|
||
fi
|
||
done
|
||
|
||
# Apps for test
|
||
cd /vagrant/yunohost/src/yunohost/tests
|
||
if [ ! -d "apps" ]; then
|
||
git clone https://github.com/YunoHost/test_apps ./apps
|
||
else
|
||
cd apps
|
||
git pull > /dev/null 2>&1
|
||
fi
|
||
|
||
# Run tests
|
||
echo "Running tests for YunoHost"
|
||
cd /vagrant/yunohost/
|
||
py.test tests
|
||
cd /vagrant/yunohost/src/yunohost
|
||
py.test tests
|
||
;;
|
||
esac
|
||
done
|
||
|
||
|
||
elif [ "$1" = "self-update" ]; then
|
||
check_yunohost_vm
|
||
cd /vagrant/vagrant && git pull origin master
|
||
cd /vagrant/ynh-dev-tools && git pull origin master && cp ynh-dev ../ynh-dev
|
||
|
||
# Fallback to print usage
|
||
else
|
||
usage
|
||
exit 101
|
||
fi
|
||
|
||
exit 0;
|