/
guest.go
121 lines (107 loc) · 3.68 KB
/
guest.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
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// 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
//
// http://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 guest
import (
"context"
"fmt"
"strings"
"golang.org/x/sync/errgroup"
"github.com/labring/sealos/fork/golang/expansion"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/env"
"github.com/labring/sealos/pkg/exec"
"github.com/labring/sealos/pkg/ssh"
v2 "github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/maps"
stringsutil "github.com/labring/sealos/pkg/utils/strings"
)
type Interface interface {
Apply(cluster *v2.Cluster, mounts []v2.MountImage, targetHosts []string) error
Delete(cluster *v2.Cluster) error
}
type Default struct{}
func NewGuestManager() (Interface, error) {
return &Default{}, nil
}
func (d *Default) Apply(cluster *v2.Cluster, mounts []v2.MountImage, targetHosts []string) error {
envGetter := env.NewEnvProcessor(cluster)
sshClient := ssh.NewCacheClientFromCluster(cluster, true)
execer, err := exec.New(sshClient)
if err != nil {
return err
}
for i, m := range mounts {
switch {
case m.IsRootFs(), m.IsPatch():
eg, ctx := errgroup.WithContext(context.Background())
for j := range targetHosts {
node := targetHosts[j]
envs := maps.Merge(m.Env, envGetter.Getenv(node))
cmds := formalizeImageCommands(cluster, i, m, envs)
eg.Go(func() error {
return execer.CmdAsyncWithContext(ctx, node,
stringsutil.RenderShellWithEnv(strings.Join(cmds, "; "), envs),
)
})
}
if err := eg.Wait(); err != nil {
return err
}
case m.IsApplication():
// on run on the first master
envs := maps.Merge(m.Env, envGetter.Getenv(cluster.GetMaster0IP()))
cmds := formalizeImageCommands(cluster, i, m, envs)
if err := execer.CmdAsync(cluster.GetMaster0IPAndPort(),
stringsutil.RenderShellWithEnv(strings.Join(cmds, "; "), envs),
); err != nil {
return err
}
}
}
return nil
}
func formalizeWorkingCommand(clusterName string, imageName string, t v2.ImageType, cmd string) string {
if cmd == "" {
return ""
}
switch t {
case v2.RootfsImage, v2.PatchImage:
return fmt.Sprintf(constants.CdAndExecCmd, constants.GetRootWorkDir(clusterName), cmd)
case v2.AppImage, "":
return fmt.Sprintf(constants.CdAndExecCmd, constants.GetAppWorkDir(clusterName, imageName), cmd)
}
return ""
}
func formalizeImageCommands(cluster *v2.Cluster, index int, m v2.MountImage, extraEnvs map[string]string) []string {
envs := maps.Merge(m.Env, extraEnvs)
envs = v2.MergeEnvWithBuiltinKeys(envs, m)
mapping := expansion.MappingFuncFor(envs)
cmds := make([]string, 0)
for i := range m.Entrypoint {
cmds = append(cmds, formalizeWorkingCommand(cluster.Name, m.Name, m.Type, expansion.Expand(m.Entrypoint[i], mapping)))
}
if index == 0 && len(cluster.Spec.Command) > 0 {
for i := range cluster.Spec.Command {
cmds = append(cmds, formalizeWorkingCommand(cluster.Name, m.Name, m.Type, expansion.Expand(cluster.Spec.Command[i], mapping)))
}
} else {
for i := range m.Cmd {
cmds = append(cmds, formalizeWorkingCommand(cluster.Name, m.Name, m.Type, expansion.Expand(m.Cmd[i], mapping)))
}
}
return cmds
}
func (d Default) Delete(_ *v2.Cluster) error {
panic("not yet implemented")
}