-
Notifications
You must be signed in to change notification settings - Fork 125
/
component_ziti_edge_tunnel.go
143 lines (117 loc) · 4 KB
/
component_ziti_edge_tunnel.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
/*
Copyright 2019 NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package zitilab
import (
"fmt"
"github.com/openziti/fablab/kernel/model"
"github.com/openziti/ziti/zititest/zitilab/stageziti"
"github.com/sirupsen/logrus"
"strings"
)
var _ model.ComponentType = (*ZitiEdgeTunnelType)(nil)
type ZitiEdgeTunnelType struct {
Version string
ZitiVersion string
LocalPath string
LogConfig string
VerbosityLevel uint16
ConfigPathF func(c *model.Component) string
}
func (self *ZitiEdgeTunnelType) Label() string {
return "ziti-edge-tunnel"
}
func (self *ZitiEdgeTunnelType) GetVersion() string {
return self.Version
}
func (self *ZitiEdgeTunnelType) GetActions() map[string]model.ComponentAction {
return map[string]model.ComponentAction{
ZitiTunnelActionsReEnroll: model.ComponentActionF(self.ReEnroll),
}
}
func (self *ZitiEdgeTunnelType) Dump() any {
return map[string]string{
"type_id": "ziti-edge-tunnel",
"version": self.Version,
"local_path": self.LocalPath,
}
}
func (self *ZitiEdgeTunnelType) InitType(*model.Component) {
if strings.HasPrefix(self.Version, "v") {
self.Version = self.Version[1:]
}
canonicalizeGoAppVersion(&self.ZitiVersion)
}
func (self *ZitiEdgeTunnelType) getBinaryName() string {
binaryName := "ziti-edge-tunnel"
version := self.Version
if version != "" {
binaryName += "-" + version
}
return binaryName
}
func (self *ZitiEdgeTunnelType) StageFiles(r model.Run, c *model.Component) error {
if err := stageziti.StageZitiEdgeTunnelOnce(r, c, self.Version, self.LocalPath); err != nil {
return err
}
return stageziti.StageZitiOnce(r, c, self.ZitiVersion, self.LocalPath)
}
func (self *ZitiEdgeTunnelType) getProcessFilter(c *model.Component) func(string) bool {
return func(s string) bool {
return strings.Contains(s, self.getBinaryName()) &&
strings.Contains(s, fmt.Sprintf("%s.json", c.Id)) &&
!strings.Contains(s, "sudo ")
}
}
func (self *ZitiEdgeTunnelType) IsRunning(_ model.Run, c *model.Component) (bool, error) {
pids, err := c.GetHost().FindProcesses(self.getProcessFilter(c))
if err != nil {
return false, err
}
return len(pids) > 0, nil
}
func (self *ZitiEdgeTunnelType) GetConfigPath(c *model.Component) string {
if self.ConfigPathF != nil {
return self.ConfigPathF(c)
}
return fmt.Sprintf("/home/%s/fablab/cfg/%s.json", c.GetHost().GetSshUser(), c.Id)
}
func (self *ZitiEdgeTunnelType) Start(_ model.Run, c *model.Component) error {
user := c.GetHost().GetSshUser()
binaryPath := fmt.Sprintf("/home/%s/fablab/bin/%s", user, self.getBinaryName())
configPath := self.GetConfigPath(c)
logsPath := fmt.Sprintf("/home/%s/logs/%s.log", user, c.Id)
logging := ""
if self.LogConfig != "" {
logging = "ZITI_LOG=" + self.LogConfig + " "
}
verbosity := ""
if self.VerbosityLevel > 0 {
verbosity = fmt.Sprintf("-v %v", self.VerbosityLevel)
}
serviceCmd := fmt.Sprintf("ZITI_TIME_FORMAT=utc %ssudo -E %s run %s -i %s > %s 2>&1 &", logging, binaryPath, verbosity, configPath, logsPath)
logrus.Infof("starting: %s", serviceCmd)
value, err := c.GetHost().ExecLogged(serviceCmd)
if err != nil {
return err
}
if len(value) > 0 {
logrus.Infof("output [%s]", strings.Trim(value, " \t\r\n"))
}
return nil
}
func (self *ZitiEdgeTunnelType) Stop(_ model.Run, c *model.Component) error {
return c.GetHost().KillProcesses("-TERM", self.getProcessFilter(c))
}
func (self *ZitiEdgeTunnelType) ReEnroll(run model.Run, c *model.Component) error {
return reEnrollIdentity(run, c, GetZitiBinaryPath(c, self.ZitiVersion), self.GetConfigPath(c))
}