Skip to content

Commit

Permalink
pack: create artifacts directories
Browse files Browse the repository at this point in the history
Create /var/<dir>/tarantool/<env> directories for packed applications.

Part of #777
  • Loading branch information
psergee committed Feb 20, 2024
1 parent 0c4bf4f commit f2d902d
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<env_name>`
- create `/var/[log | run | lib]/tarantool/<env_name>` on target system
for packed applications.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion cli/pack/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions cli/pack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -716,3 +724,20 @@ func updatePermissions(baseDir string) func(path string, entry fs.DirEntry, err
return nil
}
}

// createArtifactsDirs creates /var/<dir>/tarantool/<env> 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
}
8 changes: 6 additions & 2 deletions cli/pack/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions cli/pack/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions cli/pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions cli/pack/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions cli/pack/rpm_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions cli/pack/tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/integration/pack/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,18 @@ def prefix(suffix):
'[email protected]'),
'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'])
Expand Down

0 comments on commit f2d902d

Please sign in to comment.