Skip to content

Commit

Permalink
z0 - fix service push, service deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Saidl committed Mar 15, 2024
1 parent 86518e3 commit a211820
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 126 deletions.
12 changes: 4 additions & 8 deletions src/archiveClient/handler_tarFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ type File struct {
ArchivePath string // path to the file in archive using / as separator
}

func (h *Handler) TarFiles(w io.WriteCloser, files []File, errChan chan<- error) {
defer close(errChan)
defer w.Close()

func (h *Handler) TarFiles(w io.Writer, files []File) error {
gz := gzip.NewWriter(w)
defer gz.Close()

Expand All @@ -25,14 +22,13 @@ func (h *Handler) TarFiles(w io.WriteCloser, files []File, errChan chan<- error)
for _, file := range files {
fileInfo, err := os.Lstat(file.SourcePath)
if err != nil {
errChan <- err
return
return err
}

err = tarFile(archive, file, fileInfo)
if err != nil {
errChan <- err
return
return err
}
}
return nil
}
13 changes: 6 additions & 7 deletions src/archiveClient/handler_tarFiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ import (

func TestSymlink(t *testing.T) {
archiver := New(Config{})
errChan := make(chan error)
reader, writer := io.Pipe()

go archiver.TarFiles(
writer,
buf := bytes.NewBuffer(nil)

require.NoError(t, archiver.TarFiles(
buf,
[]File{
{
SourcePath: "./test/var/www/dir/subDir/file3.3.symlink.txt",
ArchivePath: "dir/subDir/file3.3.symlink.txt",
},
},
errChan,
)
))

gz, err := gzip.NewReader(reader)
gz, err := gzip.NewReader(buf)
require.NoError(t, err)

b, err := io.ReadAll(gz)
Expand Down
105 changes: 59 additions & 46 deletions src/cmd/serviceDeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"context"
"io"
"os"
"path"
"time"

"github.com/zeropsio/zcli/src/archiveClient"
Expand All @@ -13,7 +15,7 @@ import (
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/uxHelpers"
"github.com/zeropsio/zerops-go/dto/input/body"
"github.com/zeropsio/zerops-go/dto/input/path"
dtoPath "github.com/zeropsio/zerops-go/dto/input/path"
"github.com/zeropsio/zerops-go/types"
)

Expand Down Expand Up @@ -53,35 +55,6 @@ func serviceDeployCmd() *cmdBuilder.Cmd {

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.BuildDeployCreatingPackageStart)))

files, err := arch.FindFilesByRules(
uxBlocks,
cmdData.Params.GetString("workingDir"),
cmdData.Args["pathToFileOrDir"],
)
if err != nil {
return err
}

var reader io.Reader
pipeReader, writer := io.Pipe()
defer pipeReader.Close()
reader = pipeReader

tarErrChan := make(chan error, 1)

go arch.TarFiles(writer, files, tarErrChan)

if cmdData.Params.GetString("archiveFilePath") != "" {
packageFile, err := openPackageFile(
cmdData.Params.GetString("archiveFilePath"),
cmdData.Params.GetString("workingDir"),
)
if err != nil {
return err
}
reader = io.TeeReader(reader, packageFile)
}

appVersion, err := createAppVersion(
ctx,
cmdData.RestApiClient,
Expand All @@ -92,31 +65,71 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
return err
}

// TODO - janhajek merge with sdk client?
httpClient := httpClient.New(ctx, httpClient.Config{
HttpTimeout: time.Minute * 15,
})

err = uxHelpers.ProcessCheckWithSpinner(
ctx,
cmdData.UxBlocks,
[]uxHelpers.Process{{
F: func(ctx context.Context) error {
if err := packageUpload(ctx, httpClient, appVersion.UploadUrl.String(), reader); err != nil {
// if an error occurred while packing the app, return that error
select {
case err := <-tarErrChan:
var size int64
var reader io.Reader

if cmdData.Params.GetString("archiveFilePath") != "" {
packageFile, err := openPackageFile(
cmdData.Params.GetString("archiveFilePath"),
cmdData.Params.GetString("workingDir"),
)
if err != nil {
return err
default:
}
s, err := packageFile.Stat()
if err != nil {
return err
}
size = s.Size()
reader = packageFile
} else {
tempFile := path.Join(os.TempDir(), appVersion.Id.Native())
f, err := os.Create(tempFile)
if err != nil {
return err
}
defer os.Remove(tempFile)
files, err := arch.FindFilesByRules(
uxBlocks,
cmdData.Params.GetString("workingDir"),
cmdData.Args["pathToFileOrDir"],
)
if err != nil {
return err
}
if err := arch.TarFiles(f, files); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
readFile, err := os.Open(tempFile)
if err != nil {
return err
}
defer readFile.Close()
stat, err := readFile.Stat()
if err != nil {
return err
}
size = stat.Size()
reader = readFile
}

// wait for packing and saving to finish (should already be done after the package upload has finished)
if tarErr := <-tarErrChan; tarErr != nil {
return tarErr
}
// TODO - janhajek merge with sdk client?
client := httpClient.New(ctx, httpClient.Config{
HttpTimeout: time.Minute * 15,
})

if err := packageUpload(ctx, client, appVersion.UploadUrl.String(), reader, httpClient.ContentLength(size)); err != nil {
// if an error occurred while packing the app, return that error
return err
}
return nil
},
RunningMessage: i18n.T(i18n.BuildDeployUploadingPackageStart),
Expand All @@ -132,7 +145,7 @@ func serviceDeployCmd() *cmdBuilder.Cmd {

deployResponse, err := cmdData.RestApiClient.PutAppVersionDeploy(
ctx,
path.AppVersionId{
dtoPath.AppVersionId{
Id: appVersion.Id,
},
body.PutAppVersionDeploy{
Expand All @@ -154,7 +167,7 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
[]uxHelpers.Process{{
F: uxHelpers.CheckZeropsProcess(deployProcess.Id, cmdData.RestApiClient),
RunningMessage: i18n.T(i18n.PushRunning),
ErrorMessageMessage: i18n.T(i18n.PushRunning),
ErrorMessageMessage: i18n.T(i18n.PushFailed),
SuccessMessage: i18n.T(i18n.PushFinished),
}},
)
Expand Down
98 changes: 55 additions & 43 deletions src/cmd/servicePush.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"context"
"io"
"os"
"path"
"time"

"github.com/zeropsio/zcli/src/archiveClient"
Expand All @@ -13,7 +15,7 @@ import (
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/uxHelpers"
"github.com/zeropsio/zerops-go/dto/input/body"
"github.com/zeropsio/zerops-go/dto/input/path"
dtoPath "github.com/zeropsio/zerops-go/dto/input/path"
"github.com/zeropsio/zerops-go/types"
)

Expand Down Expand Up @@ -53,31 +55,6 @@ func servicePushCmd() *cmdBuilder.Cmd {
return err
}

files, err := arch.FindGitFiles(cmdData.Params.GetString("workingDir"))
if err != nil {
return err
}

var reader io.Reader
pipeReader, writer := io.Pipe()
defer pipeReader.Close()
reader = pipeReader

tarErrChan := make(chan error, 1)

go arch.TarFiles(writer, files, tarErrChan)

if cmdData.Params.GetString("archiveFilePath") != "" {
packageFile, err := openPackageFile(
cmdData.Params.GetString("archiveFilePath"),
cmdData.Params.GetString("workingDir"),
)
if err != nil {
return err
}
reader = io.TeeReader(reader, packageFile)
}

appVersion, err := createAppVersion(
ctx,
cmdData.RestApiClient,
Expand All @@ -88,31 +65,66 @@ func servicePushCmd() *cmdBuilder.Cmd {
return err
}

// TODO - janhajek merge with sdk client
httpClient := httpClient.New(ctx, httpClient.Config{
HttpTimeout: time.Minute * 15,
})

err = uxHelpers.ProcessCheckWithSpinner(
ctx,
cmdData.UxBlocks,
[]uxHelpers.Process{{
F: func(ctx context.Context) error {
if err := packageUpload(ctx, httpClient, appVersion.UploadUrl.String(), reader); err != nil {
// if an error occurred while packing the app, return that error
select {
case err := <-tarErrChan:
F: func(ctx context.Context) (err error) {
var size int64
var reader io.Reader

if cmdData.Params.GetString("archiveFilePath") != "" {
packageFile, err := openPackageFile(
cmdData.Params.GetString("archiveFilePath"),
cmdData.Params.GetString("workingDir"),
)
if err != nil {
return err
}
s, err := packageFile.Stat()
if err != nil {
return err
}
size = s.Size()
reader = packageFile
} else {
tempFile := path.Join(os.TempDir(), appVersion.Id.Native())
f, err := os.Create(tempFile)
if err != nil {
return err
}
defer os.Remove(tempFile)
files, err := arch.FindGitFiles(cmdData.Params.GetString("workingDir"))
if err != nil {
return err
}
if err := arch.TarFiles(f, files); err != nil {
return err
default:
}
if err := f.Close(); err != nil {
return err
}
readFile, err := os.Open(tempFile)
if err != nil {
return err
}
defer readFile.Close()
stat, err := readFile.Stat()
if err != nil {
return err
}
size = stat.Size()
reader = readFile
}

// wait for packing and saving to finish (should already be done after the package upload has finished)
if tarErr := <-tarErrChan; tarErr != nil {
return tarErr
// TODO - janhajek merge with sdk client
client := httpClient.New(ctx, httpClient.Config{
HttpTimeout: time.Minute * 15,
})
if err := packageUpload(ctx, client, appVersion.UploadUrl.String(), reader, httpClient.ContentLength(size)); err != nil {
// if an error occurred while packing the app, return that error
return err
}

return nil
},
RunningMessage: i18n.T(i18n.BuildDeployUploadingPackageStart),
Expand All @@ -138,7 +150,7 @@ func servicePushCmd() *cmdBuilder.Cmd {
}

deployResponse, err := cmdData.RestApiClient.PutAppVersionBuildAndDeploy(ctx,
path.AppVersionId{
dtoPath.AppVersionId{
Id: appVersion.Id,
},
body.PutAppVersionBuildAndDeploy{
Expand All @@ -161,7 +173,7 @@ func servicePushCmd() *cmdBuilder.Cmd {
[]uxHelpers.Process{{
F: uxHelpers.CheckZeropsProcess(deployProcess.Id, cmdData.RestApiClient),
RunningMessage: i18n.T(i18n.PushRunning),
ErrorMessageMessage: i18n.T(i18n.PushRunning),
ErrorMessageMessage: i18n.T(i18n.PushFailed),
SuccessMessage: i18n.T(i18n.PushFinished),
}},
)
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/servicePushDeployShared.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ func openPackageFile(archiveFilePath string, workingDir string) (*os.File, error
return file, nil
}

func packageUpload(ctx context.Context, client *httpClient.Handler, uploadUrl string, reader io.Reader) error {
cephResponse, err := client.PutStream(ctx, uploadUrl, reader, httpClient.ContentType("application/gzip"))
func packageUpload(ctx context.Context, client *httpClient.Handler, uploadUrl string, reader io.Reader, options ...httpClient.Option) error {
options = append(options, httpClient.ContentType("application/gzip"))
cephResponse, err := client.PutStream(ctx, uploadUrl, reader, options...)
if err != nil {
return err
}
if cephResponse.StatusCode != http.StatusCreated {
if cephResponse.StatusCode != http.StatusOK {
return errors.New(i18n.T(i18n.BuildDeployUploadPackageFailed))
}

Expand Down
Loading

0 comments on commit a211820

Please sign in to comment.