コード例 #1
0
ファイル: exec.go プロジェクト: gregorygtseng/drone-cli
func run(client dockerclient.Client, args []string, input string) (int, error) {

	image := "drone/drone-exec:latest"
	entrypoint := []string{"/bin/drone-exec"}
	args = append(args, "--", input)

	conf := &dockerclient.ContainerConfig{
		Image:      image,
		Entrypoint: entrypoint,
		Cmd:        args,
		HostConfig: dockerclient.HostConfig{
			Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
		},
		Volumes: map[string]struct{}{
			"/var/run/docker.sock": struct{}{},
		},
	}

	info, err := docker.Run(client, conf, false)
	if err != nil {
		return 0, err
	}

	client.StopContainer(info.Id, 15)
	client.RemoveContainer(info.Id, true, true)
	return info.State.ExitCode, err
}
コード例 #2
0
ファイル: build.go プロジェクト: sourcegraph/drone-exec
func (b *Build) walk(node parser.Node, state *State) (err error) {

	switch node := node.(type) {
	case *parser.ListNode:
		for _, node := range node.Nodes {
			err = b.walk(node, state)
			if err != nil {
				break
			}
		}

	case *parser.FilterNode:
		if isMatch(node, state) {
			b.walk(node.Node, state)
		}

	case *parser.DockerNode:
		if shouldSkip(b.flags, node.NodeType) {
			break
		}
		if len(node.Image) == 0 {
			break
		}

		switch node.Type() {

		case parser.NodeBuild:
			// run setup
			// node.Vargs = map[string]interface{}{}
			// node.Vargs["commands"] = node.Commands

			// conf := toContainerConfig(node)
			// conf.Cmd = toCommand(state, node)
			// conf.Image = "plugins/drone-build"
			// info, err := docker.Run(state.Client, conf, node.Pull)
			// if err != nil {
			// 	state.Exit(255)
			// } else if info.State.ExitCode != 0 {
			// 	state.Exit(info.State.ExitCode)
			// }

			// run build
			// conf := toContainerConfig(node)
			// conf.Entrypoint = []string{"/bin/sh", "-e"}
			// conf.Cmd = []string{"/drone/bin/build.sh"}

			conf := toContainerConfig(node)
			conf.Env = append(conf.Env, toEnv(state)...)
			conf.WorkingDir = state.Workspace.Path
			// conf.User = "******"
			if state.Repo.IsPrivate {
				script.Encode(state.Workspace, conf, node)
			} else {
				script.Encode(nil, conf, node)
			}

			info, err := docker.Run(state.Client, conf, node.Pull)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}

		case parser.NodeCompose:
			conf := toContainerConfig(node)
			_, err := docker.Start(state.Client, conf, node.Pull)
			if err != nil {
				state.Exit(255)
			}

		default:
			conf := toContainerConfig(node)
			conf.Cmd = toCommand(state, node)
			info, err := docker.Run(state.Client, conf, node.Pull)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}
		}
	}

	return nil
}
コード例 #3
0
ファイル: build.go プロジェクト: msteinert/drone-exec
func (b *Build) walk(node parser.Node, state *State) (err error) {

	switch node := node.(type) {
	case *parser.ListNode:
		for _, node := range node.Nodes {
			err = b.walk(node, state)
			if err != nil {
				break
			}
		}

	case *parser.FilterNode:
		if isMatch(node, state) {
			b.walk(node.Node, state)
		}

	case *parser.DockerNode:
		if shouldSkip(b.flags, node.NodeType) {
			break
		}
		if len(node.Image) == 0 {
			break
		}
		// auth for accessing private docker registries
		var auth *dockerclient.AuthConfig
		// auth to nil if password or token not set
		if len(node.AuthConfig.Password) != 0 || len(node.AuthConfig.RegistryToken) != 0 {
			auth = &dockerclient.AuthConfig{
				Username:      node.AuthConfig.Username,
				Password:      node.AuthConfig.Password,
				Email:         node.AuthConfig.Email,
				RegistryToken: node.AuthConfig.RegistryToken,
			}
		}
		switch node.Type() {

		case parser.NodeBuild:
			// run setup
			// node.Vargs = map[string]interface{}{}
			// node.Vargs["commands"] = node.Commands

			// conf := toContainerConfig(node)
			// conf.Cmd = toCommand(state, node)
			// conf.Image = "plugins/drone-build"
			// info, err := docker.Run(state.Client, conf, node.Pull)
			// if err != nil {
			// 	state.Exit(255)
			// } else if info.State.ExitCode != 0 {
			// 	state.Exit(info.State.ExitCode)
			// }

			// run build
			// conf := toContainerConfig(node)
			// conf.Entrypoint = []string{"/bin/sh", "-e"}
			// conf.Cmd = []string{"/drone/bin/build.sh"}

			conf := toContainerConfig(node)
			conf.Env = append(conf.Env, toEnv(state)...)
			conf.WorkingDir = state.Workspace.Path
			// conf.User = "******"
			if state.Repo.IsPrivate {
				script.Encode(state.Workspace, conf, node)
			} else {
				script.Encode(nil, conf, node)
			}

			info, err := docker.Run(state.Client, conf, auth, node.Pull, state.Stdout, state.Stderr)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}

		case parser.NodeCompose:
			conf := toContainerConfig(node)
			_, err := docker.Start(state.Client, conf, auth, node.Pull)
			if err != nil {
				state.Exit(255)
			}

		default:
			conf := toContainerConfig(node)
			conf.Cmd = toCommand(state, node)
			info, err := docker.Run(state.Client, conf, auth, node.Pull, state.Stdout, state.Stderr)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}
		}
	}

	return nil
}
コード例 #4
0
ファイル: build.go プロジェクト: objectpartners/drone-exec
func (b *Build) walk(node parser.Node, state *State) (err error) {

	switch node := node.(type) {
	case *parser.ListNode:
		for _, node := range node.Nodes {
			err = b.walk(node, state)
			if err != nil {
				break
			}
		}

	case *parser.FilterNode:
		if isMatch(node, state) {
			b.walk(node.Node, state)
		}

	case *parser.DockerNode:
		if shouldSkip(b.flags, node.NodeType) {
			break
		}
		if len(node.Image) == 0 {
			break
		}
		// auth for accessing private docker registries
		var auth *dockerclient.AuthConfig
		// auth to nil if password or token not set
		if len(node.AuthConfig.Password) != 0 || len(node.AuthConfig.RegistryToken) != 0 {
			auth = &dockerclient.AuthConfig{
				Username:      node.AuthConfig.Username,
				Password:      node.AuthConfig.Password,
				Email:         node.AuthConfig.Email,
				RegistryToken: node.AuthConfig.RegistryToken,
			}
		}
		switch node.Type() {

		case parser.NodeBuild:
			// TODO(bradrydzewski) this should be handled by the when block
			// by defaulting the build steps to run when not failure. This is
			// required now that we support multi-build steps.
			if state.Failed() {
				return
			}

			conf := toContainerConfig(node)
			conf.Env = append(conf.Env, toEnv(state)...)
			conf.WorkingDir = state.Workspace.Path
			if state.Repo.IsPrivate {
				script.Encode(state.Workspace, conf, node)
			} else {
				script.Encode(nil, conf, node)
			}

			info, err := docker.Run(state.Client, conf, auth, node.Pull, state.Stdout, state.Stderr)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}

		case parser.NodeCompose:
			conf := toContainerConfig(node)
			_, err := docker.Start(state.Client, conf, auth, node.Pull)
			if err != nil {
				state.Exit(255)
			}

		default:
			conf := toContainerConfig(node)
			conf.Cmd = toCommand(state, node)
			info, err := docker.Run(state.Client, conf, auth, node.Pull, state.Stdout, state.Stderr)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}
		}
	}

	return nil
}
コード例 #5
0
ファイル: build.go プロジェクト: jackspirou/drone-exec
func (b *Build) walk(node parser.Node, state *State) (err error) {

	switch node := node.(type) {
	case *parser.ListNode:
		for _, node := range node.Nodes {
			err = b.walk(node, state)
			if err != nil {
				break
			}
		}

	case *parser.FilterNode:
		if isMatch(node, state) {
			b.walk(node.Node, state)
		}

	case *parser.DockerNode:
		if shouldSkip(b.flags, node.NodeType) {
			break
		}
		if len(node.Image) == 0 {
			break
		}
		// auth for accessing private docker registries
		var auth *dockerclient.AuthConfig
		// auth to nil if password or token not set
		if len(node.AuthConfig.Password) != 0 || len(node.AuthConfig.RegistryToken) != 0 {
			auth = &dockerclient.AuthConfig{
				Username:      node.AuthConfig.Username,
				Password:      node.AuthConfig.Password,
				Email:         node.AuthConfig.Email,
				RegistryToken: node.AuthConfig.RegistryToken,
			}
		}
		switch node.Type() {

		case parser.NodeBuild:
			// TODO(bradrydzewski) this should be handled by the when block
			// by defaulting the build steps to run when not failure. This is
			// required now that we support multi-build steps.
			if state.Failed() {
				return
			}

			conf := toContainerConfig(node)
			conf.Env = append(conf.Env, toEnv(state)...)
			conf.WorkingDir = state.Workspace.Path
			if state.Repo.IsPrivate {
				script.Encode(state.Workspace, conf, node)
			} else {
				script.Encode(nil, conf, node)
			}

			info, err := docker.Run(state.Client, conf, auth, node.Pull, state.Stdout, state.Stderr)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			} else if info.State.OOMKilled && !node.OomKillDisable {
				// NOTE: we suppress this message if OomKillDisable is enabled
				// because older versions of Docker 1.7 are firing false positivies.
				log.Errorln("OOMKill received. Exiting Build")
				state.Exit(1)
			} else if info.State.Running {
				log.Errorln("Error decoding Docker logs. Exiting Build.")
				state.Exit(1)
			}

		case parser.NodeCompose:
			conf := toContainerConfig(node)
			_, err := docker.Start(state.Client, conf, auth, node.Pull)
			if err != nil {
				state.Exit(255)
			}

		default:
			// TODO(bradrydzewski) this should be handled by the when block
			// by defaulting the build steps to run when not failure. This is
			// required now that we support multi-build steps.
			//
			// Note we should always send notifications regardless of the
			// build status, since people typically want failure notifications.
			if state.Failed() && node.Type() != parser.NodeNotify {
				return
			}

			conf := toContainerConfig(node)
			conf.Cmd = toCommand(state, node)
			info, err := docker.Run(state.Client, conf, auth, node.Pull, state.Stdout, state.Stderr)
			if err != nil {
				state.Exit(255)
			} else if info.State.ExitCode != 0 {
				state.Exit(info.State.ExitCode)
			}
		}
	}

	return nil
}