Skip to content

Commit

Permalink
feat(sealos create): Add command preview functionality (#4291)
Browse files Browse the repository at this point in the history
* feat: add command preview functionality

* move formalizeWorkingCommand into guest pkg

* add license

* update: ImageType var
  • Loading branch information
LZiHaN authored Dec 23, 2023
1 parent 967c5bf commit 8abdf56
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 18 deletions.
27 changes: 27 additions & 0 deletions pkg/buildah/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/labring/sealos/pkg/guest"

"github.com/containers/buildah"

"github.com/labring/sealos/fork/golang/expansion"
v2 "github.com/labring/sealos/pkg/types/v1beta1"

stringsutil "github.com/labring/sealos/pkg/utils/strings"

Expand Down Expand Up @@ -86,6 +94,7 @@ func newCreateCmd() *cobra.Command {
}

if !opts.short {
printCommands(opts.name, opts.env, info)
logger.Info("Mount point: %s", info.MountPoint)
} else {
fmt.Println(info.MountPoint)
Expand Down Expand Up @@ -140,3 +149,21 @@ func runRender(mountPoints []string, env []string) error {

return eg.Wait()
}

func printCommands(name string, env []string, info buildah.BuilderInfo) {
envs := maps.Merge(maps.FromSlice(info.OCIv1.Config.Env), maps.FromSlice(env))
mapping := expansion.MappingFuncFor(envs)

typeKey := maps.GetFromKeys(info.OCIv1.Config.Labels, v2.ImageTypeKeys...)

cmds := make([]string, 0)
for i := range info.OCIv1.Config.Entrypoint {
cmds = append(cmds, guest.FormalizeWorkingCommand(name, info.Container, v2.ImageType(typeKey), expansion.Expand(info.OCIv1.Config.Entrypoint[i], mapping)))
}

for i := range info.OCIv1.Config.Cmd {
cmds = append(cmds, guest.FormalizeWorkingCommand(name, info.Container, v2.ImageType(typeKey), expansion.Expand(info.OCIv1.Config.Cmd[i], mapping)))
}

logger.Info("Shell command: %s", stringsutil.RenderShellWithEnv(strings.Join(cmds, "; "), envs))
}
21 changes: 3 additions & 18 deletions pkg/guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ 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"
Expand Down Expand Up @@ -81,35 +79,22 @@ func (d *Default) Apply(cluster *v2.Cluster, mounts []v2.MountImage, targetHosts
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)))
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)))
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)))
cmds = append(cmds, FormalizeWorkingCommand(cluster.Name, m.Name, m.Type, expansion.Expand(m.Cmd[i], mapping)))
}
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/guest/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2023 cuisongliu@qq.com.
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 (
"fmt"

"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/types/v1beta1"
)

func FormalizeWorkingCommand(clusterName string, imageName string, t v1beta1.ImageType, cmd string) string {
if cmd == "" {
return ""
}
switch t {
case v1beta1.RootfsImage, v1beta1.PatchImage:
return fmt.Sprintf(constants.CdAndExecCmd, constants.GetRootWorkDir(clusterName), cmd)
case v1beta1.AppImage, "":
return fmt.Sprintf(constants.CdAndExecCmd, constants.GetAppWorkDir(clusterName, imageName), cmd)
}
return ""
}
Loading

0 comments on commit 8abdf56

Please sign in to comment.