This commit is contained in:
Alexandre Aubin 2018-09-03 23:01:08 +02:00
parent 9b2dde7dd4
commit 7bff693e85

477
ynh-dev
View file

@ -1,383 +1,292 @@
#!/bin/bash #!/bin/bash
DEFAULT_VERSION="stretch-unstable" readonly THISSCRIPT=`basename $0`
DEFAULT_PROVIDER="lxc"
usage() { function show_usage() {
cat <<EOF cat <<EOF
Usage : Usage :
On the host On the host
`basename $0` create-env PATH ===========
Create a dev environment into PATH
`basename $0` run DOMAIN [VERSION] $THISSCRIPT start
Run a vagrant or virtualbox vm (Create and) starts the ynh-dev LXC
# `basename $0` kill
# Kill all vagrant $THISSCRIPT ssh
SSH into an already started ynh-dev LXC
$THISSCRIPT destroy
Destroy the ynh-dev LXC
$THISSCRIPT rebuild
Rebuild a fresh, up-to-date box
Inside the vm 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 : $THISSCRIPT ip
moulinette Give the ip of the guest container
ssowat $THISSCRIPT use-git [PACKAGES [PACKAGES ...]]
yunohost Use Git repositories from dev environment path
yunohost-admin $THISSCRIPT test [PACKAGES [PACKAGES ...]]
Deploy, update and run tests for some packages
VERSION
unstable
EOF EOF
} }
check_yunohost_vm() { function main()
if [ ! -d /etc/yunohost ] {
then local ACTION="$1"
echo "You need to install YunoHost first. Maybe you are not in a vm ?" local ARGUMENTS="${@:2}"
exit 100;
fi case "${ACTION}" in
help|-h|--help) show_usage $ARGUMENTS ;;
start|--start) start_ynhdev $ARGUMENTS ;;
ssh|--ssh) ssh_ynhdev $ARGUMENTS ;;
destroy|--destroy) destroy_ynhdev $ARGUMENTS ;;
rebuild|--rebuild) rebuild_ynhdev $ARGUMENTS ;;
ip|--ip) show_vm_ip $ARGUMENTS ;;
use-git|--use-git) use_git $ARGUMENTS ;;
test|--test) run_tests $ARGUMENTS ;;
*) critical "Unknown action ${ACTION}." ;;
esac
} }
create_sym_link() { ##################################################################
# Misc helpers #
##################################################################
readonly NORMAL=$(printf '\033[0m')
readonly BOLD=$(printf '\033[1m')
readonly faint=$(printf '\033[2m')
readonly UNDERLINE=$(printf '\033[4m')
readonly NEGATIVE=$(printf '\033[7m')
readonly RED=$(printf '\033[31m')
readonly GREEN=$(printf '\033[32m')
readonly ORANGE=$(printf '\033[33m')
readonly BLUE=$(printf '\033[34m')
readonly YELLOW=$(printf '\033[93m')
readonly WHITE=$(printf '\033[39m')
function success()
{
local msg=${1}
echo "[${BOLD}${GREEN} OK ${NORMAL}] ${msg}"
}
function info()
{
local msg=${1}
echo "[${BOLD}${BLUE}INFO${NORMAL}] ${msg}"
}
function warn()
{
local msg=${1}
echo "[${BOLD}${ORANGE}WARN${NORMAL}] ${msg}" 2>&1
}
function error()
{
local msg=${1}
echo "[${BOLD}${RED}FAIL${NORMAL}] ${msg}" 2>&1
}
function critical()
{
local msg=${1}
echo "[${BOLD}${RED}CRIT${NORMAL}] ${msg}" 2>&1
exit 1
}
function assert_inside_vm() {
[ -d /etc/yunohost ] || critical "There's no YunoHost in there. Are you sure that you are inside the LXC ?"
}
function create_sym_link() {
local DEST=$1
local LINK=$2
# Remove current sources if not a symlink # Remove current sources if not a symlink
if [ ! -L '$2' ]; then [ -L "$LINK" ] || sudo rm -rf $LINK
sudo rm -rf $2
fi
# Symlink from Git repository # Symlink from Git repository
sudo ln -sfn $1 $2 sudo ln -sfn $DEST $LINK
} }
packages=${@:2} ##################################################################
if [ "$#" = "1" ]; then # Actions #
packages=('moulinette' 'ssowat' 'yunohost' 'yunohost-admin') ##################################################################
fi
BASE_DIR=./ function start_ynhdev()
IP_BASE="192.168.33." {
BOX_NAME="yunohost/strech-unstable"
################## BOX_URL="https://build.yunohost.org/yunohost-$BOX_NAME-lxc.box"
## 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/container 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=$DEFAULT_VERSION
if [ "$#" = "3" ]; then
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
PROVIDER=$DEFAULT_PROVIDER
echo "Creating $DOMAIN virtual machine with YunoHost $VERSION version"
echo ""
BOX_NAME="yunohost/$VERSION"
BOX_URL="https://build.yunohost.org/yunohost-$BOX_NAME-$PROVIDER.box"
# 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" info "Vagrant box '$BOX_NAME' is missing. Trying to download it"
vagrant box add $BOX_NAME $BOX_URL --provider $PROVIDER vagrant box add $BOX_NAME $BOX_URL --provider $PROVIDER
echo ""
fi 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="$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 ZONE ###)|\
\"${VMNAME}\" => [\"$IP\", \"${VERSION}\"], \
\n \1|" ./Vagrantfile
}
# Run VM # Run VM
vagrant up $VMNAME --provider $PROVIDER vagrant up ynh-dev --provider lxc
# Warn user about hosts file
IP_LINE="[[:space:]]*\"${VMNAME}\"[[:space:]]*=>[[:space:]]*\[\"\K.*?(?=\", \"${VERSION}\"\],)"
IP=$(grep -Po "$IP_LINE" ./Vagrantfile )
echo "/!\ Please add '$IP_BASE$IP $DOMAIN' to your /etc/hosts file /!\\"
echo "sudo bash -c 'echo \"$IP_BASE$IP $DOMAIN\" >> /etc/hosts'"
echo ""
# Log into the VM # Log into the VM
vagrant ssh $VMNAME -c "sudo -i" vagrant ssh ynh-dev -c "sudo -i"
}
function ssh_ynhdev()
{
vagrant ssh ynh-dev -c "sudo -i"
}
##################### function destroy_ynhdev()
## Kill running VM ## {
##################### vagrant destroy ynh-dev
elif [ "$1" = "kill" ]; then }
vagrant destroy
function show_vm_ip()
####################### {
## Update current VM ## assert_inside_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 # Print IP
ip=$(/bin/ip a | grep 'inet 192.168' | awk -F " " '{print $2}' | awk -F "/" '{print $1}') ip=$(/bin/ip a | grep 'inet 192.168' | awk -F " " '{print $2}' | awk -F "/" '{print $1}')
echo "IP: $ip" echo "IP: $ip"
}
function use_git()
########################################### {
## Use Git version for YunoHost packages ## assert_inside_vm
########################################### local PACKAGES="$@"
elif [ "$1" = "use-git" ]; then for PACKAGE in "$PACKAGES";
check_yunohost_vm do
VERSION=$2 case $PACKAGE in
for i in ${!packages[@]}; do
case ${packages[i]} in
ssowat) ssowat)
echo "Using Git repository for SSOwat" create_sym_link "/ynh-dev/ssowat" "/usr/share/ssowat"
create_sym_link "/vagrant/ssowat" "/usr/share/ssowat" success "Now using Git repository for SSOwat"
echo "↳ Don't forget to do 'sudo yunohost app ssowatconf' when hacking SSOwat"
echo ""
;; ;;
moulinette) moulinette)
create_sym_link "/vagrant/moulinette/locales" "/usr/share/moulinette/locale" create_sym_link "/ynh-dev/moulinette/locales" "/usr/share/moulinette/locale"
create_sym_link "/vagrant/moulinette/moulinette" "/usr/lib/python2.7/dist-packages/moulinette" create_sym_link "/ynh-dev/moulinette/moulinette" "/usr/lib/python2.7/dist-packages/moulinette"
echo "" success "Now using Git repository for Moulinette"
;; ;;
yunohost) yunohost)
echo "Using Git repository for yunohost"
# bin # bin
create_sym_link "/vagrant/yunohost/bin/yunohost" "/usr/bin/yunohost" create_sym_link "/ynh-dev/yunohost/bin/yunohost" "/usr/bin/yunohost"
create_sym_link "/vagrant/yunohost/bin/yunohost-api" "/usr/bin/yunohost-api" create_sym_link "/ynh-dev/yunohost/bin/yunohost-api" "/usr/bin/yunohost-api"
# data # data
create_sym_link "/vagrant/yunohost/data/bash-completion.d/yunohost" "/etc/bash_completion.d/yunohost" create_sym_link "/ynh-dev/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 "/ynh-dev/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 "/ynh-dev/yunohost/data/hooks" "/usr/share/yunohost/hooks"
create_sym_link "/vagrant/yunohost/data/templates" "/usr/share/yunohost/templates" create_sym_link "/ynh-dev/yunohost/data/templates" "/usr/share/yunohost/templates"
create_sym_link "/vagrant/yunohost/data/helpers" "/usr/share/yunohost/helpers" create_sym_link "/ynh-dev/yunohost/data/helpers" "/usr/share/yunohost/helpers"
create_sym_link "/vagrant/yunohost/data/helpers.d" "/usr/share/yunohost/helpers.d" create_sym_link "/ynh-dev/yunohost/data/helpers.d" "/usr/share/yunohost/helpers.d"
create_sym_link "/vagrant/yunohost/data/other" "/usr/share/yunohost/yunohost-config/moulinette" create_sym_link "/ynh-dev/yunohost/data/other" "/usr/share/yunohost/yunohost-config/moulinette"
# debian # debian
create_sym_link "/vagrant/yunohost/debian/conf/pam/mkhomedir" "/usr/share/pam-configs/mkhomedir" create_sym_link "/ynh-dev/yunohost/debian/conf/pam/mkhomedir" "/usr/share/pam-configs/mkhomedir"
# lib # lib
create_sym_link "/vagrant/yunohost/lib/metronome/modules/ldap.lib.lua" "/usr/lib/metronome/modules/ldap.lib.lua" create_sym_link "/ynh-dev/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 "/ynh-dev/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 "/ynh-dev/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 "/ynh-dev/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" create_sym_link "/ynh-dev/yunohost/lib/metronome/modules/vcard.lib.lua" "/usr/lib/metronome/modules/vcard.lib.lua"
# src # src
create_sym_link "/vagrant/yunohost/src/yunohost" "/usr/lib/moulinette/yunohost" create_sym_link "/ynh-dev/yunohost/src/yunohost" "/usr/lib/moulinette/yunohost"
# locales # locales
create_sym_link "/vagrant/yunohost/locales" "/usr/lib/moulinette/yunohost/locales" create_sym_link "/ynh-dev/yunohost/locales" "/usr/lib/moulinette/yunohost/locales"
success "Now using Git repository for YunoHost"
echo ""
;; ;;
yunohost-admin) yunohost-admin)
# Trick to check vagrant user exists (for install on VPS..) getent passwd ynhdev > /dev/null
getent passwd vagrant > /dev/null
if [ $? -eq 2 ]; then if [ $? -eq 2 ]; then
useradd vagrant useradd ynhdev
chown -R vagrant: /vagrant/yunohost-admin chown -R ynhdev: /ynh-dev/yunohost-admin
fi fi
# Install npm dependencies if needed # Install npm dependencies if needed
which gulp > /dev/null which gulp > /dev/null
if [ $? -eq 1 ] if [ $? -eq 1 ]
then then
release=$(lsb_release -c | cut -d ":" -f 2 | sed 's/\s*//') info "Installing dependencies to develop in yunohost-admin ..."
if [ $release == "stretch" ] curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
then sudo apt install nodejs
# for watever reason npm is not in stretch anymore
# because why the fuck debian
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt install nodejs
else
sudo apt-get update --fix-missing
sudo apt-get -y install nodejs-legacy npm
fi
cd /vagrant/yunohost-admin/src cd /ynh-dev/yunohost-admin/src
sudo npm install sudo npm install
sudo npm install -g bower sudo npm install -g bower
sudo npm install -g gulp sudo npm install -g gulp
fi fi
cd /vagrant/yunohost-admin/src cd /ynh-dev/yunohost-admin/src
sudo su -c "bower install" vagrant sudo su -c "bower install" ynhdev
sudo su -c "gulp build --dev" vagrant sudo su -c "gulp build --dev" ynhdev
echo "Using Git repository for yunohost-admin" create_sym_link "/ynh-dev/yunohost-admin/src" "/usr/share/yunohost/admin"
create_sym_link "/vagrant/yunohost-admin/src" "/usr/share/yunohost/admin"
echo "--------------------------------------------------------" success "Now using Git repository for yunohost-admin"
echo "Launching gulp ... "
echo "NB : This command will keep running and watch for changes" warn "-------------------------------------------------------- "
echo " in the folder /vagrant/yunohost-admin/src, such that you" warn "Launching gulp ... "
echo "don't need to re-run npm yourself everytime you change" warn "NB : This command will keep running and watch for changes"
echo "something !" warn " in the folder /ynh-dev/yunohost-admin/src, such that you"
echo "--------------------------------------------------------" warn "don't need to re-run npm yourself everytime you change "
sudo su -c "gulp watch --dev" vagrant warn "something ! "
warn "-------------------------------------------------------- "
sudo su -c "gulp watch --dev" ynhdev
;; ;;
esac esac
done done
}
function run_tests()
elif [ "$1" = "test" ]; then {
check_yunohost_vm assert_inside_vm
VERSION=$2 local PACKAGES="$@"
for PACKAGE in "$PACKAGES";
for i in ${!packages[@]}; do do
case ${packages[i]} in case $PACKAGE in
yunohost) yunohost)
# Pytest and tests dependencies # Pytest and tests dependencies
if ! type "pytest" > /dev/null; then if ! type "pytest" > /dev/null; then
echo "=======================" info "> Installing pytest ..."
echo "> Installing pytest ..." apt-get install python-pip -y
echo "======================="
apt-get install python-pip
pip2 install pytest pip2 install pytest
fi fi
PIP_DEPENDENCIES="pytest-mock requests-mock mock" for DEP in "pytest-mock requests-mock mock"
for DEP in $PIP_DEPENDENCIES
do do
if [ -z "$(pip show $DEP)" ]; then if [ -z "$(pip show $DEP)" ]; then
echo "=======================" info "Installing $DEP with pip"
echo "Installing $DEP with pip"
echo "======================="
pip2 install $DEP pip2 install $DEP
fi fi
done done
# Apps for test # Apps for test
cd /vagrant/yunohost/src/yunohost/tests cd /ynh-dev/yunohost/src/yunohost/tests
if [ ! -d "apps" ]; then [ -d "apps" ] || git clone https://github.com/YunoHost/test_apps ./apps
git clone https://github.com/YunoHost/test_apps ./apps cd apps
else git pull > /dev/null 2>&1
cd apps
git pull > /dev/null 2>&1
fi
# Run tests # Run tests
echo "Running tests for YunoHost" echo "Running tests for YunoHost"
cd /vagrant/yunohost/ cd /ynh-dev/yunohost/
py.test tests py.test tests
cd /vagrant/yunohost/src/yunohost cd /ynh-dev/yunohost/src/yunohost
py.test tests py.test tests
;; ;;
esac esac
done done
}
main "$@"
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;