Example #1
0
// NewWatchStep is a special step for doing docker pushes
func NewWatchStep(stepConfig *core.StepConfig, options *core.PipelineOptions, dockerOptions *DockerOptions) (*WatchStep, error) {
	name := "watch"
	displayName := "watch"
	if stepConfig.Name != "" {
		displayName = stepConfig.Name
	}

	// Add a random number to the name to prevent collisions on disk
	stepSafeID := fmt.Sprintf("%s-%s", name, uuid.NewRandom().String())

	baseStep := core.NewBaseStep(core.BaseStepOptions{
		DisplayName: displayName,
		Env:         util.NewEnvironment(),
		ID:          name,
		Name:        name,
		Owner:       "wercker",
		SafeID:      stepSafeID,
		Version:     util.Version(),
	})

	return &WatchStep{
		BaseStep:      baseStep,
		options:       options,
		dockerOptions: dockerOptions,
		data:          stepConfig.Data,
		logger:        util.RootLogger().WithField("Logger", "WatchStep"),
	}, nil
}
Example #2
0
func TestBuildEnvironment(t *testing.T) {
	env := util.NewEnvironment("X_FOO=bar", "BAZ=fizz")
	passthru := env.GetPassthru().Ordered()
	if len(passthru) != 1 {
		t.Fatal("Expected only one variable in passthru")
	}
	if passthru[0][0] != "FOO" {
		t.Fatal("Expected to find key 'FOO'")
	}
	if passthru[0][1] != "bar" {
		t.Fatal("Expected to find value 'bar'")
	}
}
Example #3
0
func boxByID(s string) (core.Box, error) {
	settings := util.NewCheapSettings(nil)
	env := util.NewEnvironment()
	dockerOptions, err := NewDockerOptions(settings, env)
	if err != nil {
		return nil, err
	}
	return NewDockerBox(
		&core.BoxConfig{ID: s},
		core.EmptyPipelineOptions(),
		dockerOptions,
	)
}
Example #4
0
func DefaultTestPipelineOptions(s *util.TestSuite, more map[string]interface{}) *PipelineOptions {
	overrides := map[string]interface{}{
		"debug": true,
		// "target":      "test",
		"working-dir": s.WorkingDir(),
	}
	for k, v := range more {
		overrides[k] = v
	}

	settings := util.NewCheapSettings(overrides)

	options, err := NewPipelineOptions(settings, util.NewEnvironment())
	if err != nil {
		s.Error(err)
	}
	return options
}
Example #5
0
func (s *ConfigSuite) TestConfigBoxStructs() {
	b, err := ioutil.ReadFile("../tests/box_structs.yml")
	s.Nil(err)
	config, err := ConfigFromYaml(b)
	s.Require().Nil(err)
	s.Equal("structs_box", config.Box.ID)
	s.Equal("structs_service", config.Services[0].ID)
	assert.NotNil(s.T(), config.Box.Auth)
	assert.NotNil(s.T(), config.Services[0].Auth)

	pipeline := config.PipelinesMap["pipeline"]
	s.Equal(pipeline.Box.ID, "blue")
	s.Equal(pipeline.Steps[0].ID, "string-step")
	s.Equal(pipeline.Steps[1].ID, "script")
	s.Equal(pipeline.Steps[2].ID, "script")

	// test to see if proper authenticatables are set
	// and if those return the proper authenticators
	amzn := config.PipelinesMap["amzn"]
	assert.NotNil(s.T(), amzn.Box.Auth)
	_, ok := amzn.Box.Auth.(*AmazonAuth)
	s.Equal(ok, true)
	env := util.NewEnvironment(os.Environ()...)
	authenticator := amzn.Box.Auth.ToAuthenticator(env)
	_, ok = authenticator.(*auth.AmazonAuth)
	s.Equal(ok, true)

	docker := config.PipelinesMap["docker-v2"]
	assert.NotNil(s.T(), docker.Box.Auth)
	_, ok = docker.Box.Auth.(*DockerAuth)
	s.Equal(ok, true)
	authenticator = docker.Box.Auth.ToAuthenticator(env)
	_, ok = authenticator.(*auth.DockerAuth)
	s.Equal(ok, true)

	dockerV1 := config.PipelinesMap["docker"]
	assert.NotNil(s.T(), dockerV1.Box.Auth)
	_, ok = dockerV1.Box.Auth.(*DockerAuth)
	s.Equal(ok, true)
	authenticator = dockerV1.Box.Auth.ToAuthenticator(env)
	_, ok = authenticator.(auth.DockerAuthV1)
	s.Equal(ok, true)
}
Example #6
0
// ExportEnvironment for this pipeline result (used in after-steps)
func (pr *PipelineResult) ExportEnvironment(sessionCtx context.Context, sess *Session) error {
	e := util.NewEnvironment()
	result := "failed"
	if pr.Success {
		result = "passed"
	}
	e.Add("WERCKER_RESULT", result)
	if !pr.Success {
		e.Add("WERCKER_FAILED_STEP_DISPLAY_NAME", pr.FailedStepName)
		e.Add("WERCKER_FAILED_STEP_MESSAGE", pr.FailedStepMessage)
	}

	exit, _, err := sess.SendChecked(sessionCtx, e.Export()...)
	if err != nil {
		return err
	}
	if exit != 0 {
		return fmt.Errorf("Pipeline failed with exit code: %d", exit)
	}
	return nil
}
Example #7
0
func NewDockerPipeline(name string, config *core.Config, options *core.PipelineOptions, dockerOptions *DockerOptions, builder Builder) (*DockerPipeline, error) {
	// decide which configs to use for each thing
	// TODO(termie): this code is not all specific to docker and should be made
	//               into something shared
	pipelineName := options.Pipeline
	pipelineConfig, ok := config.PipelinesMap[pipelineName]
	if !ok {
		return nil, fmt.Errorf("No pipeline named %s", pipelineName)
	}

	// Select this pipeline's config or the global config
	rawBoxConfig := pipelineConfig.Box
	if rawBoxConfig == nil {
		rawBoxConfig = config.Box
	}
	boxConfig := rawBoxConfig.BoxConfig

	// Select this pipeline's service or the global config
	servicesConfig := pipelineConfig.Services
	if servicesConfig == nil {
		servicesConfig = config.Services
	}

	stepsConfig := pipelineConfig.Steps
	if options.DeployTarget != "" {
		sectionSteps, ok := pipelineConfig.StepsMap[options.DeployTarget]
		if ok {
			stepsConfig = sectionSteps
		}
	}

	afterStepsConfig := pipelineConfig.AfterSteps

	box, err := NewDockerBox(boxConfig, options, dockerOptions)
	if err != nil {
		return nil, err
	}

	var services []core.ServiceBox
	for _, serviceConfig := range servicesConfig {
		service, err := NewServiceBox(serviceConfig.BoxConfig, options, dockerOptions, builder)
		if err != nil {
			return nil, err
		}
		services = append(services, service)
	}

	initStep, err := core.NewWerckerInitStep(options)
	if err != nil {
		return nil, err
	}

	steps := []core.Step{initStep}
	for _, stepConfig := range stepsConfig {
		step, err := NewStep(stepConfig.StepConfig, options, dockerOptions)
		if err != nil {
			return nil, err
		}
		if step != nil {
			// we can return a nil step if it's internal and EnableDevSteps is
			// false
			steps = append(steps, step)
		}
	}

	var afterSteps []core.Step
	for _, stepConfig := range afterStepsConfig {
		step, err := NewStep(stepConfig.StepConfig, options, dockerOptions)
		if err != nil {
			return nil, err
		}
		if step != nil {
			// we can return a nil step if it's internal and EnableDevSteps is
			// false
			afterSteps = append(afterSteps, step)
		}
	}
	// if we found some valid after steps, prepend init
	if len(afterSteps) > 0 {
		afterSteps = append([]core.Step{initStep}, afterSteps...)
	}

	logger := util.RootLogger().WithField("Logger", "Pipeline")
	base := core.NewBasePipeline(core.BasePipelineOptions{
		Options:    options,
		Env:        util.NewEnvironment(),
		Box:        box,
		Services:   services,
		Steps:      steps,
		AfterSteps: afterSteps,
		Logger:     logger,
	})
	return &DockerPipeline{BasePipeline: base, options: options, dockerOptions: dockerOptions}, nil
}
Example #8
0
	"github.com/wercker/wercker/util"
	"golang.org/x/net/context"
	"golang.org/x/sys/unix"
)

var (
	cliLogger    = util.RootLogger().WithField("Logger", "CLI")
	buildCommand = cli.Command{
		Name:      "build",
		ShortName: "b",
		Usage:     "build a project",
		Action: func(c *cli.Context) {
			envfile := c.GlobalString("environment")
			_ = godotenv.Load(envfile)

			env := util.NewEnvironment(os.Environ()...)

			settings := util.NewCLISettings(c)
			opts, err := core.NewBuildOptions(settings, env)
			if err != nil {
				cliLogger.Errorln("Invalid options\n", err)
				os.Exit(1)
			}
			dockerOptions, err := dockerlocal.NewDockerOptions(settings, env)
			if err != nil {
				cliLogger.Errorln("Invalid options\n", err)
				os.Exit(1)
			}
			_, err = cmdBuild(context.Background(), opts, dockerOptions)
			if err != nil {
				cliLogger.Fatal(err)
Example #9
0
// NewStep sets up the basic parts of a Step.
// Step names can come in a couple forms (x means currently supported):
//   x setup-go-environment (fetches from api)
//   x wercker/hipchat-notify (fetches from api)
//   x wercker/hipchat-notify "http://someurl/thingee.tar" (downloads tarball)
//   x setup-go-environment "file:///some_path" (uses local path)
func NewStep(stepConfig *StepConfig, options *PipelineOptions) (*ExternalStep, error) {
	var identifier string
	var name string
	var owner string
	var version string

	url := ""

	stepID := stepConfig.ID
	data := stepConfig.Data

	// Check for urls
	_, err := fmt.Sscanf(stepID, "%s %q", &identifier, &url)
	if err != nil {
		// There was probably no url part
		identifier = stepID
	}

	// Check for owner/name
	parts := strings.SplitN(identifier, "/", 2)
	if len(parts) > 1 {
		owner = parts[0]
		name = parts[1]
	} else {
		// No owner, "wercker" is the default
		owner = "wercker"
		name = identifier
	}

	versionParts := strings.SplitN(name, "@", 2)
	if len(versionParts) == 2 {
		name = versionParts[0]
		version = versionParts[1]
	} else {
		version = "*"
	}

	// Add a random number to the name to prevent collisions on disk
	stepSafeID := fmt.Sprintf("%s-%s", name, uuid.NewRandom().String())

	// Script steps need unique IDs
	if name == "script" {
		stepID = uuid.NewRandom().String()
		version = util.Version()
	}

	// If there is a name in data, make it our displayName and delete it
	displayName := stepConfig.Name
	if displayName == "" {
		displayName = name
	}

	logger := util.RootLogger().WithFields(util.LogFields{
		"Logger": "Step",
		"SafeID": stepSafeID,
	})

	return &ExternalStep{
		BaseStep: &BaseStep{
			displayName: displayName,
			env:         util.NewEnvironment(),
			id:          identifier,
			name:        name,
			owner:       owner,
			safeID:      stepSafeID,
			version:     version,
			cwd:         stepConfig.Cwd,
		},
		options: options,
		data:    data,
		url:     url,
		logger:  logger,
	}, nil
}
Example #10
0
func EmptyEnv() *util.Environment {
	return util.NewEnvironment()
}
Example #11
0
func MinimalDockerOptions() *DockerOptions {
	opts := &DockerOptions{}
	guessAndUpdateDockerOptions(opts, util.NewEnvironment(os.Environ()...))
	return opts
}