Skip to content

Commit

Permalink
added handle for multiple instances for push, copy command
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Przybyl committed Sep 29, 2024
1 parent 86ea33e commit 70e5c70
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 64 deletions.
37 changes: 21 additions & 16 deletions cmd/aem/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *CLI) contentPushCmd() *cobra.Command {
Aliases: []string{"ps"},
Short: "Push content from JCR root directory or local file to running instance",
Run: func(cmd *cobra.Command, args []string) {
instance, err := c.aem.InstanceManager().One()
instances, err := c.aem.InstanceManager().Some()
if err != nil {
c.Error(err)
return
Expand All @@ -201,7 +201,7 @@ func (c *CLI) contentPushCmd() *cobra.Command {
excludePatterns := determineExcludePatterns(cmd)
clean, _ := cmd.Flags().GetBool("clean")
vault, _ := cmd.Flags().GetBool("vault")
if err = c.aem.ContentManager().Push(instance, path, clean, vault, pkg.PackageCreateOpts{
if err = c.aem.ContentManager().Push(instances, path, clean, vault, pkg.PackageCreateOpts{
PID: fmt.Sprintf("aemc:content-push:%s-SNAPSHOT", timex.FileTimestampForNow()),
FilterRoots: filterRoots,
ExcludePatterns: excludePatterns,
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c *CLI) contentCopyCmd() *cobra.Command {
c.Error(err)
return
}
targetInstance, err := determineContentTargetInstance(cmd, c.aem.InstanceManager())
targetInstances, err := determineContentTargetInstances(cmd, c.aem.InstanceManager())
if err != nil {
c.Error(err)
return
Expand All @@ -248,7 +248,7 @@ func (c *CLI) contentCopyCmd() *cobra.Command {
clean, _ := cmd.Flags().GetBool("clean")
vault, _ := cmd.Flags().GetBool("vault")
rcpArgs, _ := cmd.Flags().GetString("rcp-args")
if err = c.aem.ContentManager().Copy(instance, targetInstance, clean, vault, rcpArgs, pkg.PackageCreateOpts{
if err = c.aem.ContentManager().Copy(instance, targetInstances, clean, vault, rcpArgs, pkg.PackageCreateOpts{
PID: fmt.Sprintf("aemc:content-copy:%s-SNAPSHOT", timex.FileTimestampForNow()),
FilterRoots: filterRoots,
FilterFile: filterFile,
Expand All @@ -259,8 +259,8 @@ func (c *CLI) contentCopyCmd() *cobra.Command {
c.Changed("content copied")
},
}
cmd.Flags().StringP("instance-target-url", "u", "", "Destination instance URL")
cmd.Flags().StringP("instance-target-id", "i", "", "Destination instance ID")
cmd.Flags().StringSliceP("instance-target-url", "u", []string{}, "Destination instance URL")
cmd.Flags().StringSliceP("instance-target-id", "i", []string{}, "Destination instance ID")
cmd.MarkFlagsOneRequired("instance-target-url", "instance-target-id")
cmd.Flags().StringSliceP("filter-roots", "r", []string{}, "Vault filter root paths")
cmd.Flags().StringP("filter-file", "f", "", "Vault filter file path")
Expand All @@ -271,20 +271,25 @@ func (c *CLI) contentCopyCmd() *cobra.Command {
return cmd
}

func determineContentTargetInstance(cmd *cobra.Command, instanceManager *pkg.InstanceManager) (*pkg.Instance, error) {
var instance *pkg.Instance
url, _ := cmd.Flags().GetString("instance-target-url")
if url != "" {
instance, _ = instanceManager.NewByIDAndURL("remote_adhoc_target", url)
func determineContentTargetInstances(cmd *cobra.Command, instanceManager *pkg.InstanceManager) ([]pkg.Instance, error) {
var instances []pkg.Instance
urls, _ := cmd.Flags().GetStringSlice("instance-target-url")
for _, url := range urls {
instance, err := instanceManager.NewByIDAndURL("remote_adhoc_target", url)
if err != nil {
return nil, err
}
instances = append(instances, *instance)
}
id, _ := cmd.Flags().GetString("instance-target-id")
if id != "" {
instance = instanceManager.NewByID(id)
ids, _ := cmd.Flags().GetStringSlice("instance-target-id")
for _, id := range ids {
instance := instanceManager.NewByID(id)
instances = append(instances, *instance)
}
if instance == nil {
if instances == nil {
return nil, fmt.Errorf("missing 'instance-target-url' or 'instance-target-id'")
}
return instance, nil
return instances, nil
}

func determineContentDir(cmd *cobra.Command) (string, error) {
Expand Down
98 changes: 50 additions & 48 deletions pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,33 @@ func (cm *ContentManager) PullFile(instance *Instance, file string, clean bool,
return nil
}

func (cm *ContentManager) pushContent(instance *Instance, vault bool, opts PackageCreateOpts) error {
if vault {
mainDir, _, _ := strings.Cut(opts.ContentPath, content.JCRRoot)
jcrPath := DetermineFilterRoot(opts.ContentPath)
if err := cm.vaultCli.PushContent(instance, mainDir, jcrPath); err != nil {
return err
}
} else {
remotePath, err := instance.PackageManager().Create(opts)
defer func() { _ = instance.PackageManager().Delete(remotePath) }()
if err != nil {
return err
}
if err = instance.PackageManager().Install(remotePath); err != nil {
return err
func (cm *ContentManager) pushContent(instances []Instance, vault bool, opts PackageCreateOpts) error {
_, err := InstanceProcess(cm.aem, instances, func(instance Instance) (any, error) {
if vault {
mainDir, _, _ := strings.Cut(opts.ContentPath, content.JCRRoot)
jcrPath := DetermineFilterRoot(opts.ContentPath)
if err := cm.vaultCli.PushContent(&instance, mainDir, jcrPath); err != nil {
return nil, err
}
} else {
remotePath, err := instance.PackageManager().Create(opts)
defer func() { _ = instance.PackageManager().Delete(remotePath) }()
if err != nil {
return nil, err
}
if err = instance.PackageManager().Install(remotePath); err != nil {
return nil, err
}
}
return nil, nil
})
if err != nil {
return err
}
return nil
}

func (cm *ContentManager) Push(instance *Instance, path string, clean bool, vault bool, opts PackageCreateOpts) error {
func (cm *ContentManager) Push(instances []Instance, path string, clean bool, vault bool, opts PackageCreateOpts) error {
if !pathx.Exists(path) {
return fmt.Errorf("cannot push content as it does not exist '%s'", path)
}
Expand All @@ -185,46 +191,36 @@ func (cm *ContentManager) Push(instance *Instance, path string, clean bool, vaul
}
opts.ContentPath = filepath.Join(workDir, content.JCRRoot)
}
if err := cm.pushContent(instance, vault, opts); err != nil {
if err := cm.pushContent(instances, vault, opts); err != nil {
return err
}
return nil
}

func (cm *ContentManager) copyByPkgMgr(srcInstance *Instance, destInstance *Instance, clean bool, opts PackageCreateOpts) error {
func (cm *ContentManager) copyByPkgMgr(srcInstance *Instance, destInstances []Instance, clean bool, opts PackageCreateOpts) error {
pkgFile := pathx.RandomFileName(cm.tmpDir(), "content_copy", ".zip")
defer func() { _ = pathx.DeleteIfExists(pkgFile) }()
if clean {
workDir := pathx.RandomDir(cm.tmpDir(), "content_copy")
defer func() { _ = pathx.DeleteIfExists(workDir) }()
if err := cm.pullContent(srcInstance, workDir, false, opts); err != nil {
return err
}
if clean {
if err := cm.contentManager.Clean(filepath.Join(workDir, content.JCRRoot)); err != nil {
return err
}
if err := cm.Download(srcInstance, pkgFile, clean, false, opts); err != nil {
return err
}
_, err := InstanceProcess(cm.aem, destInstances, func(destInstance Instance) (any, error) {
remotePath, err := destInstance.PackageManager().Upload(pkgFile)
defer func() { _ = destInstance.PackageManager().Delete(remotePath) }()
if err != nil {
return nil, err
}
if err := content.Zip(workDir, pkgFile); err != nil {
return err
if err = destInstance.PackageManager().Install(remotePath); err != nil {
return nil, err
}
} else {
if err := cm.downloadByPkgMgr(srcInstance, pkgFile, opts); err != nil {
return err
}
}
remotePath, err := destInstance.PackageManager().Upload(pkgFile)
defer func() { _ = destInstance.PackageManager().Delete(remotePath) }()
return nil, err
})
if err != nil {
return err
}
if err = destInstance.PackageManager().Install(remotePath); err != nil {
return err
}
return nil
}

func (cm *ContentManager) copyByVaultCli(srcInstance *Instance, destInstance *Instance, clean bool, rcpArgs string, opts PackageCreateOpts) error {
func (cm *ContentManager) copyByVaultCli(srcInstance *Instance, destInstances []Instance, clean bool, rcpArgs string, opts PackageCreateOpts) error {
if clean || opts.FilterFile != "" {
workDir := pathx.RandomDir(cm.tmpDir(), "content_copy")
defer func() { _ = pathx.DeleteIfExists(workDir) }()
Expand All @@ -237,27 +233,33 @@ func (cm *ContentManager) copyByVaultCli(srcInstance *Instance, destInstance *In
}
}
opts.ContentPath = filepath.Join(workDir, content.JCRRoot)
if err := cm.pushContent(destInstance, true, opts); err != nil {
if err := cm.pushContent(destInstances, true, opts); err != nil {
return err
}
} else {
if rcpArgs == "" {
rcpArgs = "-b 100 -r -u"
}
for _, filterRoot := range opts.FilterRoots {
if err := cm.vaultCli.CopyContent(srcInstance, destInstance, strings.Fields(rcpArgs), filterRoot); err != nil {
return err
_, err := InstanceProcess(cm.aem, destInstances, func(destInstance Instance) (any, error) {
for _, filterRoot := range opts.FilterRoots {
if err := cm.vaultCli.CopyContent(srcInstance, &destInstance, strings.Fields(rcpArgs), filterRoot); err != nil {
return nil, err
}
}
return nil, nil
})
if err != nil {
return err
}
}
return nil
}

func (cm *ContentManager) Copy(srcInstance *Instance, destInstance *Instance, clean bool, vault bool, rcpArgs string, opts PackageCreateOpts) error {
func (cm *ContentManager) Copy(srcInstance *Instance, destInstances []Instance, clean bool, vault bool, rcpArgs string, opts PackageCreateOpts) error {
if vault {
return cm.copyByVaultCli(srcInstance, destInstance, clean, rcpArgs, opts)
return cm.copyByVaultCli(srcInstance, destInstances, clean, rcpArgs, opts)
}
return cm.copyByPkgMgr(srcInstance, destInstance, clean, opts)
return cm.copyByPkgMgr(srcInstance, destInstances, clean, opts)
}

func DetermineSyncFile(file string) string {
Expand Down

0 comments on commit 70e5c70

Please sign in to comment.