Skip to content

Commit

Permalink
Add support for sudo user with Pull command (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjpeacock authored Sep 24, 2024
2 parents 5b0df24 + c1e6d21 commit cd1b2fd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
7 changes: 5 additions & 2 deletions cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -82,14 +83,16 @@ 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)
if updateOnly {
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
Expand Down
1 change: 1 addition & 0 deletions os-diff.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
101 changes: 66 additions & 35 deletions pkg/collectcfg/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
)

var config common.Config
var Sudo bool

// TripleO information structures:
type PodmanContainer struct {
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -118,37 +123,39 @@ 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)
}
}
}
return nil
}

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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
}
}
}
}
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit cd1b2fd

Please sign in to comment.