本文整理汇总了Golang中github.com/juju/juju/cloudconfig/cloudinit.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWindowsUserdataEncoding
func (s *CloudInitSuite) TestWindowsUserdataEncoding(c *gc.C) {
series := "win8"
metricsSpoolDir := must(paths.MetricsSpoolDir("win8"))
toolsList := tools.List{
&tools.Tools{
URL: "http://foo.com/tools/released/juju1.2.3-win8-amd64.tgz",
Version: version.MustParseBinary("1.2.3-win8-amd64"),
Size: 10,
SHA256: "1234",
},
}
dataDir, err := paths.DataDir(series)
c.Assert(err, jc.ErrorIsNil)
logDir, err := paths.LogDir(series)
c.Assert(err, jc.ErrorIsNil)
cfg := instancecfg.InstanceConfig{
ControllerTag: testing.ControllerTag,
MachineId: "10",
AgentEnvironment: map[string]string{agent.ProviderType: "dummy"},
Series: series,
Jobs: []multiwatcher.MachineJob{multiwatcher.JobHostUnits},
MachineNonce: "FAKE_NONCE",
APIInfo: &api.Info{
Addrs: []string{"state-addr.testing.invalid:54321"},
Password: "bletch",
CACert: "CA CERT\n" + testing.CACert,
Tag: names.NewMachineTag("10"),
ModelTag: testing.ModelTag,
},
MachineAgentServiceName: "jujud-machine-10",
DataDir: dataDir,
LogDir: path.Join(logDir, "juju"),
MetricsSpoolDir: metricsSpoolDir,
CloudInitOutputLog: path.Join(logDir, "cloud-init-output.log"),
}
err = cfg.SetTools(toolsList)
c.Assert(err, jc.ErrorIsNil)
ci, err := cloudinit.New("win8")
c.Assert(err, jc.ErrorIsNil)
udata, err := cloudconfig.NewUserdataConfig(&cfg, ci)
c.Assert(err, jc.ErrorIsNil)
err = udata.Configure()
c.Assert(err, jc.ErrorIsNil)
data, err := ci.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
cicompose, err := cloudinit.New("win8")
c.Assert(err, jc.ErrorIsNil)
base64Data := base64.StdEncoding.EncodeToString(utils.Gzip(data))
got := []byte(fmt.Sprintf(cloudconfig.UserDataScript, base64Data))
expected, err := providerinit.ComposeUserData(&cfg, cicompose, openstack.OpenstackRenderer{})
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(got), gc.Equals, string(expected))
}
示例2: TestCloudInitConfigureBootstrapLogging
func (*cloudinitSuite) TestCloudInitConfigureBootstrapLogging(c *gc.C) {
loggo.GetLogger("").SetLogLevel(loggo.INFO)
envConfig := minimalEnvironConfig(c)
instConfig := makeBootstrapConfig("quantal").maybeSetEnvironConfig(envConfig)
rendered := instConfig.render()
cloudcfg, err := cloudinit.New(rendered.Series)
c.Assert(err, jc.ErrorIsNil)
udata, err := cloudconfig.NewUserdataConfig(&rendered, cloudcfg)
c.Assert(err, jc.ErrorIsNil)
err = udata.Configure()
c.Assert(err, jc.ErrorIsNil)
data, err := cloudcfg.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
configKeyValues := make(map[interface{}]interface{})
err = goyaml.Unmarshal(data, &configKeyValues)
c.Assert(err, jc.ErrorIsNil)
scripts := getScripts(configKeyValues)
for i, script := range scripts {
if strings.Contains(script, "bootstrap") {
c.Logf("scripts[%d]: %q", i, script)
}
}
expected := "jujud bootstrap-state --data-dir '.*' --env-config '.*'" +
" --instance-id '.*' --bootstrap-constraints 'mem=4096M'" +
" --environ-constraints 'mem=2048M' --show-log"
assertScriptMatch(c, scripts, expected, false)
}
示例3: TestWindowsCloudInit
func (*cloudinitSuite) TestWindowsCloudInit(c *gc.C) {
for i, test := range windowsCloudinitTests {
testConfig := test.cfg.render()
c.Logf("test %d", i)
ci, err := cloudinit.New("win8")
c.Assert(err, jc.ErrorIsNil)
udata, err := cloudconfig.NewUserdataConfig(&testConfig, ci)
c.Assert(err, jc.ErrorIsNil)
err = udata.Configure()
c.Assert(err, jc.ErrorIsNil)
c.Check(ci, gc.NotNil)
data, err := ci.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
stringData := strings.Replace(string(data), "\r\n", "\n", -1)
stringData = strings.Replace(stringData, "\t", " ", -1)
stringData = strings.TrimSpace(stringData)
compareString := strings.Replace(string(test.expectScripts), "\r\n", "\n", -1)
compareString = strings.Replace(compareString, "\t", " ", -1)
compareString = strings.TrimSpace(compareString)
testing.CheckString(c, stringData, compareString)
}
}
示例4: getCloudConfig
func (s *configureSuite) getCloudConfig(c *gc.C, controller bool, vers version.Binary) cloudinit.CloudConfig {
var icfg *instancecfg.InstanceConfig
var err error
modelConfig := testConfig(c, controller, vers)
if controller {
icfg, err = instancecfg.NewBootstrapInstanceConfig(
coretesting.FakeControllerConfig(),
constraints.Value{}, constraints.Value{},
vers.Series, "",
)
c.Assert(err, jc.ErrorIsNil)
icfg.APIInfo = &api.Info{
Password: "password",
CACert: coretesting.CACert,
ModelTag: coretesting.ModelTag,
}
icfg.Controller.MongoInfo = &mongo.MongoInfo{
Password: "password", Info: mongo.Info{CACert: coretesting.CACert},
}
icfg.Bootstrap.ControllerModelConfig = modelConfig
icfg.Bootstrap.BootstrapMachineInstanceId = "instance-id"
icfg.Bootstrap.HostedModelConfig = map[string]interface{}{
"name": "hosted-model",
}
icfg.Bootstrap.StateServingInfo = params.StateServingInfo{
Cert: coretesting.ServerCert,
PrivateKey: coretesting.ServerKey,
CAPrivateKey: coretesting.CAKey,
StatePort: 123,
APIPort: 456,
}
icfg.Jobs = []multiwatcher.MachineJob{multiwatcher.JobManageModel, multiwatcher.JobHostUnits}
icfg.Bootstrap.StateServingInfo = params.StateServingInfo{
Cert: coretesting.ServerCert,
PrivateKey: coretesting.ServerKey,
CAPrivateKey: coretesting.CAKey,
StatePort: 123,
APIPort: 456,
}
} else {
icfg, err = instancecfg.NewInstanceConfig(coretesting.ControllerTag, "0", "ya", imagemetadata.ReleasedStream, vers.Series, nil)
c.Assert(err, jc.ErrorIsNil)
icfg.Jobs = []multiwatcher.MachineJob{multiwatcher.JobHostUnits}
}
err = icfg.SetTools(tools.List{
&tools.Tools{
Version: vers,
URL: "http://testing.invalid/tools.tar.gz",
},
})
err = instancecfg.FinishInstanceConfig(icfg, modelConfig)
c.Assert(err, jc.ErrorIsNil)
cloudcfg, err := cloudinit.New(icfg.Series)
c.Assert(err, jc.ErrorIsNil)
udata, err := cloudconfig.NewUserdataConfig(icfg, cloudcfg)
c.Assert(err, jc.ErrorIsNil)
err = udata.Configure()
c.Assert(err, jc.ErrorIsNil)
return cloudcfg
}
示例5: getCloudConfig
func (s *configureSuite) getCloudConfig(c *gc.C, stateServer bool, vers version.Binary) cloudinit.CloudConfig {
var icfg *instancecfg.InstanceConfig
var err error
if stateServer {
icfg, err = instancecfg.NewBootstrapInstanceConfig(constraints.Value{}, vers.Series)
c.Assert(err, jc.ErrorIsNil)
icfg.InstanceId = "instance-id"
icfg.Jobs = []multiwatcher.MachineJob{multiwatcher.JobManageEnviron, multiwatcher.JobHostUnits}
} else {
icfg, err = instancecfg.NewInstanceConfig("0", "ya", imagemetadata.ReleasedStream, vers.Series, true, nil, nil, nil)
c.Assert(err, jc.ErrorIsNil)
icfg.Jobs = []multiwatcher.MachineJob{multiwatcher.JobHostUnits}
}
icfg.Tools = &tools.Tools{
Version: vers,
URL: "http://testing.invalid/tools.tar.gz",
}
environConfig := testConfig(c, stateServer, vers)
err = instancecfg.FinishInstanceConfig(icfg, environConfig)
c.Assert(err, jc.ErrorIsNil)
cloudcfg, err := cloudinit.New(icfg.Series)
c.Assert(err, jc.ErrorIsNil)
udata, err := cloudconfig.NewUserdataConfig(icfg, cloudcfg)
c.Assert(err, jc.ErrorIsNil)
err = udata.Configure()
c.Assert(err, jc.ErrorIsNil)
return cloudcfg
}
示例6: ProvisioningScript
// ProvisioningScript generates a bash script that can be
// executed on a remote host to carry out the cloud-init
// configuration.
func ProvisioningScript(icfg *instancecfg.InstanceConfig) (string, error) {
cloudcfg, err := cloudinit.New(icfg.Series)
if err != nil {
return "", errors.Annotate(err, "error generating cloud-config")
}
cloudcfg.SetSystemUpdate(icfg.EnableOSRefreshUpdate)
cloudcfg.SetSystemUpgrade(icfg.EnableOSUpgrade)
udata, err := cloudconfig.NewUserdataConfig(icfg, cloudcfg)
if err != nil {
return "", errors.Annotate(err, "error generating cloud-config")
}
if err := udata.ConfigureJuju(); err != nil {
return "", errors.Annotate(err, "error generating cloud-config")
}
configScript, err := cloudcfg.RenderScript()
if err != nil {
return "", errors.Annotate(err, "error converting cloud-config to script")
}
var buf bytes.Buffer
// Always remove the cloud-init-output.log file first, if it exists.
fmt.Fprintf(&buf, "rm -f %s\n", utils.ShQuote(icfg.CloudInitOutputLog))
// If something goes wrong, dump cloud-init-output.log to stderr.
buf.WriteString(shell.DumpFileOnErrorScript(icfg.CloudInitOutputLog))
buf.WriteString(configScript)
return buf.String(), nil
}
示例7: ComposeUserData
// ComposeUserData fills out the provided cloudinit configuration structure
// so it is suitable for initialising a machine with the given configuration,
// and then renders it and encodes it using the supplied renderer.
// When calling ComposeUserData a encoding implementation must be chosen from
// the providerinit/encoders package according to the need of the provider.
//
// If the provided cloudcfg is nil, a new one will be created internally.
func ComposeUserData(icfg *instancecfg.InstanceConfig, cloudcfg cloudinit.CloudConfig, renderer renderers.ProviderRenderer) ([]byte, error) {
if cloudcfg == nil {
var err error
cloudcfg, err = cloudinit.New(icfg.Series)
if err != nil {
return nil, errors.Trace(err)
}
}
_, err := configureCloudinit(icfg, cloudcfg)
if err != nil {
return nil, errors.Trace(err)
}
operatingSystem, err := series.GetOSFromSeries(icfg.Series)
if err != nil {
return nil, errors.Trace(err)
}
// This might get replaced by a renderer.RenderUserdata which will either
// render it as YAML or Bash since some CentOS images might ship without cloudnit
udata, err := cloudcfg.RenderYAML()
if err != nil {
return nil, errors.Trace(err)
}
udata, err = renderer.EncodeUserdata(udata, operatingSystem)
if err != nil {
return nil, errors.Trace(err)
}
logger.Tracef("Generated cloud init:\n%s", string(udata))
return udata, err
}
示例8: TestSetOutput
func (S) TestSetOutput(c *gc.C) {
type test struct {
kind cloudinit.OutputKind
stdout string
stderr string
}
tests := []test{{
cloudinit.OutAll, "a", "",
}, {
cloudinit.OutAll, "", "b",
}, {
cloudinit.OutInit, "a", "b",
}, {
cloudinit.OutAll, "a", "b",
}, {
cloudinit.OutAll, "", "",
}}
cfg, err := cloudinit.New("trusty")
c.Assert(err, jc.ErrorIsNil)
stdout, stderr := cfg.Output(cloudinit.OutAll)
c.Assert(stdout, gc.Equals, "")
c.Assert(stderr, gc.Equals, "")
for i, t := range tests {
c.Logf("test %d: %+v", i, t)
cfg.SetOutput(t.kind, t.stdout, t.stderr)
stdout, stderr = cfg.Output(t.kind)
c.Assert(stdout, gc.Equals, t.stdout)
c.Assert(stderr, gc.Equals, t.stderr)
}
}
示例9: TestSetUbuntuUserPreciseNoKeys
func (*cloudinitSuite) TestSetUbuntuUserPreciseNoKeys(c *gc.C) {
ci, err := cloudinit.New("precise")
c.Assert(err, jc.ErrorIsNil)
cloudconfig.SetUbuntuUser(ci, "")
data, err := ci.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), jc.YAMLEquals, map[string]interface{}{})
}
示例10: TestCloudInit
// TestCloudInit checks that the output from the various tests
// in cloudinitTests is well formed.
func (*cloudinitSuite) TestCloudInit(c *gc.C) {
for i, test := range cloudinitTests {
c.Logf("test %d", i)
var envConfig *config.Config
if test.setEnvConfig {
envConfig = minimalEnvironConfig(c)
}
testConfig := test.cfg.maybeSetEnvironConfig(envConfig).render()
ci, err := cloudinit.New(testConfig.Series)
c.Assert(err, jc.ErrorIsNil)
udata, err := cloudconfig.NewUserdataConfig(&testConfig, ci)
c.Assert(err, jc.ErrorIsNil)
err = udata.Configure()
c.Assert(err, jc.ErrorIsNil)
c.Check(ci, gc.NotNil)
// render the cloudinit config to bytes, and then
// back to a map so we can introspect it without
// worrying about internal details of the cloudinit
// package.
data, err := ci.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
configKeyValues := make(map[interface{}]interface{})
err = goyaml.Unmarshal(data, &configKeyValues)
c.Assert(err, jc.ErrorIsNil)
if testConfig.EnableOSRefreshUpdate {
c.Check(configKeyValues["package_update"], jc.IsTrue)
} else {
c.Check(configKeyValues["package_update"], jc.IsFalse)
}
if testConfig.EnableOSUpgrade {
c.Check(configKeyValues["package_upgrade"], jc.IsTrue)
} else {
c.Check(configKeyValues["package_upgrade"], jc.IsFalse)
}
scripts := getScripts(configKeyValues)
assertScriptMatch(c, scripts, test.expectScripts, !test.inexactMatch)
if testConfig.Config != nil {
checkEnvConfig(c, testConfig.Config, configKeyValues, scripts)
}
// curl should always be installed, since it's required by jujud.
checkPackage(c, configKeyValues, "curl", true)
tag := names.NewMachineTag(testConfig.MachineId).String()
acfg := getAgentConfig(c, tag, scripts)
c.Assert(acfg, jc.Contains, "AGENT_SERVICE_NAME: jujud-"+tag)
c.Assert(acfg, jc.Contains, "upgradedToVersion: 1.2.3\n")
source := "deb http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/cloud-tools main"
needCloudArchive := testConfig.Series == "precise"
checkAptSource(c, configKeyValues, source, pacconf.UbuntuCloudArchiveSigningKey, needCloudArchive)
}
}
示例11: TestPackages
func (S) TestPackages(c *gc.C) {
cfg, err := cloudinit.New("precise")
c.Assert(err, jc.ErrorIsNil)
c.Assert(cfg.Packages(), gc.HasLen, 0)
cfg.AddPackage("a b c")
cfg.AddPackage("d!")
expectedPackages := []string{"a b c", "d!"}
c.Assert(cfg.Packages(), gc.DeepEquals, expectedPackages)
}
示例12: GetCloudConfig
// GetCloudConfig implements ProviderConfigurator interface.
func (c *rackspaceConfigurator) GetCloudConfig(args environs.StartInstanceParams) (cloudinit.CloudConfig, error) {
cloudcfg, err := cloudinit.New(args.Tools.OneSeries())
if err != nil {
return nil, errors.Trace(err)
}
// Additional package required for sshInstanceConfigurator, to save
// iptables state between restarts.
cloudcfg.AddPackage("iptables-persistent")
return cloudcfg, nil
}
示例13: TestWindowsRender
func (S) TestWindowsRender(c *gc.C) {
compareOutput := "#ps1_sysnative\r\n\r\npowershell"
cfg, err := cloudinit.New("win8")
c.Assert(err, jc.ErrorIsNil)
cfg.AddRunCmd("powershell")
data, err := cfg.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
c.Assert(data, gc.NotNil)
c.Assert(string(data), gc.Equals, compareOutput, gc.Commentf("test %q output differs", "windows renderer"))
}
示例14: TestRunCmds
func (S) TestRunCmds(c *gc.C) {
cfg, err := cloudinit.New("precise")
c.Assert(err, jc.ErrorIsNil)
c.Assert(cfg.RunCmds(), gc.HasLen, 0)
cfg.AddScripts("a", "b")
cfg.AddRunCmd("e")
c.Assert(cfg.RunCmds(), gc.DeepEquals, []string{
"a", "b", "e",
})
}
示例15: TestSetUbuntuUserCentOS
func (*cloudinitSuite) TestSetUbuntuUserCentOS(c *gc.C) {
ci, err := cloudinit.New("centos7")
c.Assert(err, jc.ErrorIsNil)
cloudconfig.SetUbuntuUser(ci, "akey\n#also\nbkey")
data, err := ci.RenderYAML()
c.Assert(err, jc.ErrorIsNil)
keys := []string{"akey", "bkey"}
expected := expectedUbuntuUser(cloudconfig.CentOSGroups, keys)
c.Assert(string(data), jc.YAMLEquals, expected)
}