-
Notifications
You must be signed in to change notification settings - Fork 2
/
unixOSCommand.go
106 lines (90 loc) · 3.11 KB
/
unixOSCommand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package command
import (
"fmt"
"strings"
"github.com/alessio/shellescape"
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
const (
linuxTempDir = "/tmp"
linuxHomeDir = "$HOME"
)
var _ OSCommand = (*unixOSCommand)(nil)
type unixOSCommand struct{}
func NewUnixOSCommand() OSCommand {
return unixOSCommand{}
}
// CreateDirectory if it does not exist
func (unixOSCommand) CreateDirectory(
runner *Runner,
name string,
remotePath pulumi.StringInput,
useSudo bool,
opts ...pulumi.ResourceOption,
) (*remote.Command, error) {
createCmd := fmt.Sprintf("mkdir -p %v", remotePath)
deleteCmd := fmt.Sprintf(`bash -c 'if [ -z "$(ls -A %v)" ]; then rm -d %v; fi'`, remotePath, remotePath)
// check if directory already exist
return createDirectory(
runner,
name,
createCmd,
deleteCmd,
useSudo,
opts...)
}
func (unixOSCommand) CopyInlineFile(
runner *Runner,
fileContent pulumi.StringInput,
remotePath string,
useSudo bool,
opts ...pulumi.ResourceOption,
) (*remote.Command, error) {
backupPath := remotePath + "." + backupExtension
backupCmd := fmt.Sprintf("if [ -f '%v' ]; then mv -f '%v' '%v'; fi", remotePath, remotePath, backupPath)
createCmd := fmt.Sprintf("bash -c '(%v) && cat - | tee %v > /dev/null'", backupCmd, remotePath)
deleteCmd := fmt.Sprintf("bash -c 'if [ -f '%v' ]; then mv -f '%v' '%v'; else rm -f '%v'; fi'", backupPath, backupPath, remotePath, remotePath)
opts = append(opts, pulumi.ReplaceOnChanges([]string{"*"}), pulumi.DeleteBeforeReplace(true))
return copyInlineFile(remotePath, runner, fileContent, useSudo, createCmd, deleteCmd, opts...)
}
func (fs unixOSCommand) GetTemporaryDirectory() string {
return linuxTempDir
}
func (fs unixOSCommand) GetHomeDirectory() string {
return linuxHomeDir
}
// BuildCommandString properly format the command string
// command can be nil
func (fs unixOSCommand) BuildCommandString(command pulumi.StringInput, env pulumi.StringMap, sudo bool, password bool, user string) pulumi.StringInput {
formattedCommand := formatCommandIfNeeded(command, sudo, password, user)
var envVars pulumi.StringArray
for varName, varValue := range env {
envVars = append(envVars, pulumi.Sprintf(`%v="%v"`, varName, varValue))
}
return buildCommandString(formattedCommand, envVars, func(envVarsStr pulumi.StringOutput) pulumi.StringInput {
return pulumi.Sprintf("%s %s", envVarsStr, formattedCommand)
})
}
func (fs unixOSCommand) IsPathAbsolute(path string) bool {
return strings.HasPrefix(path, "/")
}
func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool, user string) pulumi.StringInput {
if command == nil {
return nil
}
if !sudo && user == "" {
return command
}
var formattedCommand pulumi.StringInput
if sudo && password {
formattedCommand = pulumi.Sprintf("sudo -S %v", command)
} else if sudo {
formattedCommand = pulumi.Sprintf("sudo %v", command)
} else if user != "" {
formattedCommand = command.ToStringOutput().ApplyT(func(cmd string) string {
return fmt.Sprintf("sudo -u %v bash -c %v", user, shellescape.Quote(cmd))
}).(pulumi.StringOutput)
}
return formattedCommand
}