Skip to content

Commit

Permalink
Prerequisite rsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed Dec 20, 2020
1 parent c6cb62c commit bb7ce19
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {

var configCmd = &cobra.Command{
Use: "config",
Short: "Print the onfig that is being used by lagoon-sync",
Short: "Print the config that is being used by lagoon-sync",
Run: func(v *cobra.Command, args []string) {
PrintConfigOut()
},
Expand Down
4 changes: 1 addition & 3 deletions prerequisite/prerequisitedefs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package prerequisite

import "log"

type GatheredPrerequisite struct {
Name string `json:"name"`
Value string `json:"value"`
Expand All @@ -19,7 +17,7 @@ type ConfigPrerequisite interface {
var configPrerequisiteList []ConfigPrerequisite

func RegisterConfigPrerequisite(name string, config ConfigPrerequisite) {
log.Println("Registering: " + name)
//log.Println("Registering: " + name)

configPrerequisiteList = append(configPrerequisiteList, config)
}
Expand Down
2 changes: 1 addition & 1 deletion prerequisite/rsyncprerequisite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (p *rsyncPrerequisite) GetValue() bool {
}

p.RsyncPath = strings.TrimSuffix(stdout.String(), "\n")
log.Println("Found rsync path: " + p.RsyncPath)
//log.Println("Found rsync path: " + p.RsyncPath)

return true
}
Expand Down
2 changes: 1 addition & 1 deletion synchers/drupalconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (root DrupalconfigSyncRoot) PrepareSyncer() (Syncer, error) {
return root, nil
}

func (root DrupalconfigSyncRoot) GetPrerequisiteCommand(environment Environment) SyncCommand {
func (root DrupalconfigSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
return SyncCommand{}
}

Expand Down
2 changes: 1 addition & 1 deletion synchers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (root FilesSyncRoot) PrepareSyncer() (Syncer, error) {
return root, nil
}

func (root FilesSyncRoot) GetPrerequisiteCommand(environment Environment) SyncCommand {
func (root FilesSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
return SyncCommand{}
}

Expand Down
9 changes: 6 additions & 3 deletions synchers/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ func (root MariadbSyncRoot) PrepareSyncer() (Syncer, error) {
return root, nil
}

func (root MariadbSyncRoot) GetPrerequisiteCommand(environment Environment) SyncCommand {
func (root MariadbSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
lagoonSyncBin := "/tmp/lagoon-sync"

return SyncCommand{
command: fmt.Sprintf("./lagoon-sync {{ .config }}"),
command: fmt.Sprintf("{{ .bin }} {{ .command }} 2> /dev/null"),
substitutions: map[string]interface{}{
"config": "config",
"bin": lagoonSyncBin,
"command": command,
},
}
}
Expand Down
9 changes: 6 additions & 3 deletions synchers/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ func (root PostgresSyncRoot) PrepareSyncer() (Syncer, error) {
return root, nil
}

func (root PostgresSyncRoot) GetPrerequisiteCommand(environment Environment) SyncCommand {
func (root PostgresSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
lagoonSyncBin := "which lagoon-sync"

return SyncCommand{
command: fmt.Sprintf("./lagoon-sync {{ .config }}"),
command: fmt.Sprintf("{{ .bin }} {{ .command }}"),
substitutions: map[string]interface{}{
"config": "config",
"bin": lagoonSyncBin,
"command": command,
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion synchers/syncdefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const LOCAL_ENVIRONMENT_NAME = "local"

type Syncer interface {
GetPrerequisiteCommand(environmnt Environment) SyncCommand
GetPrerequisiteCommand(environmnt Environment, command string) SyncCommand
// GetRemoteCommand will return the command to be run on the source system
GetRemoteCommand(environment Environment) SyncCommand
// GetLocalCommand will return the command to be run on the target system
Expand Down Expand Up @@ -40,6 +40,8 @@ type Environment struct {
EnvironmentName string
ServiceName string //This is used to determine which Lagoon service we need to rsync
RsyncAvailable bool
RsyncPath string
RsyncLocalPath string
}

func (r Environment) getOpenshiftProjectName() string {
Expand Down
143 changes: 128 additions & 15 deletions synchers/syncutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"text/template"

"gopkg.in/yaml.v2"
Expand All @@ -25,13 +26,12 @@ func UnmarshallLagoonYamlToLagoonSyncStructure(data []byte) (SyncherConfigRoot,

func RunSyncProcess(sourceEnvironment Environment, targetEnvironment Environment, lagoonSyncer Syncer, dryRun bool) error {
var err error
err = RunPrerequisiteCommand(sourceEnvironment, lagoonSyncer, dryRun)
sourceRsyncPath, err := RunPrerequisiteCommand(sourceEnvironment, lagoonSyncer, dryRun)
if err != nil {
_ = PrerequisiteCleanUp(sourceEnvironment, sourceRsyncPath, dryRun)
return err
}

os.Exit(1)

err = SyncRunSourceCommand(sourceEnvironment, lagoonSyncer, dryRun)
if err != nil {
_ = SyncCleanUp(sourceEnvironment, lagoonSyncer, dryRun)
Expand All @@ -43,27 +43,34 @@ func RunSyncProcess(sourceEnvironment Environment, targetEnvironment Environment
return err
}

targetRsyncPath, err := RunPrerequisiteCommand(targetEnvironment, lagoonSyncer, dryRun)
if err != nil {
_ = PrerequisiteCleanUp(targetEnvironment, targetRsyncPath, dryRun)
return err
}
err = SyncRunTargetCommand(targetEnvironment, lagoonSyncer, dryRun)
if err != nil {
_ = SyncCleanUp(sourceEnvironment, lagoonSyncer, dryRun)
_ = SyncCleanUp(targetEnvironment, lagoonSyncer, dryRun)
return err
}

_ = PrerequisiteCleanUp(sourceEnvironment, sourceRsyncPath, dryRun)
_ = PrerequisiteCleanUp(targetEnvironment, targetRsyncPath, dryRun)
_ = SyncCleanUp(sourceEnvironment, lagoonSyncer, dryRun)
_ = SyncCleanUp(targetEnvironment, lagoonSyncer, dryRun)

return nil
}

func RunPrerequisiteCommand(environment Environment, syncer Syncer, dryRun bool) error {
func RunPrerequisiteCommand(environment Environment, syncer Syncer, dryRun bool) (string, error) {
log.Printf("Running prerequisite checks on %s environment", environment.EnvironmentName)

var execString string

command, commandErr := syncer.GetPrerequisiteCommand(environment).GetCommand()
command, commandErr := syncer.GetPrerequisiteCommand(environment, "config").GetCommand()
if commandErr != nil {
return commandErr
return "", commandErr
}

if environment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
Expand All @@ -78,19 +85,41 @@ func RunPrerequisiteCommand(environment Environment, syncer Syncer, dryRun bool)
err, responseJson, errstring := Shellout(execString)
if err != nil {
fmt.Println(errstring)
return err
return "", err
}

data := &PreRequisiteResponse{}
json.Unmarshal([]byte(responseJson), &data)

fmt.Println("Response: %s", data)
// check if environment has rsync
if data.RysncPrequisite != nil {
environment.RsyncAvailable = true
for _, c := range data.RysncPrequisite {
if c.Value != "" {
environment.RsyncPath = c.Value
}
}
}

lagoonVersion := ""
if data.Version != "" {
lagoonVersion = data.Version
}

// if data.RysncPrequisite != "" {
// environment.RsyncAvailable = true
// }
if !environment.RsyncAvailable {
// add rsync to env
rsyncPath, err := createRsync(environment, syncer, lagoonVersion)
if err != nil {
fmt.Println(errstring)
return "", err
}

log.Printf("Rsync path: %s", rsyncPath)
return rsyncPath, nil
}
}
return nil

return "", nil
}

func SyncRunSourceCommand(remoteEnvironment Environment, syncer Syncer, dryRun bool) error {
Expand Down Expand Up @@ -190,8 +219,6 @@ func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environmen
targetEnvironmentName)

if executeRsyncRemotelyOnTarget {
log.Print("Check if rsync is available")

execString = generateRemoteCommand(targetEnvironment, execString)
}

Expand Down Expand Up @@ -302,7 +329,7 @@ func (c SyncCommand) GetCommand() (string, error) {
}

func generateRemoteCommand(remoteEnvironment Environment, command string) string {
return fmt.Sprintf("ssh -t -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -p 32222 %[email protected] '%v'",
return fmt.Sprintf("ssh -tt -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -p 32222 %[email protected] '%v'",
remoteEnvironment.getOpenshiftProjectName(), command)
}

Expand All @@ -324,3 +351,89 @@ func getEnv(key string, defaultVal string) string {
}
return defaultVal
}

const RsyncAssetPath = "./binaries/rsync"

// will add bundled rsync onto environment and return the new rsync path as string
func createRsync(environment Environment, syncer Syncer, lagoonVersion string) (string, error) {
// if local, we bail out for now.
if environment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
return "Local environment doesn't have rsync", nil
}

environmentName := syncer.GetTransferResource(environment).Name
if syncer.GetTransferResource(environment).IsDirectory == true {
environmentName += "/"
}

rsyncLocalResource := fmt.Sprintf("%vlagoon_sync_rsync_%v", "./binaries/", strings.ReplaceAll(lagoonVersion, ".", "_"))
environment.RsyncLocalPath = rsyncLocalResource
rsyncDestinationPath := fmt.Sprintf("%vlagoon_sync_rsync_%v", "/tmp/", strings.ReplaceAll(lagoonVersion, ".", "_"))

// rename rsync binary with latest lagoon version
cpRsyncPath := fmt.Sprintf("cp %s %s",
RsyncAssetPath,
fmt.Sprintf("%vlagoon_sync_rsync_%v", "./binaries/", strings.ReplaceAll(lagoonVersion, ".", "_")))

if err, _, errstring := Shellout(cpRsyncPath); err != nil {
log.Println(errstring)
return "", err
}

lagoonRsyncService := "cli"
rsyncRemoteSystemUsername := ""

if environment.EnvironmentName != LOCAL_ENVIRONMENT_NAME {
environmentName = fmt.Sprintf(":%s", environmentName)
rsyncRemoteSystemUsername = environment.getOpenshiftProjectName()
if environment.ServiceName != "" {
lagoonRsyncService = environment.ServiceName
}
}

execString := fmt.Sprintf("rsync -a %s -e \"ssh -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 32222 -l %v ssh.lagoon.amazeeio.cloud service=%v\" :%s",
rsyncLocalResource,
rsyncRemoteSystemUsername,
lagoonRsyncService,
rsyncDestinationPath)

log.Printf("Running the following for:- %s", execString)

if err, _, errstring := Shellout(execString); err != nil {
log.Println(errstring)
return "", err
}

removeLocalRsyncCopyExecString := fmt.Sprintf("rm -rf %v", rsyncLocalResource)
if err, _, errstring := Shellout(removeLocalRsyncCopyExecString); err != nil {
log.Println(errstring)
return "", err
}

return rsyncDestinationPath, nil
}

func PrerequisiteCleanUp(environment Environment, rsyncPath string, dryRun bool) error {
log.Printf("Beginning prerequisite resource cleanup on %s", environment.EnvironmentName)
if rsyncPath == "" {
return nil
}
execString := fmt.Sprintf("rm -r %s", rsyncPath)

if environment.EnvironmentName != LOCAL_ENVIRONMENT_NAME {
execString = generateRemoteCommand(environment, execString)
}

log.Printf("Running the following: %s", execString)

if !dryRun {
err, _, errstring := Shellout(execString)

if err != nil {
fmt.Println(errstring)
return err
}
}

return nil
}

0 comments on commit bb7ce19

Please sign in to comment.