Skip to content
libre 2.92 KiB
Newer Older
#!/bin/bash -eu


function error_path {
  >&2 echo "Error: you must be in either /data/domains/*/ or /system/*/ to execute these commands"
  exit 1
}

function systemctl_param {
  first_level_path=`pwd | cut -d'/' -f2`
  second_level_path=`pwd | cut -d'/' -f3`

  if [ "$first_level_path" == "system" ]; then
    module=`pwd | cut -d'/' -f3`
    if [ -n "$module" ]; then
      echo s@$module
    else
      error_path
    fi
  elif [ "$first_level_path" == "data" ] && [ "$second_level_path" == "domains" ]; then
    domain=`pwd | cut -d'/' -f4`
    if [ -n "$domain" ]; then
      echo u@$domain
    else
      error_path
    fi
  else
    error_path
  fi
}

gust's avatar
gust committed
function show_usage {
  echo "Usage:"
Michel Memeteau's avatar
Michel Memeteau committed
  echo " - provision -a <app_repo_url> -u <domainname> -s : install and start a libre.sh service."
gust's avatar
gust committed
  echo " - start|status|enable|disable|restart|stop: command sent to systemctl."
  echo " - ps|exec|logs: command sent to docker compose."
  echo " - update: to update the current folder."
  echo " - stats: show docker stats with names."
  echo " - delete <domainname>: remove a libre.sh service."
Benzidane's avatar
Benzidane committed
  echo " - getsize <domainname>: give you the size of the installed application"
gust's avatar
gust committed
  exit 1
}

if [ $# -eq 0 ]; then
  show_usage
fi

case "$1" in
  start|status|enable|disable|restart|stop)
    if [ -n "$(systemctl_param)" ]; then
      echo "systemctl $1 $(systemctl_param)"
      systemctl $1 $(systemctl_param)
    fi;;
Pierre Ozoux's avatar
Pierre Ozoux committed
  journal)
    if [ -n "$(systemctl_param)" ]; then
      journalctl -fu $(systemctl_param)
    fi;;
  ps|exec|logs)
    if [ -f ./env ]; then
      env $(cat ./env | xargs) docker-compose $1 ${@:2}
    else
      docker-compose $1 ${@:2}
    fi;;
  update)
    if [ "$(pwd)" == "/libre.sh" ]; then
      git pull
Pierre Ozoux's avatar
Pierre Ozoux committed
      cp /libre.sh/unit-files/* /etc/systemd/system && systemctl daemon-reload
      cp /libre.sh/utils/* /opt/bin/
    elif [ -n "$(systemctl_param)" ]; then
      git pull
      docker-compose pull
      docker-compose build
Pierre Ozoux's avatar
Pierre Ozoux committed
      /opt/bin/libre restart
    fi;;
  provision)
    provision ${@:2};;
JꙨdцӍoηҬ's avatar
JꙨdцӍoηҬ committed
  stats)
    docker stats $(docker ps|grep -v "NAMES"|awk '{ print $NF }'|tr "\n" " ");;
Pierre Ozoux's avatar
Pierre Ozoux committed
  delete)
gust's avatar
gust committed
    if [ $# -ne 2 ]; then
      echo "delete requires a domainname argument."
      exit 1
    fi

    read -p "Are you sure you want to delete ${2}? (yN)" -n 1 -r
Pierre Ozoux's avatar
Pierre Ozoux committed
    echo    # (optional) move to a new line
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
      cd /data/domains/${2}
      libre stop
      libre disable
      cd /data/domains
      tar cvzf ${2}.tgz ./${2}
      if [ -f ./${2}.tgz ]; then
	if [ ! -d /data/trash/ ]; then
	  mkdir /data/trash/
gust's avatar
gust committed
	fi
Pierre Ozoux's avatar
Pierre Ozoux committed
        rm -rf /data/domains/${2}
        rm -rf /system/haproxy/certs/${2}
        rm /system/haproxy/haproxy/certs/${2}.pem
      fi
    fi;;
Benzidane's avatar
Benzidane committed
    # check the current size
    if [ $# -ne 2 ]; then
      echo "getsize requires a domainname argument."
      exit 1
    fi
    echo $(du -hs /data/domains/${2}) |cut -d ' ' -f 1;;
gust's avatar
gust committed
    show_usage