vade-cli 0.1.0

A command-line tool to deploy applications on Linux servers
######
# 1. # Tear down the original application
######

# Stop, disable, and delete the currently active deployment's systemd units if present
# (installed unit paths are derived them from their copies in the active deployment)
if [ -d "{{ vade.internal.app.paths.active_systemd_unit_copies }}" ]; then
  for unit_copy in "{{ vade.internal.app.paths.active_systemd_unit_copies }}"/*; do
    [ -e "$unit_copy" ] || continue
    unit_name="$(basename "$unit_copy")"
    installed_path="/etc/systemd/system/$unit_name"
    if [ -f "$installed_path" ]; then
      systemctl is-active --quiet "$unit_name" && systemctl stop "$unit_name"
      systemctl disable "$unit_name"
      rm "$installed_path"
    fi
  done

  systemctl daemon-reload
fi

# Downgrade or remove active deployment dir
{% if downgraded_dir is defined %}
if [ -d "{{ vade.internal.app.paths.deployment_active }}" ]; then
  mv "{{ vade.internal.app.paths.deployment_active }}" "{{ downgraded_dir }}"
fi
{% else %}
rm -rf "{{ vade.internal.app.paths.deployment_active }}"
{% endif %}

# Let Caddy know that any previously deployed Caddyfile for this app no longer exists
systemctl reload caddy

######
# 2. # Launch the application from the active deployment
######

# Promote source dir
mv -T "{{ src_dir }}" "{{ vade.internal.app.paths.deployment_active }}"

# Install, enable, and start systemd units
{%- for unit in vade.internal.app.paths.systemd_units %}
  if [ -f "{{ unit.active_path }}" ]; then
    cp "{{ unit.active_path }}" "{{ unit.installed_path }}"
    chmod 0644 "{{ unit.installed_path }}"
    chown root:root "{{ unit.installed_path }}"
    systemctl daemon-reload
    if [[ $(systemctl is-enabled "{{ unit.name }}") != "static" ]]; then
      systemctl enable --now "{{ unit.name }}"
    fi
  fi
{%- endfor %}

# Let Caddy know a new Caddyfile might exist
systemctl reload caddy