func createInstallScripts()

in packaging/linux/deb/debroot.go [366:559]


func createInstallScripts(worker llb.State, spec *dalec.Spec, dir, target string) []llb.State {
	artifacts := spec.GetArtifacts(target)

	states := make([]llb.State, 1)
	base := llb.Scratch().File(llb.Mkdir(dir, 0o755, llb.WithParents(true)))

	installBuf := bytes.NewBuffer(nil)
	writeInstallHeader := sync.OnceFunc(func() {
		fmt.Fprintln(installBuf, string(debianInstall))
	})

	writeInstall := func(src, dir, name string) {
		// This is wrapped in a sync.OnceFunc so that this only has an effect the
		// first time it is called.
		writeInstallHeader()

		name = strings.TrimSuffix(name, "*")
		dest := filepath.Join("debian", spec.Name, dir, name)
		fmt.Fprintln(installBuf, "do_install", filepath.Dir(dest), dest, src)

	}

	if len(artifacts.Binaries) > 0 {
		sorted := dalec.SortMapKeys(artifacts.Binaries)
		for _, key := range sorted {
			cfg := artifacts.Binaries[key]
			writeInstall(key, filepath.Join(BinariesPath, cfg.SubPath), cfg.ResolveName(key))
		}
	}

	if len(artifacts.ConfigFiles) > 0 {
		sorted := dalec.SortMapKeys(artifacts.ConfigFiles)
		for _, p := range sorted {
			cfg := artifacts.ConfigFiles[p]

			dir := filepath.Join(ConfigFilesPath, cfg.SubPath)
			name := cfg.ResolveName(p)
			writeInstall(p, dir, name)
		}
	}

	if len(artifacts.Manpages) > 0 {
		buf := bytes.NewBuffer(nil)

		sorted := dalec.SortMapKeys(artifacts.Manpages)
		for _, key := range sorted {
			cfg := artifacts.Manpages[key]
			if cfg.Name != "" || (cfg.SubPath != "" && cfg.SubPath != filepath.Base(filepath.Dir(key))) {
				resolved := cfg.ResolveName(key)
				writeInstall(key, filepath.Join(ManpagesPath, spec.Name, cfg.SubPath), resolved)
				continue
			}
			fmt.Fprintln(buf, key)
		}
		if buf.Len() > 0 {
			states = append(states, base.File(llb.Mkfile(filepath.Join(dir, spec.Name+".manpages"), 0o640, buf.Bytes())))
		}

	}

	if artifacts.Directories != nil {
		buf := bytes.NewBuffer(nil)

		sorted := dalec.SortMapKeys(artifacts.Directories.Config)
		for _, name := range sorted {
			fmt.Fprintln(buf, filepath.Join("/etc", name))
		}

		sorted = dalec.SortMapKeys(artifacts.Directories.State)
		for _, name := range sorted {
			fmt.Fprintln(buf, filepath.Join("/var/lib", name))
		}

		states = append(states, base.File(llb.Mkfile(filepath.Join(dir, spec.Name+".dirs"), 0o640, buf.Bytes())))
	}

	if len(artifacts.Docs) > 0 || len(artifacts.Licenses) > 0 {
		buf := bytes.NewBuffer(nil)

		sorted := dalec.SortMapKeys(artifacts.Docs)
		for _, key := range sorted {
			cfg := artifacts.Docs[key]
			resolved := cfg.ResolveName(key)
			if resolved != key || cfg.SubPath != "" {
				writeInstall(key, filepath.Join(DocsPath, spec.Name, cfg.SubPath), resolved)
			} else {
				fmt.Fprintln(buf, key)
			}
		}

		sorted = dalec.SortMapKeys(artifacts.Licenses)
		for _, key := range sorted {
			cfg := artifacts.Licenses[key]
			resolved := cfg.ResolveName(key)
			if resolved != key || cfg.SubPath != "" {
				writeInstall(key, filepath.Join(LicensesPath, spec.Name, cfg.SubPath), resolved)
			} else {
				fmt.Fprintln(buf, key)
			}
		}

		if buf.Len() > 0 {
			states = append(states, base.File(llb.Mkfile(filepath.Join(dir, spec.Name+".docs"), 0o640, buf.Bytes())))
		}
	}

	if len(artifacts.Headers) > 0 {
		sorted := dalec.SortMapKeys(artifacts.Headers)
		for _, key := range sorted {
			cfg := artifacts.Headers[key]
			resolved := cfg.ResolveName(key)
			writeInstall(key, filepath.Join(HeadersPath, cfg.SubPath), resolved)
		}
	}

	if units := artifacts.Systemd.GetUnits(); len(units) > 0 {
		// deb-systemd will look for service files in DEBIAN/<package-name>[.<service-name>].<unit-type>
		// To handle this we'll create symlinks to the actual unit files in the source.
		// https://manpages.debian.org/testing/debhelper/dh_installsystemd.1.en.html#FILES

		// Maps the base name of a unit, e.g. "foo.service" -> foo, to the list of
		// units that fall under that basename
		// (e.g. "foo.socket" and  "foo.service")
		// We need to track this in cases where some units under a base are
		// enabled and some are not since dh_installsystemd does not support this
		// directly.

		sorted := dalec.SortMapKeys(units)
		for _, key := range sorted {
			cfg := units[key]
			name, suffix := cfg.SplitName(key)
			if name != spec.Name {
				name = spec.Name + "." + name
			}

			name = name + "." + suffix

			// Unforutnately there is not currently any way to create a symlink
			// directory with llb, so we need to use the worker to create the
			// symlink for us.
			st := worker.Run(
				llb.Dir(filepath.Join("/tmp/work", dir)),
				dalec.ShArgs("ln -s ../"+key+" "+name),
			).AddMount("/tmp/work", llb.Scratch())

			states = append(states, st)
		}
	}

	if dropins := artifacts.Systemd.GetDropins(); len(dropins) > 0 {
		sorted := dalec.SortMapKeys(dropins)
		for _, key := range sorted {
			cfg := dropins[key]
			cfgA := cfg.Artifact()
			name := cfgA.ResolveName(key)

			writeInstall(key, filepath.Join("/lib/systemd/system", cfg.Unit+".d"), name)
		}
	}

	if len(artifacts.DataDirs) > 0 {
		sorted := dalec.SortMapKeys(artifacts.DataDirs)
		for _, key := range sorted {
			cfg := artifacts.DataDirs[key]
			resolved := cfg.ResolveName(key)
			writeInstall(key, filepath.Join(DataDirsPath, cfg.SubPath), resolved)
		}
	}

	if len(artifacts.Libexec) > 0 {
		sorted := dalec.SortMapKeys(artifacts.Libexec)
		for _, key := range sorted {
			cfg := artifacts.Libexec[key]
			resolved := cfg.ResolveName(key)
			targetDir := filepath.Join(LibexecPath, cfg.SubPath)
			writeInstall(key, targetDir, resolved)
		}
	}

	if len(artifacts.Libs) > 0 {
		sorted := dalec.SortMapKeys(artifacts.Libs)
		for _, key := range sorted {
			cfg := artifacts.Libs[key]
			resolved := cfg.ResolveName(key)
			writeInstall(key, filepath.Join(LibsPath, cfg.SubPath), resolved)
		}
	}

	if installBuf.Len() > 0 {
		states = append(states, base.File(llb.Mkfile(filepath.Join(dir, spec.Name+".install"), 0o700, installBuf.Bytes())))
	}

	return states
}