blob: 98627a53e1264bcf61ecd7354ae90b46779552b5 [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8 "os/exec"
9 "os/signal"
Filip Tehlar1a9dc752022-11-22 12:49:22 +010010 "reflect"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000011)
12
Filip Tehlar1a9dc752022-11-22 12:49:22 +010013var actions Actions
Filip Tehlar229f5fc2022-08-09 14:44:47 +000014
15func newVppContext() (context.Context, context.CancelFunc) {
16 ctx, cancel := signal.NotifyContext(
17 context.Background(),
18 os.Interrupt,
19 )
20 return ctx, cancel
21}
22
23func Vppcli(runDir, command string) (string, error) {
24 cmd := exec.Command("vppctl", "-s", fmt.Sprintf("%s/var/run/vpp/cli.sock", runDir), command)
25 o, err := cmd.CombinedOutput()
26 if err != nil {
27 fmt.Printf("failed to execute command: '%v'.\n", err)
28 }
29 fmt.Printf("Command output %s", string(o))
30 return string(o), err
31}
32
33func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
34 // If we already have an error, log it and exit
35 select {
36 case err := <-errCh:
37 fmt.Printf("%v", err)
38 default:
39 }
40 go func(ctx context.Context, errCh <-chan error) {
41 <-errCh
42 cancel()
43 }(ctx, errCh)
44}
45
46func writeSyncFile(res *ActionResult) error {
47 syncFile := "/tmp/sync/rc"
48
49 var jsonRes JsonResult
50
51 jsonRes.ErrOutput = res.ErrOutput
52 jsonRes.StdOutput = res.StdOutput
53 if res.Err != nil {
54 jsonRes.Code = 1
55 jsonRes.Desc = fmt.Sprintf("%s :%v", res.Desc, res.Err)
56 } else {
57 jsonRes.Code = 0
58 }
59
60 str, err := json.Marshal(jsonRes)
61 if err != nil {
62 return fmt.Errorf("error marshaling json result data! %v", err)
63 }
64
65 _, err = os.Open(syncFile)
66 if err != nil {
67 // expecting the file does not exist
68 f, e := os.Create(syncFile)
69 if e != nil {
70 return fmt.Errorf("failed to open sync file")
71 }
72 defer f.Close()
73 f.Write([]byte(str))
74 } else {
Maros Ondrejicka0db15752022-10-12 22:58:01 +020075 return fmt.Errorf("sync file exists, delete the file first")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000076 }
77 return nil
78}
79
80func NewActionResult(err error, opts ...ActionResultOptionFn) *ActionResult {
81 res := &ActionResult{
82 Err: err,
83 }
84 for _, o := range opts {
85 o(res)
86 }
87 return res
88}
89
90type ActionResultOptionFn func(res *ActionResult)
91
92func ActionResultWithDesc(s string) ActionResultOptionFn {
93 return func(res *ActionResult) {
94 res.Desc = s
95 }
96}
97
98func ActionResultWithStderr(s string) ActionResultOptionFn {
99 return func(res *ActionResult) {
100 res.ErrOutput = s
101 }
102}
103
104func ActionResultWithStdout(s string) ActionResultOptionFn {
105 return func(res *ActionResult) {
Maros Ondrejicka0db15752022-10-12 22:58:01 +0200106 res.StdOutput = s
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000107 }
108}
109
110func OkResult() *ActionResult {
111 return NewActionResult(nil)
112}
113
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000114func processArgs() *ActionResult {
Filip Tehlar1a9dc752022-11-22 12:49:22 +0100115 nArgs := len(os.Args) - 1 // skip program name
116 if nArgs < 1 {
117 return NewActionResult(fmt.Errorf("internal: no action specified!"))
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000118 }
Filip Tehlar1a9dc752022-11-22 12:49:22 +0100119 action := os.Args[1]
120 methodValue := reflect.ValueOf(&actions).MethodByName(action)
121 if !methodValue.IsValid() {
122 return NewActionResult(fmt.Errorf("internal unknown action %s!", action))
123 }
124 methodIface := methodValue.Interface()
125 fn := methodIface.(func([]string) *ActionResult)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000126 return fn(os.Args)
127}
128
129func main() {
130 if len(os.Args) == 0 {
131 fmt.Println("args required")
132 return
133 }
134
135 if os.Args[1] == "rm" {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100136 topology, err := LoadTopology(NetworkTopologyDir, os.Args[2])
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000137 if err != nil {
138 fmt.Printf("falied to load topologies: %v\n", err)
139 os.Exit(1)
140 }
141 topology.Unconfigure()
142 os.Exit(0)
143 }
144
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000145 var err error
146 res := processArgs()
147 err = writeSyncFile(res)
148 if err != nil {
149 fmt.Printf("failed to write to sync file: %v\n", err)
150 }
151}