Ejemplo n.º 1
0
func SaveImage(digest string) error {
	bsConf, err := provision.FindScopedConfig(bsUniqueID)
	if err != nil {
		return err
	}
	return bsConf.SetExtra("image", digest)
}
Ejemplo n.º 2
0
func (s *S) TestLoadConfigPoolFiltering(c *check.C) {
	base := provision.ScopedConfig{
		Envs: []provision.Entry{{Name: "USER", Value: "root"}},
		Pools: []provision.PoolEntry{
			{Name: "pool1", Envs: []provision.Entry{{Name: "USER", Value: "nonroot"}}},
			{Name: "pool2", Envs: []provision.Entry{{Name: "USER", Value: "superroot"}}},
			{Name: "pool3", Envs: []provision.Entry{{Name: "USER", Value: "watroot"}}},
			{Name: "pool4", Envs: []provision.Entry{{Name: "USER", Value: "kindaroot"}}},
		},
	}
	conf, err := provision.FindScopedConfig(bsUniqueID)
	c.Assert(err, check.IsNil)
	err = conf.UpdateWith(&base)
	c.Assert(err, check.IsNil)
	conf, err = LoadConfig([]string{"pool1", "pool4"})
	c.Assert(err, check.IsNil)
	expectedConfig := provision.ScopedConfig{
		Envs: []provision.Entry{{Name: "USER", Value: "root"}},
		Pools: []provision.PoolEntry{
			{Name: "pool1", Envs: []provision.Entry{{Name: "USER", Value: "nonroot"}}},
			{Name: "pool4", Envs: []provision.Entry{{Name: "USER", Value: "kindaroot"}}},
		},
	}
	c.Assert(conf.Envs, check.DeepEquals, expectedConfig.Envs)
	c.Assert(conf.Pools, check.DeepEquals, expectedConfig.Pools)
}
Ejemplo n.º 3
0
func LoadConfig(pools []string) (*provision.ScopedConfig, error) {
	bsConf, err := provision.FindScopedConfig(bsUniqueID)
	if err != nil {
		return nil, err
	}
	bsConf.FilterPools(pools)
	return bsConf, nil
}
Ejemplo n.º 4
0
func createContainer(dockerEndpoint, poolName string, p DockerProvisioner, relaunch bool) error {
	client, err := dockerClient(dockerEndpoint)
	if err != nil {
		return err
	}
	bsConf, err := provision.FindScopedConfig(bsUniqueID)
	if err != nil {
		return err
	}
	bsImage := getImage(bsConf)
	err = pullBsImage(bsImage, dockerEndpoint, p)
	if err != nil {
		return err
	}
	hostConfig := docker.HostConfig{
		RestartPolicy: docker.AlwaysRestart(),
		Privileged:    true,
		NetworkMode:   "host",
	}
	socket, _ := config.GetString("docker:bs:socket")
	if socket != "" {
		hostConfig.Binds = []string{fmt.Sprintf("%s:/var/run/docker.sock:rw", socket)}
	}
	env, err := EnvListForEndpoint(dockerEndpoint, poolName)
	if err != nil {
		return err
	}
	opts := docker.CreateContainerOptions{
		Name:       "big-sibling",
		HostConfig: &hostConfig,
		Config: &docker.Config{
			Image: bsImage,
			Env:   env,
		},
	}
	container, err := client.CreateContainer(opts)
	if relaunch && err == docker.ErrContainerAlreadyExists {
		err = client.RemoveContainer(docker.RemoveContainerOptions{ID: opts.Name, Force: true})
		if err != nil {
			return err
		}
		container, err = client.CreateContainer(opts)
	}
	if err != nil && err != docker.ErrContainerAlreadyExists {
		return err
	}
	if container == nil {
		container, err = client.InspectContainer("big-sibling")
		if err != nil {
			return err
		}
	}
	err = client.StartContainer(container.ID, &hostConfig)
	if _, ok := err.(*docker.ContainerAlreadyRunning); !ok {
		return err
	}
	return nil
}
Ejemplo n.º 5
0
func (d *DockerLog) loadConfig() error {
	if d.conf != nil {
		return nil
	}
	var err error
	d.conf, err = provision.FindScopedConfig(dockerLogConfigEntry)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 6
0
func logsConfigGetHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	pools, err := listContextValues(t, permission.PermPoolUpdateLogs, true)
	if err != nil {
		return err
	}
	conf, err := provision.FindScopedConfig("logs")
	if err != nil {
		return err
	}
	conf.FilterPools(pools)
	return json.NewEncoder(w).Encode(conf)
}
Ejemplo n.º 7
0
Archivo: bs.go Proyecto: Endika/tsuru
func LoadConfig(pools []string) (*provision.ScopedConfig, error) {
	bsConf, err := provision.FindScopedConfig(bsUniqueID)
	if err != nil {
		return nil, err
	}
	if pools != nil {
		poolEntries := make([]provision.PoolEntry, 0, len(pools))
		for _, pool := range pools {
			for _, poolEntry := range bsConf.Pools {
				if poolEntry.Name == pool {
					poolEntries = append(poolEntries, poolEntry)
					break
				}
			}
		}
		bsConf.Pools = poolEntries
	}
	return bsConf, nil
}
Ejemplo n.º 8
0
func EnvListForEndpoint(dockerEndpoint, poolName string) ([]string, error) {
	bsConf, err := provision.FindScopedConfig(bsUniqueID)
	if err != nil {
		return nil, err
	}
	tsuruEndpoint, _ := config.GetString("host")
	if !strings.HasPrefix(tsuruEndpoint, "http://") && !strings.HasPrefix(tsuruEndpoint, "https://") {
		tsuruEndpoint = "http://" + tsuruEndpoint
	}
	tsuruEndpoint = strings.TrimRight(tsuruEndpoint, "/") + "/"
	endpoint := dockerEndpoint
	socket, _ := config.GetString("docker:bs:socket")
	if socket != "" {
		endpoint = "unix:///var/run/docker.sock"
	}
	token, err := getToken(bsConf)
	if err != nil {
		return nil, err
	}
	baseEnvMap := map[string]string{
		"DOCKER_ENDPOINT":       endpoint,
		"TSURU_ENDPOINT":        tsuruEndpoint,
		"TSURU_TOKEN":           token,
		"SYSLOG_LISTEN_ADDRESS": fmt.Sprintf("udp://0.0.0.0:%d", container.BsSysLogPort()),
	}
	var envList []string
	for envName, envValue := range bsConf.PoolEntries(poolName) {
		if _, isBase := baseEnvMap[envName]; isBase {
			continue
		}
		envList = append(envList, fmt.Sprintf("%s=%s", envName, envValue.Value))
	}
	for name, value := range baseEnvMap {
		envList = append(envList, fmt.Sprintf("%s=%s", name, value))
	}
	return envList, nil
}
Ejemplo n.º 9
0
func (s *S) TestDockerLogUpdate(c *check.C) {
	testCases := []struct {
		conf    provision.ScopedConfig
		entries []provision.Entry
		pools   []provision.PoolEntry
		err     error
	}{
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "fluentd"},
					{Name: "fluentd-address", Value: "localhost:24224"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "fluentd-address", Value: "localhost:24224"},
			}, []provision.PoolEntry{}, nil,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "bs"},
					{Name: "tag", Value: "ahoy"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "fluentd-address", Value: "localhost:24224"},
			}, []provision.PoolEntry{}, ErrLogDriverBSNoParams,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "tag", Value: "ahoy"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "fluentd-address", Value: "localhost:24224"},
			}, []provision.PoolEntry{}, ErrLogDriverMandatory,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "bs"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "bs"},
			}, []provision.PoolEntry{}, nil,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "fluentd"},
					{Name: "tag", Value: "x"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "tag", Value: "x"},
			}, []provision.PoolEntry{}, nil,
		},
		{
			provision.ScopedConfig{
				Pools: []provision.PoolEntry{
					{Name: "p1", Envs: []provision.Entry{
						{Name: "log-driver", Value: "journald"},
						{Name: "tag", Value: "y"},
					}},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "tag", Value: "x"},
			}, []provision.PoolEntry{
				{Name: "p1", Envs: []provision.Entry{
					{Name: "log-driver", Value: "journald"},
					{Name: "tag", Value: "y"},
				}},
			}, nil,
		},
	}
	logConf := DockerLog{}
	for _, testData := range testCases {
		err := logConf.Update(&testData.conf)
		c.Assert(err, check.DeepEquals, testData.err)
		conf, err := provision.FindScopedConfig(dockerLogConfigEntry)
		c.Assert(err, check.IsNil)
		sort.Sort(provision.ConfigEntryList(conf.Envs))
		sort.Sort(provision.ConfigEntryList(testData.entries))
		sort.Sort(provision.ConfigPoolEntryList(conf.Pools))
		sort.Sort(provision.ConfigPoolEntryList(testData.pools))
		for i := range conf.Pools {
			sort.Sort(provision.ConfigEntryList(conf.Pools[i].Envs))
		}
		for i := range testData.pools {
			sort.Sort(provision.ConfigEntryList(testData.pools[i].Envs))
		}
		c.Assert(conf.Envs, check.DeepEquals, testData.entries)
		c.Assert(conf.Pools, check.DeepEquals, testData.pools)
	}
	driver, opts, err := logConf.LogOpts("p1")
	c.Assert(err, check.IsNil)
	c.Assert(driver, check.Equals, "journald")
	c.Assert(opts, check.DeepEquals, map[string]string{"tag": "y"})
	driver, opts, err = logConf.LogOpts("other")
	c.Assert(err, check.IsNil)
	c.Assert(driver, check.Equals, "fluentd")
	c.Assert(opts, check.DeepEquals, map[string]string{"tag": "x"})
}