Example #1
0
File: main.go Project: nyodas/cnt
func Run(overrideEnvVarName string, target string, templaterDir string, continueOnError bool) {
	attrMerger, err := merger.NewAttributesMerger(templaterDir + pathAttributes)
	if err != nil {
		logs.WithE(err).Warn("Failed to prepare attributes")
	}
	attributes := attrMerger.Merge()
	attributes = overrideWithJsonIfNeeded(overrideEnvVarName, attributes)
	tt, err := merger.ProcessAttributesTemplating(attributes, attributes)
	attributes = tt.(map[string]interface{})
	if err != nil {
		logs.WithField("dir", templaterDir+pathTemplates).Fatal("Failed to template attributes")
	}
	logs.WithField("content", attributes).Debug("Final attributes resolution")

	info, _ := os.Stat(templaterDir + pathTemplates)
	if info == nil {
		logs.WithField("dir", templaterDir+pathTemplates).Debug("Template dir is empty. Nothing to template")
		return
	}
	tmpl, err := template.NewTemplateDir(templaterDir+pathTemplates, target, continueOnError)
	if err != nil {
		logs.WithE(err).WithField("dir", templaterDir+pathTemplates).Fatal("Failed to load template dir")
	}
	err = tmpl.Process(attributes)
	if err != nil {
		logs.WithE(err).WithField("dir", templaterDir+pathTemplates).Fatal("Failed to process template dir")
	}
}
Example #2
0
File: home.go Project: puckel/dgr
func NewHome(path string) HomeStruct {
	logs.WithField("path", path).Debug("Loading home")

	var config Config
	if source, err := ioutil.ReadFile(path + "/config.yml"); err == nil {
		err = yaml.Unmarshal([]byte(source), &config)
		if err != nil {
			logs.WithEF(err, data.WithField("path", path+"/config.yml")).Fatal("Failed to process configuration file")
		}
	} else if source, err := ioutil.ReadFile(DefaultHomeFolder("cnt") + "/config.yml"); err == nil {
		logs.WithField("old", DefaultHomeFolder("cnt")+"/config.yml").WithField("new", DefaultHomeFolder("")).Warn("You are using old home folder")
		err = yaml.Unmarshal([]byte(source), &config)
		if err != nil {
			logs.WithEF(err, data.WithField("path", path+"/config.yml")).Fatal("Failed to process configuration file")
		}
	}

	if Args.NoStore {
		config.Rkt.NoStore = true
	}
	if Args.StoreOnly {
		config.Rkt.StoreOnly = true
	}

	rkt, err := common.NewRktClient(config.Rkt)
	if err != nil {
		logs.WithEF(err, data.WithField("config", config.Rkt)).Fatal("Rkt access failed")
	}

	return HomeStruct{
		path:   path,
		Config: config,
		Rkt:    rkt,
	}
}
Example #3
0
File: main.go Project: nyodas/cnt
func overrideWithJsonIfNeeded(overrideEnvVarName string, attributes map[string]interface{}) map[string]interface{} {
	if overrideEnvVarName != "" {
		if envjson := os.Getenv(overrideEnvVarName); envjson != "" {
			if len(envjson) > 8 && envjson[0:7] == "base64," {
				logs.WithField("EnvVar", overrideEnvVarName).Debug("Environment variable is base64 encoded")
				b64EnvJson := envjson[7:len(envjson)]
				envjsonBase64Decoded, err := base64.StdEncoding.DecodeString(b64EnvJson)
				if err != nil {
					logs.WithE(err).WithField("base64", b64EnvJson).Fatal("Failed to base64 decode")
				}
				envjson = string(envjsonBase64Decoded)
			}
			logs.WithField("content", envjson).Debug("Override var content")
			var envattr map[string]interface{}
			err := json.Unmarshal([]byte(envjson), &envattr)
			if err != nil {
				logs.WithE(err).
					WithField("varName", overrideEnvVarName).
					WithField("content", envjson).
					Fatal("Invalid format for environment override content")
			}
			attributes = mergemap.Merge(attributes, envattr)
		}
	}
	return attributes
}
Example #4
0
func (aci *Aci) tarAci(zip bool) {
	target := PATH_IMAGE_ACI[1:]
	if zip {
		target = PATH_IMAGE_ACI_ZIP[1:]
	}
	dir, _ := os.Getwd()
	logs.WithField("path", aci.target).Debug("chdir")
	os.Chdir(aci.target)
	utils.Tar(zip, target, PATH_MANIFEST[1:], PATH_ROOTFS[1:])
	logs.WithField("path", dir).Debug("chdir")
	os.Chdir(dir)
}
Example #5
0
func (b *Builder) runBuildSetup() error { //TODO REMOVE
	if empty, err := common.IsDirEmpty(b.aciHomePath + PATH_RUNLEVELS + PATH_BUILD_SETUP); empty || err != nil {
		return nil
	}

	logs.WithF(b.fields).Info("Running build setup")

	for _, e := range manifestApp(b.pod).App.Environment {
		logs.WithField("name", e.Name).WithField("value", e.Value).Debug("Adding environment var")
		os.Setenv(e.Name, e.Value)
	}

	logs.WithF(b.fields).Warn("Build setup is deprecated and will be removed. it create unreproductible builds and run as root directly on the host. Please use builder dependencies and builder runlevels instead")
	time.Sleep(5 * time.Second)

	os.Setenv("BASEDIR", b.aciHomePath)
	os.Setenv("TARGET", b.stage2Rootfs+"/..")
	os.Setenv("ROOTFS", b.stage2Rootfs+"/../rootfs")
	os.Setenv(common.EnvLogLevel, logs.GetLevel().String())

	if err := common.ExecCmd(b.stage1Rootfs + PATH_DGR + PATH_BUILDER + "/stage2/build-setup.sh"); err != nil {
		return errs.WithEF(err, b.fields, "Build setup failed")
	}

	return nil
}
Example #6
0
// list input files
func (in *inputs) listFiles() error {
	list_l1, err := ioutil.ReadDir(in.Directory)
	if err != nil {
		return err
	}
	for _, f_l1 := range list_l1 {
		if f_l1.Mode()&os.ModeSymlink == os.ModeSymlink {
			followed_file, err := os.Readlink(in.Directory + "/" + f_l1.Name())
			if err != nil {
				return err
			}
			if followed_file[0] != '/' {
				followed_file = in.Directory + "/" + followed_file
			}
			f_l1, err = os.Lstat(followed_file)
			if err != nil {
				return err
			}
			logs.WithField("followed_link", f_l1.Name()).Trace("Followed Link")
		}
		if f_l1.IsDir() {
			list_l2, err := ioutil.ReadDir(in.Directory + "/" + f_l1.Name())
			if err != nil {
				return err
			}
			for _, f_l2 := range list_l2 {
				in.Files = append(in.Files, f_l1.Name()+"/"+f_l2.Name())
			}
		} else {
			in.Files = append(in.Files, f_l1.Name())
		}
	}
	return nil
}
Example #7
0
func (b *Builder) tarAci() error {
	upperId, err := b.upperTreeStoreId()
	if err != nil {
		return err
	}

	upperPath := b.pod.Root + PATH_OVERLAY + "/" + upperId + PATH_UPPER
	upperNamedRootfs := upperPath + "/" + manifestApp(b.pod).Name.String()
	upperRootfs := upperPath + common.PathRootfs

	if err := os.Rename(upperNamedRootfs, upperRootfs); err != nil { // TODO this is dirty and can probably be renamed during tar
		return errs.WithEF(err, b.fields.WithField("path", upperNamedRootfs), "Failed to rename rootfs")
	}
	defer os.Rename(upperRootfs, upperNamedRootfs)

	dir, err := os.Getwd()
	if err != nil {
		return errs.WithEF(err, b.fields, "Failed to get current working directory")
	}
	defer func() {
		if err := os.Chdir(dir); err != nil {
			logs.WithEF(err, b.fields.WithField("path", dir)).Warn("Failed to chdir back")
		}
	}()

	if err := os.Chdir(upperPath); err != nil {
		return errs.WithEF(err, b.fields.WithField("path", upperPath), "Failed to chdir to upper base path")
	}
	if err := common.Tar(b.aciTargetPath+common.PathImageAci, common.PathManifest[1:], common.PathRootfs[1:]+"/"); err != nil {
		return errs.WithEF(err, b.fields, "Failed to tar aci")
	}
	logs.WithField("path", dir).Debug("chdir")
	return nil
}
Example #8
0
func NewAciWithManifest(path string, args BuildArgs, manifest spec.AciManifest, checked *chan bool) (*Aci, error) {
	if manifest.NameAndVersion == "" {
		logs.WithField("path", path).Fatal("name is mandatory in manifest")
	}

	fields := data.WithField("aci", manifest.NameAndVersion.String())
	logs.WithF(fields).WithFields(data.Fields{"args": args, "path": path, "manifest": manifest}).Debug("New aci")

	fullPath, err := filepath.Abs(path)
	if err != nil {
		return nil, errs.WithEF(err, fields, "Cannot get fullpath of project")
	}

	target := fullPath + PATH_TARGET
	if cnt.Home.Config.TargetWorkDir != "" {
		currentAbsDir, err := filepath.Abs(cnt.Home.Config.TargetWorkDir + "/" + manifest.NameAndVersion.ShortName())
		if err != nil {
			return nil, errs.WithEF(err, fields.WithField("path", path), "Invalid target path")
		}
		target = currentAbsDir
	}

	aci := &Aci{
		fields:          fields,
		args:            args,
		path:            fullPath,
		manifest:        manifest,
		target:          target,
		rootfs:          target + PATH_ROOTFS,
		FullyResolveDep: true,
	}

	go aci.checkLatestVersions(checked)
	return aci, nil
}
Example #9
0
func (b *Builder) runBuild() error {
	command, err := b.getCommandPath()
	if err != nil {
		return err
	}

	logs.WithF(b.fields).Debug("Running build command")
	args, env, err := b.prepareNspawnArgsAndEnv(command)
	if err != nil {
		return err
	}

	os.Remove(b.stage1Rootfs + "/etc/machine-id")

	if logs.IsDebugEnabled() {
		logs.WithField("command", strings.Join([]string{args[0], " ", strings.Join(args[1:], " ")}, " ")).Debug("Running external command")
	}
	//	var stderr bytes.Buffer
	cmd := exec.Command(args[0], args[1:]...)
	cmd.Env = env
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr

	if err := cmd.Run(); err != nil {
		return errs.WithEF(err, b.fields, "Builder run failed")
	}

	return nil
}
Example #10
0
func (w Work) ListEnvs() []string {
	path := ggn.Home.Config.WorkPath + PATH_ENV
	if _, err := os.Stat(path); os.IsNotExist(err) {
		logs.WithEF(err, w.fields).WithField("path", path).Fatal("env directory not found")
	}

	files, err := ioutil.ReadDir(path)
	if err != nil {
		logs.WithEF(err, w.fields).WithField("path", path).Fatal("Cannot read env directory")
	}

	var envs []string
	for _, file := range files {
		if file.Mode()&os.ModeSymlink == os.ModeSymlink {
			followed_file, err := os.Readlink(path + "/" + file.Name())
			if err != nil {
				continue
			}
			if followed_file[0] != '/' {
				followed_file = path + "/" + followed_file
			}
			file, err = os.Lstat(followed_file)
			if err != nil {
				continue
			}
			logs.WithField("followed_link", file.Name()).Trace("Followed Link")
		}

		if !file.IsDir() {
			continue
		}
		envs = append(envs, file.Name())
	}
	return envs
}
Example #11
0
func ExecCommandFull(cmd []string, env []string, timeoutInMilli int) error {
	command := exec.Command(cmd[0], cmd[1:]...)
	var b bytes.Buffer
	command.Stdout = &b
	command.Stderr = &b
	command.Env = env

	if err := command.Start(); err != nil {
		return errs.WithEF(err, data.WithField("cmd", cmd), "Failed to start command")
	}

	var after *errs.EntryError
	timer := time.AfterFunc(time.Duration(timeoutInMilli)*time.Millisecond, func() {
		data := data.WithField("command", strings.Join(cmd, " ")).WithField("timeout", timeoutInMilli)
		logs.WithF(data).Debug("Command timeout")
		after = errs.WithF(data, "Exec command timeout")
		command.Process.Kill()
	})

	err := command.Wait()
	timer.Stop()
	if logs.IsTraceEnabled() {
		logs.WithField("cmd", cmd).WithField("output", string(b.Bytes())).Trace("Command output")
	}
	if err != nil {
		return errs.WithEF(err, data.WithField("cmd", cmd).
			WithField("output", string(b.Bytes())), "Command failed").
			WithErr(after)
	}
	return nil
}
Example #12
0
func loadEnvCommandsReturnNewRoot(osArgs []string, rootCmd *cobra.Command) *cobra.Command {
	logs.WithField("path", ggn.Home.Config.WorkPath).Debug("Loading envs")
	work := work.NewWork(ggn.Home.Config.WorkPath)

	newRootCmd := &cobra.Command{
		Use: "ggn",
	}

	for _, envNames := range work.ListEnvs() {
		env := work.LoadEnv(envNames)

		envCmd := &cobra.Command{
			Use:   env.GetName(),
			Short: "Run command for " + env.GetName(),
			Run: func(cmd *cobra.Command, args []string) {

				newRootCmd.AddCommand(prepareEnvCommands(env))
				newRootCmd.SetArgs(osArgs[1:])
				newRootCmd.Execute()
			},
		}
		rootCmd.AddCommand(envCmd)
	}
	return newRootCmd
}
Example #13
0
func (n ACFullname) LatestVersion() (string, error) {
	app, err := discovery.NewAppFromString(n.Name() + ":latest")
	if app.Labels["os"] == "" {
		app.Labels["os"] = "linux"
	}
	if app.Labels["arch"] == "" {
		app.Labels["arch"] = "amd64"
	}

	endpoints, _, err := discovery.DiscoverACIEndpoints(*app, nil, discovery.InsecureTLS|discovery.InsecureHTTP) //TODO support security
	if err != nil {
		return "", errors.Annotate(err, "Latest discovery fail")
	}

	r, _ := regexp.Compile(`^\d+(.\d+){0,2}(-[\.\-\dA-Za-z]+){0,1}$`) // TODO this is nexus specific

	if len(endpoints) == 0 {
		return "", errs.WithF(data.WithField("aci", string(n)), "Discovery does not give an endpoint to check latest version")
	}

	url := getRedirectForLatest(endpoints[0].ACI)
	logs.WithField("url", url).Debug("latest verion url")

	for _, part := range strings.Split(url, "/") {
		if r.Match([]byte(part)) {
			return part, nil
		}
	}
	return "", errors.New("No latest version found")
}
Example #14
0
func (n ACFullname) LatestVersion() (string, error) {
	app, err := discovery.NewAppFromString(n.Name() + ":latest")
	if app.Labels["os"] == "" {
		app.Labels["os"] = "linux"
	}
	if app.Labels["arch"] == "" {
		app.Labels["arch"] = "amd64"
	}

	endpoint, _, err := discovery.DiscoverEndpoints(*app, nil, false)
	if err != nil {
		return "", errors.Annotate(err, "Latest discovery fail")
	}

	r, _ := regexp.Compile(`^(\d+\.)?(\d+\.)?(\*|\d+)(\-[\dA-Za-z]+){0,1}$`)

	url := getRedirectForLatest(endpoint.ACIEndpoints[0].ACI)
	logs.WithField("url", url).Debug("latest verion url")

	for _, part := range strings.Split(url, "/") {
		if r.Match([]byte(part)) {
			return part, nil
		}
	}
	return "", errors.New("No latest version found")
}
Example #15
0
func ExecCmd(head string, parts ...string) error {
	if logs.IsDebugEnabled() {
		logs.WithField("command", strings.Join([]string{head, " ", strings.Join(parts, " ")}, " ")).Debug("Running external command")
	}
	cmd := exec.Command(head, parts...)
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	return cmd.Run()
}
Example #16
0
func buildAciOrPod(path string, args builder.BuildArgs) spec.CntCommand {
	if aci, err := builder.NewAci(path, args); err == nil {
		return aci
	} else if pod, err2 := builder.NewPod(path, args); err2 == nil {
		return pod
	} else {
		logs.WithField("path", path).WithField("err", err).WithField("err2", err2).Fatal("Cannot construct aci or pod")
	}
	return nil
}
Example #17
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())
	sigQuitThreadDump()

	var logLevel string
	var version bool

	rootCmd := &cobra.Command{
		Use: "nerve config.yml",
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			if version {
				fmt.Println("Nerve")
				fmt.Println("Version :", Version)
				fmt.Println("Build Time :", BuildTime)
				os.Exit(0)
			}

			if logLevel != "" {
				level, err := logs.ParseLevel(logLevel)
				if err != nil {
					logs.WithField("value", logLevel).Fatal("Unknown log level")
				}
				logs.SetLevel(level)
			}
		},
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 1 {
				logs.Fatal("Nerve require a configuration file as argument")
			}
			nerve, err := LoadConfig(args[0])
			if err != nil {
				logs.WithE(err).Fatal("Cannot start, failed to load configuration")
			}

			if err := nerve.Init(Version, BuildTime, logLevel != ""); err != nil {
				logs.WithE(err).Fatal("Failed to init nerve")
			}

			startStatus := make(chan error)
			go nerve.Start(startStatus)
			if status := <-startStatus; status != nil {
				logs.WithE(status).Fatal("Failed to start nerve")
			}
			waitForSignal()
			nerve.Stop()
		},
	}

	rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "L", "", "Set log level")
	rootCmd.PersistentFlags().BoolVarP(&version, "version", "V", false, "Display version")

	if err := rootCmd.Execute(); err != nil {
		logs.WithE(err).Fatal("Failed to process args")
	}
}
Example #18
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())
	sigQuitThreadDump()

	var logLevel string
	var version bool
	var oneshot bool

	rootCmd := &cobra.Command{
		Use: "synapse config.yml",
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			if version {
				fmt.Println("Synapse")
				fmt.Println("Version :", Version)
				fmt.Println("Build Time :", BuildTime)
				os.Exit(0)
			}

			if logLevel != "" {
				level, err := logs.ParseLevel(logLevel)
				if err != nil {
					logs.WithField("value", logLevel).Fatal("Unknown log level")
				}
				logs.SetLevel(level)
			}
		},
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 1 {
				logs.Fatal("Synapse require a configuration file as argument")
			}
			synapse, err := LoadConfig(args[0])
			if err != nil {
				logs.WithE(err).Fatal("Cannot start, failed to load configuration")
			}

			if err := synapse.Init(Version, BuildTime, logLevel != ""); err != nil {
				logs.WithE(err).Fatal("Failed to init synapse")
			}

			if err := synapse.Start(oneshot); err != nil {
				logs.WithE(err).Fatal("Failed to start synapse")
			}
			waitForSignal()
			synapse.Stop()
		},
	}

	rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "L", "", "Set log level")
	rootCmd.PersistentFlags().BoolVarP(&version, "version", "V", false, "Display version")
	//rootCmd.PersistentFlags().BoolVarP(&oneshot, "oneshot", "O", false, "run watchers/router only once and exit")

	if err := rootCmd.Execute(); err != nil {
		logs.WithE(err).Fatal("Failed to process args")
	}
}
Example #19
0
File: aci.go Project: puckel/dgr
func NewAciWithManifest(path string, args BuildArgs, manifestTmpl string, checkWg *sync.WaitGroup) (*Aci, error) {
	manifest, err := common.ProcessManifestTemplate(manifestTmpl, nil, false)
	if err != nil {
		return nil, errs.WithEF(err, data.WithField("content", manifestTmpl), "Failed to process manifest")
	}
	if manifest.NameAndVersion == "" {
		logs.WithField("path", path).Fatal("name is mandatory in manifest")
	}

	fields := data.WithField("aci", manifest.NameAndVersion.String())
	logs.WithF(fields).WithFields(data.Fields{"args": args, "path": path, "manifest": manifest}).Debug("New aci")

	fullPath, err := filepath.Abs(path)
	if err != nil {
		return nil, errs.WithEF(err, fields, "Cannot get fullpath of project")
	}

	target := fullPath + pathTarget
	if Home.Config.TargetWorkDir != "" {
		currentAbsDir, err := filepath.Abs(Home.Config.TargetWorkDir + "/" + manifest.NameAndVersion.ShortName())
		if err != nil {
			return nil, errs.WithEF(err, fields.WithField("path", path), "Invalid target path")
		}
		target = currentAbsDir
	}

	aci := &Aci{
		fields:          fields,
		args:            args,
		path:            fullPath,
		manifestTmpl:    manifestTmpl,
		manifest:        manifest,
		target:          target,
		FullyResolveDep: true,
		checkWg:         checkWg,
	}

	froms, err := manifest.GetFroms()
	if err != nil {
		logs.WithEF(err, aci.fields).Fatal("Invalid from data")
	}
	if len(froms) != 0 {
		if froms[0].String() == "" {
			logs.WithF(aci.fields).Warn("From is deprecated and empty, remove it")
		} else {
			logs.WithF(aci.fields).Warn("From is deprecated and processed as dependency. move from to dependencies")
			aci.manifest.Aci.Dependencies = append(froms, aci.manifest.Aci.Dependencies...)
		}
	}

	return aci, nil
}
Example #20
0
File: aci.go Project: puckel/dgr
func CheckLatestVersion(deps []common.ACFullname, warnText string) {
	for _, dep := range deps {
		if dep.Version() == "" {
			continue
		}
		version, _ := dep.LatestVersion()
		if version != "" && common.Version(dep.Version()).LessThan(common.Version(version)) {
			logs.WithField("newer", dep.Name()+":"+version).
				WithField("current", dep.String()).
				Warn("Newer " + warnText + " version")
		}
	}
}
Example #21
0
func ExecCmdGetStderr(head string, parts ...string) (string, error) {
	var stderr bytes.Buffer

	if logs.IsDebugEnabled() {
		logs.WithField("command", strings.Join([]string{head, " ", strings.Join(parts, " ")}, " ")).Debug("Running external command")
	}
	cmd := exec.Command(head, parts...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = &stderr
	cmd.Start()
	err := cmd.Wait()
	return strings.TrimSpace(stderr.String()), err
}
Example #22
0
func (aci *Aci) RunBuilderCommand(command common.BuilderCommand) error {
	defer aci.giveBackUserRightsToTarget()
	logs.WithF(aci.fields).Info("Building")

	if err := os.MkdirAll(aci.target, 0777); err != nil {
		return errs.WithEF(err, aci.fields, "Cannot create target directory")
	}

	if err := ioutil.WriteFile(aci.target+common.PathManifestYmlTmpl, []byte(aci.manifestTmpl), 0644); err != nil {
		return errs.WithEF(err, aci.fields.WithField("file", aci.target+common.PathManifestYmlTmpl), "Failed to write manifest template")
	}

	stage1Hash, err := aci.prepareStage1aci()
	if err != nil {
		return errs.WithEF(err, aci.fields, "Failed to prepare stage1 image")
	}

	builderHash, err := aci.prepareBuildAci()
	if err != nil {
		return errs.WithEF(err, aci.fields, "Failed to prepare build image")
	}

	logs.WithF(aci.fields).Info("Calling rkt to start build")
	defer aci.cleanupRun(builderHash, stage1Hash)
	if err := Home.Rkt.Run(aci.prepareRktRunArguments(command, builderHash, stage1Hash)); err != nil {
		return errs.WithEF(err, aci.fields, "Builder container return with failed status")
	}

	content, err := common.ExtractManifestContentFromAci(aci.target + pathImageAci)
	if err != nil {
		logs.WithEF(err, aci.fields).Warn("Failed to write manifest.json")
	}

	if err := ioutil.WriteFile(aci.target+pathManifestJson, content, 0644); err != nil {
		logs.WithEF(err, aci.fields).Warn("Failed to write manifest.json")
	}

	im := &schema.ImageManifest{}
	if err = im.UnmarshalJSON(content); err != nil {
		return errs.WithEF(err, aci.fields.WithField("content", string(content)), "Cannot unmarshall json content")
	}

	fullname := common.ExtractNameVersionFromManifest(im)
	logs.WithField("fullname", *fullname).Info("Finished building aci")
	if err := ioutil.WriteFile(aci.target+pathVersion, []byte(*fullname), 0644); err != nil {
		return errs.WithEF(err, aci.fields, "Failed to write version file in target")
	}

	return nil
}
Example #23
0
File: merger.go Project: nyodas/cnt
func templateAttribute(text string, attributes interface{}) (string, error) {
	tmpl, err := template.New("").Funcs(tpl.TemplateFunctions).Funcs(map[string]interface{}(gtf.GtfFuncMap)).Parse(text)
	if err != nil {
		return "", errs.WithEF(err, data.WithField("attribute", text), "Failed to parse template for attribute")
	}
	var b bytes.Buffer
	if err := tmpl.Execute(&b, attributes); err != nil {
		return "", errs.WithEF(err, data.WithField("attribute", text), "Failed to template attribute")
	}
	res := b.String()
	if logs.IsDebugEnabled() {
		logs.WithField("from", text).WithField("to", res).Debug("attribute templated")
	}
	return res, nil
}
Example #24
0
func NewHome(path string) HomeStruct {
	logs.WithField("path", path).Debug("Loading home")

	var config Config
	if source, err := ioutil.ReadFile(path + "/config.yml"); err == nil {
		err = yaml.Unmarshal([]byte(source), &config)
		if err != nil {
			panic(err)
		}
	}

	return HomeStruct{
		path:   path,
		Config: config,
	}
}
Example #25
0
File: main.go Project: puckel/dgr
func overrideWithJsonIfNeeded(overrideEnvVarName string, attributes map[string]interface{}) map[string]interface{} {
	if overrideEnvVarName != "" {
		if envjson := os.Getenv(overrideEnvVarName); envjson != "" {
			logs.WithField("content", envjson).Debug("Override var content")
			var envattr map[string]interface{}
			err := json.Unmarshal([]byte(envjson), &envattr)
			if err != nil {
				logs.WithE(err).
					WithField("varName", overrideEnvVarName).
					WithField("content", envjson).
					Fatal("Invalid format for environment override content")
			}
			attributes = mergemap.Merge(attributes, envattr)
		}
	}
	return attributes
}
Example #26
0
func (u *Unit) Check(command string) {
	if err := u.Service.Generate(); err != nil {
		logs.WithEF(err, u.Fields).Fatal("Generate failed")
	}
	logs.WithFields(u.Fields).Debug("Check")

	info := HookInfo{
		Service: u.Service,
		Unit:    u,
		Action:  "check",
		Command: command,
	}
	u.Service.GetEnv().RunEarlyHook(info)
	defer u.Service.GetEnv().RunLateHook(info)

	statuses := u.Service.GetEnv().ListUnits()
	var status UnitStatus
	if _, ok := statuses[u.Filename]; !ok {
		logs.WithFields(u.Fields).Warn("cannot find unit on fleet")
		return
	}
	status = statuses[u.Filename]
	logs.WithField("status", status).Debug("status")

	if status.Active != ACTIVE_ACTIVE {
		logs.WithFields(u.Fields).WithField("active", status.Active).Warn("unit status is not active")
		return
	}
	if status.Sub != SUB_RUNNING {
		logs.WithFields(u.Fields).WithField("sub", status.Sub).Warn("unit sub is not running")
		return
	}

	same, err := u.IsLocalContentSameAsRemote()
	if err != nil {
		logs.WithFields(u.Fields).Error("Cannot read unit")
		return
	}
	if !same {
		logs.WithFields(u.Fields).Warn("Unit is not up to date")
		return
	}
}
Example #27
0
func ProcessManifestTemplate(manifestContent string, data2 interface{}, checkNoValue bool) (*AciManifest, error) {
	manifest := AciManifest{Aci: AciDefinition{}}
	fields := data.WithField("source", manifestContent)

	template, err := template.NewTemplating(nil, "", manifestContent)
	if err != nil {
		return nil, errs.WithEF(err, fields, "Failed to load templating of manifest")
	}

	var b bytes.Buffer
	writer := bufio.NewWriter(&b)
	if err := template.Execute(writer, data2); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to template manifest")
	}
	if err := writer.Flush(); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to flush buffer")
	}

	templated := b.Bytes()
	if logs.IsDebugEnabled() {
		logs.WithField("content", string(templated)).Debug("Templated manifest")
	}

	if checkNoValue {
		scanner := bufio.NewScanner(bytes.NewReader(templated))
		scanner.Split(bufio.ScanLines)
		for i := 1; scanner.Scan(); i++ {
			text := scanner.Text()
			if bytes.Contains([]byte(text), []byte("<no value>")) {
				return nil, errs.WithF(fields.WithField("line", i).WithField("text", text), "Templating result of manifest have <no value>")
			}
		}
	}

	err = yaml.Unmarshal(templated, &manifest)
	if err != nil {
		return nil, errs.WithEF(err, fields, "Cannot unmarshall manifest")
	}

	return &manifest, nil
}
Example #28
0
File: pod.go Project: puckel/dgr
func NewPod(path string, args BuildArgs, checkWg *sync.WaitGroup) (*Pod, error) {
	if (args.CatchOnError || args.CatchOnStep) && !args.SerialBuild {
		args.SerialBuild = true
	}

	fullPath, err := filepath.Abs(path)
	if err != nil {
		logs.WithE(err).WithField("path", path).Fatal("Cannot get fullpath")
	}

	manifest, err := readPodManifest(fullPath + pathPodManifestYml)
	if err != nil {
		manifest2, err2 := readPodManifest(fullPath + "/cnt-pod-manifest.yml")
		if err2 != nil {
			return nil, errs.WithEF(err, data.WithField("path", fullPath+pathPodManifestYml).WithField("err2", err2), "Failed to read pod manifest")
		}
		logs.WithField("old", "cnt-pod-manifest.yml").WithField("new", "pod-manifest.yml").Warn("You are using the old aci configuration file")
		manifest = manifest2
	}
	fields := data.WithField("pod", manifest.Name.String())

	target := path + pathTarget
	if Home.Config.TargetWorkDir != "" {
		currentAbsDir, err := filepath.Abs(Home.Config.TargetWorkDir + "/" + manifest.Name.ShortName())
		if err != nil {
			logs.WithEF(err, fields).Panic("invalid target path")
		}
		target = currentAbsDir
	}

	pod := &Pod{
		checkWg:  checkWg,
		fields:   fields,
		path:     fullPath,
		args:     args,
		target:   target,
		manifest: *manifest,
	}

	return pod, nil
}
Example #29
0
File: env.go Project: blablacar/ggn
func (e *Env) addIncludeFiles(files []string) ([]string, error) {
	type includeFiles struct {
		Include []string
	}
	for _, file := range files {
		var f includeFiles
		yml, err := ioutil.ReadFile(file)
		if err != nil {
			return nil, err
		}
		err = yaml.Unmarshal(yml, &f)
		for _, inclusion := range f.Include {
			sepCount := strings.Count(inclusion, ":")
			if sepCount == 2 {
				fields := strings.Split(inclusion, ":")
				includeFile := strings.Replace(fields[2], ".", "/", -1) + ".yml"
				if fields[1] == "" {
					logs.WithField("include", inclusion).Fatal("Trying to include environment attributes from itself")
				} else { // env:prod-dc1:some.include
					includeFile = fmt.Sprintf("%v%v/%v%v/%v",
						ggn.Home.Config.WorkPath,
						PATH_ENV,
						fields[1],
						PATH_COMMON_ATTRIBUTES,
						includeFile,
					)
					files = append(files, includeFile)
				}
			} else { // some.global.include
				includeFile := strings.Replace(inclusion, ".", "/", -1) + ".yml"
				includeFile = fmt.Sprintf("%v%v/%v", ggn.Home.Config.WorkPath, PATH_COMMON_ATTRIBUTES, includeFile)
				files = append(files, includeFile)

			}
		}
	}
	return files, nil

}
Example #30
0
func NewRktClient(config RktConfig) (*RktClient, error) {
	if len(config.InsecureOptions) == 0 {
		config.InsecureOptions = []string{"ondisk", "image"}
	}

	rkt := &RktClient{
		fields:     data.WithField("config", config),
		config:     config,
		globalArgs: config.prepareGlobalArgs(config.InsecureOptions),
	}

	v, err := rkt.Version()
	if err != nil {
		return nil, err
	}
	if v.LessThan(rktSupportedVersion) {
		return nil, errs.WithF(rkt.fields.WithField("current", v).WithField("required", ">="+rktSupportedVersion), "Unsupported version of rkt")
	}

	logs.WithField("version", v).WithField("args", rkt.globalArgs).Debug("New rkt client")
	return rkt, nil
}