Example #1
0
func splitTypeHostId(value string) (res ResourceType, host string, id containers.Identifier, err error) {
	if value == "" {
		err = errors.New("The identifier must be specified as <host>/<id> or <id>")
		return
	}

	locatorParts := strings.SplitN(value, "://", 2)
	if len(locatorParts) == 2 {
		res = ResourceType(locatorParts[0])
		value = locatorParts[1]
	}

	sections := strings.SplitN(value, "/", 2)
	if len(sections) == 1 {
		id, err = containers.NewIdentifier(sections[0])
		return
	}
	id, err = containers.NewIdentifier(sections[1])
	if err != nil {
		return
	}
	if strings.TrimSpace(sections[0]) == "" {
		err = errors.New("You must specify <host>/<id> or <id>")
		return
	}
	host = sections[0]
	return
}
Example #2
0
func (s *IntegrationTestSuite) TestSamePortRejected(c *chk.C) {
	id, err := containers.NewIdentifier("TestSamePortRejected")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--ports=8080:39485")
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	active, _ := s.unitState(id)
	c.Assert(active, chk.Equals, "inactive")

	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
	paths, err := filepath.Glob(id.VersionedUnitPathFor("*"))
	c.Assert(err, chk.IsNil)
	for _, p := range paths {
		s.assertFilePresent(c, p, 0664, true)
	}

	id2, _ := containers.NewIdentifier("TestSamePortRejected2")
	cmd = exec.Command("/usr/bin/gear", "install", TestImage, fmt.Sprintf("%v/%v", s.daemonURI, id2), "--ports=8080:39485")
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.ErrorMatches, "exit status 1")
	state, substate := s.unitState(id2)
	c.Assert(state, chk.Equals, "inactive")
	c.Assert(substate, chk.Equals, "dead")
}
Example #3
0
func GetDockerContainerPacketCounts(d *docker.DockerClient) (map[containers.Identifier]int, error) {
	serviceFiles, err := filepath.Glob(filepath.Join(gearconfig.ContainerBasePath(), "units", "**", containers.IdentifierPrefix+"*.service"))
	if err != nil {
		return nil, err
	}

	ids := make([]string, 0)
	packetCount := make(map[containers.Identifier]int)

	for _, s := range serviceFiles {
		id := filepath.Base(s)
		if strings.HasPrefix(id, containers.IdentifierPrefix) && strings.HasSuffix(id, ".service") {
			id = id[len(containers.IdentifierPrefix):(len(id) - len(".service"))]
			if id, err := containers.NewIdentifier(id); err == nil {
				ids = append(ids, string(id))
				packetCount[id] = 0
			}
		}
	}

	containerIPs, err := d.GetContainerIPs(ids)
	if err != nil {
		return nil, err
	}

	cmd := exec.Command("/sbin/iptables-save", "-c")
	output, err := cmd.Output()
	if err != nil {
		return nil, err
	}

	scan := bufio.NewScanner(bytes.NewBuffer(output))
	for scan.Scan() {
		line := scan.Text()
		if strings.Contains(line, "-A DOCKER ! -i docker0") && strings.Contains(line, "-j DNAT") {
			//Example: [0:0] -A DOCKER ! -i docker0 -p tcp -m tcp --dport 4000 -j DNAT --to-destination 172.17.0.3:8080
			items := strings.Fields(line)
			packets, _ := strconv.Atoi(strings.Split(items[0], ":")[0][1:])
			destIp := strings.Split(items[15], ":")[0]
			id, _ := containers.NewIdentifier(containerIPs[destIp])

			packetCount[id] = packetCount[id] + packets
		}

		if strings.Contains(line, "-A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport") && strings.Contains(line, "-m comment --comment ") {
			//Example: [5850:394136] -A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 4000 -m comment --comment 0001 -j ACCEPT
			items := strings.Fields(line)
			packets, _ := strconv.Atoi(strings.Split(items[0], ":")[0][1:])
			if id, err := containers.NewIdentifier(items[14]); err == nil {
				packetCount[id] = packetCount[id] + packets
			}
		}
	}

	return packetCount, nil
}
Example #4
0
func (s *IntegrationTestSuite) TestInstallEnvFile(c *chk.C) {
	id, err := containers.NewIdentifier("TestInstallEnvFile")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	// get the full path to this .go file so we can get the correct path to the
	// simple.env file
	_, filename, _, _ := runtime.Caller(0)
	envFile := path.Join(path.Dir(filename), "..", "deployment", "fixtures", "simple.env")
	cmd := exec.Command("/usr/bin/gear", "install", EnvImage, hostContainerId, "--env-file="+envFile, "--start")
	data, err := cmd.CombinedOutput()
	c.Log(cmd.Args)
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerStarts(c, id)

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "TEST=value"), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "QUOTED=\\\"foo\\\""), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "IGNORED"), chk.Equals, false)
}
Example #5
0
func (p Port) IdentifierFor() (containers.Identifier, error) {
	_, portPath := p.PortPathsFor()

	r, err := os.Open(portPath)
	if err != nil {
		return "", err
	}
	defer r.Close()

	scan := bufio.NewScanner(r)
	for scan.Scan() {
		line := scan.Text()
		if strings.HasPrefix(line, "X-ContainerId=") {
			id, err := containers.NewIdentifier(strings.TrimPrefix(line, "X-ContainerId="))
			if err != nil {
				return "", err
			}
			return id, nil
		}
	}
	if scan.Err() != nil {
		return "", scan.Err()
	}
	return "", fmt.Errorf("Container ID not found")
}
Example #6
0
func TestShouldSendRemoteJob(t *testing.T) {
	trans := &testTransport{}
	localhost := &testLocator{"localhost"}
	initCalled := false
	locator := &ResourceLocator{"ctr", "foobar", localhost}

	Executor{
		On: Locators{locator},
		Serial: func(on Locator) JobRequest {
			if on != locator {
				t.Fatalf("Expected locator passed to Serial() to be identical to %+v", locator)
			}
			id, _ := containers.NewIdentifier(on.(*ResourceLocator).Id)
			return &cjobs.StoppedContainerStateRequest{
				Id: id,
			}
		},
		Transport: trans,
	}.Gather()

	if initCalled {
		t.Errorf("Local initialization should be bypassed for remote transports")
	}
	if _, ok := trans.Translated["localhost"]; !ok {
		t.Errorf("Job for localhost was not enqueued in %+v", trans.Invoked)
	}
	if _, ok := trans.Invoked["localhost"]; !ok {
		t.Errorf("Job for localhost was not enqueued in %+v", trans.Invoked)
	}
}
Example #7
0
func HandleDeleteContainerRequest(conf *http.HttpConfiguration, context *http.HttpContext, r *rest.Request) (interface{}, error) {
	id, errg := containers.NewIdentifier(r.PathParam("id"))
	if errg != nil {
		return nil, errg
	}
	return &cjobs.DeleteContainerRequest{Id: id}, nil
}
Example #8
0
func (s *IntegrationTestSuite) TestInstallEnv(c *chk.C) {
	id, err := containers.NewIdentifier("TestInstallEnv")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)
	hostEnvId := fmt.Sprintf("%v/%v", s.daemonURI, "foobar")

	cmd := exec.Command("/usr/bin/gear", "install", EnvImage, hostContainerId, "--env-id=foobar", "A=B", "C=D", "--start")
	data, err := cmd.CombinedOutput()
	c.Log(cmd.Args)
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerStarts(c, id)

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "A=B"), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "C=D"), chk.Equals, true)

	cmd = exec.Command("/usr/bin/gear", "env", hostEnvId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(string(data), chk.Equals, "A=B\nC=D\n")
}
Example #9
0
func (s *IntegrationTestSuite) TestStartStopContainer(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest003")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--ports=8080:4001", "--isolate")
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)

	cmd = exec.Command("/usr/bin/gear", "start", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerState(c, id, CONTAINER_STARTED)
	s.assertFilePresent(c, filepath.Join(id.HomePath(), "container-init.sh"), 0700, false)

	ports, err := containers.GetExistingPorts(id)
	c.Assert(err, chk.IsNil)
	c.Assert(len(ports), chk.Equals, 1)

	resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:%v", ports[0].External))
	c.Assert(err, chk.IsNil)
	c.Assert(resp.StatusCode, chk.Equals, 200)

	cmd = exec.Command("/usr/bin/gear", "stop", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerState(c, id, CONTAINER_STOPPED)
}
Example #10
0
func (s *IntegrationTestSuite) TestSimpleInstallWithEnv(c *chk.C) {
	if !*hasEnvFile {
		c.Skip("-env-file not specified")
	}

	id, err := containers.NewIdentifier("IntTest008")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", EnvImage, hostContainerId, "--env-file=deployment/fixtures/simple.env", "--start")
	data, err := cmd.CombinedOutput()
	c.Log(cmd.Args)
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	time.Sleep(time.Second * 5) // startup time is indeterminate unfortunately because gear init --post continues to run

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "TEST=\"value\""), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "QUOTED=\"foo\""), chk.Equals, true)
}
Example #11
0
func (r repositoryPermission) CreatePermission(locator ssh.KeyLocator, value *utils.RawMessage) error {
	p := RepositoryPermission{}
	if value != nil {
		if err := json.Unmarshal(*value, &p); err != nil {
			return err
		}
	}

	id, err := containers.NewIdentifier(p.Id)
	if err != nil {
		return err
	}
	repoId := RepoIdentifier(id)

	if _, err := os.Stat(repoId.RepositoryPathFor()); err != nil {
		return err
	}
	accessPath := repoId.GitAccessPathFor(locator.NameForKey(), p.Write)

	if err := os.Symlink(locator.PathToKey(), accessPath); err != nil && !os.IsExist(err) {
		return err
	}
	negAccessPath := repoId.GitAccessPathFor(locator.NameForKey(), !p.Write)
	if err := os.Remove(negAccessPath); err != nil && !os.IsNotExist(err) {
		return err
	}
	if _, err := os.Stat(repoId.AuthKeysPathFor()); err == nil {
		if err := os.Remove(repoId.AuthKeysPathFor()); err != nil && !os.IsNotExist(err) {
			return err
		}
	}
	return nil
}
Example #12
0
func (s *IntegrationTestSuite) TestLongContainerName(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest006xxxxxxxxxxxxxx")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--start", "--ports=8080:4003", "--isolate")
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerState(c, id, CONTAINER_STARTED)

	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
	s.assertFilePresent(c, filepath.Join(id.HomePath(), "container-init.sh"), 0700, false)

	ports, err := containers.GetExistingPorts(id)
	c.Assert(err, chk.IsNil)
	c.Assert(len(ports), chk.Equals, 1)

	t := time.NewTicker(time.Second / 10)
	defer t.Stop()
	select {
	case <-t.C:
		resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:%v", ports[0].External))
		if err == nil {
			c.Assert(resp.StatusCode, chk.Equals, 200)
		}
	case <-time.After(time.Second * 15):
		c.Fail()
	}
}
Example #13
0
func (s *IntegrationTestSuite) TestSimpleInstallAndStartImage(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest000")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId)
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerState(c, id, CONTAINER_STOPPED)

	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
	paths, err := filepath.Glob(id.VersionedUnitPathFor("*"))
	c.Assert(err, chk.IsNil)
	for _, p := range paths {
		s.assertFilePresent(c, p, 0664, true)
	}
	s.assertFileAbsent(c, filepath.Join(id.HomePath(), "container-init.sh"))

	ports, err := containers.GetExistingPorts(id)
	c.Assert(err, chk.IsNil)
	c.Assert(len(ports), chk.Equals, 0)

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "Loaded: loaded (/var/lib/containers/units/In/ctr-IntTest000.service; enabled)"), chk.Equals, true)
	s.assertContainerState(c, id, CONTAINER_STOPPED)
}
Example #14
0
func (s *IntegrationTestSuite) TestContainerNetLinks(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest007")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--ports=8080:4004", "--isolate")
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)

	cmd = exec.Command("/usr/bin/gear", "link", "-n", "127.0.0.1:8081:74.125.239.114:80", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)

	cmd = exec.Command("/usr/bin/gear", "start", hostContainerId)
	data, err = cmd.CombinedOutput()
	s.assertContainerState(c, id, CONTAINER_STARTED)
	s.assertFilePresent(c, filepath.Join(id.HomePath(), "container-init.sh"), 0700, false)

	cmd = exec.Command("/usr/bin/switchns", "--container="+id.ContainerFor(), "--", "/sbin/iptables", "-t", "nat", "-L")
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "tcp dpt:tproxy to:74.125.239.114"), chk.Equals, true)

	cmd = exec.Command("/usr/bin/gear", "stop", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerState(c, id, CONTAINER_STOPPED)
}
Example #15
0
func (c containerPermission) CreatePermission(locator KeyLocator, value *utils.RawMessage) error {
	var idString string
	if value != nil {
		if err := json.Unmarshal(*value, &idString); err != nil {
			return err
		}
	}

	id, err := containers.NewIdentifier(idString)
	if err != nil {
		return err
	}

	if _, err := os.Stat(id.UnitPathFor()); err != nil {
		return err
	}
	if err := os.Symlink(locator.PathToKey(), SshAccessPathFor(id, locator.NameForKey())); err != nil && !os.IsExist(err) {
		return err
	}
	if _, err := os.Stat(id.AuthKeysPathFor()); err == nil {
		if err := os.Remove(id.AuthKeysPathFor()); err != nil {
			return err
		}
	}
	return nil
}
Example #16
0
func (d *Deployment) createInstances(c *Container) error {
	for i := len(c.instances); i < c.Count; i++ {
		var id containers.Identifier
		var err error
		if d.RandomizeIds {
			id, err = containers.NewRandomIdentifier(d.IdPrefix)
		} else {
			id, err = containers.NewIdentifier(d.IdPrefix + c.Name + "-" + strconv.Itoa(i+1))
		}
		if err != nil {
			return err
		}
		instance := &Instance{
			Id:    id,
			From:  c.Name,
			Image: c.Image,
			Ports: newPortMappings(c.PublicPorts),

			container: c,
			add:       true,
		}
		c.AddInstance(instance)
	}
	return nil
}
Example #17
0
func (s *IntegrationTestSuite) TestLongContainerName(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest006xxxxxxxxxxxxxx")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--start", "--ports=8080:0", "--isolate")
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerStarts(c, id)

	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
	s.assertFilePresent(c, filepath.Join(id.RunPathFor(), "container-init.sh"), 0700, false)

	ports, err := containers.GetExistingPorts(id)
	c.Assert(err, chk.IsNil)
	c.Assert(len(ports), chk.Equals, 1)

	httpAlive := func() bool {
		resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:%v", ports[0].External))
		if err == nil {
			c.Assert(resp.StatusCode, chk.Equals, 200)
			return true
		}
		return false
	}
	if !until(TimeoutContainerStateChange, IntervalHttpCheck, httpAlive) {
		c.Errorf("Unable to retrieve a 200 status code from port %d", ports[0].External)
		c.FailNow()
	}
}
Example #18
0
func (s *IntegrationTestSuite) TestRestartContainer(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest004")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--ports=8080:4002", "--start", "--isolate")
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
	s.assertContainerState(c, id, CONTAINER_STARTED)
	s.assertFilePresent(c, filepath.Join(id.HomePath(), "container-init.sh"), 0700, false)
	oldPid := s.getContainerPid(id)

	cmd = exec.Command("/usr/bin/gear", "restart", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerState(c, id, CONTAINER_RESTARTED)

	newPid := s.getContainerPid(id)
	c.Assert(oldPid, chk.Not(chk.Equals), newPid)
}
Example #19
0
func (s *IntegrationTestSuite) TestSimpleInstallWithEnv(c *chk.C) {
	if !*hasEnvFile {
		c.Skip("-env-file not specified")
	}

	id, err := containers.NewIdentifier("IntTest008")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", EnvImage, hostContainerId, "--env-file=deployment/fixtures/simple.env", "--start")
	data, err := cmd.CombinedOutput()
	c.Log(cmd.Args)
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerStarts(c, id)

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()

	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "TEST=\"value\""), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "QUOTED=\"\\\"foo\\\"\""), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "IGNORED"), chk.Equals, false)

	cmd = exec.Command("/usr/bin/gear", "stop", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertContainerStops(c, id, true)
}
Example #20
0
func (h *HttpRestartContainerRequest) Handler(conf *http.HttpConfiguration) http.JobHandler {
	return func(context *jobs.JobContext, r *rest.Request) (interface{}, error) {
		id, errg := containers.NewIdentifier(r.PathParam("id"))
		if errg != nil {
			return nil, errg
		}
		return &cjobs.RestartContainerRequest{id}, nil
	}
}
Example #21
0
func (h *HttpDeleteContainerRequest) Handler(conf *HttpConfiguration) JobHandler {
	return func(context *jobs.JobContext, r *rest.Request) (jobs.Job, error) {
		id, errg := containers.NewIdentifier(r.PathParam("id"))
		if errg != nil {
			return nil, errg
		}
		return &jobs.DeleteContainerRequest{id}, nil
	}
}
Example #22
0
func NewIdentifierFromUser(u *user.User) (RepoIdentifier, error) {
	if !strings.HasPrefix(u.Username, RepoIdentifierPrefix) || u.Name != "Repository user" {
		return "", errors.New("Not a repository user")
	}
	id := strings.TrimLeft(u.Username, RepoIdentifierPrefix)
	containerId, err := containers.NewIdentifier(id)
	if err != nil {
		return "", err
	}
	return RepoIdentifier(containerId), nil
}
Example #23
0
func HandleCreateRepositoryRequest(conf *http.HttpConfiguration, context *http.HttpContext, r *rest.Request) (interface{}, error) {
	repositoryId, errg := containers.NewIdentifier(r.PathParam("id"))
	if errg != nil {
		return nil, errg
	}
	// TODO: convert token into a safe clone spec and commit hash
	return &gitjobs.CreateRepositoryRequest{
		git.RepoIdentifier(repositoryId),
		r.URL.Query().Get("source"),
		context.Id,
	}, nil
}
Example #24
0
func HandlePatchEnvironmentRequest(conf *http.HttpConfiguration, context *http.HttpContext, r *rest.Request) (interface{}, error) {
	id, errg := containers.NewIdentifier(r.PathParam("id"))
	if errg != nil {
		return nil, errg
	}
	data := containers.EnvironmentDescription{}
	if err := decodeAndCheck(r, &data); err != nil {
		return nil, err
	}
	data.Id = id
	return &cjobs.PatchEnvironmentRequest{data}, nil
}
Example #25
0
func HandleStopContainerRequest(conf *http.HttpConfiguration, context *http.HttpContext, r *rest.Request) (interface{}, error) {
	id, errg := containers.NewIdentifier(r.PathParam("id"))
	if errg != nil {
		return nil, errg
	}
	data := &cjobs.StoppedContainerStateRequest{}
	if err := decodeAndCheck(r, data); err != nil {
		return nil, err
	}
	data.Id = id
	return data, nil
}
Example #26
0
func NewContainerLocators(t transport.Transport, values ...string) (Locators, error) {
	locators, err := NewResourceLocators(t, ResourceTypeContainer, values...)
	if err != nil {
		return Locators{}, err
	}
	for i := range locators {
		_, err := containers.NewIdentifier(locators[i].(*ResourceLocator).Id)
		if err != nil {
			return Locators{}, err
		}
	}
	return locators, nil
}
Example #27
0
func GetIdlerRules(lookupId containers.Identifier, active bool) (map[string]bool, error) {
	cmd := exec.Command("/sbin/iptables-save", "-c")
	output, err := cmd.Output()
	if err != nil {
		return nil, err
	}

	scan := bufio.NewScanner(bytes.NewBuffer(output))
	ports := make(map[string]bool)

	for scan.Scan() {
		line := scan.Text()
		items := strings.Fields(line)

		var (
			port string
			id   string
		)

		if config.UsePreroutingIdler {
			if !active && strings.Contains(line, "-A PREROUTING") && strings.Contains(line, "-j NFQUEUE --queue-num 0") {
				port = items[10]
				id = items[14]
			} else if active && strings.Contains(line, "-A OUTPUT") && strings.Contains(line, "-j ACCEPT") && strings.Contains(line, "-m comment --comment ") {
				port = items[10]
				id = items[14]
			} else {
				continue
			}
		} else {
			if active && strings.Contains(line, "-A INPUT") && strings.Contains(line, "-j NFQUEUE --queue-num 0") {
				port = items[10]
				id = items[14]
			} else {
				continue
			}
		}

		ruleId, err := containers.NewIdentifier(id)
		if err != nil {
			return nil, err
		}

		if ruleId != lookupId {
			continue
		}

		ports[port] = true
	}
	return ports, nil
}
Example #28
0
func (link *ContainerLink) Check() error {
	if link.Id == "" {
		return errors.New("Container identifier may not be empty")
	}
	if _, err := containers.NewIdentifier(string(link.Id)); err != nil {
		return err
	}
	for i := range link.NetworkLinks {
		if err := link.NetworkLinks[i].Check(); err != nil {
			return err
		}
	}
	return nil
}
Example #29
0
func HandleGitArchiveContentRequest(conf *http.HttpConfiguration, context *http.HttpContext, r *rest.Request) (interface{}, error) {
	repoId, errr := containers.NewIdentifier(r.PathParam("id"))
	if errr != nil {
		return nil, jobs.SimpleError{jobs.ResponseInvalidRequest, fmt.Sprintf("Invalid repository identifier: %s", errr.Error())}
	}
	ref, errc := gitjobs.NewGitCommitRef(r.PathParam("ref"))
	if errc != nil {
		return nil, jobs.SimpleError{jobs.ResponseInvalidRequest, fmt.Sprintf("Invalid commit ref: %s", errc.Error())}
	}

	return &gitjobs.GitArchiveContentRequest{
		git.RepoIdentifier(repoId),
		ref,
	}, nil
}
Example #30
0
func (s *IntegrationTestSuite) TestStatus(c *chk.C) {
	id, err := containers.NewIdentifier("IntTest005")
	c.Assert(err, chk.IsNil)
	s.containerIds = append(s.containerIds, id)

	hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

	cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId)
	data, err := cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
	active, _ := s.unitState(id)
	if active == "failed" {
		c.Logf("Container %s has previous recorded 'failed' state, convert to 'inactive'", id)
		active = "inactive"
	}
	c.Assert(active, chk.Equals, "inactive")

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "Loaded: loaded (/var/lib/containers/units/In/ctr-IntTest005.service; enabled)"), chk.Equals, true)

	cmd = exec.Command("/usr/bin/gear", "start", hostContainerId)
	_, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	s.assertContainerStarts(c, id)

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Log(string(data))
	c.Assert(err, chk.IsNil)
	c.Assert(strings.Contains(string(data), "Loaded: loaded (/var/lib/containers/units/In/ctr-IntTest005.service; enabled)"), chk.Equals, true)
	c.Assert(strings.Contains(string(data), "Active: active (running)"), chk.Equals, true)

	cmd = exec.Command("/usr/bin/gear", "stop", hostContainerId)
	_, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	s.assertContainerStops(c, id, true)

	cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
	data, err = cmd.CombinedOutput()
	c.Assert(err, chk.IsNil)
	c.Log(string(data))
	c.Assert(strings.Contains(string(data), "Loaded: loaded (/var/lib/containers/units/In/ctr-IntTest005.service; enabled)"), chk.Equals, true)
}