-
Notifications
You must be signed in to change notification settings - Fork 129
/
component_common.go
132 lines (106 loc) · 3.92 KB
/
component_common.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
/*
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"
zitilib_actions "github.com/openziti/ziti/zititest/zitilab/actions"
"github.com/sirupsen/logrus"
"path/filepath"
"strings"
)
func getZitiProcessFilter(c *model.Component, zitiType string) func(string) bool {
return func(s string) bool {
matches := strings.Contains(s, "ziti") &&
strings.Contains(s, zitiType) &&
strings.Contains(s, fmt.Sprintf("--cli-agent-alias %s ", c.Id)) &&
!strings.Contains(s, "sudo ")
return matches
}
}
func startZitiComponent(c *model.Component, zitiType string, version string, configName string, extraArgs string) error {
user := c.GetHost().GetSshUser()
binaryPath := GetZitiBinaryPath(c, version)
configPath := fmt.Sprintf("/home/%s/fablab/cfg/%s", user, configName)
logsPath := fmt.Sprintf("/home/%s/logs/%s.log", user, c.Id)
useSudo := ""
if zitiType == "tunnel" || c.HasTag("tunneler") {
useSudo = "sudo"
}
serviceCmd := fmt.Sprintf("nohup %s %s %s run %s --cli-agent-alias %s --log-formatter json %s > %s 2>&1 &",
useSudo, binaryPath, zitiType, extraArgs, c.Id, configPath, logsPath)
if quiet, _ := c.GetBoolVariable("quiet_startup"); !quiet {
logrus.Info(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 canonicalizeGoAppVersion(version *string) {
if version != nil {
if *version != "" && *version != "latest" && !strings.HasPrefix(*version, "v") {
*version = "v" + *version
}
}
}
func GetZitiBinaryPath(c *model.Component, version string) string {
return getBinaryPath(c, "ziti", version)
}
func getBinaryPath(c *model.Component, binaryName string, version string) string {
if version != "" {
binaryName += "-" + version
}
user := c.GetHost().GetSshUser()
return fmt.Sprintf("/home/%s/fablab/bin/%s", user, binaryName)
}
func reEnrollIdentity(run model.Run, c *model.Component, zitiBinaryPath string, configPath string) error {
if err := zitilib_actions.EdgeExec(run.GetModel(), "delete", "authenticator", "where", fmt.Sprintf("identity=\"%v\"", c.Id)); err != nil {
return err
}
if err := zitilib_actions.EdgeExec(run.GetModel(), "delete", "enrollment", "where", fmt.Sprintf("identity=\"%v\"", c.Id)); err != nil {
return err
}
jwtFileName := filepath.Join(model.ConfigBuild(), c.Id+".jwt")
args := []string{"create", "enrollment", "ott", "--jwt-output-file", jwtFileName, "--", c.Id}
if err := zitilib_actions.EdgeExec(c.GetModel(), args...); err != nil {
return err
}
configDir := filepath.Dir(configPath)
remoteJwt := configDir + c.Id + ".jwt"
if err := c.GetHost().SendFile(jwtFileName, remoteJwt); err != nil {
return err
}
tmpl := "set -o pipefail; mkdir -p %s; %s edge enroll %s -o %s 2>&1 | tee /home/ubuntu/logs/%s.identity.enroll.log "
cmd := fmt.Sprintf(tmpl, configDir, zitiBinaryPath, remoteJwt, configPath, c.Id)
return c.GetHost().ExecLogOnlyOnError(cmd)
}
func setupDnsForTunneler(c *model.Component) error {
key := "ziti_tunnel.resolve_setup_done"
if _, found := c.Host.Data[key]; !found {
cmds := []string{
"sudo sed -i 's/#DNS=/DNS=127.0.0.1/g' /etc/systemd/resolved.conf",
"sudo systemctl restart systemd-resolved",
}
if err := c.Host.ExecLogOnlyOnError(cmds...); err != nil {
return err
}
c.Host.Data[key] = true
return nil
}
return nil
}