diff --git a/ynh-dev b/ynh-dev index 4c51ad2..378efd2 100755 --- a/ynh-dev +++ b/ynh-dev @@ -1,51 +1,148 @@ #!/bin/bash -usage() { - cat < /dev/null || { @@ -152,14 +229,14 @@ elif [ "$1" = "run" ]; then for i in `seq 1 254`; do grep "${IP_BASE//./\.}$i" Vagrantfile &> /dev/null || { - IP="${IP_BASE}$i" + local IP="${IP_BASE}$i" break } done sed -i "/ ### END AUTOMATIC YNH-DEV ###/ i \\ config.vm.define \"${VMNAME}\" do |${VMNAME}| \ -\n ${VMNAME}.vm.box = \"yunohost/jessie-${VERSION}\" \ +\n ${VMNAME}.vm.box = \"${BOX_NAME}\" \ \n ${VMNAME}.vm.network :private_network, ip: \"${IP}\" \ \n end \ \n" ./Vagrantfile @@ -169,8 +246,8 @@ elif [ "$1" = "run" ]; then vagrant up $VERSION --provider virtualbox # Warn user about hosts file - IP_LINE="\s\s*${VMNAME}.vm.network\s\s*:private_network,\s*ip:\s*\"" - IP=$(grep "$IP_LINE" Vagrantfile | sed "s/${IP_LINE}//") + local IP_LINE="\s\s*${VMNAME}.vm.network\s\s*:private_network,\s*ip:\s*\"" + local IP=$(grep "$IP_LINE" Vagrantfile | sed "s/${IP_LINE}//") IP=${IP::-1} echo "/!\ Please add '$IP $DOMAIN' to your /etc/hosts file /!\\" echo "sudo sh -s 'echo \"$IP $DOMAIN\" >> /etc/hosts'" @@ -179,91 +256,107 @@ elif [ "$1" = "run" ]; then # Log into the VM vagrant ssh $VERSION +} -##################### -## Kill running VM ## -##################### -elif [ "$1" = "kill" ]; then +############################################################################### +# Kill running VM # +############################################################################### + +kill_vm() +{ vagrant destroy +} +############################################################################### +# Upgrade current VM # +############################################################################### -####################### -## Update current VM ## -####################### -elif [ "$1" = "upgrade" ]; then - check_yunohost_vm +upgrade_vm() +{ + assert_inside_yunohost_vm sudo apt-get update sudo apt-get -y upgrade sudo apt-get -y dist-upgrade +} +############################################################################### +# Get current VM IP # +############################################################################### -####################### -## Get current VM IP ## -####################### -elif [ "$1" = "ip" ]; then - check_yunohost_vm +show_vm_ip() +{ + assert_inside_yunohost_vm # Print IP - ip=$(/bin/ip a | grep 'inet 192.168' | awk -F " " '{print $2}' | awk -F "/" '{print $1}') + local 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 # +############################################################################### -########################################### -## Use Git version for YunoHost packages ## -########################################### -elif [ "$1" = "use-git" ]; then - check_yunohost_vm - VERSION=$2 +use_git() +{ + assert_inside_yunohost_vm + PACKAGES="$@" - for i in ${!packages[@]}; do - case ${packages[i]} in + is_not_empty_string "$PACKAGES" || PACKAGES=${KNOWN_PACKAGES} + + echo $PACKAGES + + local PACKAGE + for PACKAGE in $PACKAGES; do + echo $PACKAGE + assert_package_is_known $PACKAGE + case "$PACKAGE" in ssowat) echo "Using Git repository for SSOwat" - create_sym_link "/vagrant/ssowat" "/usr/share/ssowat" + create_sym_link "${BASE_INSIDE_VM}/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/locale" - create_sym_link "/vagrant/moulinette/moulinette/authenticators" "/usr/lib/python2.7/dist-packages/moulinette/authenticator" - 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" - echo "↳ If you add files at the root of this directory /vagrant/moulinette/moulinette/ you should adapt ynh-dev" + create_sym_link "${BASE_INSIDE_VM}/moulinette/locales" "/usr/share/moulinette/locale" + create_sym_link "${BASE_INSIDE_VM}/moulinette/moulinette/authenticators" "/usr/lib/python2.7/dist-packages/moulinette/authenticator" + create_sym_link "${BASE_INSIDE_VM}/moulinette/moulinette/interfaces" "/usr/lib/python2.7/dist-packages/moulinette/interfaces" + create_sym_link "${BASE_INSIDE_VM}/moulinette/moulinette/utils" "/usr/lib/python2.7/dist-packages/moulinette/utils" + create_sym_link "${BASE_INSIDE_VM}/moulinette/moulinette/__init__.py" "/usr/lib/python2.7/dist-packages/moulinette/__init__.py" + create_sym_link "${BASE_INSIDE_VM}/moulinette/moulinette/core.py" "/usr/lib/python2.7/dist-packages/moulinette/core.py" + create_sym_link "${BASE_INSIDE_VM}/moulinette/moulinette/actionsmap.py" "/usr/lib/python2.7/dist-packages/moulinette/actionsmap.py" + echo "↳ If you add files at the root of this directory ${BASE_INSIDE_VM}/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" + create_sym_link "${BASE_INSIDE_VM}/yunohost/bin/yunohost" "/usr/bin/yunohost" + create_sym_link "${BASE_INSIDE_VM}/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" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/bash-completion.d/yunohost" "/etc/bash_completion.d/yunohost" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/actionsmap/yunohost.yml" "/usr/share/moulinette/actionsmap/yunohost.yml" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/hooks" "/usr/share/yunohost/hooks" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/templates" "/usr/share/yunohost/templates" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/helpers" "/usr/share/yunohost/helpers" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/helpers.d" "/usr/share/yunohost/helpers.d" + create_sym_link "${BASE_INSIDE_VM}/yunohost/data/other" "/usr/share/yunohost/yunohost-config/moulinette" # debian - create_sym_link "/vagrant/yunohost/debian/conf/pam/mkhomedir" "/usr/share/pam-configs/mkhomedir" + create_sym_link "${BASE_INSIDE_VM}/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" + create_sym_link "${BASE_INSIDE_VM}/yunohost/lib/metronome/modules/ldap.lib.lua" "/usr/lib/metronome/modules/ldap.lib.lua" + create_sym_link "${BASE_INSIDE_VM}/yunohost/lib/metronome/modules/mod_auth_ldap2.lua" "/usr/lib/metronome/modules/mod_auth_ldap2.lua" + create_sym_link "${BASE_INSIDE_VM}/yunohost/lib/metronome/modules/mod_legacyauth.lua" "/usr/lib/metronome/modules/mod_legacyauth.lua" + create_sym_link "${BASE_INSIDE_VM}/yunohost/lib/metronome/modules/mod_storage_ldap.lua" "/usr/lib/metronome/modules/mod_storage_ldap.lua" + create_sym_link "${BASE_INSIDE_VM}/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" + create_sym_link "${BASE_INSIDE_VM}/yunohost/src/yunohost" "/usr/lib/moulinette/yunohost" # locales - create_sym_link "/vagrant/yunohost/locales" "/usr/lib/moulinette/yunohost/locales" + create_sym_link "${BASE_INSIDE_VM}/yunohost/locales" "/usr/lib/moulinette/yunohost/locales" echo "" ;; @@ -273,7 +366,7 @@ elif [ "$1" = "use-git" ]; then getent passwd vagrant > /dev/null if [ $? -eq 2 ]; then useradd vagrant - chown -R vagrant: /vagrant/yunohost-admin + chown -R vagrant: ${BASE_INSIDE_VM}/yunohost-admin fi # Install npm dependencies if needed @@ -282,22 +375,22 @@ elif [ "$1" = "use-git" ]; then then sudo apt-get update --fix-missing sudo apt-get -y install nodejs-legacy npm - cd /vagrant/yunohost-admin/src + cd ${BASE_INSIDE_VM}/yunohost-admin/src sudo npm install sudo npm install -g bower sudo npm install -g gulp fi - cd /vagrant/yunohost-admin/src + cd ${BASE_INSIDE_VM}/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" + create_sym_link "${BASE_INSIDE_VM}/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 " in the folder ${BASE_INSIDE_VM}/yunohost-admin/src, such that you" echo "don't need to re-run npm yourself everytime you change" echo "something !" echo "--------------------------------------------------------" @@ -306,63 +399,89 @@ elif [ "$1" = "use-git" ]; then ;; esac done +} +############################################################################### +# Run tests for a given package # +############################################################################### -elif [ "$1" = "test" ]; then - check_yunohost_vm - VERSION=$2 +readonly PIP_DEPENDENCIES_FOR_TESTS="pytest-mock requests-mock" - for i in ${!packages[@]}; do - case ${packages[i]} in +install_or_update_test_dependencies() +{ + # Pytest + if ! type "pytest" > /dev/null; then + echo "=======================" + echo "> Installing pytest ..." + echo "=======================" + apt-get install python-pip + pip2 install pytest + fi + for DEP in "$PIP_DEPENDENCIES_FOR_TESTS" + do + # Install this dependency if it's not already there + is_not_empty_string "$(pip show $DEP)" || { + echo "=======================" + echo "Installing $DEP with pip" + echo "=======================" + pip2 install $DEP + } + done + + # Apps for test + cd ${BASE_INSIDE_VM}/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() +{ + PACKAGES="$@" + + assert_inside_yunohost_vm + install_or_update_test_dependencies + is_not_empty_string "$PACKAGES" || PACKAGES=${KNOWN_PACKAGES} + + for PACKAGE in $PACKAGES; do + assert_package_is_known $PACKAGE + echo "Running tests for $PACKAGE" + case "$PACKAGE" 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" - 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/ + cd ${BASE_INSIDE_VM}/yunohost/ py.test tests - cd /vagrant/yunohost/src/yunohost + cd ${BASE_INSIDE_VM}/yunohost/src/yunohost py.test tests ;; esac done +} +############################################################################### +# Self-update # +############################################################################### -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 +self_update() +{ + assert_inside_yunohost_vm + cd ${BASE_INSIDE_VM}/vagrant && git pull origin master + cd ${BASE_INSIDE_VM}/ynh-dev-tools && git pull origin master && cp ynh-dev ../ynh-dev +} -# Fallback to print usage -else - usage - exit 101 -fi +############################################################################### +# Main # +############################################################################### -exit 0; +main() +{ + local ACTION="$1" + local ARGUMENTS="${@:2}" + + execute_action "$ACTION" "$ARGUMENTS" +} + +main "$@"