Welcome to kriss.run!


Back

deploy

#!/usr/bin/env bash

__prep_deploy() {
  # This is to prepare this server for deployment
  # - Installs the randomhost.service
  # - Removes .bash_history
  # - Removes .sudo_as_admin_successful

  # if [ -z "$SCRIPTS_DIR" ]; then
  #     echo "SCRIPTS_DIR is not set. Please run init.sh first."
  #     return 1
  # fi

  if [ -f "$SCRIPTS_DIR/.deploy" ]; then
      echo "This server is already marked as deployed."
      echo "To re-deploy, please run 'rm $SCRIPTS_DIR/.deploy', and re-run this command."
      return 1
  fi

  echo -n "This will prepare $HOSTNAME for deployment. Are you sure? [Y/n]"
  read -r response

  case "$response" in
    [Nn][Oo]|[Nn])
      echo "Deployment preparation aborted."
      return 1
      ;;
    *)
      echo "Proceeding with deployment preparation..."
      # Add your deployment preparation commands here

      sethostname

      if systemctl is-enabled --quiet randomhost; then
        echo "Disabling randomhost.service..."
        systemctl disable randomhost
      else
        echo "randomhost.service is not enabled."
      fi

      echo "Removing .bash_history..."
      rm -f "$HOME/.bash_history"

      rm -f "$SCRIPTS_DIR/.template"
      touch "$SCRIPTS_DIR/.deploy"

      # Remove and regenerate SSH keys
      rm /etc/ssh/ssh_host_*
      ssh-keygen -A

      # Remove machine-id
      echo -n > /etc/machine-id
      rm /var/lib/dbus/machine-id

      echo "$HOSTNAME has been deployed! Please reboot the server."
      ;;
  esac
}

# ─────────────────────────────────────────────────────────────────────── #
#                             __prep_template                             #
# ─────────────────────────────────────────────────────────────────────── #
__prep_template() {
  # This is to prepare this server for templating
  # - Disables the randomhost.service
  # - Sets hostname back to template-ubuntu

  # if [ -z "$SCRIPTS_DIR" ]; then
  #     echo "SCRIPTS_DIR is not set. Please run init.sh first."
  #     return 1
  # fi

  if [ -f "$SCRIPTS_DIR/.template" ]; then
      echo "This server is already marked as template."
      echo "To re-template, please run 'rm $SCRIPTS_DIR/.template', and re-run this command."
      return 1
  fi

  echo -n "This will prepare $HOSTNAME for templating. Are you sure? [Y/n]"
  read -r response

  case "$response" in
    [Nn][Oo]|[Nn])
      echo "Templating preparation aborted."
      exit 1
      ;;
    *)
      echo "Proceeding with templating preparation..."

      # Add your templating preparation commands here
      if ! systemctl is-enabled --quiet randomhost; then
        echo "Enabling randomhost.service..."
        systemctl enable randomhost
      else
        echo "randomhost.service is already enabled."
      fi

      # Set hostname back to template-ubuntu
      echo "Setting hostname to template-ubuntu..."
      hostnamectl set-hostname template-ubuntu

      touch "$SCRIPTS_DIR/.template"
      rm -f "$SCRIPTS_DIR/.deploy"

      # Remove and regenerate SSH keys
      rm /etc/ssh/ssh_host_*
      ssh-keygen -A

      # Remove machine-id
      echo -n > /etc/machine-id
      rm /var/lib/dbus/machine-id

      echo "$HOSTNAME is now ready as a template. Please shut down the server and convert it to a template."
    ;;
  esac
}


main() {
  if [ "$#" -ne 1 ]; then
    echo "Usage: $0 {deploy|template}"
    exit 1
  fi

  case "$1" in
    deploy)
      __prep_deploy
      ;;
    template)
      __prep_template
      ;;
    *)
      echo "Invalid argument: $1"
      echo "Usage: $0 {deploy|template}"
      exit 1
      ;;
  esac
}

main "$@"