name: Cleans up diskspace

description: Cleans up diskspace if the root directory has used more than seventy percent of your diskspace.

inputs:
    diskspace-cutoff:
        description: The percent amount after which docker prune is run.
        required: true
        default: 70

runs:
  using: composite
  steps:
    - name: Cleans up diskspace
      shell: bash
      run: |
        set -ex
        diskspace_cutoff=${{ inputs.diskspace-cutoff }}
        docker_root_dir=$(docker info -f '{{.DockerRootDir}}')
        if [ ! -d "$docker_root_dir" ]; then
            echo "Docker root directory ($docker_root_dir) does not exist. Skipping disk space check."
            exit 0
        fi
        diskspace=$(df -H --output=pcent ${docker_root_dir} | sed -n 2p | sed 's/%//' | sed 's/ //')
        msg="Please file an issue on pytorch/pytorch reporting the faulty runner. Include a link to the runner logs so the runner can be identified"
        if [[ "$diskspace" -ge "$diskspace_cutoff" ]] ; then
            docker system prune -af
            diskspace_new=$(df -H --output=pcent ${docker_root_dir} | sed -n 2p | sed 's/%//' | sed 's/ //')
            if [[ "$diskspace_new" -gt "$diskspace_cutoff" ]] ; then
                echo "Error: Available diskspace is less than $diskspace_cutoff percent. Not enough diskspace."
                echo "$msg"
                exit 1
            else
                difference=$((diskspace - diskspace_new))
                echo "Diskspace saved: $difference percent"
            fi
        fi
