-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (136 loc) · 4.09 KB
/
main.go
File metadata and controls
154 lines (136 loc) · 4.09 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// nginx port-forward example for the Slicer Go SDK.
//
// Flow:
// 1. Create a VM with userdata that apt-installs nginx and starts it. Block
// server-side via wait=userdata so the daemon's long-poll hands back a
// ready-to-serve VM.
// 2. Open a host-side port-forward: 127.0.0.1:8080 → VM 127.0.0.1:80 via
// the slicervm/sdk/forward subpackage.
// 3. Fetch the welcome page from the host through the forward and assert
// the bytes look right.
// 4. Tear down the forward and delete the VM.
//
// Usage:
//
// SLICER_URL=~/slicer-mac/slicer.sock go run .
// SLICER_URL=https://slicer.example.com SLICER_TOKEN=... go run .
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
slicer "github.com/slicervm/sdk"
"github.com/slicervm/sdk/forward"
)
const userdata = `#!/bin/bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update -qy
apt-get install -qy nginx
systemctl enable --now nginx
`
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
totalStart := time.Now()
baseURL := resolveBaseURL()
token := os.Getenv("SLICER_TOKEN")
hostGroup := envOrDefault("SLICER_HOST_GROUP", "sbox")
if token == "" && !isUnixSocket(baseURL) {
return fmt.Errorf("SLICER_TOKEN is required when SLICER_URL is not a unix socket")
}
client := slicer.NewSlicerClient(baseURL, token, "slicer-nginx-example/1.0", nil)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
fmt.Printf("→ creating VM in %q with nginx userdata (blocking on wait=userdata)…\n", hostGroup)
createStart := time.Now()
node, err := client.CreateVMWithOptions(ctx, hostGroup,
slicer.SlicerCreateNodeRequest{
RamBytes: slicer.GiB(1),
CPUs: 1,
Userdata: userdata,
Tags: []string{"nginx-example"},
},
slicer.SlicerCreateNodeOptions{
Wait: slicer.SlicerCreateNodeWaitUserdata,
Timeout: 5 * time.Minute,
},
)
if err != nil {
return fmt.Errorf("create VM: %w", err)
}
fmt.Printf(" VM %s (%s) ready in %s\n", node.Hostname, node.IP, ms(time.Since(createStart)))
defer func() {
fmt.Printf("→ deleting VM %s…\n", node.Hostname)
if err := client.DeleteNode(hostGroup, node.Hostname); err != nil {
fmt.Fprintf(os.Stderr, " delete failed: %v\n", err)
}
}()
fmt.Println("→ opening forward 127.0.0.1:8080 → VM:80…")
fwd, err := forward.Start(ctx, forward.Options{
BaseURL: baseURL,
Token: token,
VMName: node.Hostname,
Specs: []string{"127.0.0.1:8080:127.0.0.1:80"},
})
if err != nil {
return fmt.Errorf("open forward: %w", err)
}
defer fwd.Close()
for _, ln := range fwd.Listeners() {
fmt.Printf(" %s → %s\n", ln.Local, ln.Remote)
}
fmt.Println("→ GET http://127.0.0.1:8080/")
res, err := http.Get("http://127.0.0.1:8080/")
if err != nil {
return fmt.Errorf("GET through forward: %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read response: %w", err)
}
fmt.Printf(" status=%d bytes=%d\n", res.StatusCode, len(body))
if res.StatusCode != http.StatusOK {
return fmt.Errorf("expected 200, got %d", res.StatusCode)
}
if !strings.Contains(string(body), "Welcome to nginx") {
return fmt.Errorf("response body did not contain \"Welcome to nginx\"")
}
fmt.Println(" ✓ welcome page served end-to-end via port-forward")
fmt.Printf("done in %s\n", ms(time.Since(totalStart)))
return nil
}
func resolveBaseURL() string {
baseURL := envOrDefault("SLICER_URL", "~/slicer-mac/slicer.sock")
if strings.HasPrefix(baseURL, "~/") {
if home, err := os.UserHomeDir(); err == nil {
baseURL = filepath.Join(home, baseURL[2:])
}
}
return baseURL
}
func isUnixSocket(s string) bool {
return strings.HasPrefix(s, "/") || strings.HasPrefix(s, "./") || strings.HasPrefix(s, "unix://")
}
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func ms(d time.Duration) string {
if d < time.Second {
return fmt.Sprintf("%dms", d.Milliseconds())
}
return fmt.Sprintf("%.1fs", d.Seconds())
}