Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
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
}
Ejemplo n.º 3
0
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
}
Ejemplo n.º 4
0
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
}