From f2d902d7e278be0c818792c5d8a2676d294087b5 Mon Sep 17 00:00:00 2001 From: psergee Date: Thu, 15 Feb 2024 11:58:57 +0300 Subject: [PATCH] pack: create artifacts directories Create /var//tarantool/ directories for packed applications. Part of #777 --- CHANGELOG.md | 2 ++ cli/pack/archive.go | 2 +- cli/pack/common.go | 25 +++++++++++++++++++++++++ cli/pack/deb.go | 8 ++++++-- cli/pack/opts.go | 2 ++ cli/pack/pack.go | 2 ++ cli/pack/rpm.go | 4 ++++ cli/pack/rpm_header.go | 14 ++++++++++---- cli/pack/tgz.go | 21 +++++++++++++++++---- test/integration/pack/test_pack.py | 12 ++++++++++++ 10 files changed, 81 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a0025803..a29599b6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. Common (all instances) unit is removed. - `tt pack` default data, run, log files location is changed for rpm/deb packages to `/var/[log | run | lib]/tarantool/` +- create `/var/[log | run | lib]/tarantool/` on target system +for packed applications. ### Fixed diff --git a/cli/pack/archive.go b/cli/pack/archive.go index db0d5d3ac..c082961d8 100644 --- a/cli/pack/archive.go +++ b/cli/pack/archive.go @@ -63,7 +63,7 @@ func (packer *archivePacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, } tarName = filepath.Join(currentDir, tarName) - err = WriteTgzArchive(bundlePath, tarName) + err = writeTgzArchive(bundlePath, tarName, *packCtx) if err != nil { if err := os.Remove(tarName); err != nil { log.Warnf("Failed to remove a tarball file %s: %s", tarName, err) diff --git a/cli/pack/common.go b/cli/pack/common.go index 776df373e..66272f0bc 100644 --- a/cli/pack/common.go +++ b/cli/pack/common.go @@ -53,6 +53,14 @@ var ( type RocksVersions map[string][]string +// packFileInfo contains information to set for files/dirs in rpm/deb packages. +type packFileInfo struct { + // owner is an owner of file/dir. + owner string + // group is a file/dir group. + group string +} + // skipDefaults filters out sockets and git dirs. func skipDefaults(src string) (bool, error) { fileInfo, err := os.Stat(src) @@ -716,3 +724,20 @@ func updatePermissions(baseDir string) func(path string, entry fs.DirEntry, err return nil } } + +// createArtifactsDirs creates /var//tarantool/ artifact directories in rpm/deb package +// and sets owner and group info for these directories. +func createArtifactsDirs(pkgDataDir string, packCtx *PackCtx) error { + for _, dirToCreate := range []string{configure.VarDataPath, configure.VarLogPath, + configure.VarRunPath} { + artifactEnvDir := filepath.Join(pkgDataDir, dirToCreate, "tarantool", packCtx.Name) + if err := os.MkdirAll(artifactEnvDir, dirPermissions); err != nil { + return fmt.Errorf("cannot create %q: %s", artifactEnvDir, err) + } + } + for _, dir := range [...]string{"lib", "log", "run"} { + packCtx.RpmDeb.pkgFilesInfo[fmt.Sprintf("var/%s/tarantool/%s", dir, packCtx.Name)] = + packFileInfo{"tarantool", "tarantool"} + } + return nil +} diff --git a/cli/pack/deb.go b/cli/pack/deb.go index c5e6adc93..9ab566266 100644 --- a/cli/pack/deb.go +++ b/cli/pack/deb.go @@ -110,6 +110,10 @@ func (packer *debPacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, return err } + if err = createArtifactsDirs(packageDataDir, packCtx); err != nil { + return err + } + // App directory. if err = copy.Copy(bundlePath, packagePrefixedPath); err != nil { return err @@ -122,7 +126,7 @@ func (packer *debPacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, // Create data.tar.gz. dataArchivePath := filepath.Join(packageDir, dataArchiveName) - err = WriteTgzArchive(packageDataDir, dataArchivePath) + err = writeTgzArchive(packageDataDir, dataArchivePath, *packCtx) if err != nil { return err } @@ -138,7 +142,7 @@ func (packer *debPacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, // Create control.tar.gz. controlArchivePath := filepath.Join(packageDir, controlArchiveName) - err = WriteTgzArchive(controlDirPath, controlArchivePath) + err = writeTgzArchive(controlDirPath, controlArchivePath, *packCtx) if err != nil { return err } diff --git a/cli/pack/opts.go b/cli/pack/opts.go index b26df12a1..5772fb602 100644 --- a/cli/pack/opts.go +++ b/cli/pack/opts.go @@ -74,6 +74,8 @@ func FillCtx(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, cliOpts *config.CliOpt return fmt.Errorf("package type is not provided") } + packCtx.RpmDeb.pkgFilesInfo = make(map[string]packFileInfo) + if (packCtx.IntegrityPrivateKey != "") && packCtx.CartridgeCompat { return errors.New("cannot pack with integrity checks in cartridge-compat mode") } diff --git a/cli/pack/pack.go b/cli/pack/pack.go index 04af63fbb..a0ee44800 100644 --- a/cli/pack/pack.go +++ b/cli/pack/pack.go @@ -62,4 +62,6 @@ type RpmDebCtx struct { DepsFile string // SystemdUnitParamsFile is a path to file with systemd unit parameters. SystemdUnitParamsFile string + // pkgFilesInfo files info to modify in result rpm/deb package. + pkgFilesInfo map[string]packFileInfo } diff --git a/cli/pack/rpm.go b/cli/pack/rpm.go index e0619ba58..413ffeed2 100644 --- a/cli/pack/rpm.go +++ b/cli/pack/rpm.go @@ -88,6 +88,10 @@ func (packer *rpmPacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, return err } + if err = createArtifactsDirs(packageDir, packCtx); err != nil { + return err + } + err = packRpm(cmdCtx, packCtx, opts, packageDir, resPackagePath) if err != nil { diff --git a/cli/pack/rpm_header.go b/cli/pack/rpm_header.go index 083bf5ebb..e5a9c104b 100644 --- a/cli/pack/rpm_header.go +++ b/cli/pack/rpm_header.go @@ -140,7 +140,7 @@ func genRpmHeader(relPaths []string, cpioPath, compresedCpioPath, packageFilesDi payloadSize := cpioFileInfo.Size() // Generate fileinfo. - filesInfo, err := getFilesInfo(relPaths, packageFilesDir) + filesInfo, err := getFilesInfo(relPaths, packageFilesDir, packCtx.RpmDeb.pkgFilesInfo) if err != nil { return nil, fmt.Errorf("failed to get files info: %s", err) } @@ -219,7 +219,8 @@ func genRpmHeader(relPaths []string, cpioPath, compresedCpioPath, packageFilesDi // getFilesInfo returns the meta information about all items inside the passed // directory needed for packing it into rpm headers. -func getFilesInfo(relPaths []string, dirPath string) (filesInfo, error) { +func getFilesInfo(relPaths []string, dirPath string, + pkgFiles map[string]packFileInfo) (filesInfo, error) { info := filesInfo{} for _, relPath := range relPaths { @@ -251,8 +252,13 @@ func getFilesInfo(relPaths []string, dirPath string) (filesInfo, error) { info.BaseNames = append(info.BaseNames, filepath.Base(relPath)) info.FileMtimes = append(info.FileMtimes, int32(fileInfo.ModTime().Unix())) - info.FileUserNames = append(info.FileUserNames, defaultFileUser) - info.FileGroupNames = append(info.FileGroupNames, defaultFileGroup) + if packFileInfo, found := pkgFiles[relPath]; found { + info.FileUserNames = append(info.FileUserNames, packFileInfo.owner) + info.FileGroupNames = append(info.FileGroupNames, packFileInfo.group) + } else { + info.FileUserNames = append(info.FileUserNames, defaultFileUser) + info.FileGroupNames = append(info.FileGroupNames, defaultFileGroup) + } info.FileLangs = append(info.FileLangs, defaultFileLang) sysFileInfo, ok := fileInfo.Sys().(*syscall.Stat_t) diff --git a/cli/pack/tgz.go b/cli/pack/tgz.go index 8835a45d6..dd02854a9 100644 --- a/cli/pack/tgz.go +++ b/cli/pack/tgz.go @@ -12,8 +12,8 @@ import ( "github.com/tarantool/tt/cli/configure" ) -// WriteTgzArchive creates TGZ archive of specified path. -func WriteTgzArchive(srcDirPath string, destFilePath string) error { +// writeTgzArchive creates TGZ archive of specified path. +func writeTgzArchive(srcDirPath string, destFilePath string, packCtx PackCtx) error { destFile, err := os.Create(destFilePath) if err != nil { return fmt.Errorf("failed to create result TGZ file %s: %s", destFilePath, err) @@ -22,7 +22,7 @@ func WriteTgzArchive(srcDirPath string, destFilePath string) error { gzipWriter := gzip.NewWriter(destFile) defer gzipWriter.Close() - err = WriteTarArchive(srcDirPath, gzipWriter) + err = WriteTarArchive(srcDirPath, gzipWriter, packCtx.RpmDeb.pkgFilesInfo) if err != nil { return err } @@ -32,7 +32,8 @@ func WriteTgzArchive(srcDirPath string, destFilePath string) error { // WriteTarArchive creates Tar archive of specified path // using specified writer -func WriteTarArchive(srcDirPath string, compressWriter io.Writer) error { +func WriteTarArchive(srcDirPath string, compressWriter io.Writer, + pkgFiles map[string]packFileInfo) error { tarWriter := tar.NewWriter(compressWriter) defer tarWriter.Close() @@ -61,6 +62,18 @@ func WriteTarArchive(srcDirPath string, compressWriter io.Writer) error { } } + relPath, err := filepath.Rel(srcDirPath, filePath) + if err != nil { + return fmt.Errorf("failed to get relative path of %q: %s", filePath, err) + } + if packFileInfo, found := pkgFiles[relPath]; found { + tarHeader.Uname = packFileInfo.owner + tarHeader.Gname = packFileInfo.group + } else { + tarHeader.Uname = defaultFileUser + tarHeader.Gname = defaultFileGroup + } + tarHeader.Name, err = filepath.Rel(srcDirPath, filePath) if err != nil { return err diff --git a/test/integration/pack/test_pack.py b/test/integration/pack/test_pack.py index c698a0844..57d87b767 100644 --- a/test/integration/pack/test_pack.py +++ b/test/integration/pack/test_pack.py @@ -995,6 +995,18 @@ def prefix(suffix): 'app2@.service'), 'perms': stat.S_IFREG }, + { + 'path': os.path.join(pkg_dir, 'var', 'lib', 'tarantool', 'bundle1'), + 'perms': stat.S_IFDIR + }, + { + 'path': os.path.join(pkg_dir, 'var', 'log', 'tarantool', 'bundle1'), + 'perms': stat.S_IFDIR + }, + { + 'path': os.path.join(pkg_dir, 'var', 'run', 'tarantool', 'bundle1'), + 'perms': stat.S_IFDIR + }, ] for unpacked in check_paths: assert os.path.exists(unpacked['path'])