-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor cloudinit packages to include support for windows userdata * fix NewMachineConfig to store corect paths when on windows
- Loading branch information
1 parent
2c66731
commit 258c01e
Showing
45 changed files
with
1,952 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cloudinit | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
|
||
"github.com/juju/juju/version" | ||
) | ||
|
||
type Renderer interface { | ||
|
||
// Mkdir returns an OS specific script for creating a directory | ||
Mkdir(path string) []string | ||
|
||
// WriteFile returns a command to write data | ||
WriteFile(filename string, contents string, permission int) []string | ||
|
||
// Render renders the userdata script for a particular OS type | ||
Render(conf *Config) ([]byte, error) | ||
|
||
// FromSlash returns the result of replacing each slash ('/') character | ||
// in path with a separator character. Multiple slashes are replaced by | ||
// multiple separators. | ||
|
||
FromSlash(path string) string | ||
// PathJoin will join a path using OS specific path separator. | ||
// This works for selected OS instead of current OS | ||
|
||
PathJoin(path ...string) string | ||
} | ||
|
||
// NewRenderer returns a Renderer interface for selected series | ||
func NewRenderer(series string) (Renderer, error) { | ||
operatingSystem, err := version.GetOSFromSeries(series) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch operatingSystem { | ||
case version.Windows: | ||
return &WindowsRenderer{}, nil | ||
case version.Ubuntu: | ||
return &UbuntuRenderer{}, nil | ||
default: | ||
return nil, errors.Errorf("No renderer could be found for %s", series) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cloudinit | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"strings" | ||
|
||
"github.com/juju/utils" | ||
"gopkg.in/yaml.v1" | ||
) | ||
|
||
// UbuntuRenderer represents an Ubuntu specific script render | ||
// type that is responsible for this particular OS. It implements | ||
// the Renderer interface | ||
type UbuntuRenderer struct{} | ||
|
||
func (w *UbuntuRenderer) Mkdir(path string) []string { | ||
return []string{fmt.Sprintf(`mkdir -p %s`, utils.ShQuote(path))} | ||
} | ||
|
||
func (w *UbuntuRenderer) WriteFile(filename string, contents string, permission int) []string { | ||
quotedFilename := utils.ShQuote(filename) | ||
quotedContents := utils.ShQuote(contents) | ||
return []string{ | ||
fmt.Sprintf("install -m %o /dev/null %s", permission, quotedFilename), | ||
fmt.Sprintf(`printf '%%s\n' %s > %s`, quotedContents, quotedFilename), | ||
} | ||
} | ||
|
||
func (w *UbuntuRenderer) FromSlash(filepath string) string { | ||
return filepath | ||
} | ||
|
||
func (w *UbuntuRenderer) PathJoin(filepath ...string) string { | ||
return path.Join(filepath...) | ||
} | ||
|
||
func (w *UbuntuRenderer) Render(conf *Config) ([]byte, error) { | ||
data, err := yaml.Marshal(conf.attrs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append([]byte("#cloud-config\n"), data...), nil | ||
} | ||
|
||
// WindowsRenderer represents a Windows specific script render | ||
// type that is responsible for this particular OS. It implements | ||
// the Renderer interface | ||
type WindowsRenderer struct{} | ||
|
||
func (w *WindowsRenderer) Mkdir(path string) []string { | ||
return []string{fmt.Sprintf(`mkdir %s`, w.FromSlash(path))} | ||
} | ||
|
||
func (w *WindowsRenderer) WriteFile(filename string, contents string, permission int) []string { | ||
return []string{ | ||
fmt.Sprintf("Set-Content '%s' @\"\n%s\n\"@", filename, contents), | ||
} | ||
} | ||
|
||
func (w *WindowsRenderer) PathJoin(filepath ...string) string { | ||
return strings.Join(filepath, `\`) | ||
} | ||
|
||
func (w *WindowsRenderer) FromSlash(path string) string { | ||
return strings.Replace(path, "/", `\`, -1) | ||
} | ||
|
||
func (w *WindowsRenderer) Render(conf *Config) ([]byte, error) { | ||
winCmds := conf.attrs["runcmd"] | ||
var script []byte | ||
newline := "\r\n" | ||
header := "#ps1_sysnative\r\n" | ||
script = append(script, header...) | ||
for _, value := range winCmds.([]*command) { | ||
script = append(script, newline...) | ||
script = append(script, value.literal...) | ||
|
||
} | ||
return script, nil | ||
} |
Oops, something went wrong.