示例#1
0
文件: userdata.go 项目: kapilt/juju
func cloudInitUserData(machineConfig *cloudinit.MachineConfig) ([]byte, error) {
	cloudConfig := coreCloudinit.New()
	udata, err := cloudinit.NewUserdataConfig(machineConfig, cloudConfig)
	if err != nil {
		return nil, err
	}
	err = udata.Configure()
	if err != nil {
		return nil, err
	}
	// Run ifconfig to get the addresses of the internal container at least
	// logged in the host.
	cloudConfig.AddRunCmd("ifconfig")

	renderer, err := coreCloudinit.NewRenderer(machineConfig.Series)
	if err != nil {
		return nil, err
	}

	data, err := renderer.Render(cloudConfig)
	if err != nil {
		return nil, err
	}
	return data, nil
}
示例#2
0
// templateUserData returns a minimal user data necessary for the template.
// This should have the authorized keys, base packages, the cloud archive if
// necessary,  initial apt proxy config, and it should do the apt-get
// update/upgrade initially.
func templateUserData(
	series string,
	authorizedKeys string,
	aptProxy proxy.Settings,
	enablePackageUpdates bool,
	enableOSUpgrades bool,
) ([]byte, error) {
	config := corecloudinit.New()
	config.AddScripts(
		"set -xe", // ensure we run all the scripts or abort.
	)
	config.AddSSHAuthorizedKeys(authorizedKeys)
	if enablePackageUpdates {
		cloudinit.MaybeAddCloudArchiveCloudTools(config, series)
	}
	cloudinit.AddAptCommands(aptProxy, config, enablePackageUpdates, enableOSUpgrades)
	config.AddScripts(
		fmt.Sprintf(
			"printf '%%s\n' %s > %s",
			utils.ShQuote(templateShutdownUpstartScript),
			templateShutdownUpstartFilename,
		))

	renderer, err := corecloudinit.NewRenderer(series)
	if err != nil {
		return nil, err
	}
	data, err := renderer.Render(config)
	if err != nil {
		return nil, err
	}
	return data, nil
}
示例#3
0
func (w *ubuntuConfigure) init() error {
	renderer, err := cloudinit.NewRenderer(w.mcfg.Series)
	if err != nil {
		return err
	}
	w.renderer = renderer
	return nil
}
示例#4
0
func (S) TestWindowsMkdir(c *gc.C) {
	render, err := cloudinit.NewRenderer("win8")
	compareOutput := "mkdir fake_dir"
	c.Assert(err, gc.IsNil)
	output := render.Mkdir("fake_dir")
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output[0], gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows mkdir"))
}
示例#5
0
func (S) TestUbuntuMkdir(c *gc.C) {
	compareOutput := "mkdir -p 'fake_dir'"
	render, err := cloudinit.NewRenderer("precise")
	c.Assert(err, gc.IsNil)
	output := render.Mkdir("fake_dir")
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output[0], gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows mkdir"))
}
示例#6
0
func (S) TestWindowsPathJoin(c *gc.C) {
	dirPath := path.Join("fake", "dir")
	compareOutput := "fake\\dir\\fakeFile"

	render, err := cloudinit.NewRenderer("win8")
	c.Assert(err, gc.IsNil)
	output := render.PathJoin(render.FromSlash(dirPath), "fakeFile")
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output, gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows writefile"))
}
示例#7
0
func (S) TestWindowsFromSlash(c *gc.C) {
	filePath := path.Join("fake/file\\path//value\\/\\")
	compareOutput := "fake\\file\\path\\value\\\\\\"

	render, err := cloudinit.NewRenderer("win8")
	c.Assert(err, gc.IsNil)
	output := render.FromSlash(filePath)
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output, gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows fromslash"))
}
示例#8
0
func (S) TestWindowsWriteFile(c *gc.C) {
	filePath := path.Join("fake_dir", "test_file")
	compareOutput := "Set-Content '" + filePath + "' @\"\nfake output\n\"@"

	render, err := cloudinit.NewRenderer("win8")
	c.Assert(err, gc.IsNil)
	output := render.WriteFile(filePath, "fake output", 7777)
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output[0], gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows writefile"))
}
示例#9
0
func (S) TestWindowsRender(c *gc.C) {
	compareOutput := "#ps1_sysnative\r\n\r\npowershell"
	cfg := cloudinit.New()
	cfg.AddRunCmd("powershell")
	render, err := cloudinit.NewRenderer("win8")
	c.Assert(err, gc.IsNil)
	data, err := render.Render(cfg)
	c.Assert(err, gc.IsNil)
	c.Assert(data, gc.NotNil)
	c.Assert(string(data), gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows renderer"))
}
示例#10
0
func (S) TestUbuntuPathJoin(c *gc.C) {
	dirPath := path.Join("fake", "dir")
	compareOutput := "fake/dir/fakeFile"

	render, err := cloudinit.NewRenderer("precise")
	c.Assert(err, gc.IsNil)
	output := render.PathJoin(dirPath, "fakeFile")
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output, gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows writefile"))
}
示例#11
0
func (S) TestUbuntuWriteFile(c *gc.C) {
	filePath := path.Join("fake_dir", "test_file")
	compareOutput := "install -m 17141 /dev/null 'fake_dir/test_file'"

	render, err := cloudinit.NewRenderer("precise")
	c.Assert(err, gc.IsNil)
	output := render.WriteFile(filePath, "fake output", 7777)
	c.Assert(err, gc.IsNil)
	c.Assert(output, gc.NotNil)
	c.Assert(output[0], gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows writefile"))
}
示例#12
0
func (S) TestOutput(c *gc.C) {
	for _, t := range ctests {
		cfg := cloudinit.New()
		t.setOption(cfg)
		renderer, err := cloudinit.NewRenderer("quantal")
		c.Assert(err, gc.IsNil)
		data, err := renderer.Render(cfg)
		c.Assert(err, gc.IsNil)
		c.Assert(data, gc.NotNil)
		c.Assert(string(data), gc.Equals, header+t.expect, gc.Commentf("test %q output differs", t.name))
	}
}
示例#13
0
文件: agent.go 项目: kapilt/juju
func (c *configInternal) WriteCommands(series string) ([]string, error) {
	renderer, err := cloudinit.NewRenderer(series)
	if err != nil {
		return nil, err
	}
	data, err := c.fileContents()
	if err != nil {
		return nil, err
	}
	commands := renderer.Mkdir(c.Dir())
	commands = append(commands, renderer.WriteFile(c.File(agentConfigFilename), string(data), 0600)...)
	return commands, nil
}
示例#14
0
//#cloud-config
//packages:
//- juju
//- ubuntu
func ExampleConfig() {
	cfg := cloudinit.New()
	cfg.AddPackage("juju")
	cfg.AddPackage("ubuntu")
	renderer, err := cloudinit.NewRenderer("quantal")
	if err != nil {
		fmt.Printf("render error: %v", err)
		return
	}
	data, err := renderer.Render(cfg)
	if err != nil {
		fmt.Printf("render error: %v", err)
		return
	}
	fmt.Printf("%s", data)
}
示例#15
0
文件: util.go 项目: kapilt/juju
// cloudinitRunCmd returns the shell command that, when run, will create the
// "machine info" file containing the hostname of a machine.
// That command is destined to be used by cloudinit.
func (info *machineInfo) cloudinitRunCmd(series string) (string, error) {
	dataDir, err := paths.DataDir(series)
	if err != nil {
		return "", err
	}
	renderer, err := cloudinit.NewRenderer(series)
	if err != nil {
		return "", err
	}

	yaml, err := goyaml.Marshal(info)
	if err != nil {
		return "", err
	}
	fileName := renderer.PathJoin(renderer.FromSlash(dataDir), "MAASmachine.txt")
	script := renderer.Mkdir(dataDir)
	contents := utils.ShQuote(string(yaml))
	script = append(script, renderer.WriteFile(fileName, contents, 0755)...)
	return strings.Join(script, "\n"), nil
}