[BE] Use `teardown-linux`/`chown` actions for binary builds (#84449)

Also embed `wait_for_ssh_to_drain.sh` into the action (to make it more reusable across repos) and delete unused teardown_linux template from `common.yml`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/84449
Approved by: https://github.com/kit1980
diff --git a/.github/actions/teardown-linux/action.yml b/.github/actions/teardown-linux/action.yml
index 9238a07..024bb3c 100644
--- a/.github/actions/teardown-linux/action.yml
+++ b/.github/actions/teardown-linux/action.yml
@@ -16,7 +16,18 @@
       # Always hold for active ssh sessions
       shell: bash
       if: inputs.skip-wait-ssh == ''
-      run: .github/scripts/wait_for_ssh_to_drain.sh
+      run: |
+        set -eou pipefail
+
+        echo "Holding runner for 2 hours until all ssh sessions have logged out"
+        for _ in $(seq 1440); do
+            # Break if no ssh session exists anymore
+            if [ "$(who)" = "" ]; then
+              break
+            fi
+            echo "."
+            sleep 5
+        done
 
     - name: Kill containers, clean up images
       shell: bash
diff --git a/.github/scripts/wait_for_ssh_to_drain.sh b/.github/scripts/wait_for_ssh_to_drain.sh
deleted file mode 100755
index f33d807..0000000
--- a/.github/scripts/wait_for_ssh_to_drain.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/usr/bin/env bash
-
-set -eou pipefail
-
-echo "Holding runner for 2 hours until all ssh sessions have logged out"
-for _ in $(seq 1440); do
-    # Break if no ssh session exists anymore
-    if [ "$(who)" = "" ]; then
-      break
-    fi
-    echo "."
-    sleep 5
-done
diff --git a/.github/templates/common.yml.j2 b/.github/templates/common.yml.j2
index b80b82f..37f89ef 100644
--- a/.github/templates/common.yml.j2
+++ b/.github/templates/common.yml.j2
@@ -199,29 +199,6 @@
           env | grep '^CI' >> "/tmp/github_env_${GITHUB_RUN_ID}"
 {%- endmacro -%}
 
-{%- macro teardown_ec2_linux(pytorch_directory="") -%}
-      - name: Hold runner for 2 hours or until ssh sessions have drained
-{%- if pytorch_directory %}
-        working-directory: !{{ pytorch_directory }}
-{%- endif %}
-        # Always hold for active ssh sessions
-        if: always()
-        run: .github/scripts/wait_for_ssh_to_drain.sh
-      - name: Chown workspace
-        if: always()
-        run: |
-          # Ensure the working directory gets chowned back to the current user
-          docker run --rm -v "$(pwd)":/v -w /v "${ALPINE_IMAGE}" chown -R "$(id -u):$(id -g)" .
-      - name: Kill containers, clean up images
-        if: always()
-        run: |
-          # ignore expansion of "docker ps -q" since it could be empty
-          # shellcheck disable=SC2046
-          docker stop $(docker ps -q) || true
-          # Prune all of the docker images
-          docker system prune -af
-{%- endmacro -%}
-
 {%- macro teardown_rocm_linux() -%}
       - name: Kill containers, clean up images
         if: always()
diff --git a/.github/workflows/_binary-build-linux.yml b/.github/workflows/_binary-build-linux.yml
index dc69e3a..8ddd4af 100644
--- a/.github/workflows/_binary-build-linux.yml
+++ b/.github/workflows/_binary-build-linux.yml
@@ -167,11 +167,9 @@
         run: |
           echo "BUILD_SPLIT_CUDA='ON'" >> "$GITHUB_ENV"
       - name: Pull Docker image
-        run: |
-          retry () {
-              "$@"  || (sleep 1 && "$@") || (sleep 2 && "$@")
-          }
-          retry docker pull "${DOCKER_IMAGE}"
+        uses: ./pytorch/.github/actions/pull-docker-image
+        with:
+          docker-image: ${{ inputs.DOCKER_IMAGE }}
       - name: Build PyTorch binary
         run: |
           set -x
@@ -204,10 +202,7 @@
           docker exec -t "${container_name}" bash -c "source ${BINARY_ENV_FILE} && bash /builder/${{ inputs.PACKAGE_TYPE }}/build.sh"
       - name: Chown artifacts
         if: always()
-        shell: bash
-        run: |
-          # Ensure the working directory gets chowned back to the current user
-          docker run --rm -v "${RUNNER_TEMP}/artifacts:/v" -w /v "${ALPINE_IMAGE}" chown -R "$(id -u):$(id -g)" .
+        uses: ./pytorch/.github/actions/chown-workspace
 
       - uses: actions/upload-artifact@v3
         with:
@@ -216,21 +211,10 @@
           path:
             ${{ runner.temp }}/artifacts/*
 
-      - name: Hold runner for 2 hours or until ssh sessions have drained
-        working-directory: pytorch/
-        # Always hold for active ssh sessions
+      - name: Teardown Linux
         if: always()
-        run: .github/scripts/wait_for_ssh_to_drain.sh
+        uses: ./pytorch/.github/actions/teardown-linux
+
       - name: Chown workspace
         if: always()
-        run: |
-          # Ensure the working directory gets chowned back to the current user
-          docker run --rm -v "$(pwd)":/v -w /v "${ALPINE_IMAGE}" chown -R "$(id -u):$(id -g)" .
-      - name: Kill containers, clean up images
-        if: always()
-        run: |
-          # ignore expansion of "docker ps -q" since it could be empty
-          # shellcheck disable=SC2046
-          docker stop $(docker ps -q) || true
-          # Prune all of the docker images
-          docker system prune -af
+        uses: ./pytorch/.github/actions/chown-workspace
diff --git a/.github/workflows/_binary-test-linux.yml b/.github/workflows/_binary-test-linux.yml
index e8749c5..c5e9fab 100644
--- a/.github/workflows/_binary-test-linux.yml
+++ b/.github/workflows/_binary-test-linux.yml
@@ -192,21 +192,10 @@
       - name: Test Pytorch binary
         uses: ./pytorch/.github/actions/test-pytorch-binary
 
-      - name: Hold runner for 2 hours or until ssh sessions have drained
-        working-directory: pytorch/
-        # Always hold for active ssh sessions
+      - name: Teardown Linux
         if: always()
-        run: .github/scripts/wait_for_ssh_to_drain.sh
+        uses: ./pytorch/.github/actions/teardown-linux
+
       - name: Chown workspace
         if: always()
-        run: |
-          # Ensure the working directory gets chowned back to the current user
-          docker run --rm -v "$(pwd)":/v -w /v "${ALPINE_IMAGE}" chown -R "$(id -u):$(id -g)" .
-      - name: Kill containers, clean up images
-        if: always()
-        run: |
-          # ignore expansion of "docker ps -q" since it could be empty
-          # shellcheck disable=SC2046
-          docker stop $(docker ps -q) || true
-          # Prune all of the docker images
-          docker system prune -af
+        uses: ./pytorch/.github/actions/chown-workspace