Beispiel #1
0
// Builds returns the builds out of the given template that pass the
// configured options.
func (f *BuildOptions) Builds(t *packer.Template, cf *packer.ComponentFinder) ([]packer.Build, error) {
	buildNames := t.BuildNames()
	builds := make([]packer.Build, 0, len(buildNames))
	for _, buildName := range buildNames {
		if len(f.Except) > 0 {
			found := false
			for _, except := range f.Except {
				if buildName == except {
					found = true
					break
				}
			}

			if found {
				log.Printf("Skipping build '%s' because specified by -except.", buildName)
				continue
			}
		}

		if len(f.Only) > 0 {
			found := false
			for _, only := range f.Only {
				if buildName == only {
					found = true
					break
				}
			}

			if !found {
				log.Printf("Skipping build '%s' because not specified by -only.", buildName)
				continue
			}
		}

		log.Printf("Creating build: %s", buildName)
		build, err := t.Build(buildName, cf)
		if err != nil {
			return nil, fmt.Errorf("Failed to create build '%s': \n\n%s", buildName, err)
		}

		builds = append(builds, build)
	}

	return builds, nil
}
Beispiel #2
0
// Builds returns the builds out of the given template that pass the
// configured options.
func (f *BuildOptions) Builds(t *packer.Template, cf *packer.ComponentFinder) ([]packer.Build, error) {
	buildNames := t.BuildNames()

	checks := make(map[string][]string)
	checks["except"] = f.Except
	checks["only"] = f.Only
	for t, ns := range checks {
		for _, n := range ns {
			found := false
			for _, actual := range buildNames {
				if actual == n {
					found = true
					break
				}
			}

			if !found {
				return nil, fmt.Errorf(
					"Unknown build in '%s' flag: %s", t, n)
			}
		}
	}

	builds := make([]packer.Build, 0, len(buildNames))
	for _, buildName := range buildNames {
		if len(f.Except) > 0 {
			found := false
			for _, except := range f.Except {
				if buildName == except {
					found = true
					break
				}
			}

			if found {
				log.Printf("Skipping build '%s' because specified by -except.", buildName)
				continue
			}
		}

		if len(f.Only) > 0 {
			found := false
			for _, only := range f.Only {
				if buildName == only {
					found = true
					break
				}
			}

			if !found {
				log.Printf("Skipping build '%s' because not specified by -only.", buildName)
				continue
			}
		}

		log.Printf("Creating build: %s", buildName)
		build, err := t.Build(buildName, cf)
		if err != nil {
			return nil, fmt.Errorf("Failed to create build '%s': \n\n%s", buildName, err)
		}

		builds = append(builds, build)
	}

	return builds, nil
}
Beispiel #3
0
func (p *Provider) Build() error {
	var template *packer.Template
	var err error

	if p.Builder != nil {
		template, err = p.NewTemplate()
		if err != nil {
			return err
		}
	} else if p.TemplatePath != "" {
		template, err = p.NewTemplateFile()
		if err != nil {
			return err
		}
	} else {
		return errors.New("Can't find Template source, neither p.Builder or p.TemplatePath is defined")
	}

	if len(template.Builders) == 0 {
		return errors.New("No builder is available")
	}

	if len(template.Builders) != 1 {
		return errors.New("Only on builder is supported currently")
	}

	if _, ok := template.Builders[p.BuildName]; !ok {
		return fmt.Errorf("Build '%s' does not exist", p.BuildName)
	}

	build, err := template.Build(p.BuildName, newComponentFinder())
	if err != nil {
		return err
	}

	if !p.EnableDebug {
		build.SetDebug(false)
		log.SetOutput(ioutil.Discard)
	}

	_, err = build.Prepare()
	if err != nil {
		return err
	}

	// Handle interrupts for this build
	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt)
	defer signal.Stop(sig)
	go func(b packer.Build) {
		<-sig

		log.Printf("Stopping build: %s", b.Name())
		b.Cancel()
		log.Printf("Build cancelled: %s", b.Name())
	}(build)

	artifacts, err := build.Run(p.coloredUi(), p.cache())
	if err != nil {
		return err
	}

	for _, a := range artifacts {
		fmt.Println(a.Files(), a.BuilderId(), a.Id(), a.String())
	}

	return nil
}