diff --git a/cmd/pull.go b/cmd/pull.go index 44fe33b..434b391 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -64,7 +64,7 @@ This command will populate the config.yaml file with the podman and image Ids an } // OCP Settings localOCPDir := config.Openshift.OcpLocalConfigPath - err := collectcfg.FetchConfigFromEnv(configPath, localOCPDir, "", false, config.Openshift.Connection, "", "", filters) + err := collectcfg.FetchConfigFromEnv(configPath, localOCPDir, "", false, config.Openshift.Connection, "", "", filters, "") if err != nil { fmt.Println("Error while collecting config: ", err) return @@ -73,6 +73,7 @@ This command will populate the config.yaml file with the podman and image Ids an // TRIPLEO Settings: sshCmd := config.Tripleo.SshCmd fullCmd, directorHost, err := common.BuildFullSshCmd(sshCmd, config.Tripleo.DirectorHost) + collectcfg.Sudo = config.Tripleo.Sudo if err != nil { fmt.Println(err) return @@ -82,6 +83,8 @@ This command will populate the config.yaml file with the podman and image Ids an if !common.TestSshConnection(fullCmd) { fmt.Println("Please check your SSH configuration: " + fullCmd) return + } else { + fmt.Println("SSH connection successful !") } if update || updateOnly { collectcfg.SetTripleODataEnv(configPath, fullCmd, filters, true) @@ -89,7 +92,7 @@ This command will populate the config.yaml file with the podman and image Ids an return } } - err = collectcfg.FetchConfigFromEnv(configPath, localConfigDir, remoteConfigDir, true, config.Tripleo.Connection, sshCmd, directorHost, filters) + err = collectcfg.FetchConfigFromEnv(configPath, localConfigDir, remoteConfigDir, true, config.Tripleo.Connection, fullCmd, directorHost, filters, sshCmd) if err != nil { fmt.Println("Error while collecting config: ", err) return diff --git a/os-diff.cfg b/os-diff.cfg index f82221c..75eab9a 100644 --- a/os-diff.cfg +++ b/os-diff.cfg @@ -8,6 +8,7 @@ service_config_file=config.yaml ssh_cmd=ssh -F ssh.config director_host=standalone container_engine=podman +sudo=False connection=ssh remote_config_path=/tmp/tripleo local_config_path=/tmp/ diff --git a/pkg/collectcfg/fetch.go b/pkg/collectcfg/fetch.go index 207e9c5..5cd6840 100644 --- a/pkg/collectcfg/fetch.go +++ b/pkg/collectcfg/fetch.go @@ -31,6 +31,7 @@ import ( ) var config common.Config +var Sudo bool // TripleO information structures: type PodmanContainer struct { @@ -60,12 +61,13 @@ func PullConfigs(configDir string, tripleo bool, sshCmd string, undercloud strin filterMap[filter] = struct{}{} } for service := range config.Services { - if _, ok := filterMap[service]; ok || len(filters) == 0 { - if tripleo && (config.Services[service].PodmanName == "" || config.Services[service].PodmanId == "") { - fullCmd := sshCmd + " " + undercloud - PullConfig(service, tripleo, configDir, fullCmd) - } else { - PullConfigFromHosts(service, configDir, sshCmd, undercloud) + if config.Services[service].Enable { + if _, ok := filterMap[service]; ok || len(filters) == 0 { + if tripleo && (config.Services[service].PodmanName == "" || config.Services[service].PodmanId == "") { + PullConfig(service, tripleo, configDir, sshCmd) + } else { + PullConfigFromHosts(service, configDir, sshCmd, undercloud) + } } } } @@ -104,6 +106,9 @@ func PullConfig(serviceName string, tripleo bool, configDir string, sshCmd strin func GetPodmanIds(sshCmd string, all bool) ([]byte, error) { var cmd string + if Sudo { + sshCmd = sshCmd + " sudo " + } if all { cmd = sshCmd + " podman ps -a --format json" } else { @@ -118,30 +123,29 @@ func PullConfigFromHosts(service string, configDir string, sshCmd string, underc if len(config.Services[service].Hosts) != 0 { // if the services are not on the Undercloud/Director node for _, h := range config.Services[service].Hosts { - fullCmd := sshCmd + " " + h + sshCmd = strings.Replace(sshCmd, undercloud, h, -1) // check if its config files or command output if config.Services[service].ServiceCommand != "" && config.Services[service].CatOutput { for _, path := range config.Services[service].Path { - GetCommandOutput(config.Services[service].ServiceCommand, configDir+"/"+service+"/"+h+"/"+path, fullCmd) + GetCommandOutput(config.Services[service].ServiceCommand, configDir+"/"+service+"/"+h+"/"+path, sshCmd) } } else { // else if config files for _, path := range config.Services[service].Path { - PullLocalFiles(path, configDir+"/"+service+"/"+h+"/"+path, fullCmd) + PullLocalFiles(path, configDir+"/"+service+"/"+h+"/"+path, sshCmd) } } } } else { - fullCmd := sshCmd + " " + undercloud // check if its config files or command output if config.Services[service].ServiceCommand != "" && config.Services[service].CatOutput { for _, path := range config.Services[service].Path { - GetCommandOutput(config.Services[service].ServiceCommand, configDir+"/"+service+"/"+undercloud+"/"+path, fullCmd) + GetCommandOutput(config.Services[service].ServiceCommand, configDir+"/"+service+"/"+undercloud+"/"+path, sshCmd) } } else { // else if config files for _, path := range config.Services[service].Path { - PullLocalFiles(path, configDir+"/"+service+"/"+undercloud+"/"+path, fullCmd) + PullLocalFiles(path, configDir+"/"+service+"/"+undercloud+"/"+path, sshCmd) } } } @@ -149,6 +153,9 @@ func PullConfigFromHosts(service string, configDir string, sshCmd string, underc } func GetPodmanId(containerName string, sshCmd string) (string, error) { + if Sudo { + sshCmd = sshCmd + " sudo " + } cmd := sshCmd + " podman ps -a | awk '/" + containerName + "$/ {print $1}'" output, err := common.ExecCmd(cmd) return output[0], err @@ -172,6 +179,9 @@ func GetCommandOutput(command string, localPath string, sshCmd string) error { } func PullLocalFiles(orgPath string, destPath string, sshCmd string) error { + if Sudo { + sshCmd = sshCmd + " sudo " + } cmd := sshCmd + " cp -R " + orgPath + " " + destPath _, err := common.ExecCmd(cmd) if err != nil { @@ -181,6 +191,9 @@ func PullLocalFiles(orgPath string, destPath string, sshCmd string) error { } func PullPodmanFiles(podmanId string, remotePath string, localPath string, sshCmd string) error { + if Sudo { + sshCmd = sshCmd + " sudo " + } cmd := sshCmd + " podman cp " + podmanId + ":" + remotePath + " " + localPath _, err := common.ExecCmd(cmd) if err != nil { @@ -201,14 +214,25 @@ func PullPodFiles(podId string, containerName string, remotePath string, localPa func SyncConfigDir(localPath string, remotePath string, sshCmd string, undercloud string) error { // make sure localPath exists + var cmd string err := os.MkdirAll(localPath, os.ModePerm) if err != nil { return err } - hosts := GetListHosts(undercloud) - for _, h := range hosts { - cmd := "rsync -a -e '" + sshCmd + " " + h + "' :" + remotePath + " " + localPath + if undercloud != "" { + cmd = "rsync -a -e '" + sshCmd + "' :" + remotePath + " " + localPath common.ExecCmd(cmd) + } else { + hosts := GetListHosts(undercloud) + + for _, h := range hosts { + if strings.Contains(sshCmd, "-F") { + cmd = "rsync -a -e '" + sshCmd + " " + h + "' :" + remotePath + " " + localPath + } else { + cmd = "rsync -a -e '" + sshCmd + h + "' :" + remotePath + " " + localPath + } + common.ExecCmd(cmd) + } } return nil } @@ -230,6 +254,9 @@ func CleanUp(remotePath string, sshCmd string) error { if remotePath == "" || remotePath == "/" { return fmt.Errorf("Clean up Error - Empty or wrong path: " + remotePath + ". Please make sure you provided a correct path.") } + if Sudo { + sshCmd = sshCmd + " sudo " + } cmd := sshCmd + " rm -rf " + remotePath common.ExecCmd(cmd) return nil @@ -242,26 +269,27 @@ func CreateServicesTrees(configDir string, sshCmd string, undercloud string, fil } for service := range config.Services { - if _, ok := filterMap[service]; ok || len(filters) == 0 { - if len(config.Services[service].Hosts) != 0 { - for _, h := range config.Services[service].Hosts { - // Create trees for each hosts describe in config Yaml file - fullCmd := sshCmd + " " + h + if config.Services[service].Enable { + if _, ok := filterMap[service]; ok || len(filters) == 0 { + if len(config.Services[service].Hosts) != 0 { + for _, h := range config.Services[service].Hosts { + // Create trees for each hosts describe in config Yaml file + sshCmd = strings.Replace(sshCmd, undercloud, h, -1) + for _, path := range config.Services[service].Path { + output, err := CreateServiceTree(service, path, configDir, sshCmd, h) + if err != nil { + return output, err + } + } + } + } else { for _, path := range config.Services[service].Path { - output, err := CreateServiceTree(service, path, configDir, fullCmd, h) + output, err := CreateServiceTree(service, path, configDir, sshCmd, "") if err != nil { return output, err } } } - } else { - fullCmd := sshCmd + " " + undercloud - for _, path := range config.Services[service].Path { - output, err := CreateServiceTree(service, path, configDir, fullCmd, "") - if err != nil { - return output, err - } - } } } } @@ -270,6 +298,9 @@ func CreateServicesTrees(configDir string, sshCmd string, undercloud string, fil func CreateServiceTree(serviceName string, path string, configDir string, sshCmd string, host string) (string, error) { fullPath := configDir + "/" + serviceName + "/" + host + "/" + getDir(path) + if Sudo { + sshCmd = sshCmd + " sudo " + } cmd := sshCmd + " mkdir -p " + fullPath output, err := common.ExecCmdSimple(cmd) return output, err @@ -280,7 +311,7 @@ func getDir(s string) string { } func FetchConfigFromEnv(configPath string, - localDir string, remoteDir string, tripleo bool, connection, sshCmd string, undercloud string, filters []string) error { + localDir string, remoteDir string, tripleo bool, connection, fullCmd string, undercloud string, filters []string, sshCmd string) error { var local bool cfg, err := common.LoadServiceConfigFile(configPath) @@ -296,21 +327,21 @@ func FetchConfigFromEnv(configPath string, } if local { - output, err := CreateServicesTrees(localDir, sshCmd, undercloud, filters) + output, err := CreateServicesTrees(localDir, fullCmd, undercloud, filters) if err != nil { fmt.Println(output) return err } - PullConfigs(localDir, tripleo, sshCmd, undercloud, filters) + PullConfigs(localDir, tripleo, fullCmd, undercloud, filters) } else { - output, err := CreateServicesTrees(remoteDir, sshCmd, undercloud, filters) + output, err := CreateServicesTrees(remoteDir, fullCmd, undercloud, filters) if err != nil { fmt.Println(output) return err } - PullConfigs(remoteDir, tripleo, sshCmd, undercloud, filters) + PullConfigs(remoteDir, tripleo, fullCmd, undercloud, filters) SyncConfigDir(localDir, remoteDir, sshCmd, undercloud) - CleanUp(remoteDir, sshCmd) + CleanUp(remoteDir, fullCmd) } return nil } diff --git a/pkg/common/config.go b/pkg/common/config.go index f249349..936f8ec 100644 --- a/pkg/common/config.go +++ b/pkg/common/config.go @@ -36,6 +36,7 @@ type ODConfig struct { SshCmd string `ini:"ssh_cmd"` DirectorHost string `ini:"director_host"` ContainerEngine string `ini:"container_engine"` + Sudo bool `ini:"sudo"` Connection string `ini:"connection"` RemoteConfigPath string `ini:"remote_config_path"` LocalConfigPath string `ini:"local_config_path"`