Files
sealos/pkg/bootstrap/context.go
T
fengxsong 95493cfa3e Sweep: run guest command one by one and support run patch-type image (#3677)
* refactor: run guest command one by one and support run patch-type image

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: ci lints and remove mounts parameter

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: test case

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: vip address rendering

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: test case

Signed-off-by: fengxsong <fengxsong@outlook.com>

* feature(main): add test case

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* fix: inject rootfs env into bootstrap process

Signed-off-by: fengxsong <fengxsong@outlook.com>

* feature(main): add join using guest process

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* fix: add targetHosts parameter as guest commands are not always applied to all hosts

Signed-off-by: fengxsong <fengxsong@outlook.com>

* feature(main): add patch image

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* refactor: add context parameter and pass it along side to the end

Signed-off-by: fengxsong <fengxsong@outlook.com>

* test(main): add test for patch image

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* test(main): add test for patch image

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* test(main): add test for patch image

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* test(main): add test for patch image

Signed-off-by: cuisongliu <cuisongliu@qq.com>

---------

Signed-off-by: fengxsong <fengxsong@outlook.com>
Signed-off-by: cuisongliu <cuisongliu@qq.com>
Co-authored-by: cuisongliu <cuisongliu@qq.com>
2023-08-17 21:23:04 +08:00

84 lines
2.3 KiB
Go

// Copyright © 2022 sealos.
//
// 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 bootstrap
import (
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/env"
"github.com/labring/sealos/pkg/remote"
"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 Context interface {
GetBash() constants.Bash
GetCluster() *v2.Cluster
GetData() constants.Data
GetExecer() ssh.Interface
GetRemoter() remote.Interface
}
type realContext struct {
bash constants.Bash
cluster *v2.Cluster
data constants.Data
execer ssh.Interface
remoter remote.Interface
}
func (ctx realContext) GetBash() constants.Bash {
return ctx.bash
}
func (ctx realContext) GetCluster() *v2.Cluster {
return ctx.cluster
}
func (ctx realContext) GetData() constants.Data {
return ctx.data
}
func (ctx realContext) GetExecer() ssh.Interface {
return ctx.execer
}
func (ctx realContext) GetRemoter() remote.Interface {
return ctx.remoter
}
func NewContextFrom(cluster *v2.Cluster) Context {
execer := ssh.NewSSHByCluster(cluster, true)
envProcessor := env.NewEnvProcessor(cluster)
remoter := remote.New(cluster.GetName(), execer)
rootfsImage := cluster.GetRootfsImage()
rootfsEnvs := v2.MergeEnvWithBuiltinKeys(rootfsImage.Env, *rootfsImage)
// bootstrap process depends on the envs in the rootfs image
shellWrapper := func(host, shell string) string {
envs := maps.MergeMap(rootfsEnvs, envProcessor.Getenv(host))
return stringsutil.RenderShellFromEnv(shell, envs)
}
return &realContext{
cluster: cluster,
execer: execer,
bash: constants.NewBash(cluster.GetName(), cluster.GetImageLabels(), shellWrapper),
data: constants.NewData(cluster.GetName()),
remoter: remoter,
}
}