github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/build/buildinstruction/cmd.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package buildinstruction 16 17 import ( 18 "fmt" 19 20 "github.com/alibaba/sealer/utils/mount" 21 22 "github.com/moby/buildkit/frontend/dockerfile/shell" 23 24 "github.com/alibaba/sealer/utils" 25 26 "github.com/opencontainers/go-digest" 27 28 "github.com/alibaba/sealer/common" 29 "github.com/alibaba/sealer/logger" 30 "github.com/alibaba/sealer/pkg/command" 31 "github.com/alibaba/sealer/pkg/image/cache" 32 v1 "github.com/alibaba/sealer/types/api/v1" 33 ) 34 35 type CmdInstruction struct { 36 cmdValue string 37 rawLayer v1.Layer 38 mounter mount.Service 39 ex *shell.Lex 40 } 41 42 func (c CmdInstruction) Exec(execContext ExecContext) (out Out, err error) { 43 var ( 44 hitCache bool 45 chainID cache.ChainID 46 layerID digest.Digest 47 ) 48 defer func() { 49 out.ContinueCache = hitCache 50 out.ParentID = chainID 51 }() 52 53 if execContext.ContinueCache { 54 hitCache, layerID, chainID = tryCache(execContext.ParentID, c.rawLayer, execContext.CacheSvc, execContext.Prober, "") 55 if hitCache { 56 out.LayerID = layerID 57 return out, nil 58 } 59 } 60 61 err = c.mounter.TempMount() 62 if err != nil { 63 return out, err 64 } 65 defer c.mounter.CleanUp() 66 67 err = utils.SetRootfsBinToSystemEnv(c.mounter.GetMountTarget()) 68 if err != nil { 69 return out, fmt.Errorf("failed to set temp rootfs %s to system $PATH : %v", c.mounter.GetMountTarget(), err) 70 } 71 72 // if no variable at cmd value,nothing will change. 73 // if no build args is matched at cmd value,then the variable will be null. 74 cmdline, err := c.ex.ProcessWordWithMap(c.cmdValue, execContext.BuildArgs) 75 if err != nil { 76 return out, fmt.Errorf("failed to render build args: %v", err) 77 } 78 79 cmd := fmt.Sprintf(common.CdAndExecCmd, c.mounter.GetMountTarget(), cmdline) 80 output, err := command.NewSimpleCommand(cmd).Exec() 81 logger.Info(output) 82 83 if err != nil { 84 return out, fmt.Errorf("failed to exec %s, err: %v", cmd, err) 85 } 86 87 // cmd do not contain layer ,so no need to calculate layer 88 if c.rawLayer.Type == common.CMDCOMMAND { 89 return out, nil 90 } 91 92 out.LayerID, err = execContext.LayerStore.RegisterLayerForBuilder(c.mounter.GetMountUpper()) 93 return out, err 94 } 95 96 func NewCmdInstruction(ctx InstructionContext) (*CmdInstruction, error) { 97 lowerLayers := GetBaseLayersPath(ctx.BaseLayers) 98 mountService, err := mount.NewMountService("", "", lowerLayers) 99 if err != nil { 100 return nil, err 101 } 102 103 return &CmdInstruction{ 104 mounter: mountService, 105 cmdValue: ctx.CurrentLayer.Value, 106 rawLayer: *ctx.CurrentLayer, 107 ex: shell.NewLex('\\'), 108 }, nil 109 }