-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (114 loc) · 4.25 KB
/
main.go
File metadata and controls
137 lines (114 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
slicer "github.com/slicervm/sdk"
)
const sampleInput = `hello from host
this file will be copied into the VM
then transformed to upper-case
`
func main() {
baseURL := envOrDefault("SLICER_URL", "http://192.168.1.34:8080")
token := os.Getenv("SLICER_TOKEN")
hostGroup := envOrDefault("SLICER_HOST_GROUP", "vm")
tag := envOrDefault("FILE_TRANSFER_TAG", fmt.Sprintf("example=transform-%d", time.Now().Unix()))
inputContent := envOrDefault("FILE_TRANSFER_INPUT", sampleInput)
outputName := envOrDefault("FILE_TRANSFER_OUTPUT", "processed.txt")
if token == "" {
fmt.Println("SLICER_TOKEN is required")
os.Exit(1)
}
client := slicer.NewSlicerClient(baseURL, token, "slicer-file-transfer/1.0", nil)
log.Printf("configured base_url=%s host_group=%s tag=%s output=%s", baseURL, hostGroup, tag, outputName)
createCtx, createCancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer createCancel()
createStart := time.Now()
log.Printf("creating VM wait=agent timeout=2m host_group=%s tag=%s cpus=2 ram_gb=2", hostGroup, tag)
node, err := client.CreateVMWithOptions(createCtx, hostGroup, slicer.SlicerCreateNodeRequest{
CPUs: 2,
RamBytes: slicer.GiB(2),
Tags: []string{tag},
}, slicer.SlicerCreateNodeOptions{
Wait: slicer.SlicerCreateNodeWaitAgent,
Timeout: 2 * time.Minute,
})
if err != nil {
fmt.Printf("create VM failed: %v\n", err)
os.Exit(1)
}
log.Printf("created ready VM hostname=%s ip=%s elapsed=%s", node.Hostname, node.IP, time.Since(createStart).Round(time.Millisecond))
fmt.Printf("created ready VM: hostname=%s ip=%s tag=%s\n", node.Hostname, node.IP, tag)
execCtx, execCancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer execCancel()
localInput := "input-" + node.Hostname + ".txt"
log.Printf("writing local input path=%s bytes=%d", localInput, len(inputContent))
if err := os.WriteFile(localInput, []byte(inputContent), 0o600); err != nil {
fmt.Printf("write local input failed: %v\n", err)
os.Exit(1)
}
log.Printf("copying input to VM hostname=%s", node.Hostname)
if err := client.CpToVM(execCtx, node.Hostname, localInput, "/home/ubuntu/input.txt", 1000, 1000, "600", "binary"); err != nil {
fmt.Printf("copy input to VM failed: %v\n", err)
os.Exit(1)
}
fmt.Printf("copied local file to VM: %s -> /home/ubuntu/input.txt\n", localInput)
log.Printf("running transform command hostname=%s", node.Hostname)
out, err := runFileTransform(execCtx, client, node.Hostname)
if err != nil {
fmt.Printf("transform command failed: %v\n", err)
if strings.TrimSpace(out) != "" {
fmt.Printf("transform output:\n%s\n", strings.TrimSpace(out))
}
os.Exit(1)
}
localOutput := "output-" + node.Hostname + ".txt"
log.Printf("copying output from VM hostname=%s path=%s", node.Hostname, localOutput)
if err := client.CpFromVM(execCtx, node.Hostname, "/home/ubuntu/output.txt", localOutput, "600", "binary"); err != nil {
fmt.Printf("copy output from VM failed: %v\n", err)
os.Exit(1)
}
result, err := os.ReadFile(localOutput)
if err != nil {
fmt.Printf("read local output failed: %v\n", err)
os.Exit(1)
}
fmt.Printf("transform output copied to: ./%s\n", localOutput)
fmt.Printf("content:\n%s", string(result))
if outputName != "" && outputName != localOutput {
if err := os.WriteFile(outputName, result, 0o600); err != nil {
fmt.Printf("rename output failed: %v\n", err)
os.Exit(1)
}
fmt.Printf("also written to: ./%s\n", outputName)
}
}
func runFileTransform(ctx context.Context, client *slicer.SlicerClient, nodeName string) (string, error) {
// Upper-case transform to demonstrate processing on the VM.
cmd := client.CommandContext(ctx, nodeName, "bash", "-lc",
"tr '[:lower:]' '[:upper:]' < /home/ubuntu/input.txt > /home/ubuntu/output.txt")
cmd.UID = 1000
cmd.GID = 1000
stdout, err := cmd.Output()
if err != nil {
if exitErr := new(slicer.ExitError); errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 {
return string(stdout) + string(exitErr.Stderr), err
}
return string(stdout), err
}
if len(strings.TrimSpace(string(stdout))) == 0 {
return "", nil
}
return string(stdout), nil
}
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}