2015-08-19 13:38:31 -04:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const UnixOSType = "unix"
|
|
|
|
const WindowsOSType = "windows"
|
|
|
|
const DefaultOSType = UnixOSType
|
|
|
|
|
|
|
|
type guestOSTypeCommand struct {
|
|
|
|
chmod string
|
|
|
|
mkdir string
|
|
|
|
removeDir string
|
2017-12-12 16:39:13 -05:00
|
|
|
statPath string
|
2017-12-12 17:32:40 -05:00
|
|
|
mv string
|
2015-08-19 13:38:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var guestOSTypeCommands = map[string]guestOSTypeCommand{
|
2016-11-01 17:08:04 -04:00
|
|
|
UnixOSType: {
|
2015-08-19 13:38:31 -04:00
|
|
|
chmod: "chmod %s '%s'",
|
|
|
|
mkdir: "mkdir -p '%s'",
|
|
|
|
removeDir: "rm -rf '%s'",
|
2017-12-12 16:39:13 -05:00
|
|
|
statPath: "stat '%s'",
|
2017-12-12 17:32:40 -05:00
|
|
|
mv: "mv '%s' '%s'",
|
2015-08-19 13:38:31 -04:00
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
WindowsOSType: {
|
2015-08-19 13:38:31 -04:00
|
|
|
chmod: "echo 'skipping chmod %s %s'", // no-op
|
2015-08-20 16:25:27 -04:00
|
|
|
mkdir: "powershell.exe -Command \"New-Item -ItemType directory -Force -ErrorAction SilentlyContinue -Path %s\"",
|
2017-12-13 12:49:38 -05:00
|
|
|
removeDir: "powershell.exe -Command \"rm %s -recurse -force\"",
|
2017-12-13 12:20:27 -05:00
|
|
|
statPath: "powershell.exe -Command { if (test-path %s) { exit 0 } else { exit 1 } }",
|
2019-02-05 12:47:00 -05:00
|
|
|
mv: "powershell.exe -Command \"mv %s %s -force\"",
|
2015-08-19 13:38:31 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type GuestCommands struct {
|
|
|
|
GuestOSType string
|
|
|
|
Sudo bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGuestCommands(osType string, sudo bool) (*GuestCommands, error) {
|
|
|
|
_, ok := guestOSTypeCommands[osType]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Invalid osType: \"%s\"", osType)
|
|
|
|
}
|
|
|
|
return &GuestCommands{GuestOSType: osType, Sudo: sudo}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GuestCommands) Chmod(path string, mode string) string {
|
|
|
|
return g.sudo(fmt.Sprintf(g.commands().chmod, mode, g.escapePath(path)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GuestCommands) CreateDir(path string) string {
|
|
|
|
return g.sudo(fmt.Sprintf(g.commands().mkdir, g.escapePath(path)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GuestCommands) RemoveDir(path string) string {
|
|
|
|
return g.sudo(fmt.Sprintf(g.commands().removeDir, g.escapePath(path)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GuestCommands) commands() guestOSTypeCommand {
|
|
|
|
return guestOSTypeCommands[g.GuestOSType]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GuestCommands) escapePath(path string) string {
|
|
|
|
if g.GuestOSType == WindowsOSType {
|
|
|
|
return strings.Replace(path, " ", "` ", -1)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:39:13 -05:00
|
|
|
func (g *GuestCommands) StatPath(path string) string {
|
|
|
|
return g.sudo(fmt.Sprintf(g.commands().statPath, g.escapePath(path)))
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:32:40 -05:00
|
|
|
func (g *GuestCommands) MovePath(srcPath string, dstPath string) string {
|
2017-12-12 17:22:10 -05:00
|
|
|
return g.sudo(fmt.Sprintf(g.commands().mv, g.escapePath(srcPath), g.escapePath(dstPath)))
|
|
|
|
}
|
|
|
|
|
2015-08-19 13:38:31 -04:00
|
|
|
func (g *GuestCommands) sudo(cmd string) string {
|
|
|
|
if g.GuestOSType == UnixOSType && g.Sudo {
|
|
|
|
return "sudo " + cmd
|
|
|
|
}
|
|
|
|
return cmd
|
2016-02-08 20:34:06 -05:00
|
|
|
}
|