Beispiel #1
0
// TestCloudInitVerify checks that required fields are appropriately
// checked for by NewCloudInit.
func (cloudinitSuite) TestCloudInitVerify(c *C) {
	cfg := &cloudinit.MachineConfig{
		StateServer:        true,
		StateServerCert:    serverCert,
		StateServerKey:     serverKey,
		InstanceIdAccessor: "$instance_id",
		ProviderType:       "ec2",
		MachineId:          "99",
		Tools:              newSimpleTools("9.9.9-linux-arble"),
		AuthorizedKeys:     "sshkey1",
		StateInfo: &state.Info{
			Addrs:  []string{"host:98765"},
			CACert: []byte(testing.CACert),
		},
		Config:  envConfig,
		DataDir: "/var/lib/juju",
	}
	// check that the base configuration does not give an error
	_, err := cloudinit.New(cfg)
	c.Assert(err, IsNil)

	for i, test := range verifyTests {
		c.Logf("test %d. %s", i, test.err)
		cfg1 := *cfg
		test.mutate(&cfg1)
		t, err := cloudinit.New(&cfg1)
		c.Assert(err, ErrorMatches, "invalid machine configuration: "+test.err)
		c.Assert(t, IsNil)
	}
}
Beispiel #2
0
func (t *cloudinitTest) check(c *C) {
	ci, err := cloudinit.New(&t.cfg)
	c.Assert(err, IsNil)
	c.Check(ci, 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.Render()
	c.Assert(err, IsNil)

	x := make(map[interface{}]interface{})
	err = goyaml.Unmarshal(data, &x)
	c.Assert(err, IsNil)

	c.Check(x["apt_upgrade"], Equals, true)
	c.Check(x["apt_update"], Equals, true)

	scripts := getScripts(x)
	scriptDiff(c, scripts, t.expectScripts)
	if t.cfg.Config != nil {
		checkEnvConfig(c, t.cfg.Config, x, scripts)
	}
	checkPackage(c, x, "git", true)
}
Beispiel #3
0
func cloudInitUserData(
	machineId, nonce string,
	tools *tools.Tools,
	environConfig *config.Config,
	stateInfo *state.Info,
	apiInfo *api.Info,
) ([]byte, error) {
	machineConfig := &cloudinit.MachineConfig{
		MachineId:            machineId,
		MachineNonce:         nonce,
		MachineContainerType: instance.LXC,
		StateInfo:            stateInfo,
		APIInfo:              apiInfo,
		DataDir:              "/var/lib/juju",
		Tools:                tools,
	}
	if err := environs.FinishMachineConfig(machineConfig, environConfig, constraints.Value{}); err != nil {
		return nil, err
	}
	cloudConfig, err := cloudinit.New(machineConfig)
	if err != nil {
		return nil, err
	}
	data, err := cloudConfig.Render()
	if err != nil {
		return nil, err
	}
	return data, nil
}
Beispiel #4
0
func (e *environ) userData(scfg *startInstanceParams) ([]byte, error) {
	cfg := &cloudinit.MachineConfig{
		StateServer:        scfg.stateServer,
		StateInfo:          scfg.info,
		StateServerCert:    scfg.stateServerCert,
		StateServerKey:     scfg.stateServerKey,
		InstanceIdAccessor: "$(curl http://169.254.169.254/1.0/meta-data/instance-id)",
		ProviderType:       "ec2",
		DataDir:            "/var/lib/juju",
		Tools:              scfg.tools,
		MachineId:          scfg.machineId,
		AuthorizedKeys:     e.ecfg().AuthorizedKeys(),
		Config:             scfg.config,
	}
	cloudcfg, err := cloudinit.New(cfg)
	if err != nil {
		return nil, err
	}
	data, err := cloudcfg.Render()
	if err != nil {
		return nil, err
	}
	cdata := trivial.Gzip(data)
	log.Debugf("environs/ec2: ec2 user data; %d bytes: %q", len(cdata), data)
	return cdata, nil
}
Beispiel #5
0
func cloudInitUserData(
	machineId, nonce string,
	tools *tools.Tools,
	environConfig *config.Config,
	stateInfo *state.Info,
	apiInfo *api.Info,
) ([]byte, error) {
	machineConfig := &cloudinit.MachineConfig{
		MachineId:            machineId,
		MachineNonce:         nonce,
		MachineContainerType: instance.LXC,
		StateInfo:            stateInfo,
		APIInfo:              apiInfo,
		DataDir:              "/var/lib/juju",
		Tools:                tools,
	}
	if err := environs.FinishMachineConfig(machineConfig, environConfig, constraints.Value{}); err != nil {
		return nil, err
	}
	cloudConfig, err := cloudinit.New(machineConfig)
	if err != nil {
		return nil, err
	}

	// Run apt-config to fetch proxy settings from host. If no proxy
	// settings are configured, then we don't set up any proxy information
	// on the container.
	proxyConfig, err := utils.AptConfigProxy()
	if err != nil {
		return nil, err
	}
	if proxyConfig != "" {
		var proxyLines []string
		for _, line := range strings.Split(proxyConfig, "\n") {
			line = strings.TrimSpace(line)
			if m := aptHTTPProxyRE.FindStringSubmatch(line); m != nil {
				cloudConfig.SetAptProxy(m[1])
			} else {
				proxyLines = append(proxyLines, line)
			}
		}
		if len(proxyLines) > 0 {
			cloudConfig.AddFile(
				"/etc/apt/apt.conf.d/99proxy-extra",
				strings.Join(proxyLines, "\n"),
				0644)
		}
	}

	// Run ifconfig to get the addresses of the internal container at least
	// logged in the host.
	cloudConfig.AddRunCmd("ifconfig")

	data, err := cloudConfig.Render()
	if err != nil {
		return nil, err
	}
	return data, nil
}
Beispiel #6
0
// TestCloudInit checks that the output from the various tests
// in cloudinitTests is well formed.
func (cloudinitSuite) TestCloudInit(c *C) {
	for i, test := range cloudinitTests {
		c.Logf("test %d", i)
		ci, err := cloudinit.New(&test.cfg)
		c.Assert(err, IsNil)
		c.Check(ci, NotNil)

		test.check(c)
	}
}
Beispiel #7
0
// TestCloudInitVerify checks that required fields are appropriately
// checked for by NewCloudInit.
func (*cloudinitSuite) TestCloudInitVerify(c *C) {
	cfg := &cloudinit.MachineConfig{
		StateServer:     true,
		StateServerCert: serverCert,
		StateServerKey:  serverKey,
		StatePort:       1234,
		APIPort:         1235,
		MachineId:       "99",
		Tools:           newSimpleTools("9.9.9-linux-arble"),
		AuthorizedKeys:  "sshkey1",
		ProviderType:    "dummy",
		StateInfo: &state.Info{
			Addrs:  []string{"host:98765"},
			CACert: []byte(testing.CACert),
		},
		APIInfo: &api.Info{
			Addrs:  []string{"host:9999"},
			CACert: []byte(testing.CACert),
		},
		Config:       minimalConfig(c),
		DataDir:      environs.DataDir,
		MachineNonce: "FAKE_NONCE",
	}
	// check that the base configuration does not give an error
	_, err := cloudinit.New(cfg)
	c.Assert(err, IsNil)

	for i, test := range verifyTests {
		c.Logf("test %d. %s", i, test.err)
		cfg1 := *cfg
		test.mutate(&cfg1)
		t, err := cloudinit.New(&cfg1)
		c.Assert(err, ErrorMatches, "invalid machine configuration: "+test.err)
		c.Assert(t, IsNil)
	}
}
Beispiel #8
0
// TestCloudInit checks that the output from the various tests
// in cloudinitTests is well formed.
func (*cloudinitSuite) TestCloudInit(c *C) {
	for i, test := range cloudinitTests {
		c.Logf("test %d", i)
		if test.setEnvConfig {
			test.cfg.Config = minimalConfig(c)
		}
		ci, err := cloudinit.New(&test.cfg)
		c.Assert(err, IsNil)
		c.Check(ci, 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.Render()
		c.Assert(err, IsNil)

		x := make(map[interface{}]interface{})
		err = goyaml.Unmarshal(data, &x)
		c.Assert(err, IsNil)

		c.Check(x["apt_upgrade"], Equals, true)
		c.Check(x["apt_update"], Equals, true)

		scripts := getScripts(x)
		scriptDiff(c, scripts, test.expectScripts)
		if test.cfg.Config != nil {
			checkEnvConfig(c, test.cfg.Config, x, scripts)
		}
		checkPackage(c, x, "git", true)
		// The lxc package should only be there if the machine container type is not lxc.
		hasLxc := test.cfg.MachineContainerType != "lxc"
		checkPackage(c, x, "lxc", hasLxc)
		if test.cfg.StateServer {
			checkPackage(c, x, "mongodb-server", true)
			source := struct{ source, key string }{
				source: "ppa:juju/experimental",
				key:    "1024R/C8068B11",
			}
			checkAptSource(c, x, source, test.cfg.NeedMongoPPA())
		}
	}
}
Beispiel #9
0
func (e *environ) userData(scfg *startInstanceParams) ([]byte, error) {
	cfg := &cloudinit.MachineConfig{
		StateServer:        scfg.stateServer,
		StateInfo:          scfg.info,
		StateServerCert:    scfg.stateServerCert,
		StateServerKey:     scfg.stateServerKey,
		InstanceIdAccessor: "$(curl http://169.254.169.254/1.0/meta-data/instance-id)",
		ProviderType:       "openstack",
		DataDir:            "/var/lib/juju",
		Tools:              scfg.tools,
		MachineId:          scfg.machineId,
		AuthorizedKeys:     e.ecfg().AuthorizedKeys(),
		Config:             scfg.config,
	}
	cloudcfg, err := cloudinit.New(cfg)
	if err != nil {
		return nil, err
	}
	bytes, err := cloudcfg.Render()
	if err != nil {
		return nil, err
	}
	return bytes, nil
}