Skip to content

Commit

Permalink
[ADXT-783] Made Runner interface for Remote and Local runners (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR authored Dec 26, 2024
1 parent 36860d3 commit 1a62db6
Show file tree
Hide file tree
Showing 34 changed files with 373 additions and 288 deletions.
5 changes: 2 additions & 3 deletions components/activedirectory/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package activedirectory
import (
"github.com/DataDog/test-infra-definitions/common/utils"
"github.com/DataDog/test-infra-definitions/components/command"
pulumiRemote "github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumiverse/pulumi-time/sdk/go/time"
)
Expand Down Expand Up @@ -49,7 +48,7 @@ func WithDomain(domainFqdn, domainAdmin, domainAdminPassword string) Option {
}

func (adCtx *activeDirectoryContext) joinActiveDirectoryDomain(params *JoinDomainConfiguration) error {
var joinCmd *pulumiRemote.Command
var joinCmd command.Command
joinCmd, err := adCtx.comp.host.OS.Runner().Command(adCtx.comp.namer.ResourceName("join-domain"), &command.Args{
Create: pulumi.Sprintf(`
Add-Computer -DomainName %s -Credential (New-Object System.Management.Automation.PSCredential -ArgumentList %s, %s)
Expand Down Expand Up @@ -90,7 +89,7 @@ func WithDomainController(domainFqdn, adminPassword string) func(*Configuration)
}

func (adCtx *activeDirectoryContext) installDomainController(params *DomainControllerConfiguration) error {
var installCmd *pulumiRemote.Command
var installCmd command.Command
installCmd, err := adCtx.comp.host.OS.Runner().Command(adCtx.comp.namer.ResourceName("install-forest"), &command.Args{
Create: pulumi.Sprintf(`
Add-WindowsFeature -name ad-domain-services -IncludeManagementTools;
Expand Down
19 changes: 9 additions & 10 deletions components/command/filemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import (
"runtime"
"strings"

"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

type FileManager struct {
runner *Runner
runner Runner
command OSCommand
}

func NewFileManager(runner *Runner) *FileManager {
func NewFileManager(runner Runner) *FileManager {
return &FileManager{
runner: runner,
command: runner.osCommand,
command: runner.OsCommand(),
}
}

Expand All @@ -31,26 +30,26 @@ func (fm *FileManager) IsPathAbsolute(path string) bool {
}

// CreateDirectoryFromPulumiString if it does not exist from directory name as a Pulumi String
func (fm *FileManager) CreateDirectoryFromPulumiString(name string, remotePath pulumi.String, useSudo bool, opts ...pulumi.ResourceOption) (*remote.Command, error) {
func (fm *FileManager) CreateDirectoryFromPulumiString(name string, remotePath pulumi.String, useSudo bool, opts ...pulumi.ResourceOption) (Command, error) {
return fm.command.CreateDirectory(fm.runner, name, remotePath, useSudo, opts...)
}

// CreateDirectoryForFile if the directory does not exist
// To avoid pulumi.URN collisions if multiple files use the same directory, use the full filePath as URN and path.Split out the folderPath for creation
func (fm *FileManager) CreateDirectoryForFile(remotePath string, useSudo bool, opts ...pulumi.ResourceOption) (*remote.Command, error) {
func (fm *FileManager) CreateDirectoryForFile(remotePath string, useSudo bool, opts ...pulumi.ResourceOption) (Command, error) {
// if given just a directory path, path.Split returns "" as file
// eg. path.Split("/a/b/c/") -> "/a/b/c/", ""
folderPath, _ := path.Split(remotePath)
return fm.command.CreateDirectory(fm.runner, "create-directory-"+remotePath, pulumi.String(folderPath), useSudo, opts...)
}

// CreateDirectory if it does not exist
func (fm *FileManager) CreateDirectory(remotePath string, useSudo bool, opts ...pulumi.ResourceOption) (*remote.Command, error) {
func (fm *FileManager) CreateDirectory(remotePath string, useSudo bool, opts ...pulumi.ResourceOption) (Command, error) {
return fm.command.CreateDirectory(fm.runner, "create-directory-"+remotePath, pulumi.String(remotePath), useSudo, opts...)
}

// TempDirectory creates a temporary directory
func (fm *FileManager) TempDirectory(folderName string, opts ...pulumi.ResourceOption) (*remote.Command, string, error) {
func (fm *FileManager) TempDirectory(folderName string, opts ...pulumi.ResourceOption) (Command, string, error) {
tempDir := path.Join(fm.command.GetTemporaryDirectory(), folderName)
folderCmd, err := fm.CreateDirectory(tempDir, false, opts...)
return folderCmd, tempDir, err
Expand All @@ -59,14 +58,14 @@ func (fm *FileManager) TempDirectory(folderName string, opts ...pulumi.ResourceO
// HomeDirectory creates a directory in home directory, if it does not exist
// A home directory is a file system directory on a multi-user operating system containing files for a given user of the system.
// It does not require sudo, using sudo in home directory allows to change default ownership and it is discouraged.
func (fm *FileManager) HomeDirectory(folderName string, opts ...pulumi.ResourceOption) (*remote.Command, string, error) {
func (fm *FileManager) HomeDirectory(folderName string, opts ...pulumi.ResourceOption) (Command, string, error) {
homeDir := path.Join(fm.command.GetHomeDirectory(), folderName)
folderCmd, err := fm.CreateDirectory(homeDir, false, opts...)
return folderCmd, homeDir, err
}

func (fm *FileManager) CopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return fm.runner.NewCopyFile(name, localPath, remotePath, opts...)
return fm.runner.newCopyFile(name, localPath, remotePath, opts...)
}

func (fm *FileManager) CopyInlineFile(fileContent pulumi.StringInput, remotePath string, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
Expand Down
27 changes: 19 additions & 8 deletions components/command/osCommand.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package command

import (
"runtime"
"strings"

"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

Expand All @@ -13,11 +13,12 @@ type OSCommand interface {
GetHomeDirectory() string

CreateDirectory(
runner *Runner,
runner Runner,
resourceName string,
remotePath pulumi.StringInput,
useSudo bool,
opts ...pulumi.ResourceOption) (*remote.Command, error)
opts ...pulumi.ResourceOption) (Command, error)
MoveFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error)

BuildCommandString(
command pulumi.StringInput,
Expand All @@ -28,7 +29,9 @@ type OSCommand interface {

IsPathAbsolute(path string) bool

NewCopyFile(runner *Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
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)
}

// ------------------------------
Expand All @@ -38,13 +41,13 @@ type OSCommand interface {
const backupExtension = "pulumi.backup"

func createDirectory(
runner *Runner,
runner Runner,
name string,
createCmd string,
deleteCmd string,
useSudo bool,
opts ...pulumi.ResourceOption,
) (*remote.Command, error) {
) (Command, error) {
// If the folder was previously created, make sure to delete it before creating it.
opts = append(opts, pulumi.DeleteBeforeReplace(true))
return runner.Command(name,
Expand Down Expand Up @@ -73,13 +76,13 @@ func buildCommandString(
}

func copyRemoteFile(
runner *Runner,
runner Runner,
name string,
createCommand pulumi.StringInput,
deleteCommand pulumi.StringInput,
useSudo bool,
opts ...pulumi.ResourceOption,
) (*remote.Command, error) {
) (Command, error) {
return runner.Command(name,
&Args{
Create: createCommand,
Expand All @@ -88,3 +91,11 @@ func copyRemoteFile(
Triggers: pulumi.Array{createCommand, deleteCommand, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func NewLocalOSCommand() OSCommand {
if runtime.GOOS == "windows" {
return NewWindowsOSCommand()
}

return NewUnixOSCommand()
}
13 changes: 6 additions & 7 deletions components/command/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ import (

"github.com/DataDog/test-infra-definitions/common/namer"
"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"
)

type GenericPackageManager struct {
namer namer.Namer
updateDBCommand *remote.Command
runner *Runner
updateDBCommand Command
runner Runner
opts []pulumi.ResourceOption
installCmd string
updateCmd string
env pulumi.StringMap
}

func NewGenericPackageManager(
runner *Runner,
runner Runner,
name string,
installCmd string,
updateCmd string,
env pulumi.StringMap,
) *GenericPackageManager {
packageManager := &GenericPackageManager{
namer: namer.NewNamer(runner.e.Ctx(), name),
namer: namer.NewNamer(runner.Environment().Ctx(), name),
runner: runner,
installCmd: installCmd,
updateCmd: updateCmd,
Expand All @@ -37,7 +36,7 @@ func NewGenericPackageManager(
return packageManager
}

func (m *GenericPackageManager) Ensure(packageRef string, transform Transformer, checkBinary string, opts ...pulumi.ResourceOption) (*remote.Command, error) {
func (m *GenericPackageManager) Ensure(packageRef string, transform Transformer, checkBinary string, opts ...pulumi.ResourceOption) (Command, error) {
opts = append(opts, m.opts...)
if m.updateCmd != "" {
updateDB, err := m.updateDB(opts)
Expand Down Expand Up @@ -76,7 +75,7 @@ func (m *GenericPackageManager) Ensure(packageRef string, transform Transformer,
return cmd, nil
}

func (m *GenericPackageManager) updateDB(opts []pulumi.ResourceOption) (*remote.Command, error) {
func (m *GenericPackageManager) updateDB(opts []pulumi.ResourceOption) (Command, error) {
if m.updateDBCommand != nil {
return m.updateDBCommand, nil
}
Expand Down
Loading

0 comments on commit 1a62db6

Please sign in to comment.