Skip to content

Commit

Permalink
[refactor-runner-adxt-783] wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR committed Dec 13, 2024
1 parent 2b64d46 commit ab0b21a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 25 deletions.
1 change: 1 addition & 0 deletions components/command/osCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type OSCommand interface {
remotePath pulumi.StringInput,
useSudo bool,
opts ...pulumi.ResourceOption) (Command, error)
MoveRemoteFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error)

BuildCommandString(
command pulumi.StringInput,
Expand Down
64 changes: 64 additions & 0 deletions components/command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"path/filepath"

"github.com/pulumi/pulumi-command/sdk/go/command/local"
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
Expand Down Expand Up @@ -95,6 +96,8 @@ 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)
PulumiOptions() []pulumi.ResourceOption
}

Expand Down Expand Up @@ -246,3 +249,64 @@ func (r *LocalRunner) NewCopyFile(name string, localPath, remotePath pulumi.Stri
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) {
create := pulumi.Sprintf("Copy-Item -Path '%v' -Destination '%v'", src, dst)
delete := pulumi.Sprintf("Remove-Item -Path '%v'", dst)

Check failure on line 255 in components/command/runner.go

View workflow job for this annotation

GitHub Actions / lint-go

redefines-builtin-id: redefinition of the built-in function delete (revive)
useSudo := false // TODO A

return r.Command(name,
&Args{
Create: create,
Delete: delete,
Sudo: useSudo,
Triggers: pulumi.Array{create, delete, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func (r *LocalRunner) CopyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
create := pulumi.Sprintf("cp '%v' '%v'", src, dst)
delete := pulumi.Sprintf("rm '%v'", dst)

Check failure on line 269 in components/command/runner.go

View workflow job for this annotation

GitHub Actions / lint-go

redefines-builtin-id: redefinition of the built-in function delete (revive)
useSudo := false // TODO A

return r.Command(name,
&Args{
Create: create,
Delete: delete,
Sudo: useSudo,
Triggers: pulumi.Array{create, delete, 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().MoveRemoteFile(r, name, tempRemotePath, dst, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...)
if err != nil {
return nil, err
}

return moveCommand, err
}
47 changes: 23 additions & 24 deletions components/command/unixOSCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ 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"
)

Expand Down Expand Up @@ -73,27 +71,28 @@ 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) {
tempRemotePath := localPath.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: localPath,
RemotePath: tempRemotePath,
Triggers: pulumi.Array{localPath, tempRemotePath},
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)

if err != nil {
return nil, err
}

moveCommand, err := fs.moveRemoteFile(runner, name, tempRemotePath, remotePath, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...)
if err != nil {
return nil, err
}

return moveCommand, err
return runner.CopyUnixFile(name, localPath, remotePath, opts...)
// tempRemotePath := localPath.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: localPath,
// RemotePath: tempRemotePath,
// Triggers: pulumi.Array{localPath, tempRemotePath},
// }, utils.MergeOptions(runner.PulumiOptions(), opts...)...)

// if err != nil {
// return nil, err
// }

// moveCommand, err := fs.MoveRemoteFile(runner, name, tempRemotePath, remotePath, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...)
// if err != nil {
// return nil, err
// }

// return moveCommand, err
}

func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool, user string) pulumi.StringInput {
Expand All @@ -117,7 +116,7 @@ func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool,
return formattedCommand
}

func (fs unixOSCommand) moveRemoteFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
func (fs unixOSCommand) MoveRemoteFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
backupPath := pulumi.Sprintf("%v.%s", destination, backupExtension)
copyCommand := pulumi.Sprintf(`cp '%v' '%v'`, source, destination)
createCommand := pulumi.Sprintf(`bash -c 'if [ -f '%v' ]; then mv -f '%v' '%v'; fi; %v'`, destination, destination, backupPath, copyCommand)
Expand Down
8 changes: 8 additions & 0 deletions components/command/windowsOSCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ func (fs windowsOSCommand) NewCopyFile(runner Runner, name string, localPath, re
Triggers: pulumi.Array{localPath, remotePath},
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)
}

func (fs windowsOSCommand) MoveRemoteFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
backupPath := pulumi.Sprintf("%v.%s", destination, backupExtension)
copyCommand := pulumi.Sprintf(`Copy-Item -Path %v -Destination %v`, source, destination)
createCommand := pulumi.Sprintf(`if (Test-Path %v) { Move-Item -Force -Path %v -Destination %v }; %v`, destination, destination, backupPath, copyCommand)
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))...)
}
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/DataDog/test-infra-definitions/registry"

"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)
Expand All @@ -20,6 +19,16 @@ const (

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// TODO A
// e, _ := local.NewEnvironment(ctx)
// runner := command.NewLocalRunner(&e, command.LocalRunnerArgs{OSCommand: command.NewUnixOSCommand()})
// // _, err := runner.NewCopyFile("copy-hey-ho", pulumi.String("/tmp/hey"), pulumi.String("/tmp/ho"))
// fm := command.NewFileManager(runner)
// fm.CreateDirectory("/tmp/hmm", false)
// _, err := fm.CopyFile("copy-file", pulumi.String("/tmp/hey"), pulumi.String("/tmp/ho"))

// return err

scenarioName := os.Getenv(scenarioEnvVarName)
rootConfig := config.New(ctx, "")
if s := rootConfig.Get(scenarioParamName); s != "" {
Expand Down
19 changes: 19 additions & 0 deletions resources/local/podman/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ var dockerfileContent string
var customDockerConfig = "{}"

func NewInstance(e resourceslocal.Environment, args VMArgs, opts ...pulumi.ResourceOption) (address pulumi.StringOutput, user string, port int, err error) {
// TODO A: Unix command / windows
// runner := command.NewLocalRunner(&e, command.LocalRunnerArgs{OSCommand: command.NewUnixOSCommand()})
// fileManager := command.NewFileManager(runner)

// // runner.CopyUnixFile("copy-hey-ho", pulumi.String("/tmp/hey"), pulumi.String("/tmp/ho"))

// runner.Command("hey-ho", &command.Args{
// Create: pulumi.String("cp /tmp/hey /tmp/ho"),
// Delete: pulumi.String("rm /tmp/ho"),
// })

// println("END")

// return pulumi.StringOutput{}, "", -1, fmt.Errorf("not implemented")

interpreter := []string{"/bin/bash", "-c"}
if runtime.GOOS == "windows" {
interpreter = []string{"powershell", "-Command"}
Expand All @@ -41,14 +56,18 @@ func NewInstance(e resourceslocal.Environment, args VMArgs, opts ...pulumi.Resou
// TODO clean up the folder on stack destroy
// Requires a Runner refactor to reuse crossplatform commands
err = os.MkdirAll(dataPath, 0700)
// _, err = fileManager.CreateDirectory(dataPath, false)
if err != nil {
return pulumi.StringOutput{}, "", -1, err
}
println("DIR")
dockerfilePath := path.Join(dataPath, "Dockerfile")
// _, err = fileManager.CopyInlineFile(pulumi.String(dockerfileContent), dockerfilePath)
err = os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0600)
if err != nil {
return pulumi.StringOutput{}, "", -1, err
}
println("COPY")

// Use a config to avoid docker hooks that can call vault or other services (credHelpers)
err = os.WriteFile(path.Join(dataPath, "config.json"), []byte(customDockerConfig), 0600)
Expand Down

0 comments on commit ab0b21a

Please sign in to comment.