-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.go
104 lines (83 loc) · 2.65 KB
/
mod.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
package simnet
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"go.dedis.ch/simnet/sim"
"golang.org/x/xerrors"
)
var doCleaning bool
var doDeploy bool
var doExecute bool
var doStats bool
var vpnCommand string
var errMissingArgs = errors.New("expect at least one argument")
// Simulation is a Kubernetes simulation.
type Simulation struct {
strategy sim.Strategy
round sim.Round
out io.Writer
}
// NewSimulation creates a new simulation from the engine and the round.
func NewSimulation(r sim.Round, e sim.Strategy) *Simulation {
return &Simulation{
strategy: e,
round: r,
out: os.Stdout,
}
}
// Run uses the round interface to run the simulation.
func (s *Simulation) Run(args []string) error {
if len(args) == 0 {
return errMissingArgs
}
flagset := flag.NewFlagSet(args[0], flag.ContinueOnError)
flagset.BoolVar(&doCleaning, "do-clean", false, "override the usual flow to only wipe simulation resources")
flagset.BoolVar(&doDeploy, "do-deploy", false, "override the usual flow to only deploy simulation resources")
flagset.BoolVar(&doExecute, "do-execute", false, "override the usual flow to only run the simulation round")
flagset.BoolVar(&doStats, "do-stats", false, "override the usual flow to only write statistics")
flagset.StringVar(&vpnCommand, "vpn", defaultOpenVPNPath, "path to the OpenVPN executable")
flagset.Parse(args[1:])
doAll := !doCleaning && !doDeploy && !doExecute && !doStats
fmt.Fprintf(s.out, "Using strategy %v\n", s.strategy)
// Set global options to the strategy.
s.strategy.Option(sim.WithVPN(vpnCommand))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if doCleaning || doAll {
defer func() {
// Anything bad happening during the cleaning phase will be printed
// so an error of the simulation is returned if any.
fmt.Println("Starting cleaning...")
err := s.strategy.Clean(ctx)
if err != nil {
fmt.Fprintln(s.out, "An error occurred during cleaning: ", err)
fmt.Fprintln(s.out, "Please make sure to clean remaining components.")
}
}()
}
if doDeploy || doAll {
err := s.strategy.Deploy(ctx, s.round)
if err != nil {
return xerrors.Errorf("couldn't deploy experiment: %v", err)
}
}
if doExecute || doAll {
err := s.strategy.Execute(ctx, s.round)
if err != nil {
return xerrors.Errorf("couldn't execute experiment: %v", err)
}
}
if doStats || doExecute || doAll {
fmt.Fprintln(s.out, "Writing statistics")
// The user can request to only write the statistics to the file.
err := s.strategy.WriteStats(ctx, "result.json")
if err != nil {
return xerrors.Errorf("couldn't write statistics: %v", err)
}
}
return nil
}