diff --git a/components/command/osCommand.go b/components/command/osCommand.go index 2165eec5e..7b0983289 100644 --- a/components/command/osCommand.go +++ b/components/command/osCommand.go @@ -30,6 +30,8 @@ type OSCommand interface { IsPathAbsolute(path string) bool NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) + copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) + copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) } // ------------------------------ diff --git a/components/command/runner.go b/components/command/runner.go index 63529e9bb..8ff4832d2 100644 --- a/components/command/runner.go +++ b/components/command/runner.go @@ -2,7 +2,6 @@ package command import ( "fmt" - "path/filepath" "github.com/pulumi/pulumi-command/sdk/go/command/local" "github.com/pulumi/pulumi-command/sdk/go/command/remote" @@ -111,8 +110,6 @@ type Runner interface { Command(name string, args *Args, opts ...pulumi.ResourceOption) (Command, error) newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) - copyWindowsFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) - copyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) } var _ Runner = &RemoteRunner{} @@ -202,7 +199,7 @@ func (r *RemoteRunner) Command(name string, args *Args, opts ...pulumi.ResourceO } func (r *RemoteRunner) newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - return r.osCommand.NewCopyFile(r, name, localPath, remotePath, opts...) + return r.osCommand.copyRemoteFile(r, name, localPath, remotePath, opts...) } func (r *RemoteRunner) PulumiOptions() []pulumi.ResourceOption { @@ -267,70 +264,9 @@ func (r *LocalRunner) Command(name string, args *Args, opts ...pulumi.ResourceOp } func (r *LocalRunner) newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - return r.osCommand.NewCopyFile(r, name, localPath, remotePath, opts...) + return r.osCommand.copyLocalFile(r, name, localPath, remotePath, opts...) } func (r *LocalRunner) PulumiOptions() []pulumi.ResourceOption { return []pulumi.ResourceOption{} } - -func (r *LocalRunner) copyWindowsFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - createCmd := pulumi.Sprintf("Copy-Item -Path '%v' -Destination '%v'", src, dst) - deleteCmd := pulumi.Sprintf("Remove-Item -Path '%v'", dst) - useSudo := false - - return r.Command(name, - &Args{ - Create: createCmd, - Delete: deleteCmd, - Sudo: useSudo, - Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)}, - }, opts...) -} - -func (r *LocalRunner) copyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - createCmd := pulumi.Sprintf("cp '%v' '%v'", src, dst) - deleteCmd := pulumi.Sprintf("rm '%v'", dst) - useSudo := false - - return r.Command(name, - &Args{ - Create: createCmd, - Delete: deleteCmd, - Sudo: useSudo, - Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)}, - }, opts...) -} - -func (r *RemoteRunner) copyWindowsFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - return remote.NewCopyFile(r.Environment().Ctx(), r.Namer().ResourceName("copy", name), &remote.CopyFileArgs{ - Connection: r.Config().connection, - LocalPath: src, - RemotePath: dst, - Triggers: pulumi.Array{src, dst}, - }, utils.MergeOptions(r.PulumiOptions(), opts...)...) -} - -func (r *RemoteRunner) copyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - tempRemotePath := src.ToStringOutput().ApplyT(func(path string) string { - return filepath.Join(r.OsCommand().GetTemporaryDirectory(), filepath.Base(path)) - }).(pulumi.StringOutput) - - tempCopyFile, err := remote.NewCopyFile(r.Environment().Ctx(), r.Namer().ResourceName("copy", name), &remote.CopyFileArgs{ - Connection: r.Config().connection, - LocalPath: src, - RemotePath: tempRemotePath, - Triggers: pulumi.Array{src, tempRemotePath}, - }, utils.MergeOptions(r.PulumiOptions(), opts...)...) - - if err != nil { - return nil, err - } - - moveCommand, err := r.OsCommand().MoveFile(r, name, tempRemotePath, dst, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...) - if err != nil { - return nil, err - } - - return moveCommand, err -} diff --git a/components/command/unixOSCommand.go b/components/command/unixOSCommand.go index 8698fb190..9514aeb41 100644 --- a/components/command/unixOSCommand.go +++ b/components/command/unixOSCommand.go @@ -2,11 +2,13 @@ package command import ( "fmt" + "path/filepath" "strings" "github.com/DataDog/test-infra-definitions/common/utils" "github.com/alessio/shellescape" + "github.com/pulumi/pulumi-command/sdk/go/command/remote" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" ) @@ -71,7 +73,7 @@ func (fs unixOSCommand) IsPathAbsolute(path string) bool { } func (fs unixOSCommand) NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - return runner.copyUnixFile(name, localPath, remotePath, opts...) + return runner.newCopyFile(name, localPath, remotePath, opts...) } func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool, user string) pulumi.StringInput { @@ -102,3 +104,41 @@ func (fs unixOSCommand) MoveFile(runner Runner, name string, source, destination deleteCommand := pulumi.Sprintf(`bash -c 'if [ -f "%v" ]; then mv -f "%v" "%v"; else rm -f "%v"; fi'`, backupPath, backupPath, destination, destination) return copyRemoteFile(runner, fmt.Sprintf("move-file-%s", name), createCommand, deleteCommand, sudo, utils.MergeOptions(opts, pulumi.ReplaceOnChanges([]string{"*"}), pulumi.DeleteBeforeReplace(true))...) } + +func (fs unixOSCommand) copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { + createCmd := pulumi.Sprintf("cp '%v' '%v'", src, dst) + deleteCmd := pulumi.Sprintf("rm '%v'", dst) + useSudo := false + + return runner.Command(name, + &Args{ + Create: createCmd, + Delete: deleteCmd, + Sudo: useSudo, + Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)}, + }, opts...) +} + +func (fs unixOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { + tempRemotePath := src.ToStringOutput().ApplyT(func(path string) string { + return filepath.Join(runner.OsCommand().GetTemporaryDirectory(), filepath.Base(path)) + }).(pulumi.StringOutput) + + tempCopyFile, err := remote.NewCopyFile(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyFileArgs{ + Connection: runner.Config().connection, + LocalPath: src, + RemotePath: tempRemotePath, + Triggers: pulumi.Array{src, tempRemotePath}, + }, utils.MergeOptions(runner.PulumiOptions(), opts...)...) + + if err != nil { + return nil, err + } + + moveCommand, err := runner.OsCommand().MoveFile(runner, name, tempRemotePath, dst, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...) + if err != nil { + return nil, err + } + + return moveCommand, err +} diff --git a/components/command/windowsOSCommand.go b/components/command/windowsOSCommand.go index 2cf9bf5f4..440cdbaf5 100644 --- a/components/command/windowsOSCommand.go +++ b/components/command/windowsOSCommand.go @@ -6,6 +6,7 @@ import ( "github.com/DataDog/test-infra-definitions/common/utils" + "github.com/pulumi/pulumi-command/sdk/go/command/remote" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" ) @@ -78,7 +79,7 @@ func (fs windowsOSCommand) IsPathAbsolute(path string) bool { } func (fs windowsOSCommand) NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { - return runner.copyWindowsFile(name, localPath, remotePath, opts...) + return runner.newCopyFile(name, localPath, remotePath, opts...) } func (fs windowsOSCommand) MoveFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) { @@ -88,3 +89,26 @@ func (fs windowsOSCommand) MoveFile(runner Runner, name string, source, destinat deleteCommand := pulumi.Sprintf(`if (Test-Path '%v') { Move-Item -Force -Path '%v' -Destination '%v' } else { Remove-Item -Force -Path %v }`, backupPath, backupPath, destination, destination) return copyRemoteFile(runner, fmt.Sprintf("move-file-%s", name), createCommand, deleteCommand, sudo, utils.MergeOptions(opts, pulumi.ReplaceOnChanges([]string{"*"}), pulumi.DeleteBeforeReplace(true))...) } + +func (fs windowsOSCommand) copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { + createCmd := pulumi.Sprintf("Copy-Item -Path '%v' -Destination '%v'", src, dst) + deleteCmd := pulumi.Sprintf("Remove-Item -Path '%v'", dst) + useSudo := false + + return runner.Command(name, + &Args{ + Create: createCmd, + Delete: deleteCmd, + Sudo: useSudo, + Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)}, + }, opts...) +} + +func (fs windowsOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { + return remote.NewCopyFile(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyFileArgs{ + Connection: runner.Config().connection, + LocalPath: src, + RemotePath: dst, + Triggers: pulumi.Array{src, dst}, + }, utils.MergeOptions(runner.PulumiOptions(), opts...)...) +}