Skip to content

Commit 32b5503

Browse files
Do not use CopyToRemote everywhere (#1467)
1 parent 6a66b3c commit 32b5503

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

components/command/filemanager.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,21 @@ func (fm *FileManager) CopyFile(name string, localPath, remotePath pulumi.String
6868
return fm.runner.newCopyFile(name, localPath, remotePath, opts...)
6969
}
7070

71+
// CopyToRemoteFile copies a local file to a remote file. Under the hood it uses remote.CopyToRemote, so localPath will be converted to a File Asset, it breaks if the local path is not known at plan time.
72+
// Ideally it should replace CopyFile but it is not possible due to the limitation of CopyToRemote for now.
73+
func (fm *FileManager) CopyToRemoteFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
74+
return fm.command.NewCopyToRemoteFile(fm.runner, name, localPath, remotePath, opts...)
75+
}
76+
7177
func (fm *FileManager) CopyInlineFile(fileContent pulumi.StringInput, remotePath string, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
7278
// Write the content into a temporary file and get the path
73-
tempFile, err := os.CreateTemp("", filepath.Base(remotePath))
74-
if err != nil {
75-
return nil, err
76-
}
7779

78-
_ = fileContent.ToStringOutput().ApplyT(func(content string) (string, error) {
80+
localTempPath := fileContent.ToStringOutput().ApplyT(func(content string) (string, error) {
81+
tempFile, err := os.CreateTemp("", filepath.Base(remotePath))
82+
if err != nil {
83+
return "", err
84+
}
85+
7986
if err != nil {
8087
return "", err
8188
}
@@ -89,7 +96,7 @@ func (fm *FileManager) CopyInlineFile(fileContent pulumi.StringInput, remotePath
8996
return tempFilePath, nil
9097
}).(pulumi.StringInput)
9198

92-
return fm.CopyFile(remotePath, pulumi.String(tempFile.Name()), pulumi.String(remotePath), opts...)
99+
return fm.CopyFile(remotePath, localTempPath, pulumi.String(remotePath), opts...)
93100
}
94101

95102
// CopyRelativeFolder copies recursively a relative folder to a remote folder.

components/command/osCommand.go

+3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ type OSCommand interface {
3131
PathJoin(parts ...string) string
3232

3333
NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
34+
NewCopyToRemoteFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
3435
copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
3536
copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
37+
// copyToRemoteFileV2 rely on CopyToRemote to copy files to remote, which uses a File asset instead of a Pulumi.StringInput with the path. It breaks when the path is not determined at runtime.
38+
copyRemoteFileV2(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
3639
}
3740

3841
// ------------------------------

components/command/runner.go

+9
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ type Runner interface {
141141
Command(name string, args RunnerCommandArgs, opts ...pulumi.ResourceOption) (Command, error)
142142

143143
newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
144+
newCopyToRemoteFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
144145
}
145146

146147
var _ Runner = &RemoteRunner{}
@@ -233,6 +234,10 @@ func (r *RemoteRunner) newCopyFile(name string, localPath, remotePath pulumi.Str
233234
return r.osCommand.copyRemoteFile(r, name, localPath, remotePath, opts...)
234235
}
235236

237+
func (r *RemoteRunner) newCopyToRemoteFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
238+
return r.osCommand.copyRemoteFileV2(r, name, localPath, remotePath, opts...)
239+
}
240+
236241
func (r *RemoteRunner) PulumiOptions() []pulumi.ResourceOption {
237242
return r.options
238243
}
@@ -298,6 +303,10 @@ func (r *LocalRunner) newCopyFile(name string, localPath, remotePath pulumi.Stri
298303
return r.osCommand.copyLocalFile(r, name, localPath, remotePath, opts...)
299304
}
300305

306+
func (r *LocalRunner) newCopyToRemoteFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
307+
return r.newCopyFile(name, localPath, remotePath, opts...)
308+
}
309+
301310
func (r *LocalRunner) PulumiOptions() []pulumi.ResourceOption {
302311
return []pulumi.ResourceOption{}
303312
}

components/command/unixOSCommand.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func (fs unixOSCommand) NewCopyFile(runner Runner, name string, localPath, remot
8080
return runner.newCopyFile(name, localPath, remotePath, opts...)
8181
}
8282

83+
func (fs unixOSCommand) NewCopyToRemoteFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
84+
return runner.newCopyToRemoteFile(name, localPath, remotePath, opts...)
85+
}
86+
8387
func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool, user string) pulumi.StringInput {
8488
if command == nil {
8589
return nil
@@ -128,12 +132,9 @@ func (fs unixOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, d
128132
return fs.PathJoin(runner.OsCommand().GetTemporaryDirectory(), filepath.Base(path))
129133
}).(pulumi.StringOutput)
130134

131-
srcAsset := src.ToStringOutput().ApplyT(func(path string) pulumi.AssetOrArchive {
132-
return pulumi.NewFileAsset(path)
133-
}).(pulumi.AssetOrArchiveOutput)
134-
tempCopyFile, err := remote.NewCopyToRemote(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyToRemoteArgs{
135+
tempCopyFile, err := remote.NewCopyFile(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyFileArgs{
135136
Connection: runner.Config().connection,
136-
Source: srcAsset,
137+
LocalPath: src,
137138
RemotePath: tempRemotePath,
138139
Triggers: pulumi.Array{src, tempRemotePath},
139140
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)
@@ -149,3 +150,16 @@ func (fs unixOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, d
149150

150151
return moveCommand, err
151152
}
153+
154+
func (fs unixOSCommand) copyRemoteFileV2(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
155+
srcAsset := src.ToStringOutput().ApplyT(func(path string) pulumi.AssetOrArchive {
156+
return pulumi.NewFileAsset(path)
157+
}).(pulumi.AssetOrArchiveOutput)
158+
159+
return remote.NewCopyToRemote(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyToRemoteArgs{
160+
Connection: runner.Config().connection,
161+
Source: srcAsset,
162+
RemotePath: dst,
163+
Triggers: pulumi.Array{src, dst},
164+
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)
165+
}

components/command/windowsOSCommand.go

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func (fs windowsOSCommand) NewCopyFile(runner Runner, name string, localPath, re
8686
return runner.newCopyFile(name, localPath, remotePath, opts...)
8787
}
8888

89+
func (fs windowsOSCommand) NewCopyToRemoteFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
90+
return runner.newCopyToRemoteFile(name, localPath, remotePath, opts...)
91+
}
92+
8993
func (fs windowsOSCommand) MoveFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
9094
backupPath := pulumi.Sprintf("%v.%s", destination, backupExtension)
9195
copyCommand := pulumi.Sprintf(`Copy-Item -Path '%v' -Destination '%v'`, source, destination)
@@ -109,6 +113,15 @@ func (fs windowsOSCommand) copyLocalFile(runner *LocalRunner, name string, src,
109113
}
110114

111115
func (fs windowsOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
116+
return remote.NewCopyFile(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyFileArgs{
117+
Connection: runner.Config().connection,
118+
LocalPath: src,
119+
RemotePath: dst,
120+
Triggers: pulumi.Array{src, dst},
121+
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)
122+
}
123+
124+
func (fs windowsOSCommand) copyRemoteFileV2(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
112125
srcAsset := src.ToStringOutput().ApplyT(func(path string) pulumi.AssetOrArchive {
113126
return pulumi.NewFileAsset(path)
114127
}).(pulumi.AssetOrArchiveOutput)

components/datadog/agent/host.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (h *HostAgent) directInstallInstallation(env config.Env, params *agentparam
132132
}
133133
packageToInstall := matches[0]
134134
env.Ctx().Log.Info(fmt.Sprintf("Found local package to install %s", packageToInstall), nil)
135-
uploadCmd, err := h.Host.OS.FileManager().CopyFile("copy-agent-package", pulumi.String(packagePath), pulumi.String("./"), baseOpts...)
135+
uploadCmd, err := h.Host.OS.FileManager().CopyToRemoteFile("copy-agent-package", pulumi.String(packagePath), pulumi.String("./"), baseOpts...)
136136
if err != nil {
137137
return nil, err
138138
}

0 commit comments

Comments
 (0)