apiVersion: v1
kind: ConfigMap
metadata:
  name: nsenter-actions
  labels:
    app: nsenter
data:
  donothing: |
    #!/usr/bin/env bash
    set -x

    if [[ -f /opt/sentinel ]]; then
      if [[ ! -z "$1" ]]; then
        echo "received positional argument, forcing cleanup"
        rm /opt/sentinel || true
      else
        echo "Already ran, exiting"
        exit 0
      fi
    fi

    echo "Doing nothing, dropping sentinel, and rebooting once"
    touch /opt/sentinel
    reboot now
  clear_cache: |
    #!/usr/bin/env bash
    set -x

    if [[ -f /opt/cleared ]]; then
      if [[ ! -z "$1" ]]; then
        echo "received positional argument, forcing cleanup"
        rm /opt/cleared || true
      else
        echo "Already ran, exiting"
        exit 0
      fi
    fi

    echo "Checking free memory"
    free -h

    echo "Clearing caches"
    sync; echo 3 > /proc/sys/vm/drop_caches

    echo "Checking free memory after cache drop"
    free -h

    echo "Dropping sentinel file"
    touch /opt/cleared

    reboot now
  scheduler_mq_deadline: |
    #!/usr/bin/env bash
    set -ux

    echo "Checking current scheduler"
    if [[ -f /sys/block/sda/queue/scheduler ]]; then
      FOUND="$(cat /sys/block/sda/queue/scheduler | grep "\[mq-deadline\]")"
      if [[ -n "${FOUND}" ]]; then
        echo "Found scheduler already, will exit if permanently set"
      fi
      FOUND="$(cat /boot/grub/grub.cfg | grep "elevator=mq-deadline")"
      if [[ -n "${FOUND}" ]]; then
        echo "elevator permanently set, will exit early"
        exit 0
      fi
    fi
    
    echo "Setting io scheduler"
    echo "mq-deadline" > /sys/block/sda/queue/scheduler
  
    echo "Set scheduler, reading back for sanity"
    cat /sys/block/sda/queue/scheduler

    FOUND="$(cat /sys/block/sda/queue/scheduler | grep "\[mq-deadline\]")"
    if [[ -z "${FOUND}" ]]; then
      echo "Expected to find scheduler mq-deadline, but instead found: '$FOUND'"
    fi

    echo "Permanently setting scheduler"
    echo 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0 earlyprintk=ttyS0 elevator=mq-deadline"' > /etc/default/grub.d/60-aks-dropin.cfg
  
    echo "Updating grub"
    update-grub

    echo "Grub updated, logging new boot config"
    cat /boot/grub/grub.cfg

    FOUND="$(cat /boot/grub/grub.cfg | grep "elevator=mq-deadline")"
    if [[ -z "${FOUND}" ]]; then
      echo "Expected to find elevator mq-deadline, was not present"
    fi

    reboot now
  scheduler_noop: |
    #!/usr/bin/env bash
    set -ux

    echo "Checking current scheduler"
    if [[ -f /sys/block/sda/queue/scheduler ]]; then
      FOUND=$(cat /sys/block/sda/queue/scheduler | grep "\[none\]")
      if [[ -n "${FOUND}" ]]; then
        echo "Found none scheduler already, will exit if permanently set"
      fi
      FOUND="$(cat /boot/grub/grub.cfg | grep "elevator=noop")"
      if [[ -n "${FOUND}" ]]; then
        echo "elevator permanently set, will exit early"
        exit 0
      fi
    fi
    
    echo "Setting io scheduler"
    echo "none" > /sys/block/sda/queue/scheduler
  
    echo "Set scheduler, reading back for sanity"
    cat /sys/block/sda/queue/scheduler

    FOUND="$(cat /sys/block/sda/queue/scheduler | grep "\[none\]")"
    if [[ -z "${FOUND}" ]]; then
      echo "Expected to find scheduler none, but instead found: '$FOUND'"
    fi

    echo "Permanently setting scheduler"
    echo 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0 earlyprintk=ttyS0 elevator=noop"' > /etc/default/grub.d/60-aks-dropin.cfg
  
    echo "Updating grub"
    update-grub

    echo "Grub updated, logging new boot config"
    cat /boot/grub/grub.cfg

    FOUND="$(cat /boot/grub/grub.cfg | grep "elevator=noop")"
    if [[ -z "${FOUND}" ]]; then
      echo "Expected to find elevator noop, but was not present"
    fi

    reboot now
