Exemple #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")
	}
}
Exemple #2
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")
	}
}
Exemple #3
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")
	}
}
Exemple #4
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())
	}
}
Exemple #5
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")
	}
}
Exemple #6
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)
		//		}
	}
}
Exemple #7
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)
}