Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional to-file and from-file sub commands #96

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var dryRun bool
var verboseSSH bool
var RsyncArguments string
var runSyncProcess synchers.RunSyncProcessFunctionType
var skipSourceRun bool
var skipSourceCleanup bool
var skipTargetCleanup bool
var skipTargetImport bool
Expand All @@ -52,8 +53,19 @@ var syncCmd = &cobra.Command{
}

func syncCommandRun(cmd *cobra.Command, args []string) {
SyncerType := args[0]
viper.Set("syncer-type", args[0])
SyncerType := ""
if len(args) > 0 {
SyncerType = args[0]
viper.Set("syncer-type", args[0])
} else {
if cmd.Name() == "to-file" || cmd.Name() == "from-file" {
// set default as mariadb for now if no arg is given
SyncerType = "mariadb"
} else {
fmt.Println("Error: No SyncerType provided.")
return
}
}

var configRoot synchers.SyncherConfigRoot

Expand Down Expand Up @@ -200,8 +212,8 @@ func syncCommandRun(cmd *cobra.Command, args []string) {

}

// let's update the named transfer resource if it is set
if namedTransferResource != "" {
// let's update the named transfer resource if it is set & not syncing to/from a file
if namedTransferResource != "" && !strings.Contains(cmd.Name(), "-file") {
err = lagoonSyncer.SetTransferResource(namedTransferResource)
if err != nil {
utils.LogFatalError(err.Error(), nil)
Expand All @@ -222,6 +234,7 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
SyncerType: SyncerType,
DryRun: dryRun,
//SshOptions: sshOptions,
SkipSourceRun: skipSourceRun,
SshOptionWrapper: sshOptionWrapper,
SkipTargetCleanup: skipTargetCleanup,
SkipSourceCleanup: skipSourceCleanup,
Expand Down Expand Up @@ -289,6 +302,39 @@ func confirmPrompt(message string) (bool, error) {
return false, err
}

var toFileCmd = &cobra.Command{
Use: "to-file",
Short: "Sync to a file",
Long: "Sync/Dump to a file to produce a resource dump",
Run: func(cmd *cobra.Command, args []string) {
fileFlag, _ := cmd.Flags().GetString("transfer-resource-name")
fmt.Println("You are about to dump to:", fileFlag)

// set the skipTargetImport and skipTargetCleanup flags to true
cmd.Parent().PersistentFlags().Set("skip-target-import", "true")
cmd.Parent().PersistentFlags().Set("skip-source-cleanup", "true")
cmd.Parent().PersistentFlags().Set("skip-target-cleanup", "true")

syncCommandRun(cmd, args)
},
}

var fromFileCmd = &cobra.Command{
Use: "from-file",
Short: "Sync from a file",
Long: "Sync from a file and perform related actions",
Run: func(cmd *cobra.Command, args []string) {
fileFlag, _ := cmd.Flags().GetString("transfer-resource-name")
fmt.Println("You are about to import from:", fileFlag)

cmd.Parent().PersistentFlags().Set("skip-source-run", "true")
cmd.Parent().PersistentFlags().Set("skip-source-cleanup", "true")
cmd.Parent().PersistentFlags().Set("skip-target-cleanup", "true")

syncCommandRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(syncCmd)
syncCmd.PersistentFlags().StringVarP(&ProjectName, "project-name", "p", "", "The Lagoon project name of the remote system")
Expand All @@ -305,12 +351,17 @@ func init() {
syncCmd.PersistentFlags().BoolVar(&noCliInteraction, "no-interaction", false, "Disallow interaction")
syncCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Don't run the commands, just preview what will be run")
syncCmd.PersistentFlags().StringVarP(&RsyncArguments, "rsync-args", "r", "--omit-dir-times --no-perms --no-group --no-owner --chmod=ugo=rwX --recursive --compress", "Pass through arguments to change the behaviour of rsync")
syncCmd.PersistentFlags().BoolVar(&skipSourceRun, "skip-source-run", false, "Don't run any ops on the source")
syncCmd.PersistentFlags().BoolVar(&skipSourceCleanup, "skip-source-cleanup", false, "Don't clean up any of the files generated on the source")
syncCmd.PersistentFlags().BoolVar(&skipTargetCleanup, "skip-target-cleanup", false, "Don't clean up any of the files generated on the target")
syncCmd.PersistentFlags().BoolVar(&skipTargetImport, "skip-target-import", false, "This will skip the import step on the target, in combination with 'no-target-cleanup' this essentially produces a resource dump")
syncCmd.PersistentFlags().StringVarP(&namedTransferResource, "transfer-resource-name", "", "", "The name of the temporary file to be used to transfer generated resources (db dumps, etc) - random /tmp file otherwise")
syncCmd.PersistentFlags().StringVarP(&apiEndpoint, "api", "A", "", "Specify your lagoon api endpoint - required for ssh-portal integration")
syncCmd.PersistentFlags().BoolVar(&useSshPortal, "use-ssh-portal", false, "This will use the SSH Portal rather than the (soon to be removed) SSH Service on Lagoon core. Will become default in a future release.")
syncCmd.PersistentFlags().StringVarP(&namedTransferResource, "transfer-resource-name", "f", "", "The name of the temporary file to be used to transfer generated resources (db dumps, etc) - random /tmp file otherwise")

syncCmd.AddCommand(toFileCmd)
syncCmd.AddCommand(fromFileCmd)

// By default, we hook up the syncers.RunSyncProcess function to the runSyncProcess variable
// by doing this, it lets us easily override it for testing the command - but for most of the time
// this should be okay.
Expand Down
11 changes: 10 additions & 1 deletion synchers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -111,12 +112,20 @@ func (m *FilesSyncRoot) GetFilesToCleanup(environment Environment) []string {

func (m *FilesSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
config := m.Config
isDirectory := true
if environment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
config = m.getEffectiveLocalDetails()
}
isFile, err := regexp.MatchString(`^(?:\.|~)?\/?[\w\/\-\.]*\.\w+$`, config.SyncPath)
if err != nil {
log.Fatalf("Error while matching file path: %v", err)
}
if isFile {
isDirectory = false
}
return SyncerTransferResource{
Name: fmt.Sprintf(config.SyncPath),
IsDirectory: true,
IsDirectory: isDirectory,
SkipCleanup: true,
ExcludeResources: m.Config.Exclude,
}
Expand Down
22 changes: 16 additions & 6 deletions synchers/syncutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RunSyncProcessFunctionTypeArguments struct {
SyncerType string
DryRun bool
SshOptionWrapper *SSHOptionWrapper
SkipSourceRun bool
SkipSourceCleanup bool
SkipTargetCleanup bool
SkipTargetImport bool
Expand All @@ -58,10 +59,12 @@ func RunSyncProcess(args RunSyncProcessFunctionTypeArguments) error {
return err
}

err = SyncRunSourceCommand(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper)
if err != nil {
_ = SyncCleanUp(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper)
return err
if !args.SkipSourceRun {
err = SyncRunSourceCommand(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper)
if err != nil {
_ = SyncCleanUp(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper)
return err
}
}

args.TargetEnvironment, err = RunPrerequisiteCommand(args.TargetEnvironment, args.LagoonSyncer, args.SyncerType, args.DryRun, args.SshOptionWrapper)
Expand All @@ -71,7 +74,7 @@ func RunSyncProcess(args RunSyncProcessFunctionTypeArguments) error {
return err
}

err = SyncRunTransfer(args.SourceEnvironment, args.TargetEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper)
err = SyncRunTransfer(args.SourceEnvironment, args.TargetEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper, args.TransferResourceName)
if err != nil {
_ = PrerequisiteCleanUp(args.SourceEnvironment, sourceRsyncPath, args.DryRun, args.SshOptionWrapper)
_ = SyncCleanUp(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptionWrapper)
Expand Down Expand Up @@ -154,7 +157,7 @@ func SyncRunSourceCommand(remoteEnvironment Environment, syncer Syncer, dryRun b
return nil
}

func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environment, syncer Syncer, dryRun bool, sshOptionWrapper *SSHOptionWrapper) error {
func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environment, syncer Syncer, dryRun bool, sshOptionWrapper *SSHOptionWrapper, transferResourceName string) error {
utils.LogProcessStep("Beginning file transfer logic", nil)

// TODO: This is going to be the trickiest of the ssh option calculations.
Expand Down Expand Up @@ -190,6 +193,13 @@ func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environmen
// rsyncRemoteSystemUsername is used by the rsync command to set up the ssh tunnel
rsyncRemoteSystemUsername := ""

// checks transferResourceName is provided for a file sync
if transferResourceName != "" {
if _, ok := syncer.(*FilesSyncRoot); ok {
sourceEnvironmentName = transferResourceName
}
}

if sourceEnvironment.EnvironmentName != LOCAL_ENVIRONMENT_NAME {
//sourceEnvironmentName = fmt.Sprintf("%[email protected]:%s", sourceEnvironment.GetOpenshiftProjectName(), sourceEnvironmentName)
sourceEnvironmentName = fmt.Sprintf(":%s", sourceEnvironmentName)
Expand Down
Loading