Skip to content

Commit

Permalink
pack: add compatibility with the cartridge cli
Browse files Browse the repository at this point in the history
New flag `--cartridge-compat` added for pack command, that fixes some differences between
`tt pack` and `cartridge-cli pack`.
It is only allowed for tgz pack type.
It solves naming differences (of resulting packages and applications in them).
Also VERSION and VERSION.lua files will be present in resulting bundle.

Closes #504
  • Loading branch information
DerekBum authored and LeonidVas committed Jul 26, 2023
1 parent e101058 commit 3abba2e
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
symlink will be switched to the latest installed version, so that `tt` can
continue working with the program.
- `tt connect`: support for multi-line commands in the history.
- New `tt pack` flag `--cartridge-compat` is added to maintain backward compatibility
with the cartridge-cli. It is supported only by `tgz` type packing.

### Fixed

Expand Down
6 changes: 6 additions & 0 deletions cli/cmd/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ The supported types are: tgz, deb, rpm`,
err = fmt.Errorf("incorrect combination of command parameters: %s", err.Error())
log.Fatalf(err.Error())
}
if packCtx.CartridgeCompat && args[0] != "tgz" {
err = fmt.Errorf("cartridge-compat flag can only be used while packing tgz bundle")
log.Fatalf(err.Error())
}
cmdCtx.CommandName = cmd.Name()
err = modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalPackModule, args)
handleCmdErr(cmd, err)
Expand All @@ -51,6 +55,8 @@ The supported types are: tgz, deb, rpm`,
packCtx.WithoutBinaries, "Don't include tarantool and tt binaries to the result package")
packCmd.Flags().BoolVar(&packCtx.WithBinaries, "with-binaries", packCtx.WithoutBinaries,
"Include tarantool and tt binaries to the result package")
packCmd.Flags().BoolVar(&packCtx.CartridgeCompat, "cartridge-compat", false,
"Pack cartridge cli compatible archive (only for tgz type)")

// TarGZ flags.
packCmd.Flags().BoolVar(&packCtx.Archive.All, "all", packCtx.Archive.All,
Expand Down
77 changes: 76 additions & 1 deletion cli/pack/archive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pack

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -19,7 +20,7 @@ type archivePacker struct {
// Run of ArchivePacker packs the bundle into tarball.
func (packer *archivePacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
opts *config.CliOpts) error {
bundlePath, err := prepareBundle(cmdCtx, *packCtx, opts, true)
bundlePath, err := prepareBundle(cmdCtx, packCtx, opts, true)
if err != nil {
return err
}
Expand All @@ -42,6 +43,18 @@ func (packer *archivePacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
return err
}

if packCtx.CartridgeCompat {
// Generate VERSION file.
if err := generateVersionFile(bundlePath, cmdCtx, packCtx); err != nil {
log.Warnf("Failed to generate VERSION file: %s", err)
}

// Generate VERSION.lua file.
if err := generateVersionLuaFile(bundlePath, packCtx); err != nil {
log.Warnf("Failed to generate VERSION.lua file: %s", err)
}
}

log.Infof("Creating tarball.")

currentDir, err := os.Getwd()
Expand All @@ -61,6 +74,68 @@ func (packer *archivePacker) Run(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
return nil
}

// generateVersionLuaFile generates VERSION.lua file (for cartridge-cli compatibility).
func generateVersionLuaFile(bundlePath string, packCtx *PackCtx) error {
log.Infof("Generate %s file", versionLuaFileName)

versionLuaFilePath := filepath.Join(bundlePath, packCtx.Name, versionLuaFileName)
// Check if the file already exists.
if _, err := os.Stat(versionLuaFilePath); err == nil {
log.Warnf("File %s will be overwritten", versionLuaFileName)
}

err := os.WriteFile(versionLuaFilePath,
[]byte(fmt.Sprintf("return '%s'", packCtx.Version)), 0644)
if err != nil {
return fmt.Errorf("failed to write VERSION.lua file %s: %s", versionLuaFilePath, err)
}

return nil
}

// generateVersionFile generates VERSION file (for cartridge-cli compatibility).
func generateVersionFile(bundlePath string, cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx) error {
log.Infof("Generate %s file", versionFileName)

var versionFileLines []string

// Application version.
appVersionLine := fmt.Sprintf("%s=%s", packCtx.Name, packCtx.Version)
versionFileLines = append(versionFileLines, appVersionLine)

// Tarantool version.
tarantoolVersionLine := fmt.Sprintf("TARANTOOL=%s", cmdCtx.Cli.TarantoolVersion)
versionFileLines = append(versionFileLines, tarantoolVersionLine)

// Rocks versions.
rocksVersionsMap, err := LuaGetRocksVersions(filepath.Join(bundlePath, packCtx.Name))

if err != nil {
log.Warnf("Can't process rocks manifest file. Dependency information can't be "+
"shipped to the resulting package: %s", err)
} else {
for rockName, versions := range rocksVersionsMap {
if rockName != packCtx.Name {
rockLine := fmt.Sprintf("%s=%s", rockName, versions[len(versions)-1])
versionFileLines = append(versionFileLines, rockLine)
}

if len(versions) > 1 {
log.Warnf("Found multiple versions of %s in rocks manifest: %s",
rockName, strings.Join(versions, ", "))
}
}
}

versionFilePath := filepath.Join(bundlePath, packCtx.Name, versionFileName)
err = os.WriteFile(versionFilePath, []byte(strings.Join(versionFileLines, "\n")+"\n"), 0644)
if err != nil {
return fmt.Errorf("failed to write VERSION file %s: %s", versionFilePath, err)
}

return nil
}

// getTgzSuffix returns suffix for a tarball.
func getTgzSuffix() (string, error) {
arch, err := util.GetArch()
Expand Down
Loading

0 comments on commit 3abba2e

Please sign in to comment.