Example #1
0
func (cnt *Img) runBuildLate() {
	res, err := utils.IsDirEmpty(cnt.target + PATH_RUNLEVELS + PATH_BUILD_LATE)
	res2, err2 := utils.IsDirEmpty(cnt.rootfs + PATH_CNT + PATH_RUNLEVELS + PATH_INHERIT_BUILD_LATE)
	if (res && res2) || (err != nil && err2 != nil) {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT_LATE, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build-late.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "target/build-late.sh"); err != nil {
			panic("Build step did not succeed" + err.Error())
		}
	} else {
		panic("systemd-nspawn is required")
	}
}
Example #2
0
func (cnt *Img) importAciBats() {
	if err := utils.ExecCmd("bash", "-c", "rkt image list --fields name --no-legend | grep -q "+BATS_ACI); err != nil {
		content, _ := dist.Asset("dist/bindata/aci-bats.aci")
		if err := ioutil.WriteFile("/tmp/aci-bats.aci", content, 0644); err != nil {
			panic(err)
		}
		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", "/tmp/aci-bats.aci")
		os.Remove("/tmp/aci-bats.aci")
	}
}
Example #3
0
func (cnt *Img) processFrom() {
	if cnt.manifest.From == "" {
		return
	}
	if err := utils.ExecCmd("bash", "-c", "rkt image list --fields name --no-legend | grep -q "+cnt.manifest.From.String()); err != nil {
		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.manifest.From.String())
	}
	if err := utils.ExecCmd("rkt", "image", "render", "--overwrite", cnt.manifest.From.String(), cnt.target); err != nil {
		panic("Cannot render from image" + cnt.manifest.From.String() + err.Error())
	}
}
Example #4
0
func (p *Pod) Push() {
	logs.WithF(p.fields).Info("Pushing")

	p.Build()

	checkVersion := make(chan bool, 1)

	for _, e := range p.manifest.Pod.Apps {
		aci, err := NewAciWithManifest(p.path+"/"+e.Name, p.args, p.toAciManifest(e), &checkVersion)
		if err != nil {
			logs.WithEF(err, p.fields.WithField("name", e.Name)).Fatal("Cannot prepare aci")
		}
		aci.podName = &p.manifest.Name
		aci.Push()
	}

	for range p.manifest.Pod.Apps {
		<-checkVersion
	}

	if err := utils.ExecCmd("curl", "-i",
		"-F", "r=releases",
		"-F", "hasPom=false",
		"-F", "e=pod",
		"-F", "g=com.blablacar.aci.linux.amd64",
		"-F", "p=pod",
		"-F", "v="+p.manifest.Name.Version(),
		"-F", "a="+p.manifest.Name.ShortName(),
		"-F", "file=@"+p.target+"/pod-manifest.json",
		"-u", cnt.Home.Config.Push.Username+":"+cnt.Home.Config.Push.Password,
		cnt.Home.Config.Push.Url+"/service/local/artifact/maven/content"); err != nil {
		logs.WithEF(err, p.fields).Fatal("Cannot push pod")
	}

}
Example #5
0
func (e Env) runFleetCmdInternal(getOutput bool, args ...string) (string, error) {
	if e.config.Fleet.Endpoint == "" {
		return "", errors.New("Cannot find fleet.endpoint env config to call fleetctl")
	}
	endpoint := "http://" + e.config.Fleet.Endpoint
	os.Setenv(FLEETCTL_ENDPOINT, endpoint)
	if e.config.Fleet.Username != "" {
		os.Setenv(FLEETCTL_SSH_USERNAME, e.config.Fleet.Username)
	}
	os.Setenv(FLEETCTL_STRICT_HOST_KEY_CHECKING, fmt.Sprintf("%t", e.config.Fleet.Strict_host_key_checking))
	os.Setenv(FLEETCTL_SUDO, fmt.Sprintf("%t", e.config.Fleet.Sudo))

	var out string
	var err error
	if getOutput {
		out, err = cntUtils.ExecCmdGetOutput("fleetctl", args...)
	} else {
		err = cntUtils.ExecCmd("fleetctl", args...)
	}

	os.Setenv(FLEETCTL_ENDPOINT, "")
	os.Setenv(FLEETCTL_SSH_USERNAME, "")
	os.Setenv(FLEETCTL_STRICT_HOST_KEY_CHECKING, "")
	os.Setenv(FLEETCTL_SUDO, "")
	return out, err
}
Example #6
0
func (p *Pod) Push() {
	log.Get().Info("Push POD", p.manifest.Name)

	p.Build()

	for _, e := range p.manifest.Pod.Apps {
		aci, err := NewAciWithManifest(p.path+"/"+e.Name, p.args, p.toAciManifest(e))
		if err != nil {
			log.Get().Panic(err)
		}
		aci.PodName = &p.manifest.Name
		aci.Push()
	}

	utils.ExecCmd("curl", "-i",
		"-F", "r=releases",
		"-F", "hasPom=false",
		"-F", "e=pod",
		"-F", "g=com.blablacar.aci.linux.amd64",
		"-F", "p=pod",
		"-F", "v="+p.manifest.Name.Version(),
		"-F", "a="+p.manifest.Name.ShortName(),
		"-F", "file=@"+p.target+POD_TARGET_MANIFEST,
		"-u", config.GetConfig().Push.Username+":"+config.GetConfig().Push.Password,
		config.GetConfig().Push.Url+"/service/local/artifact/maven/content")

}
Example #7
0
func (aci *Aci) Push() {
	if cnt.Home.Config.Push.Type == "" {
		panic("Can't push, push is not configured in cnt global configuration file")
	}

	aci.CheckBuilt()
	if aci.args.Test {
		aci.args.Test = false
		aci.Test()
	}

	aci.tarAci(true)

	im := utils.ExtractManifestFromAci(aci.target + PATH_IMAGE_ACI_ZIP)
	val, _ := im.Labels.Get("version")
	if err := utils.ExecCmd("curl", "-f", "-i",
		"-F", "r=releases",
		"-F", "hasPom=false",
		"-F", "e=aci",
		"-F", "g=com.blablacar.aci.linux.amd64",
		"-F", "p=aci",
		"-F", "v="+val,
		"-F", "a="+strings.Split(string(im.Name), "/")[1],
		"-F", "file=@"+aci.target+PATH_IMAGE_ACI_ZIP,
		"-u", cnt.Home.Config.Push.Username+":"+cnt.Home.Config.Push.Password,
		cnt.Home.Config.Push.Url+"/service/local/artifact/maven/content"); err != nil {
		panic("Cannot push aci" + err.Error())
	}
}
Example #8
0
func (p *Pod) Push() {
	log.Info("Push POD", p.manifest.Name)

	p.Build()

	checkVersion := make(chan bool, 1)

	for _, e := range p.manifest.Pod.Apps {
		aci, err := NewAciWithManifest(p.path+"/"+e.Name, p.args, p.toAciManifest(e), &checkVersion)
		if err != nil {
			panic(err)
		}
		aci.PodName = &p.manifest.Name
		aci.Push()
	}

	for range p.manifest.Pod.Apps {
		<-checkVersion
	}

	if err := utils.ExecCmd("curl", "-i",
		"-F", "r=releases",
		"-F", "hasPom=false",
		"-F", "e=service",
		"-F", "g=com.blablacar.aci.linux.amd64",
		"-F", "p=service",
		"-F", "v="+p.manifest.Name.Version(),
		"-F", "a="+p.manifest.Name.ShortName(),
		"-F", "file=@"+p.target+"/"+p.manifest.Name.ShortName()+"@.service",
		"-u", config.GetConfig().Push.Username+":"+config.GetConfig().Push.Password,
		config.GetConfig().Push.Url+"/service/local/artifact/maven/content"); err != nil {
		panic("Cannot push pod" + err.Error())
	}

}
Example #9
0
func (cnt *Img) Test() {
	cnt.Install()
	log.Info("Testing " + cnt.manifest.NameAndVersion)

	if _, err := os.Stat(cnt.path + PATH_TESTS); err != nil {
		if cnt.args.NoTestFail {
			panic("Test directory does not exists but tests are mandatory")
		}
		log.Warn("Tests directory does not exists")
		return
	}

	cnt.importAciBats()

	testAci, err := cnt.prepareTestAci()
	if err != nil {
		panic(err)
	}
	testAci.Build()

	os.MkdirAll(cnt.target+PATH_TESTS+PATH_TARGET+PATH_RESULT, 0777)

	if err := utils.ExecCmd("rkt",
		"--insecure-skip-verify=true",
		"run",
		"--mds-register=false",
		"--no-overlay=true",
		"--volume=result,kind=host,source="+cnt.target+PATH_TESTS+PATH_TARGET+PATH_RESULT,
		cnt.target+PATH_TESTS+PATH_TARGET+"/image.aci"); err != nil {
		// rkt+systemd cannot exit with fail status yet
		panic(err)
	}

	cnt.checkResult()
}
Example #10
0
func (cnt *Img) runBuild() {
	if res, err := utils.IsDirEmpty(cnt.target + PATH_RUNLEVELS + PATH_BUILD); res || err != nil {
		return
	}
	if err := utils.ExecCmd("systemd-nspawn", "--version"); err != nil {
		panic("systemd-nspawn is required")
	}

	rootfs := "${TARGET}/rootfs"
	if cnt.manifest.Build.NoBuildImage() {
		rootfs = ""
	}
	build := strings.Replace(BUILD_SCRIPT, "%%ROOTFS%%", rootfs, 1)
	ioutil.WriteFile(cnt.target+"/build.sh", []byte(build), 0777)

	if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
		"--bind="+cnt.target+"/:/target", "target/build.sh"); err != nil {
		panic("Build step did not succeed" + err.Error())
	}
}
Example #11
0
func (cnt *Img) processFrom() {
	if cnt.manifest.From != "" {
		log.Get().Info("Prepare rootfs from " + cnt.manifest.From)

		app, err := discovery.NewAppFromString(string(cnt.manifest.From))
		if app.Labels["os"] == "" {
			app.Labels["os"] = "linux"
		}
		if app.Labels["arch"] == "" {
			app.Labels["arch"] = "amd64"
		}

		endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
		if err != nil {
			panic(err)
		}

		url := endpoint.ACIEndpoints[0].ACI

		aciPath := config.GetConfig().AciPath + "/" + string(cnt.manifest.From)
		if _, err := os.Stat(aciPath + "/image.aci"); cnt.args.ForceUpdate || os.IsNotExist(err) {
			if err := os.MkdirAll(aciPath, 0755); err != nil {
				log.Get().Panic(err)
			}
			if err = utils.ExecCmd("wget", "-O", aciPath+"/image.aci", url); err != nil {
				os.Remove(aciPath + "/image.aci")
				log.Get().Panic("Cannot download from image", err)
			}
		} else {
			log.Get().Info("Image " + cnt.manifest.From + " Already exists locally, will not be downloaded")
		}

		utils.ExecCmd("tar", "xpf", aciPath+"/image.aci", "-C", cnt.target)

		//		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.manifest.From)
		//		utils.ExecCmd("rkt", "image", "export", "--overwrite", cnt.manifest.From, cnt.target + "/from.aci")
		//		utils.ExecCmd("tar", "xf", cnt.target + "/from.aci", "-C", cnt.target)
		//		os.Remove(cnt.target + "/from.aci")
	}
}
Example #12
0
func (aci *Aci) processFrom() {
	froms, err := aci.manifest.GetFroms()
	if err != nil {
		logs.WithEF(err, aci.fields).Fatal("Cannot process from")
	}
	for _, from := range froms {
		if from == "" {
			continue
		}

		logs.WithF(aci.fields).WithField("from", from).Info("Rendering from")

		if err := utils.ExecCmd("bash", "-c", "rkt image list --fields name --no-legend | grep -q "+from.String()); err != nil {
			utils.ExecCmd("rkt", "--insecure-options=image", "fetch", from.String())
		}
		if err := utils.ExecCmd("rkt", "image", "render", "--overwrite", from.String(), aci.target); err != nil {
			logs.WithEF(err, aci.fields).WithField("from", from.String()).Fatal("Failed to render from")
		}
	}

	os.Remove(aci.target + PATH_MANIFEST)
}
Example #13
0
func (cnt *Img) runBuildLate() {
	if res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD_LATE); res || err != nil {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT_LATE, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build-late.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "--share-system", "target/build-late.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Panic("systemd-nspawn is required")
	}
}
Example #14
0
func (aci *Aci) runLevelBuildSetup() {
	files, err := ioutil.ReadDir(aci.path + PATH_RUNLEVELS + PATH_BUILD_SETUP)
	if err != nil {
		return
	}

	os.Setenv("BASEDIR", aci.path)
	os.Setenv("TARGET", aci.target)
	for _, f := range files {
		if !f.IsDir() {
			logs.WithF(aci.fields.WithField("file", f.Name())).Debug("Running Build setup level script")
			if err := utils.ExecCmd(aci.path + PATH_RUNLEVELS + PATH_BUILD_SETUP + "/" + f.Name()); err != nil {
				panic(err)
			}
		}
	}
}
Example #15
0
func (cnt *Img) runLevelBuildSetup() {
	files, err := ioutil.ReadDir(cnt.path + PATH_RUNLEVELS + PATH_BUILD_SETUP)
	if err != nil {
		return
	}

	os.Setenv("BASEDIR", cnt.path)
	os.Setenv("TARGET", cnt.target)
	for _, f := range files {
		if !f.IsDir() {
			log.Info("Running Build setup level : ", f.Name())
			if err := utils.ExecCmd(cnt.path + PATH_RUNLEVELS + PATH_BUILD_SETUP + "/" + f.Name()); err != nil {
				panic(err)
			}
		}
	}
}
Example #16
0
func (cnt *Img) Push() {
	cnt.checkBuilt()
	if config.GetConfig().Push.Type == "" {
		log.Get().Panic("Can't push, push is not configured in cnt global configuration file")
	}

	im := extractManifestFromAci(cnt.target + "/image.aci")
	val, _ := im.Labels.Get("version")
	utils.ExecCmd("curl", "-i",
		"-F", "r=releases",
		"-F", "hasPom=false",
		"-F", "e=aci",
		"-F", "g=com.blablacar.aci.linux.amd64",
		"-F", "p=aci",
		"-F", "v="+val,
		"-F", "a="+ShortNameId(im.Name),
		"-F", "file=@"+cnt.target+"/image.aci",
		"-u", config.GetConfig().Push.Username+":"+config.GetConfig().Push.Password,
		config.GetConfig().Push.Url+"/service/local/artifact/maven/content")
}
Example #17
0
func (aci *Aci) runBuild() {
	if res, err := utils.IsDirEmpty(aci.target + PATH_RUNLEVELS + PATH_BUILD); res || err != nil {
		return
	}

	checkSystemdNspawn()

	rootfs := "${TARGET}/rootfs"
	if aci.manifest.Build.NoBuildImage() {
		rootfs = ""
	}
	build := strings.Replace(BUILD_SCRIPT, "%%ROOTFS%%", rootfs, 1)
	ioutil.WriteFile(aci.target+"/build.sh", []byte(build), 0777)

	logs.WithF(aci.fields).Info("Starting systemd-nspawn to run Build scripts")
	if err := utils.ExecCmd("systemd-nspawn", "--directory="+aci.rootfs, "--capability=all",
		"--bind="+aci.target+"/:/target", "target/build.sh"); err != nil {
		logs.WithEF(err, aci.fields).Fatal("Build part failed")
	}
}
Example #18
0
func (aci *Aci) runBuildLate() {
	res, err := utils.IsDirEmpty(aci.target + PATH_RUNLEVELS + PATH_BUILD_LATE)
	res2, err2 := utils.IsDirEmpty(aci.rootfs + PATH_CNT + PATH_RUNLEVELS + PATH_INHERIT_BUILD_LATE)
	if (res && res2) || (err != nil && err2 != nil) {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if aci.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT_LATE, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(aci.target+"/build-late.sh", []byte(build), 0777)
	}

	checkSystemdNspawn()

	logs.WithF(aci.fields).Info("Starting systemd-nspawn to run Build late scripts")
	if err := utils.ExecCmd("systemd-nspawn", "--directory="+aci.rootfs, "--capability=all",
		"--bind="+aci.target+"/:/target", "target/build-late.sh"); err != nil {
		logs.WithEF(err, aci.fields).Fatal("Build late part failed")
	}
}
Example #19
0
func discoverAndRunInitType(path string, args builder.BuildArgs) {
	fields := data.WithField("path", path)
	if _, err := os.Stat(path); err != nil {
		if err := os.MkdirAll(path, 0755); err != nil {
			logs.WithEF(err, fields).Fatal("Cannot create path directory")
		}
	}

	empty, err := utils.IsDirEmpty(path)
	if err != nil {
		logs.WithEF(err, fields).Fatal("Cannot read path directory")
	}
	if !initForce {
		if !empty {
			logs.WithEF(err, fields).Fatal("Path is not empty cannot init")
		}
	}

	logs.WithEF(err, fields).Info("Init project")

	files := make(map[string]string)

	files[builder.PATH_RUNLEVELS+builder.PATH_PRESTART_EARLY+"/10.prestart-early.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a prestart early script that is run before templating"
`
	files[builder.PATH_RUNLEVELS+builder.PATH_PRESTART_LATE+"/10.prestart-late.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a prestart late script that is run after templating"
`
	files[builder.PATH_RUNLEVELS+builder.PATH_BUILD+"/10.install.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a build script that is run to install applications"
`
	files[builder.PATH_RUNLEVELS+builder.PATH_BUILD_SETUP+"/10.setup.sh"] = `#!/bin/sh
echo "I'm build setup script file that is run from $BASEDIR to prepare $TARGET/rootfs before running build scripts"
`
	files[builder.PATH_RUNLEVELS+builder.PATH_BUILD_LATE+"/10.setup.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a build late script that is run to install applications after the copy of files,template,etc..."
`
	files[builder.PATH_RUNLEVELS+builder.PATH_INHERIT_BUILD_EARLY+"/10.inherit-build-early.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a inherit build early script that is run on this image and all images that have me as From during build"
`
	files[builder.PATH_RUNLEVELS+builder.PATH_INHERIT_BUILD_LATE+"/10.inherit-build-early.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a inherit build late script that is run on this image and all images that have me as From during build"
`
	files[builder.PATH_FILES+"/dummy"] = `Dummy file
`
	files[builder.PATH_ATTRIBUTES+"/attributes.yml"] = `default:
  dummy: world
`
	files[builder.PATH_CONFD+builder.PATH_CONFDOTD+"/templated.toml"] = `[template]
src = "templated.tmpl"
dest = "/templated"
uid = 0
gid = 0
mode = "0644"
keys = ["/"]
`
	files[builder.PATH_CONFD+builder.PATH_TEMPLATES+"/templated.tmpl"] = `{{$data := json (getv "/data")}}Hello {{ $data.dummy }}
`
	files[".gitignore"] = `target/
`
	files["cnt-manifest.yml"] = `from: ""
name: aci.example.com/aci-dummy:1
aci:
  app:
    exec: [ "/cnt/bin/busybox", "sh" ]
`
	files[builder.PATH_TESTS+"/dummy.bats"] = `#!/cnt/bin/bats -x

@test "Prestart should template" {
  result="$(cat /templated)"
  [ "$result" == "Hello world" ]
}

@test "Cnt should copy files" {
  result="$(cat /dummy)"
  [ "$result" == "Dummy file" ]
}
`
	files[builder.PATH_TESTS+"/wait.sh"] = `exit 0`

	for filePath, data := range files {
		fpath := path + "/" + filePath
		os.MkdirAll(filepath.Dir(fpath), 0777)
		ioutil.WriteFile(fpath, []byte(data), 0777)
	}

	uid := "0"
	gid := "0"
	if os.Getenv("SUDO_UID") != "" {
		uid = os.Getenv("SUDO_UID")
		gid = os.Getenv("SUDO_GID")
	}
	utils.ExecCmd("chown", "-R", uid+":"+gid, path)
}
Example #20
0
func (cnt *Img) Install() {
	cnt.checkBuilt()
	utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.target+"/image.aci")
}
Example #21
0
func (cnt *Img) Init() {
	initPath := cnt.path
	if cnt.args.Path != "" {
		initPath = cnt.args.Path
	}
	log.Info("Setting up files three")
	uid := "0"
	gid := "0"

	if os.Getenv("SUDO_UID") != "" {
		uid = os.Getenv("SUDO_UID")
		gid = os.Getenv("SUDO_GID")
	}

	files := make(map[string]string)

	files[PATH_RUNLEVELS+PATH_PRESTART_EARLY+"/10.prestart-early.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a prestart early script that is run before templating"
`
	files[PATH_RUNLEVELS+PATH_PRESTART_LATE+"/10.prestart-late.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a prestart late script that is run after templating"
`
	files[PATH_RUNLEVELS+PATH_BUILD+"/10.install.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a build script that is run to install applications"
`
	files[PATH_RUNLEVELS+PATH_BUILD_SETUP+"/10.setup.sh"] = `#!/bin/sh
echo "I'm build setup script file that is run from $BASEDIR to prepare $TARGET/rootfs before running build scripts"
`
	files[PATH_RUNLEVELS+PATH_BUILD_LATE+"/10.setup.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a build late script that is run to install applications after the copy of files,template,etc..."
`
	files[PATH_RUNLEVELS+PATH_INHERIT_BUILD_EARLY+"/10.inherit-build-early.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a inherit build early script that is run on this image and all images that have me as From during build"
`
	files[PATH_RUNLEVELS+PATH_INHERIT_BUILD_LATE+"/10.inherit-build-early.sh"] = `#!/cnt/bin/busybox sh
echo "I'm a inherit build late script that is run on this image and all images that have me as From during build"
`
	files[PATH_FILES+"/dummy"] = `Dummy file
`
	files[PATH_ATTRIBUTES+"/attributes.yml"] = `default:
  dummy: world
`
	files[PATH_CONFD+PATH_CONFDOTD+"/templated.toml"] = `[template]
src = "templated.tmpl"
dest = "/templated"
uid = 0
gid = 0
mode = "0644"
keys = ["/"]
`
	files[PATH_CONFD+PATH_TEMPLATES+"/templated.tmpl"] = `{{$data := json (getv "/data")}}Hello {{ $data.dummy }}
`
	files[".gitignore"] = `target/
`
	files["cnt-manifest.yml"] = `from: ""
name: aci.example.com/aci-dummy:1
aci:
  app:
    exec: [ "/cnt/bin/busybox", "sh" ]
    eventHandlers:
      - { name: pre-start, exec: [ "/cnt/bin/prestart" ] }
`
	files[PATH_TESTS+"/dummy.bats"] = `#!/cnt/bin/bats -x

@test "Prestart should template" {
  result="$(cat /templated)"
  [ "$result" == "Hello world" ]
}

@test "Cnt should copy files" {
  result="$(cat /dummy)"
  [ "$result" == "Dummy file" ]
}
`

	files[PATH_TESTS+"/wait.sh"] = `exit 0`

	for filePath, data := range files {
		fpath := initPath + "/" + filePath
		os.MkdirAll(filepath.Dir(fpath), 0777)
		ioutil.WriteFile(fpath, []byte(data), 0777)
	}
	utils.ExecCmd("chown", "-R", uid+":"+gid, initPath)
}
Example #22
0
func (cnt *Img) runBuild() {
	if res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD); res || err != nil {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "--share-system", "target/build.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Panic("systemd-nspawn is required")
		//		log.Get().Info("Run with docker")
		//
		//		//
		//		log.Get().Info("Prepare Docker")
		//		first := exec.Command("bash", "-c", "cd "+cnt.rootfs+" && tar cf - .")
		//		second := exec.Command("docker", "import", "-", "")
		//
		//		reader, writer := io.Pipe()
		//		first.Stdout = writer
		//		second.Stdin = reader
		//
		//		var buff bytes.Buffer
		//		second.Stdout = &buff
		//
		//		first.Start()
		//		second.Start()
		//		first.Wait()
		//		writer.Close()
		//		second.Wait()
		//		imgId := strings.TrimSpace(buff.String())
		//
		//		//
		//		log.Get().Info("Run Docker\n")
		//		cmd := []string{"run", "--name=" + cnt.manifest.NameAndVersion.ShortName(), "-v", cnt.target + ":/target", imgId, "/target/build.sh"}
		//		utils.ExecCmd("docker", "rm", cnt.manifest.NameAndVersion.ShortName())
		//		if err := utils.ExecCmd("docker", cmd...); err != nil {
		//			panic(err)
		//		}
		//
		//		//
		//		log.Get().Info("Release Docker")
		//		if cnt.manifest.Build.NoBuildImage() {
		//			os.RemoveAll(cnt.rootfs)
		//			os.Mkdir(cnt.rootfs, 0777)
		//
		//			if err := utils.ExecCmd("docker", "export", "-o", cnt.target+"/dockerfs.tar", cnt.manifest.NameAndVersion.ShortName()); err != nil {
		//				panic(err)
		//			}
		//
		//			utils.ExecCmd("tar", "xpf", cnt.target+"/dockerfs.tar", "-C", cnt.rootfs)
		//		}
		//		if err := utils.ExecCmd("docker", "rm", cnt.manifest.NameAndVersion.ShortName()); err != nil {
		//			panic(err)
		//		}
		//		if err := utils.ExecCmd("docker", "rmi", imgId); err != nil {
		//			panic(err)
		//		}
	}
}