コード例 #1
0
ファイル: script.go プロジェクト: fracting/drone-exec
// Encode encodes the build script as a command in the
// provided Container config. For linux, the build script
// is embedded as the container entrypoint command, base64
// encoded as a one-line script.
func Encode(w *plugin.Workspace, c *dockerclient.ContainerConfig, n *parser.DockerNode) {
	var buf bytes.Buffer
	buf.WriteString(setupScript)
	buf.WriteString(forceYesScript)

	if w != nil && w.Keys != nil && w.Netrc != nil {
		buf.WriteString(writeKey(
			w.Keys.Private,
		))
		buf.WriteString(writeNetrc(
			w.Netrc.Machine,
			w.Netrc.Login,
			w.Netrc.Password,
		))
	}

	if len(n.Shell) > 0 {
		buf.WriteString(fmt.Sprintf("%s << \"EOF\"", n.Shell))
	}

	buf.WriteString(writeCmds(n.Commands))

	if len(n.Shell) > 0 {
		buf.WriteString("EOF")
	}

	buf.WriteString(teardownScript)

	c.Entrypoint = entrypoint
	c.Cmd = []string{encode(buf.Bytes())}
}
コード例 #2
0
ファイル: build.go プロジェクト: fracting/drone-exec
func maybeEscalate(conf dockerclient.ContainerConfig, node *parser.DockerNode) {
	if node.Image == "plugins/drone-docker" || node.Image == "plugins/drone-gcr" || node.Image == "plugins/drone-ecr" {
		return
	}
	conf.Volumes = nil
	conf.HostConfig.NetworkMode = ""
	conf.HostConfig.Privileged = true
	conf.Entrypoint = []string{}
	conf.Cmd = []string{}
}
コード例 #3
0
ファイル: service.go プロジェクト: yansmallb/tycoon-go
func CreateService(config *ServiceConfig) ([]string, error) {
	swarm, err := swarmclient.NewSwarmClient()
	if err != nil {
		log.Fatalf("service.CreateService():%s\n", err)
		return nil, err
	}

	containerIds := make([]string, 0)
	containerConfig := new(dockerclient.ContainerConfig)
	containerConfig.Image = config.Spec.Image
	containerConfig.Labels = config.Metadata.Labels
	portBindings := make(map[string][]dockerclient.PortBinding)
	containerConfig.Env = config.Spec.Selector
	containerConfig.Cmd = config.Spec.Cmd

	//exposed ports , so that others can't use
	if len(config.Spec.Ports) > 0 {
		ports := make(map[string]struct{})
		for index := range config.Spec.Ports {
			port := strconv.Itoa(config.Spec.Ports[index])
			ports[port] = struct{}{}
		}
		containerConfig.ExposedPorts = ports
	}

	// intit hostconfig. use host, create and start containers
	hostConfig := &dockerclient.HostConfig{}
	if config.Spec.Resources.NetworkMode != "" {
		hostConfig.NetworkMode = config.Spec.Resources.NetworkMode
	} else {
		hostConfig.NetworkMode = "host"
	}
	// use to filter Resources
	hostConfig.CpuShares = config.Spec.Resources.CpuShares
	hostConfig.CpusetCpus = config.Spec.Resources.CpusetCpus
	hostConfig.Memory = config.Spec.Resources.Memory
	hostConfig.MemorySwap = config.Spec.Resources.MemorySwap

	cpuQuota, err := strconv.Atoi(config.Spec.Resources.CpuQuota)
	if err != nil {
		hostConfig.CpuQuota = 0
	} else {
		hostConfig.CpuQuota = int64(cpuQuota)
	}

	// replicas
	numOfTimes := 0
	if config.Spec.Replicas > 0 {
		numOfTimes = config.Spec.Replicas
	} else if config.Spec.Replicas == 0 {
		numOfTimes = len(config.Spec.Ips)
		//****
		if len(config.Spec.Ports) == 0 {
			err := fmt.Errorf("service.CreateService():%+s Give ips but not give ports\n", config.Metadata.Name)
			log.Error(err)
			return nil, err
		}
	}

	for i := 0; i < numOfTimes; i++ {
		// use to filter specific ips and ports
		for _, port := range config.Spec.Ports {
			portbinding := &dockerclient.PortBinding{HostPort: strconv.Itoa(port)}
			if len(config.Spec.Ips) > 0 {
				portbinding.HostIp = config.Spec.Ips[i]
			}
			portBindings[strconv.Itoa(port)] = []dockerclient.PortBinding{*portbinding}
		}
		hostConfig.PortBindings = portBindings

		// hostconfig
		containerConfig.HostConfig = *hostConfig

		// give container different name
		containerName := config.Metadata.Name
		if role := config.Metadata.Labels["role"]; role != "" {
			containerName += "_" + role
		}
		containerName += strconv.Itoa(i)
		log.Debugf("service.CreateService():containerName:%s ; containerConfig:%+v\n", containerName, containerConfig)

		//create container
		containerId, err := swarm.CreateContainer(containerConfig, containerName)
		log.Debugf("service.CreateService():containerId:%s\n", containerId)
		if err != nil {
			log.Fatalf("service.CreateService():%s\n", err)
			fmt.Printf("[error]service.CreateService():%+v\n", err)
		}

		containerIds = append(containerIds, containerId)
		if config.Metadata.Labels["type"] != "libvirt" {
			//docker start container,libvirt do not need to start
			swarm.StartContainer(containerId, hostConfig)
			if err != nil {
				log.Fatalf("service.CreateService():%s\n", err)
				fmt.Printf("[error]service.CreateService():%+v\n", err)
				return nil, err
			}
		}
	}
	return containerIds, err
}