github.com/jerryclinesmith/packer@v0.3.7/command/build/command.go (about)

     1  package build
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	cmdcommon "github.com/mitchellh/packer/common/command"
     8  	"github.com/mitchellh/packer/packer"
     9  	"log"
    10  	"os"
    11  	"os/signal"
    12  	"strconv"
    13  	"strings"
    14  	"sync"
    15  )
    16  
    17  type Command byte
    18  
    19  func (Command) Help() string {
    20  	return strings.TrimSpace(helpText)
    21  }
    22  
    23  func (c Command) Run(env packer.Environment, args []string) int {
    24  	var cfgDebug bool
    25  	var cfgForce bool
    26  	buildOptions := new(cmdcommon.BuildOptions)
    27  
    28  	cmdFlags := flag.NewFlagSet("build", flag.ContinueOnError)
    29  	cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
    30  	cmdFlags.BoolVar(&cfgDebug, "debug", false, "debug mode for builds")
    31  	cmdFlags.BoolVar(&cfgForce, "force", false, "force a build if artifacts exist")
    32  	cmdcommon.BuildOptionFlags(cmdFlags, buildOptions)
    33  	if err := cmdFlags.Parse(args); err != nil {
    34  		return 1
    35  	}
    36  
    37  	args = cmdFlags.Args()
    38  	if len(args) != 1 {
    39  		cmdFlags.Usage()
    40  		return 1
    41  	}
    42  
    43  	if err := buildOptions.Validate(); err != nil {
    44  		env.Ui().Error(err.Error())
    45  		env.Ui().Error("")
    46  		env.Ui().Error(c.Help())
    47  		return 1
    48  	}
    49  
    50  	userVars, err := buildOptions.AllUserVars()
    51  	if err != nil {
    52  		env.Ui().Error(fmt.Sprintf("Error compiling user variables: %s", err))
    53  		env.Ui().Error("")
    54  		env.Ui().Error(c.Help())
    55  		return 1
    56  	}
    57  
    58  	// Read the file into a byte array so that we can parse the template
    59  	log.Printf("Reading template: %s", args[0])
    60  	tpl, err := packer.ParseTemplateFile(args[0])
    61  	if err != nil {
    62  		env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
    63  		return 1
    64  	}
    65  
    66  	// The component finder for our builds
    67  	components := &packer.ComponentFinder{
    68  		Builder:       env.Builder,
    69  		Hook:          env.Hook,
    70  		PostProcessor: env.PostProcessor,
    71  		Provisioner:   env.Provisioner,
    72  	}
    73  
    74  	// Go through each builder and compile the builds that we care about
    75  	builds, err := buildOptions.Builds(tpl, components)
    76  	if err != nil {
    77  		env.Ui().Error(err.Error())
    78  		return 1
    79  	}
    80  
    81  	if cfgDebug {
    82  		env.Ui().Say("Debug mode enabled. Builds will not be parallelized.")
    83  	}
    84  
    85  	// Compile all the UIs for the builds
    86  	colors := [5]packer.UiColor{
    87  		packer.UiColorGreen,
    88  		packer.UiColorCyan,
    89  		packer.UiColorMagenta,
    90  		packer.UiColorYellow,
    91  		packer.UiColorBlue,
    92  	}
    93  
    94  	buildUis := make(map[string]packer.Ui)
    95  	for i, b := range builds {
    96  		ui := &packer.ColoredUi{
    97  			Color: colors[i%len(colors)],
    98  			Ui:    env.Ui(),
    99  		}
   100  
   101  		buildUis[b.Name()] = ui
   102  		ui.Say(fmt.Sprintf("%s output will be in this color.", b.Name()))
   103  	}
   104  
   105  	// Add a newline between the color output and the actual output
   106  	env.Ui().Say("")
   107  
   108  	log.Printf("Build debug mode: %v", cfgDebug)
   109  	log.Printf("Force build: %v", cfgForce)
   110  
   111  	// Set the debug and force mode and prepare all the builds
   112  	for _, b := range builds {
   113  		log.Printf("Preparing build: %s", b.Name())
   114  		b.SetDebug(cfgDebug)
   115  		b.SetForce(cfgForce)
   116  		err := b.Prepare(userVars)
   117  		if err != nil {
   118  			env.Ui().Error(err.Error())
   119  			return 1
   120  		}
   121  	}
   122  
   123  	// Run all the builds in parallel and wait for them to complete
   124  	var interruptWg, wg sync.WaitGroup
   125  	interrupted := false
   126  	artifacts := make(map[string][]packer.Artifact)
   127  	errors := make(map[string]error)
   128  	for _, b := range builds {
   129  		// Increment the waitgroup so we wait for this item to finish properly
   130  		wg.Add(1)
   131  
   132  		// Handle interrupts for this build
   133  		sigCh := make(chan os.Signal, 1)
   134  		signal.Notify(sigCh, os.Interrupt)
   135  		defer signal.Stop(sigCh)
   136  		go func(b packer.Build) {
   137  			<-sigCh
   138  			interruptWg.Add(1)
   139  			defer interruptWg.Done()
   140  			interrupted = true
   141  
   142  			log.Printf("Stopping build: %s", b.Name())
   143  			b.Cancel()
   144  			log.Printf("Build cancelled: %s", b.Name())
   145  		}(b)
   146  
   147  		// Run the build in a goroutine
   148  		go func(b packer.Build) {
   149  			defer wg.Done()
   150  
   151  			name := b.Name()
   152  			log.Printf("Starting build run: %s", name)
   153  			ui := buildUis[name]
   154  			runArtifacts, err := b.Run(ui, env.Cache())
   155  
   156  			if err != nil {
   157  				ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err))
   158  				errors[name] = err
   159  			} else {
   160  				ui.Say(fmt.Sprintf("Build '%s' finished.", name))
   161  				artifacts[name] = runArtifacts
   162  			}
   163  		}(b)
   164  
   165  		if cfgDebug {
   166  			log.Printf("Debug enabled, so waiting for build to finish: %s", b.Name())
   167  			wg.Wait()
   168  		}
   169  
   170  		if interrupted {
   171  			log.Println("Interrupted, not going to start any more builds.")
   172  			break
   173  		}
   174  	}
   175  
   176  	// Wait for both the builds to complete and the interrupt handler,
   177  	// if it is interrupted.
   178  	log.Printf("Waiting on builds to complete...")
   179  	wg.Wait()
   180  
   181  	log.Printf("Builds completed. Waiting on interrupt barrier...")
   182  	interruptWg.Wait()
   183  
   184  	if interrupted {
   185  		env.Ui().Say("Cleanly cancelled builds after being interrupted.")
   186  		return 1
   187  	}
   188  
   189  	if len(errors) > 0 {
   190  		env.Ui().Machine("error-count", strconv.FormatInt(int64(len(errors)), 10))
   191  
   192  		env.Ui().Error("\n==> Some builds didn't complete successfully and had errors:")
   193  		for name, err := range errors {
   194  			// Create a UI for the machine readable stuff to be targetted
   195  			ui := &packer.TargettedUi{
   196  				Target: name,
   197  				Ui:     env.Ui(),
   198  			}
   199  
   200  			ui.Machine("error", err.Error())
   201  
   202  			env.Ui().Error(fmt.Sprintf("--> %s: %s", name, err))
   203  		}
   204  	}
   205  
   206  	if len(artifacts) > 0 {
   207  		env.Ui().Say("\n==> Builds finished. The artifacts of successful builds are:")
   208  		for name, buildArtifacts := range artifacts {
   209  			// Create a UI for the machine readable stuff to be targetted
   210  			ui := &packer.TargettedUi{
   211  				Target: name,
   212  				Ui:     env.Ui(),
   213  			}
   214  
   215  			// Machine-readable helpful
   216  			ui.Machine("artifact-count", strconv.FormatInt(int64(len(buildArtifacts)), 10))
   217  
   218  			for i, artifact := range buildArtifacts {
   219  				var message bytes.Buffer
   220  				fmt.Fprintf(&message, "--> %s: ", name)
   221  
   222  				if artifact != nil {
   223  					fmt.Fprintf(&message, artifact.String())
   224  				} else {
   225  					fmt.Fprint(&message, "<nothing>")
   226  				}
   227  
   228  				iStr := strconv.FormatInt(int64(i), 10)
   229  				if artifact != nil {
   230  					ui.Machine("artifact", iStr, "builder-id", artifact.BuilderId())
   231  					ui.Machine("artifact", iStr, "id", artifact.Id())
   232  					ui.Machine("artifact", iStr, "string", artifact.String())
   233  
   234  					files := artifact.Files()
   235  					ui.Machine("artifact",
   236  						iStr,
   237  						"files-count", strconv.FormatInt(int64(len(files)), 10))
   238  					for fi, file := range files {
   239  						fiStr := strconv.FormatInt(int64(fi), 10)
   240  						ui.Machine("artifact", iStr, "file", fiStr, file)
   241  					}
   242  				} else {
   243  					ui.Machine("artifact", iStr, "nil")
   244  				}
   245  
   246  				ui.Machine("artifact", iStr, "end")
   247  				env.Ui().Say(message.String())
   248  			}
   249  		}
   250  	} else {
   251  		env.Ui().Say("\n==> Builds finished but no artifacts were created.")
   252  	}
   253  
   254  	if len(errors) > 0 {
   255  		// If any errors occurred, exit with a non-zero exit status
   256  		return 1
   257  	}
   258  
   259  	return 0
   260  }
   261  
   262  func (Command) Synopsis() string {
   263  	return "build image(s) from template"
   264  }