Skip to content

Commit

Permalink
Add non standard service handling like ovs
Browse files Browse the repository at this point in the history
  • Loading branch information
matbu committed Mar 22, 2024
1 parent a8914d4 commit 9f2c439
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 37 deletions.
22 changes: 10 additions & 12 deletions cmd/pull.go
Expand Up @@ -62,30 +62,28 @@ This command will populate the config.yaml file with the podman and image Ids an
}
// OCP Settings
localOCPDir := config.Openshift.OcpLocalConfigPath
err := collectcfg.FetchConfigFromEnv(serviceConfig, localOCPDir, "", false, config.Openshift.Connection, "")
err := collectcfg.FetchConfigFromEnv(serviceConfig, localOCPDir, "", false, config.Openshift.Connection, "", "")
if err != nil {
fmt.Println("Error while collecting config: ", err)
return
}
} else if cloud == "tripleo" {
// TRIPLEO Settings:
standaloneSsh := config.Tripleo.SshCmd
sshCmd := config.Tripleo.SshCmd
fullCmd := sshCmd + " " + config.Tripleo.DirectorHost
remoteConfigDir := config.Tripleo.RemoteConfigPath
localConfigDir := config.Tripleo.LocalConfigPath

if !common.TestSshConnection(standaloneSsh) {
fmt.Println("Please check your SSH configuration: " + standaloneSsh)
if !common.TestSshConnection(fullCmd) {
fmt.Println("Please check your SSH configuration: " + fullCmd)
return
}
if update || updateOnly {
collectcfg.SetTripleODataEnv(serviceConfig, standaloneSsh, filters, true)
collectcfg.SetTripleODataEnv(serviceConfig, sshCmd, filters, true)
}
if !updateOnly {
err := collectcfg.FetchConfigFromEnv(serviceConfig, localConfigDir, remoteConfigDir, true, config.Tripleo.Connection, standaloneSsh)
if err != nil {
fmt.Println("Error while collecting config: ", err)
return
}
err := collectcfg.FetchConfigFromEnv(serviceConfig, localConfigDir, remoteConfigDir, true, config.Tripleo.Connection, sshCmd, config.Tripleo.DirectorHost)
if err != nil {
fmt.Println("Error while collecting config: ", err)
return
}
} else {
fmt.Println("Error unkown cloud", cloud)
Expand Down
7 changes: 7 additions & 0 deletions config.yaml
Expand Up @@ -25,3 +25,10 @@ services:
- /etc/keystone
- /etc/keystone/keystone.conf
- /etc/keystone/logging.conf
ovs_db:
hosts:
- standalone
service_command: ovs-vsctl list Open_vSwitch .
cat_output: true
path:
- ovs_db.output
3 changes: 2 additions & 1 deletion os-diff.cfg
Expand Up @@ -5,7 +5,8 @@ service_config_file=config.yaml

[Tripleo]

ssh_cmd=ssh -F ssh.config standalone
ssh_cmd=ssh -F ssh.config
director_host=standalone
container_engine=podman
connection=ssh
remote_config_path=/tmp/tripleo
Expand Down
145 changes: 122 additions & 23 deletions pkg/collectcfg/fetch.go
Expand Up @@ -41,6 +41,9 @@ type Service struct {
ContainerName string `yaml:"container_name"`
StrictPodNameMatch bool `yaml:"strict_pod_name_match"`
Path []string `yaml:"path"`
Hosts []string `yaml:"hosts"`
ServiceCommand string `yaml:"service_command"`
CatOutput bool `yaml:"cat_output"`
}

type Config struct {
Expand Down Expand Up @@ -85,15 +88,22 @@ func dumpConfigFile(configPath string) error {
return nil
}

func PullConfigs(configDir string, podman bool, sshCmd string) error {
func PullConfigs(configDir string, tripleo bool, sshCmd string, undercloud string) error {
// Pull configuration service by service
for service := range config.Services {
PullConfig(service, podman, configDir, sshCmd)
if tripleo && (config.Services[service].PodmanName == "" || config.Services[service].PodmanId == "") {
PullConfigFromHosts(service, configDir, sshCmd, undercloud)
} else {
fullCmd := sshCmd + " " + undercloud
PullConfig(service, tripleo, configDir, fullCmd)
}
}
return nil
}

func PullConfig(serviceName string, podman bool, configDir string, sshCmd string) error {
if podman {
func PullConfig(serviceName string, tripleo bool, configDir string, sshCmd string) error {
// Pull configuration from TripleO Podman or OCP Pods
if tripleo {
var podmanId string
if config.Services[serviceName].PodmanId != "" {
podmanId = config.Services[serviceName].PodmanId
Expand Down Expand Up @@ -132,6 +142,41 @@ func GetPodmanIds(sshCmd string, all bool) ([]byte, error) {
return output, err
}

func PullConfigFromHosts(service string, configDir string, sshCmd string, undercloud string) error {
// Pull confugiration for a given service non hosted on Podman and OCP containers
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
// 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)
}
} else {
// else if config files
for _, path := range config.Services[service].Path {
PullLocalFiles(path, configDir+"/"+service+"/"+h+"/"+path, fullCmd)
}
}
}
} 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)
}
} else {
// else if config files
for _, path := range config.Services[service].Path {
PullLocalFiles(path, configDir+"/"+service+"/"+undercloud+"/"+path, fullCmd)
}
}
}
return nil
}

func GetPodmanId(containerName string, sshCmd string) (string, error) {
cmd := sshCmd + " podman ps -a | awk '/" + containerName + "$/ {print $1}'"
output, err := common.ExecCmd(cmd)
Expand All @@ -144,25 +189,65 @@ func GetPodId(podName string) (string, error) {
return output[0], err
}

func GetCommandOutput(command string, localPath string, sshCmd string) error {
cmd := sshCmd + " '" + command + " > " + localPath + "'"
_, err := common.ExecCmd(cmd)
if err != nil {
return err
}
return nil
}

func PullLocalFiles(orgPath string, destPath string, sshCmd string) error {
cmd := sshCmd + " cp -R " + orgPath + " " + destPath
_, err := common.ExecCmd(cmd)
if err != nil {
return err
}
return nil
}

func PullPodmanFiles(podmanId string, remotePath string, localPath string, sshCmd string) error {
cmd := sshCmd + " podman cp " + podmanId + ":" + remotePath + " " + localPath
common.ExecCmd(cmd)
_, err := common.ExecCmd(cmd)
if err != nil {
return err
}
return nil
}

func PullPodFiles(podId string, containerName string, remotePath string, localPath string) error {
// Test OC connexion
cmd := "oc cp -c " + containerName + " " + podId + ":" + remotePath + " " + localPath
common.ExecCmd(cmd)
_, err := common.ExecCmd(cmd)
if err != nil {
return err
}
return nil
}

func SyncConfigDir(localPath string, remotePath string, sshCmd string) error {
cmd := "rsync -a -e '" + sshCmd + "' :" + remotePath + " " + localPath
common.ExecCmd(cmd)
func SyncConfigDir(localPath string, remotePath string, sshCmd string, undercloud string) error {
hosts := GetListHosts(undercloud)
for _, h := range hosts {
cmd := "rsync -a -e '" + sshCmd + " " + h + "' :" + remotePath + " " + localPath
common.ExecCmd(cmd)
}
return nil
}

func GetListHosts(undercloud string) []string {
var hosts []string
hosts = append(hosts, undercloud)
for service := range config.Services {
for _, h := range config.Services[service].Hosts {
if !common.StringInSlice(h, hosts) {
hosts = append(hosts, h)
}
}
}
return hosts
}

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.")
Expand All @@ -172,20 +257,34 @@ func CleanUp(remotePath string, sshCmd string) error {
return nil
}

func CreateServicesTrees(configDir string, sshCmd string) (string, error) {
for service, _ := range config.Services {
for _, path := range config.Services[service].Path {
output, err := CreateServiceTree(service, path, configDir, sshCmd)
if err != nil {
return output, err
func CreateServicesTrees(configDir string, sshCmd string, undercloud string) (string, error) {
for service := range config.Services {
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
for _, path := range config.Services[service].Path {
output, err := CreateServiceTree(service, path, configDir, fullCmd, h)
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
}
}
}
}
return "", nil
}

func CreateServiceTree(serviceName string, path string, configDir string, sshCmd string) (string, error) {
fullPath := configDir + "/" + serviceName + "/" + getDir(path)
func CreateServiceTree(serviceName string, path string, configDir string, sshCmd string, host string) (string, error) {
fullPath := configDir + "/" + serviceName + "/" + host + "/" + getDir(path)
cmd := sshCmd + " mkdir -p " + fullPath
output, err := common.ExecCmdSimple(cmd)
return output, err
Expand All @@ -196,7 +295,7 @@ func getDir(s string) string {
}

func FetchConfigFromEnv(configPath string,
localDir string, remoteDir string, podman bool, connection, sshCmd string) error {
localDir string, remoteDir string, tripleo bool, connection, sshCmd string, undercloud string) error {

var local bool
err := LoadServiceConfig(configPath)
Expand All @@ -211,20 +310,20 @@ func FetchConfigFromEnv(configPath string,
}

if local {
output, err := CreateServicesTrees(localDir, sshCmd)
output, err := CreateServicesTrees(localDir, sshCmd, undercloud)
if err != nil {
fmt.Println(output)
return err
}
PullConfigs(localDir, podman, sshCmd)
PullConfigs(localDir, tripleo, sshCmd, undercloud)
} else {
output, err := CreateServicesTrees(remoteDir, sshCmd)
output, err := CreateServicesTrees(remoteDir, sshCmd, undercloud)
if err != nil {
fmt.Println(output)
return err
}
PullConfigs(remoteDir, podman, sshCmd)
SyncConfigDir(localDir, remoteDir, sshCmd)
PullConfigs(remoteDir, tripleo, sshCmd, undercloud)
SyncConfigDir(localDir, remoteDir, sshCmd, undercloud)
CleanUp(remoteDir, sshCmd)
}
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/common/config.go
Expand Up @@ -34,6 +34,7 @@ type ODConfig struct {

Tripleo struct {
SshCmd string `ini:"ssh_cmd"`
DirectorHost string `ini:"director_host"`
ContainerEngine string `ini:"container_engine"`
Connection string `ini:"connection"`
RemoteConfigPath string `ini:"remote_config_path"`
Expand Down
9 changes: 9 additions & 0 deletions pkg/common/utils.go
Expand Up @@ -56,3 +56,12 @@ func TestSshConnection(sshCmd string) bool {
}
return true
}

func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion pkg/godiff/utils.go
Expand Up @@ -364,7 +364,7 @@ func CompareRawData(rawdata1 []byte, rawdata2 []byte, origin string, dest string

func GetConfigFromRemote(remoteCmd string, configPath string) ([]byte, error) {
// Build command:
cmd := remoteCmd + " cat" + " " + configPath
cmd := remoteCmd + " cat " + configPath
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
fmt.Println(string(out))
Expand Down

0 comments on commit 9f2c439

Please sign in to comment.