mirror of
https://github.com/hanzozt/zt.git
synced 2026-11-03 05:01:58 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
/*
|
|
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 ztlab
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hanzozt/fablab/kernel/model"
|
|
"github.com/hanzozt/zt/zttest/ztlab/stagezt"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var _ model.ComponentType = (*ZitiEdgeTunnelType)(nil)
|
|
|
|
type ZitiEdgeTunnelMode int
|
|
|
|
const (
|
|
ZitiEdgeTunnelModeDefault ZitiEdgeTunnelMode = 0
|
|
ZitiEdgeTunnelModeHost ZitiEdgeTunnelMode = 1
|
|
)
|
|
|
|
type ZitiEdgeTunnelType struct {
|
|
Mode ZitiEdgeTunnelMode
|
|
Version string
|
|
ZitiVersion string
|
|
LocalPath string
|
|
LogConfig string
|
|
VerbosityLevel uint16
|
|
ConfigPathF func(c *model.Component) string
|
|
}
|
|
|
|
func (self *ZitiEdgeTunnelType) Label() string {
|
|
return "zt-edge-tunnel"
|
|
}
|
|
|
|
func (self *ZitiEdgeTunnelType) GetVersion() string {
|
|
return self.Version
|
|
}
|
|
|
|
func (self *ZitiEdgeTunnelType) SetVersion(version string) {
|
|
self.Version = 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": "zt-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 := "zt-edge-tunnel"
|
|
version := self.Version
|
|
if version != "" {
|
|
binaryName += "-" + version
|
|
}
|
|
return binaryName
|
|
}
|
|
|
|
func (self *ZitiEdgeTunnelType) StageFiles(r model.Run, c *model.Component) error {
|
|
if err := stagezt.StageZitiEdgeTunnelOnce(r, c, self.Version, self.LocalPath); err != nil {
|
|
return err
|
|
}
|
|
return stagezt.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)
|
|
|
|
env := "ZITI_TIME_FORMAT=utc"
|
|
if self.LogConfig != "" {
|
|
env += " ZITI_LOG=" + self.LogConfig
|
|
}
|
|
|
|
verbosity := ""
|
|
if self.VerbosityLevel > 0 {
|
|
verbosity = fmt.Sprintf("-v %v", self.VerbosityLevel)
|
|
}
|
|
|
|
var serviceCmd string
|
|
if self.Mode == ZitiEdgeTunnelModeDefault {
|
|
serviceCmd = fmt.Sprintf("%s sudo -E %s run -i %s %s > %s 2>&1 &", env, binaryPath, configPath, verbosity, logsPath)
|
|
} else if self.Mode == ZitiEdgeTunnelModeHost {
|
|
serviceCmd = fmt.Sprintf("%s %s run-host -i %s %s > %s 2>&1 &", env, binaryPath, configPath, verbosity, logsPath)
|
|
} else {
|
|
return fmt.Errorf("unsupported zt-edge-tunnel mode: %v", self.Mode)
|
|
}
|
|
|
|
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))
|
|
}
|