Skip to content

Commit

Permalink
vCore: create templates directly on the hyperviser (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
elenagerman authored Jul 22, 2024
1 parent 6cba716 commit 3993df4
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 101 deletions.
119 changes: 117 additions & 2 deletions tests/system-tests/internal/remote/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
ssh "github.com/povsister/scp"
)

// ScpFileFrom transfers specific file using scp method.
// ScpFileFrom transfers specific file using scp method from remote host.
func ScpFileFrom(source, destination, remoteHostname, remoteHostUsername, remoteHostPass string) error {
if source == "" {
glog.V(100).Info("The source is empty")
Expand Down Expand Up @@ -76,7 +76,65 @@ func ScpFileFrom(source, destination, remoteHostname, remoteHostUsername, remote
return nil
}

// ScpDirectoryFrom transfers specific directory using scp method.
// ScpFileTo transfers specific file using scp method to remote host.
func ScpFileTo(source, destination, remoteHostname, remoteHostUsername, remoteHostPass string) error {
if source == "" {
glog.V(100).Info("The source is empty")

return fmt.Errorf("the source could not be empty")
}

if destination == "" {
glog.V(100).Info("The destination is empty")

return fmt.Errorf("the destination could not be empty")
}

if remoteHostname == "" {
glog.V(100).Info("The remoteHostname is empty")

return fmt.Errorf("the remoteHostname could not be empty")
}

if remoteHostUsername == "" {
glog.V(100).Info("The remoteHostUsername is empty")

return fmt.Errorf("the remoteHostUsername could not be empty")
}

if remoteHostPass == "" {
glog.V(100).Info("The remoteHostPass is empty")

return fmt.Errorf("the remoteHostPass could not be empty")
}

glog.V(100).Infof("Verify file %s exists", destination)

if _, err := os.Stat(source); !os.IsExist(err) {
glog.V(100).Infof("File file %s not found", source)
}

glog.V(100).Infof("Copy file %s to the %s@%s", source, remoteHostname, destination)
glog.V(100).Info("Build a SSH config from username/password")

sshConf := ssh.NewSSHConfigFromPassword(remoteHostUsername, remoteHostPass)

glog.V(100).Infof("Dial SSH to %s", remoteHostname)

scpClient, err := ssh.NewClient(remoteHostname, sshConf, &ssh.ClientOption{})

if err != nil {
return err
}

glog.V(100).Infof("Transfer file %s to %s", source, destination)

err = scpClient.CopyFileToRemote(source, destination, &ssh.FileTransferOption{})

return err
}

// ScpDirectoryFrom transfers specific directory using scp method from destination.
func ScpDirectoryFrom(source, destination, remoteHostname, remoteHostUsername, remoteHostPass string) error {
if source == "" {
glog.V(100).Info("The source is empty")
Expand Down Expand Up @@ -139,3 +197,60 @@ func ScpDirectoryFrom(source, destination, remoteHostname, remoteHostUsername, r

return nil
}

// ScpDirectoryTo transfers specific directory using scp method to destination.
func ScpDirectoryTo(source, destination, remoteHostname, remoteHostUsername, remoteHostPass string) error {
if source == "" {
glog.V(100).Info("The source is empty")

return fmt.Errorf("the source could not be empty")
}

if destination == "" {
glog.V(100).Info("The destination is empty")

return fmt.Errorf("the destination could not be empty")
}

if remoteHostname == "" {
glog.V(100).Info("The remoteHostname is empty")

return fmt.Errorf("the remoteHostname could not be empty")
}

if remoteHostUsername == "" {
glog.V(100).Info("The remoteHostUsername is empty")

return fmt.Errorf("the remoteHostUsername could not be empty")
}

glog.V(100).Infof("Copy directory %s from %s", source, destination)

glog.V(100).Info("Build a SSH config from username/password")

sshConf := ssh.NewSSHConfigFromPassword(remoteHostUsername, remoteHostPass)

glog.V(100).Infof("Dial SSH to %s", remoteHostname)

scpClient, err := ssh.NewClient(remoteHostname, sshConf, &ssh.ClientOption{})

if err != nil {
return err
}

glog.V(100).Infof("recursively copy from directory %s to the %s", source, destination)

err = scpClient.CopyDirToRemote(source, destination, &ssh.DirTransferOption{})

if err != nil {
return err
}

glog.V(100).Infof("Insure directory %s was transferred", destination)

if _, err := os.Stat(destination); os.IsNotExist(err) {
return err
}

return nil
}
49 changes: 13 additions & 36 deletions tests/system-tests/internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,39 @@ package template
import (
"fmt"
"os"
"path/filepath"
"text/template"

"github.com/golang/glog"
)

// SaveTemplate read template file, replace variables and save it to the provided destination folder.
func SaveTemplate(
templateDir,
fileName,
destinationDir,
finalFileName string,
source,
destination string,
variablesToReplace map[string]interface{}) error {
if fileName == "" {
glog.V(100).Infof("The filename is empty")
if source == "" {
glog.V(100).Infof("The source is empty")

return fmt.Errorf("the filename should be provided")
return fmt.Errorf("the source should be provided")
}

if finalFileName == "" {
finalFileName = fileName
}

glog.V(100).Infof("Read %s template, replace variables and save it locally to the %s/%s",
fileName, destinationDir, finalFileName)
glog.V(100).Infof("Read %s template, replace variables and save it locally to the %s",
source, destination)

if templateDir == "" {
glog.V(100).Infof("The template folder is empty")
if destination == "" {
glog.V(100).Infof("The destination file is empty")

return fmt.Errorf("the template folder should be provided")
return fmt.Errorf("the destination file should be provided")
}

pathToTemplate := filepath.Join(templateDir, fileName)
destination := filepath.Join(destinationDir, finalFileName)
tmpl, err := template.ParseFiles(pathToTemplate)
tmpl, err := template.ParseFiles(source)

if err != nil {
glog.V(100).Infof("Error to read config file %s", pathToTemplate)

return err
}
glog.V(100).Infof("Error to read config file %s", source)

glog.V(100).Infof("create %s folder if not exists", destinationDir)

err = os.Mkdir(destinationDir, 0755)

if err != nil && !os.IsExist(err) {
return err
}

err = os.Remove(destination)

if err != nil && !os.IsExist(err) {
glog.V(100).Infof("%s file not found", destination)
}

// create a new file
file, err := os.Create(destination)
if err != nil {
Expand All @@ -81,7 +58,7 @@ func SaveTemplate(
err = os.Chmod(destination, 0755)

if err != nil {
glog.V(100).Infof("Error to chmod file %", destination)
glog.V(100).Infof("Error to chmod file %s", destination)

return err
}
Expand Down
36 changes: 20 additions & 16 deletions tests/system-tests/vcore/internal/ocpcli/ocpcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,24 @@ func DownloadAndExtractOcBinaryArchive(apiClient *clients.Settings) error {

// ApplyConfig applies config using shell method.
func ApplyConfig(
templateDir,
fileName,
destinationDir,
finalFileName string,
pathToTemplate,
pathToConfigFile string,
variablesToReplace map[string]interface{}) error {
err := template.SaveTemplate(
templateDir, fileName, destinationDir, finalFileName, variablesToReplace)
err := template.SaveTemplate(pathToTemplate, pathToConfigFile, variablesToReplace)

if err != nil {
return err
}

cfgFilePath := filepath.Join(destinationDir, finalFileName)
err = remote.ScpFileTo(pathToConfigFile, pathToConfigFile, VCoreConfig.Host, VCoreConfig.User, VCoreConfig.Pass)

if err != nil {
return fmt.Errorf("failed to transfer file %s to the %s/%s due to: %w",
pathToConfigFile, VCoreConfig.Host, pathToConfigFile, err)
}

applyCmd := fmt.Sprintf("oc apply -f %s --kubeconfig=%s",
cfgFilePath, VCoreConfig.KubeconfigPath)
pathToConfigFile, VCoreConfig.KubeconfigPath)
_, err = remote.ExecCmdOnHost(VCoreConfig.Host, VCoreConfig.User, VCoreConfig.Pass, applyCmd)

if err != nil {
Expand All @@ -107,21 +109,23 @@ func ApplyConfig(

// CreateConfig creates config using shell method.
func CreateConfig(
templateDir,
fileName,
destinationDir,
finalFileName string,
pathToTemplate,
pathToConfigFile string,
variablesToReplace map[string]interface{}) error {
err := template.SaveTemplate(
templateDir, fileName, destinationDir, finalFileName, variablesToReplace)
err := template.SaveTemplate(pathToTemplate, pathToConfigFile, variablesToReplace)

if err != nil {
return err
}

cfgFilePath := filepath.Join(destinationDir, finalFileName)
err = remote.ScpFileTo(pathToConfigFile, pathToConfigFile, VCoreConfig.Host, VCoreConfig.User, VCoreConfig.Pass)

if err != nil {
return fmt.Errorf("failed to transfer file %s to the %s/%s due to: %w",
pathToConfigFile, VCoreConfig.Host, pathToConfigFile, err)
}

createCmd := fmt.Sprintf("oc create -f %s --kubeconfig=%s", cfgFilePath, VCoreConfig.KubeconfigPath)
createCmd := fmt.Sprintf("oc create -f %s --kubeconfig=%s", pathToConfigFile, VCoreConfig.KubeconfigPath)
_, err = remote.ExecCmdOnHost(VCoreConfig.Host, VCoreConfig.User, VCoreConfig.Pass, createCmd)

if err != nil {
Expand Down
12 changes: 4 additions & 8 deletions tests/system-tests/vcore/internal/vcorecommon/keda-validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,8 @@ func VerifyScaleObjectDeployment(ctx SpecContext) {
templateDir := filepath.Join(workingDir, vcoreparams.TemplateFilesFolder)

err = ocpcli.CreateConfig(
templateDir,
kedaRoleBindingTemplateName,
vcoreparams.ConfigurationFolderPath,
kedaRoleBindingTemplateName,
filepath.Join(templateDir, kedaRoleBindingTemplateName),
filepath.Join(vcoreparams.ConfigurationFolderPath, kedaRoleBindingTemplateName),
varsToReplace)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("failed to create load job due to %v", err))

Expand Down Expand Up @@ -374,10 +372,8 @@ func VerifyScaleObjectDeployment(ctx SpecContext) {
varsToReplace["AbImageURL"] = abImageURL

err = ocpcli.CreateConfig(
templateDir,
appLoadJobTemplateName,
vcoreparams.ConfigurationFolderPath,
appLoadJobTemplateName,
filepath.Join(templateDir, appLoadJobTemplateName),
filepath.Join(vcoreparams.ConfigurationFolderPath, appLoadJobTemplateName),
varsToReplace)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("failed to create load job due to %v", err))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,8 @@ func VerifySCTPModuleActivation(ctx SpecContext) {
templateDir := filepath.Join(workingDir, vcoreparams.TemplateFilesFolder)

err = ocpcli.ApplyConfig(
templateDir,
sctpModuleTemplateName,
vcoreparams.ConfigurationFolderPath,
sctpModuleTemplateName,
filepath.Join(templateDir, sctpModuleTemplateName),
filepath.Join(vcoreparams.ConfigurationFolderPath, sctpModuleTemplateName),
varsToReplace)
Expect(err).To(BeNil(), fmt.Sprint(err))

Expand Down
Loading

0 comments on commit 3993df4

Please sign in to comment.