github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/builder/template_task.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package builder
    21  
    22  import (
    23  	"path/filepath"
    24  
    25  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/action"
    26  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
    27  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/util"
    28  
    29  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    30  	"github.com/1aal/kubeblocks/pkg/gotemplate"
    31  )
    32  
    33  type Template struct {
    34  	action.BaseAction
    35  	Template string
    36  	Dst      string
    37  	Values   gotemplate.TplValues
    38  }
    39  
    40  func (t *Template) Execute(runtime connector.Runtime) error {
    41  	templateStr, err := BuildFromTemplate(&t.Values, t.Template)
    42  	if err != nil {
    43  		return cfgcore.WrapError(err, "failed to render template %s", t.Template)
    44  	}
    45  
    46  	fileName := filepath.Join(runtime.GetHostWorkDir(), t.Template)
    47  	if err := util.WriteFile(fileName, []byte(templateStr)); err != nil {
    48  		return cfgcore.WrapError(err, "failed to write file %s", fileName)
    49  	}
    50  
    51  	if err := runtime.GetRunner().SudoScp(fileName, t.Dst); err != nil {
    52  		return cfgcore.WrapError(err, "failed to scp file %s to remote %s", fileName, t.Dst)
    53  	}
    54  	return nil
    55  }