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

Adds new option for specifying transfer file name #94

Merged
merged 1 commit into from
Aug 13, 2023
Merged
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
29 changes: 20 additions & 9 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var skipSourceCleanup bool
var skipTargetCleanup bool
var skipTargetImport bool
var localTransferResourceName string
var namedTransferResource string

var syncCmd = &cobra.Command{
Use: "sync [mariadb|files|mongodb|postgres|etc.]",
Expand Down Expand Up @@ -147,18 +148,27 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
RsyncArgs: RsyncArguments,
}

// let's update the named transfer resource if it is set
if namedTransferResource != "" {
err = lagoonSyncer.SetTransferResource(namedTransferResource)
if err != nil {
utils.LogFatalError(err.Error(), nil)
}
}

utils.LogDebugInfo("Config that is used for SSH", sshOptions)

err = runSyncProcess(synchers.RunSyncProcessFunctionTypeArguments{
SourceEnvironment: sourceEnvironment,
TargetEnvironment: targetEnvironment,
LagoonSyncer: lagoonSyncer,
SyncerType: SyncerType,
DryRun: dryRun,
SshOptions: sshOptions,
SkipTargetCleanup: skipTargetCleanup,
SkipSourceCleanup: skipSourceCleanup,
SkipTargetImport: skipTargetImport,
SourceEnvironment: sourceEnvironment,
TargetEnvironment: targetEnvironment,
LagoonSyncer: lagoonSyncer,
SyncerType: SyncerType,
DryRun: dryRun,
SshOptions: sshOptions,
SkipTargetCleanup: skipTargetCleanup,
SkipSourceCleanup: skipSourceCleanup,
SkipTargetImport: skipTargetImport,
TransferResourceName: namedTransferResource,
})

if err != nil {
Expand Down Expand Up @@ -207,6 +217,7 @@ func init() {
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")

// 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
Expand Down
6 changes: 5 additions & 1 deletion synchers/drupalconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func init() {

func (root DrupalconfigSyncRoot) PrepareSyncer() (Syncer, error) {
root.TransferId = strconv.FormatInt(time.Now().UnixNano(), 10)
return root, nil
return &root, nil
}

func (m DrupalconfigSyncRoot) IsInitialized() (bool, error) {
Expand Down Expand Up @@ -104,6 +104,10 @@ func (m DrupalconfigSyncRoot) GetTransferResource(environment Environment) Synce
IsDirectory: true}
}

func (m *DrupalconfigSyncRoot) SetTransferResource(transferResourceName string) error {
return fmt.Errorf("Setting the transfer resource is not supported for drupal config")
}

func (root DrupalconfigSyncRoot) GetOutputDirectory() string {
m := root.Config
if len(m.OutputDirectory) == 0 {
Expand Down
20 changes: 12 additions & 8 deletions synchers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,39 +77,39 @@ func init() {
RegisterSyncer(FilesSyncPlugin{})
}

func (m FilesSyncRoot) IsInitialized() (bool, error) {
func (m *FilesSyncRoot) IsInitialized() (bool, error) {
return true, nil
}

func (root FilesSyncRoot) PrepareSyncer() (Syncer, error) {
func (root *FilesSyncRoot) PrepareSyncer() (Syncer, error) {
root.TransferId = strconv.FormatInt(time.Now().UnixNano(), 10)
return root, nil
}

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

func (root FilesSyncRoot) GetRemoteCommand(environment Environment) []SyncCommand {
func (root *FilesSyncRoot) GetRemoteCommand(environment Environment) []SyncCommand {
return []SyncCommand{
generateNoOpSyncCommand(),
}
}

func (m FilesSyncRoot) GetLocalCommand(environment Environment) []SyncCommand {
func (m *FilesSyncRoot) GetLocalCommand(environment Environment) []SyncCommand {
return []SyncCommand{
generateNoOpSyncCommand(),
}
}

func (m FilesSyncRoot) GetFilesToCleanup(environment Environment) []string {
func (m *FilesSyncRoot) GetFilesToCleanup(environment Environment) []string {
transferResource := m.GetTransferResource(environment)
return []string{
transferResource.Name,
}
}

func (m FilesSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
func (m *FilesSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
config := m.Config
if environment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
config = m.getEffectiveLocalDetails()
Expand All @@ -122,7 +122,11 @@ func (m FilesSyncRoot) GetTransferResource(environment Environment) SyncerTransf
}
}

func (syncConfig FilesSyncRoot) getEffectiveLocalDetails() BaseFilesSync {
func (m *FilesSyncRoot) SetTransferResource(transferResourceName string) error {
return fmt.Errorf("Setting the transfer resource is not supported for files")
}

func (syncConfig *FilesSyncRoot) getEffectiveLocalDetails() BaseFilesSync {
returnDetails := BaseFilesSync{
SyncPath: syncConfig.Config.SyncPath,
}
Expand Down
36 changes: 23 additions & 13 deletions synchers/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type MariadbSyncLocal struct {
}

type MariadbSyncRoot struct {
Config BaseMariaDbSync
LocalOverrides MariadbSyncLocal `yaml:"local"`
TransferId string
Config BaseMariaDbSync
LocalOverrides MariadbSyncLocal `yaml:"local"`
TransferId string
TransferResourceOverride string
}

// Init related types and functions follow
Expand Down Expand Up @@ -78,7 +79,7 @@ func init() {
RegisterSyncer(MariadbSyncPlugin{})
}

func (m MariadbSyncRoot) IsInitialized() (bool, error) {
func (m *MariadbSyncRoot) IsInitialized() (bool, error) {

var missingEnvvars []string

Expand All @@ -105,12 +106,12 @@ func (m MariadbSyncRoot) IsInitialized() (bool, error) {
return true, nil
}

func (root MariadbSyncRoot) PrepareSyncer() (Syncer, error) {
func (root *MariadbSyncRoot) PrepareSyncer() (Syncer, error) {
root.TransferId = strconv.FormatInt(time.Now().UnixNano(), 10)
return root, nil
}

func (root MariadbSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
func (root *MariadbSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
lagoonSyncBin, _ := utils.FindLagoonSyncOnEnv()

return SyncCommand{
Expand All @@ -122,7 +123,7 @@ func (root MariadbSyncRoot) GetPrerequisiteCommand(environment Environment, comm
}
}

func (root MariadbSyncRoot) GetRemoteCommand(sourceEnvironment Environment) []SyncCommand {
func (root *MariadbSyncRoot) GetRemoteCommand(sourceEnvironment Environment) []SyncCommand {
m := root.Config

if sourceEnvironment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
Expand Down Expand Up @@ -166,7 +167,7 @@ func (root MariadbSyncRoot) GetRemoteCommand(sourceEnvironment Environment) []Sy
}
}

func (m MariadbSyncRoot) GetLocalCommand(targetEnvironment Environment) []SyncCommand {
func (m *MariadbSyncRoot) GetLocalCommand(targetEnvironment Environment) []SyncCommand {
l := m.Config
if targetEnvironment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
l = m.getEffectiveLocalDetails()
Expand Down Expand Up @@ -195,7 +196,7 @@ func (m MariadbSyncRoot) GetLocalCommand(targetEnvironment Environment) []SyncCo
}
}

func (m MariadbSyncRoot) GetFilesToCleanup(environment Environment) []string {
func (m *MariadbSyncRoot) GetFilesToCleanup(environment Environment) []string {
transferResource := m.GetTransferResource(environment)
resourceNameWithoutGz := strings.TrimSuffix(transferResource.Name, filepath.Ext(transferResource.Name))
return []string{
Expand All @@ -204,21 +205,30 @@ func (m MariadbSyncRoot) GetFilesToCleanup(environment Environment) []string {
}
}

func (m MariadbSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
func (m *MariadbSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
resourceName := fmt.Sprintf("%vlagoon_sync_mariadb_%v.sql.gz", m.GetOutputDirectory(), m.TransferId)
if m.TransferResourceOverride != "" {
resourceName = m.TransferResourceOverride
}
return SyncerTransferResource{
Name: fmt.Sprintf("%vlagoon_sync_mariadb_%v.sql.gz", m.GetOutputDirectory(), m.TransferId),
Name: resourceName,
IsDirectory: false}
}

func (root MariadbSyncRoot) GetOutputDirectory() string {
func (m *MariadbSyncRoot) SetTransferResource(transferResourceName string) error {
m.TransferResourceOverride = transferResourceName
return nil
}

func (root *MariadbSyncRoot) GetOutputDirectory() string {
m := root.Config
if len(m.OutputDirectory) == 0 {
return "/tmp/"
}
return m.OutputDirectory
}

func (syncConfig MariadbSyncRoot) getEffectiveLocalDetails() BaseMariaDbSync {
func (syncConfig *MariadbSyncRoot) getEffectiveLocalDetails() BaseMariaDbSync {
returnDetails := BaseMariaDbSync{
DbHostname: syncConfig.Config.DbHostname,
DbUsername: syncConfig.Config.DbUsername,
Expand Down
36 changes: 23 additions & 13 deletions synchers/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ type MongoDbSyncLocal struct {
}

type MongoDbSyncRoot struct {
Config BaseMongoDbSync
LocalOverrides MongoDbSyncLocal `yaml:"local"`
TransferId string
Config BaseMongoDbSync
LocalOverrides MongoDbSyncLocal `yaml:"local"`
TransferId string
TransferResourceOverride string
}

// Init related types and functions follow
Expand Down Expand Up @@ -94,17 +95,17 @@ func init() {
RegisterSyncer(MongoDbSyncPlugin{})
}

func (m MongoDbSyncRoot) IsInitialized() (bool, error) {
func (m *MongoDbSyncRoot) IsInitialized() (bool, error) {
return true, nil
}

// Sync related functions follow
func (root MongoDbSyncRoot) PrepareSyncer() (Syncer, error) {
func (root *MongoDbSyncRoot) PrepareSyncer() (Syncer, error) {
root.TransferId = strconv.FormatInt(time.Now().UnixNano(), 10)
return root, nil
}

func (root MongoDbSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
func (root *MongoDbSyncRoot) GetPrerequisiteCommand(environment Environment, command string) SyncCommand {
lagoonSyncBin, _ := utils.FindLagoonSyncOnEnv()

return SyncCommand{
Expand All @@ -116,7 +117,7 @@ func (root MongoDbSyncRoot) GetPrerequisiteCommand(environment Environment, comm
}
}

func (root MongoDbSyncRoot) GetRemoteCommand(sourceEnvironment Environment) []SyncCommand {
func (root *MongoDbSyncRoot) GetRemoteCommand(sourceEnvironment Environment) []SyncCommand {
m := root.Config

if sourceEnvironment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
Expand All @@ -136,7 +137,7 @@ func (root MongoDbSyncRoot) GetRemoteCommand(sourceEnvironment Environment) []Sy
}
}

func (m MongoDbSyncRoot) GetLocalCommand(targetEnvironment Environment) []SyncCommand {
func (m *MongoDbSyncRoot) GetLocalCommand(targetEnvironment Environment) []SyncCommand {
l := m.Config
if targetEnvironment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
l = m.getEffectiveLocalDetails()
Expand All @@ -153,28 +154,37 @@ func (m MongoDbSyncRoot) GetLocalCommand(targetEnvironment Environment) []SyncCo
}
}

func (m MongoDbSyncRoot) GetFilesToCleanup(environment Environment) []string {
func (m *MongoDbSyncRoot) GetFilesToCleanup(environment Environment) []string {
transferResource := m.GetTransferResource(environment)
return []string{
transferResource.Name,
}
}

func (m MongoDbSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
func (m *MongoDbSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
resourceName := fmt.Sprintf("%vlagoon_sync_mongodb_%v.bson", m.GetOutputDirectory(), m.TransferId)
if m.TransferResourceOverride != "" {
resourceName = m.TransferResourceOverride
}
return SyncerTransferResource{
Name: fmt.Sprintf("%vlagoon_sync_mongodb_%v.bson", m.GetOutputDirectory(), m.TransferId),
Name: resourceName,
IsDirectory: false}
}

func (root MongoDbSyncRoot) GetOutputDirectory() string {
func (m *MongoDbSyncRoot) SetTransferResource(transferResourceName string) error {
m.TransferResourceOverride = transferResourceName
return nil
}

func (root *MongoDbSyncRoot) GetOutputDirectory() string {
m := root.Config
if len(m.OutputDirectory) == 0 {
return "/tmp/"
}
return m.OutputDirectory
}

func (syncConfig MongoDbSyncRoot) getEffectiveLocalDetails() BaseMongoDbSync {
func (syncConfig *MongoDbSyncRoot) getEffectiveLocalDetails() BaseMongoDbSync {
returnDetails := BaseMongoDbSync{
DbHostname: syncConfig.Config.DbHostname,
DbPort: syncConfig.Config.DbPort,
Expand Down
Loading