func printContextData(context *pongo2.Context, isDebug bool) { // Pretty print the available data as YAML if serializedData, err := yaml.Marshal(context); err == nil { source := util.IndentString(string(serializedData), " ") if isDebug { log.Debug(source) } else { log.Info(source) } } else { // Fallback for k, v := range *context { log.Info(" %s = %v", k, v) } } }
func compileArtifact(context *pongo2.Context, artifact *data.Artifact, providerName string, baseDir *string, workDir *string) error { outputFile := filepath.Join(*workDir, "build", providerName, artifact.Name, "user-data") outputDir := filepath.Dir(outputFile) // Create output directory if missing if exists, _ := util.FileExists(outputDir); !exists { if err := os.MkdirAll(outputDir, 0755); err != nil { return errors.New(fmt.Sprintf("Could not create output directory:\n %s\n\nnested error:\n %s", outputDir, err.Error())) } } log.Notice("▶ Compiling artifact [%s] to:\n %s", artifact.Name, outputFile) log.Debug(" Setting artifact name to the context") c := pongo2.Context{ "env": map[string]interface{}{ "provider": providerName, "artifact": artifact.Name, }, } if err := mergo.MergeWithOverwrite(context, c); err != nil { return err } printContextData(context, true) // Create the initial user data objects buffer := bytes.NewBufferString("#cloud-config\n---\n") writeFiles := make([]data.WriteFile, 0, 10) units := make([]data.Unit, 0, 10) // Process bricks bricks := artifact.Bricks for _, brick := range bricks { log.Info(" Processing brick:\n %s", brick) // Load metadata if metadata, brickBaseDir, err := loadBrickMetadata(context, baseDir, &brick); err == nil { // Process brick files log.Debug(" Processing brick files") if brickWriteFiles, err := processMetadataFiles(context, metadata, brickBaseDir); err != nil { return err } else { writeFiles = append(writeFiles, brickWriteFiles...) } // Process brick units log.Debug(" Processing brick unit") if brickUnit, err := processMetadataUnits(context, metadata, brickBaseDir); err != nil { return err } else if brickUnit != nil { units = append(units, *brickUnit) } } else { return err } } // Write compiled write-files section if len(writeFiles) > 0 { writeFilesMap := make(map[string]interface{}) writeFilesMap["write-files"] = writeFiles if output, err := yaml.Marshal(writeFilesMap); err != nil { return err } else { buffer.Write(output) } } // Write rest of config file cloudConfig := artifact.CloudConfig // Custom CoreOS if cloudConfig.CoreOS != nil { coreOs := cloudConfig.CoreOS // Add units // if coreOs["units"] != nil { coreOs["units"] = units // } // Write CoreOS section coreOsMap := make(map[string]interface{}) coreOsMap["coreos"] = coreOs if output, err := yaml.Marshal(coreOsMap); err != nil { return err } else { buffer.Write(output) } } // Append everything else to the end of the file if cloudConfig.Generic != nil { if output, err := yaml.Marshal(cloudConfig.Generic); err != nil { return err } else { buffer.Write(output) } } log.Info(" Writing output file to:\n %s", outputFile) if err := ioutil.WriteFile(outputFile, buffer.Bytes(), 0755); err != nil { return err } return nil }