-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Clifford
committed
Dec 20, 2020
1 parent
c6cb62c
commit bb7ce19
Showing
9 changed files
with
148 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"text/template" | ||
|
||
"gopkg.in/yaml.v2" | ||
|
@@ -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) | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
|
@@ -190,8 +219,6 @@ func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environmen | |
targetEnvironmentName) | ||
|
||
if executeRsyncRemotelyOnTarget { | ||
log.Print("Check if rsync is available") | ||
|
||
execString = generateRemoteCommand(targetEnvironment, execString) | ||
} | ||
|
||
|
@@ -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) | ||
} | ||
|
||
|
@@ -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 | ||
} |