Example #1
0
func getImageTags(templateRepos []string, serviceRepos []string, tags []string) (map[string][]string, error) {
	imagemap := make(map[string][]string)

	// find all the template repos
	for _, repo := range templateRepos {
		image, err := docker.FindImage(repo, false)
		if err == docker.ErrNoSuchImage {
			glog.Warningf("Could not find template image %s", repo)
			continue
		} else if err != nil {
			glog.Errorf("Could not look up repo %s: %s", repo, err)
			return nil, err
		}
		if image.ID.Tag == DockerLatest {
			image.ID.Tag = ""
		}
		images := imagemap[image.UUID]
		imagemap[image.UUID] = append(images, image.ID.String())
	}

	// find all the service repos
	for _, repo := range serviceRepos {
		image, err := docker.FindImage(repo, false)
		if err != nil {
			glog.Errorf("Could not look up repo %s: %s", repo, err)
			return nil, err
		}
		if image.ID.Tag == DockerLatest {
			image.ID.Tag = ""
		}
		images := imagemap[image.UUID]
		imagemap[image.UUID] = append(images, image.ID.String())

		for _, tag := range tags {
			image, err := docker.FindImage(commons.JoinRepoTag(repo, tag), false)
			if err == docker.ErrNoSuchImage {
				continue
			} else if err != nil {
				glog.Errorf("Could not look up repo %s: %s", commons.JoinRepoTag(repo, tag), err)
				return nil, err
			}
			images := imagemap[image.UUID]
			imagemap[image.UUID] = append(images, image.ID.String())
		}
	}

	return imagemap, nil
}
Example #2
0
func (svc *IService) create() (*docker.Container, error) {
	var config dockerclient.Config
	cd := &docker.ContainerDefinition{
		dockerclient.CreateContainerOptions{Name: svc.name(), Config: &config},
		dockerclient.HostConfig{},
	}

	config.Image = commons.JoinRepoTag(svc.Repo, svc.Tag)
	config.Cmd = []string{"/bin/sh", "-c", svc.Command()}

	// NOTE: USE WITH CARE!
	// Enabling host networking for an isvc may expose ports
	// of the isvcs to access outside of the serviced host, potentially
	// compromising security.
	if svc.HostNetwork {
		cd.NetworkMode = "host"
		glog.Warningf("Host networking enabled for isvc %s", svc.Name)
	}

	// attach all exported ports
	if svc.PortBindings != nil && len(svc.PortBindings) > 0 {
		config.ExposedPorts = make(map[dockerclient.Port]struct{})
		cd.PortBindings = make(map[dockerclient.Port][]dockerclient.PortBinding)
		for _, binding := range svc.PortBindings {
			port := dockerclient.Port(fmt.Sprintf("%d", binding.HostPort))
			config.ExposedPorts[port] = struct{}{}
			portBinding := dockerclient.PortBinding{
				HostIp:   getHostIp(binding),
				HostPort: port.Port(),
			}

			cd.PortBindings[port] = append(cd.PortBindings[port], portBinding)
		}
	}
	glog.V(1).Infof("Bindings for %s = %v", svc.Name, cd.PortBindings)

	// copy any links to other isvcs
	if svc.Links != nil && len(svc.Links) > 0 {
		// To use a link, the source container must be instantiated already, so
		//    the service using a link can't be in the first start group.
		//
		// FIXME: Other sanity checks we could add - make sure that the source
		//        container is not in the same group or a later group
		if svc.StartGroup == 0 {
			glog.Fatalf("isvc %s can not use docker Links with StartGroup=0", svc.Name)
		}
		cd.Links = make([]string, len(svc.Links))
		copy(cd.Links, svc.Links)
		glog.V(1).Infof("Links for %s = %v", svc.Name, cd.Links)
	}

	// attach all exported volumes
	config.Volumes = make(map[string]struct{})
	cd.Binds = []string{}

	// service-specific volumes
	if svc.Volumes != nil && len(svc.Volumes) > 0 {
		for src, dest := range svc.Volumes {
			hostpath := svc.getResourcePath(src)
			if exists, _ := isDir(hostpath); !exists {
				if err := os.MkdirAll(hostpath, 0777); err != nil {
					glog.Errorf("could not create %s on host: %s", hostpath, err)
					return nil, err
				}
			}
			cd.Binds = append(cd.Binds, fmt.Sprintf("%s:%s", hostpath, dest))
			config.Volumes[dest] = struct{}{}
		}
	}

	// global volumes
	if isvcsVolumes != nil && len(isvcsVolumes) > 0 {
		for src, dest := range isvcsVolumes {
			if exists, _ := isDir(src); !exists {
				glog.Warningf("Could not mount source %s: path does not exist", src)
				continue
			}
			cd.Binds = append(cd.Binds, fmt.Sprintf("%s:%s", src, dest))
			config.Volumes[dest] = struct{}{}
		}
	}

	// attach environment variables
	for key, val := range envPerService[svc.Name] {
		config.Env = append(config.Env, fmt.Sprintf("%s=%s", key, val))
	}

	return docker.NewContainer(cd, false, 5*time.Second, nil, nil)
}