@@ -7,25 +7,32 @@ import (
7
7
"strings"
8
8
"time"
9
9
10
+ "github.com/alessio/shellescape"
11
+
10
12
"terraform-provider-iterative/task/common"
11
13
)
12
14
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 := ""
15
22
for name , value := range variables .Enrich () {
16
23
escaped := strings .ReplaceAll (value , `"` , `\"` ) // FIXME: \" edge cases.
17
24
environment += fmt .Sprintf ("%s=\" %s\" \n " , name , escaped )
18
25
}
19
26
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 "
23
30
}
24
31
25
32
return fmt .Sprintf (
26
33
`#!/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
29
36
30
37
base64 --decode << END | sudo tee /usr/bin/tpi-task > /dev/null
31
38
%s
@@ -35,37 +42,44 @@ chmod u=rwx,g=rx,a=rx /usr/bin/tpi-task
35
42
sudo tee /usr/bin/tpi-task-shutdown << 'END'
36
43
#!/bin/bash
37
44
sleep 20; while pgrep rclone > /dev/null; do sleep 1; done
45
+ source /opt/task/credentials
38
46
if ! test -z "$CI"; then
39
47
cml rerun-workflow
40
48
fi
41
49
(systemctl is-system-running | grep stopping) || tpi --stop;
42
50
END
43
51
chmod u=rwx,g=rx,o=rx /usr/bin/tpi-task-shutdown
44
52
45
- base64 --decode << END | sudo tee /tmp/tpi-environment > /dev/null
53
+ base64 --decode << END | sudo tee /opt/task/variables > /dev/null
46
54
%s
47
55
END
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
49
61
50
62
while IFS= read -rd $'\0' variable; do
51
63
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 )
53
65
54
66
TPI_MACHINE_IDENTITY="$(uuidgen)"
55
67
TPI_LOG_DIRECTORY="$(mktemp --directory)"
56
- TPI_DATA_DIRECTORY="/tmp/tpi-task"
68
+ TPI_DATA_DIRECTORY="/opt/task/directory"
69
+
70
+ source /opt/task/credentials
57
71
58
72
sudo tee /etc/systemd/system/tpi-task.service > /dev/null <<END
59
73
[Unit]
60
74
After=default.target
61
75
[Service]
62
76
Type=simple
63
77
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"'
65
79
ExecStopPost=/usr/bin/tpi-task-shutdown
66
80
Environment=HOME=/root
67
- EnvironmentFile=/tmp/tpi-environment
68
- WorkingDirectory=/tmp/tpi- task
81
+ EnvironmentFile=/opt/task/variables
82
+ WorkingDirectory=/opt/ task/directory
69
83
TimeoutStartSec=%s
70
84
TimeoutStopSec=infinity
71
85
[Install]
@@ -100,7 +114,7 @@ if ! command -v rclone 2>&1 > /dev/null; then
100
114
rm --recursive rclone-*-linux-amd64*
101
115
fi
102
116
103
- rclone copy "$RCLONE_REMOTE/data" /tmp/tpi- task
117
+ rclone copy "$RCLONE_REMOTE/data" /opt/ task/directory
104
118
105
119
yes | /etc/profile.d/install-driver-prompt.sh # for GCP GPU machines
106
120
@@ -139,5 +153,6 @@ done &
139
153
` ,
140
154
base64 .StdEncoding .EncodeToString ([]byte (script )),
141
155
base64 .StdEncoding .EncodeToString ([]byte (environment )),
156
+ base64 .StdEncoding .EncodeToString ([]byte (exportCredentials )),
142
157
timeoutString )
143
158
}
0 commit comments