This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
/
run.go
179 lines (158 loc) · 5.35 KB
/
run.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/google/uuid"
"github.com/stealthrocket/timecraft/internal/chaos"
"github.com/stealthrocket/timecraft/internal/print/human"
"github.com/stealthrocket/timecraft/internal/timecraft"
"github.com/stealthrocket/timecraft/internal/timemachine"
"github.com/stealthrocket/wasi-go"
)
const runUsage = `
Usage: timecraft run [options] [--] <module> [args...]
Options:
-C, --chaotic ratio Enable artificial fault injection when running the module (raio is a decimal value between 0 and 1)
-c, --config path Path to the timecraft configuration file (overrides TIMECRAFTCONFIG)
-D, --dial addr Expose a socket connected to the specified address
--dir dir Expose a directory to the guest module
-e, --env name=value Pass an environment variable to the guest module
-f, --function function Exported function to call in the guest module (_start if empty)
--fly-blind Disable recording of the guest module execution
-h, --help Show this usage information
-L, --listen addr Expose a socket listening on the specified address
--restrict Do not automatically expose the environment and root directory to the guest module
-S, --sockets extension Enable a sockets extension, one of none, auto, path_open, wasmedgev1, wasmedgev2 (default to auto)
--record-batch-size size Number of records written per batch (default to 4096)
--record-compression type Compression to use when writing records, either snappy or zstd (default to zstd)
-T, --trace Enable strace-like logging of host function calls
`
func run(ctx context.Context, args []string) error {
var (
envs stringList
listens stringList
dials stringList
dirs stringList
chaotic = human.Ratio(0)
batchSize = human.Count(4096)
compression = compression("zstd")
sockets = sockets("auto")
flyBlind = false
restrict = false
trace = false
)
flagSet := newFlagSet("timecraft run", runUsage)
customVar(flagSet, &envs, "e", "env")
customVar(flagSet, &listens, "L", "listen")
customVar(flagSet, &dials, "D", "dial")
customVar(flagSet, &dirs, "dir")
customVar(flagSet, &sockets, "S", "sockets")
customVar(flagSet, &chaotic, "C", "chaotic")
boolVar(flagSet, &trace, "T", "trace")
boolVar(flagSet, &flyBlind, "fly-blind")
boolVar(flagSet, &restrict, "restrict")
customVar(flagSet, &batchSize, "record-batch-size")
customVar(flagSet, &compression, "record-compression")
if err := flagSet.Parse(args); err != nil {
return err
}
args = flagSet.Args()
if len(args) == 0 {
return errors.New(`missing "--" separator before the module path`)
}
var wasmPath string
wasmPath, args = args[0], args[1:]
if !restrict {
envs = append(os.Environ(), envs...)
dirs = append([]string{"/"}, dirs...)
}
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
config, err := timecraft.LoadConfig()
if err != nil {
return err
}
registry, err := timecraft.CreateRegistry(config)
if err != nil {
return err
}
runtime, err := timecraft.NewRuntime(ctx, config)
if err != nil {
return err
}
defer runtime.Close(ctx)
scheduler := &timecraft.TaskScheduler{}
defer scheduler.Close()
serverFactory := &timecraft.ServerFactory{
Scheduler: scheduler,
}
var adapter func(timecraft.ProcessID, wasi.System) wasi.System
if chaotic > 0 {
adapter = func(process timecraft.ProcessID, system wasi.System) wasi.System {
seed := int64(binary.LittleEndian.Uint64(process[8:]))
prng := rand.NewSource(seed)
chance := float64(chaotic) / 2
system = chaos.New(prng, system,
chaos.Chance(chance, chaos.Error(system)),
chaos.Chance(chance, chaos.Chunk(system)),
)
system = chaos.LowEntropy(system)
system = chaos.ClockDrift(system)
return system
}
}
processManager := timecraft.NewProcessManager(ctx, registry, runtime, serverFactory, adapter)
defer processManager.Close()
serverFactory.ProcessManager = processManager
scheduler.ProcessManager = processManager
moduleSpec := timecraft.ModuleSpec{
Path: wasmPath,
Args: args,
Env: envs,
Dirs: dirs,
Dials: dials,
Listens: listens,
Sockets: string(sockets),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
// Allow the main module to open listening sockets on the host network
// so server applications can receive connections as if they were
// running outside of timecraft.
HostNetworkBinding: true,
}
if trace {
moduleSpec.Trace = os.Stderr
}
var logSpec *timecraft.LogSpec
if !flyBlind {
logSpec = &timecraft.LogSpec{
ProcessID: uuid.New(),
StartTime: time.Now(),
BatchSize: int(batchSize),
}
switch compression {
case "snappy":
logSpec.Compression = timemachine.Snappy
case "zstd":
logSpec.Compression = timemachine.Zstd
case "none", "":
logSpec.Compression = timemachine.Uncompressed
default:
return fmt.Errorf("invalid compression type %q", compression)
}
fmt.Fprintf(os.Stderr, "%s\n", logSpec.ProcessID)
}
processID, err := processManager.Start(moduleSpec, logSpec, nil)
if err != nil {
return err
}
return processManager.Wait(processID)
}