@@ -7,25 +7,32 @@ import (
77 "strings"
88 "time"
99
10+ "github.com/alessio/shellescape"
11+
1012 "terraform-provider-iterative/task/common"
1113)
1214
13- func Script (script string , variables common.Variables , timeout time.Duration ) string {
14- var environment string
15+ func Script (script string , credentials * map [string ]string , variables common.Variables , timeout time.Duration ) string {
16+ timeoutString := strconv .Itoa (int (timeout / time .Second ))
17+ if timeout <= 0 {
18+ timeoutString = "infinity"
19+ }
20+
21+ environment := ""
1522 for name , value := range variables .Enrich () {
1623 escaped := strings .ReplaceAll (value , `"` , `\"` ) // FIXME: \" edge cases.
1724 environment += fmt .Sprintf ("%s=\" %s\" \n " , name , escaped )
1825 }
1926
20- timeoutString := strconv . Itoa ( int ( timeout / time . Second ))
21- if timeout <= 0 {
22- timeoutString = "infinity "
27+ exportCredentials := ""
28+ for name , value := range * credentials {
29+ exportCredentials + = "export " + shellescape . Quote ( name + "=" + value ) + " \n "
2330 }
2431
2532 return fmt .Sprintf (
2633 `#!/bin/bash
27- sudo mkdir --parents /tmp/tpi- task
28- chmod u=rwx,g=rwx,o=rwx /tmp/tpi- task
34+ sudo mkdir --parents /opt/ task/directory
35+ chmod u=rwx,g=rwx,o=rwx /opt/ task/directory
2936
3037base64 --decode << END | sudo tee /usr/bin/tpi-task > /dev/null
3138%s
@@ -35,37 +42,44 @@ chmod u=rwx,g=rx,a=rx /usr/bin/tpi-task
3542sudo tee /usr/bin/tpi-task-shutdown << 'END'
3643#!/bin/bash
3744sleep 20; while pgrep rclone > /dev/null; do sleep 1; done
45+ source /opt/task/credentials
3846if ! test -z "$CI"; then
3947 cml rerun-workflow
4048fi
4149(systemctl is-system-running | grep stopping) || tpi --stop;
4250END
4351chmod u=rwx,g=rx,o=rx /usr/bin/tpi-task-shutdown
4452
45- base64 --decode << END | sudo tee /tmp/tpi-environment > /dev/null
53+ base64 --decode << END | sudo tee /opt/task/variables > /dev/null
4654%s
4755END
48- chmod u=rw,g=,o= /tmp/tpi-environment
56+ base64 --decode << END | sudo tee /opt/task/credentials > /dev/null
57+ %s
58+ END
59+ chmod u=rw,g=,o= /opt/task/variables
60+ chmod u=rw,g=,o= /opt/task/credentials
4961
5062while IFS= read -rd $'\0' variable; do
5163 export "$(perl -0777p -e 's/\\"/"/g;' -e 's/(.+?)="(.+)"/$1=$2/sg' <<< "$variable")"
52- done < <(perl -0777pe 's/\n*(.+?=".*?((?<!\\)"|\\\\"))\n*/$1\x00/sg' /tmp/tpi-environment )
64+ done < <(perl -0777pe 's/\n*(.+?=".*?((?<!\\)"|\\\\"))\n*/$1\x00/sg' /opt/task/variables )
5365
5466TPI_MACHINE_IDENTITY="$(uuidgen)"
5567TPI_LOG_DIRECTORY="$(mktemp --directory)"
56- TPI_DATA_DIRECTORY="/tmp/tpi-task"
68+ TPI_DATA_DIRECTORY="/opt/task/directory"
69+
70+ source /opt/task/credentials
5771
5872sudo tee /etc/systemd/system/tpi-task.service > /dev/null <<END
5973[Unit]
6074 After=default.target
6175[Service]
6276 Type=simple
6377 ExecStart=-/bin/bash -lc 'exec /usr/bin/tpi-task'
64- ExecStop=/bin/bash -c 'systemctl is-system-running | grep stopping || echo "{\\\\"result\\\\": \\\\"\$SERVICE_RESULT\\\\", \\\\"code\\\\": \\\\"\$EXIT_STATUS\\\\", \\\\"status\\\\": \\\\"\$EXIT_CODE\\\\"}" > "$TPI_LOG_DIRECTORY/status-$TPI_MACHINE_IDENTITY" && RCLONE_CONFIG= rclone copy "$TPI_LOG_DIRECTORY" "\$RCLONE_REMOTE/reports"'
78+ ExecStop=/bin/bash -c 'source /opt/task/credentials; systemctl is-system-running | grep stopping || echo "{\\\\"result\\\\": \\\\"\$SERVICE_RESULT\\\\", \\\\"code\\\\": \\\\"\$EXIT_STATUS\\\\", \\\\"status\\\\": \\\\"\$EXIT_CODE\\\\"}" > "$TPI_LOG_DIRECTORY/status-$TPI_MACHINE_IDENTITY" && RCLONE_CONFIG= rclone copy "$TPI_LOG_DIRECTORY" "\$RCLONE_REMOTE/reports"'
6579 ExecStopPost=/usr/bin/tpi-task-shutdown
6680 Environment=HOME=/root
67- EnvironmentFile=/tmp/tpi-environment
68- WorkingDirectory=/tmp/tpi- task
81+ EnvironmentFile=/opt/task/variables
82+ WorkingDirectory=/opt/ task/directory
6983 TimeoutStartSec=%s
7084 TimeoutStopSec=infinity
7185[Install]
@@ -100,7 +114,7 @@ if ! command -v rclone 2>&1 > /dev/null; then
100114 rm --recursive rclone-*-linux-amd64*
101115fi
102116
103- rclone copy "$RCLONE_REMOTE/data" /tmp/tpi- task
117+ rclone copy "$RCLONE_REMOTE/data" /opt/ task/directory
104118
105119yes | /etc/profile.d/install-driver-prompt.sh # for GCP GPU machines
106120
@@ -139,5 +153,6 @@ done &
139153` ,
140154 base64 .StdEncoding .EncodeToString ([]byte (script )),
141155 base64 .StdEncoding .EncodeToString ([]byte (environment )),
156+ base64 .StdEncoding .EncodeToString ([]byte (exportCredentials )),
142157 timeoutString )
143158}
0 commit comments