-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from zeropsio/18288
#18288 - push command + refactoring
- Loading branch information
Showing
29 changed files
with
4,480 additions
and
2,400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package buildDeploy | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/zerops-io/zcli/src/i18n" | ||
"github.com/zerops-io/zcli/src/utils" | ||
"github.com/zerops-io/zcli/src/zeropsApiProtocol" | ||
) | ||
|
||
func (h *Handler) checkInputValues(ctx context.Context, config RunConfig) (*zeropsApiProtocol.GetServiceStackByNameResponseDto, error) { | ||
if config.ProjectName == "" { | ||
return nil, errors.New(i18n.BuildDeployProjectNameMissing) | ||
} | ||
|
||
if config.ServiceStackName == "" { | ||
return nil, errors.New(i18n.BuildDeployServiceStackNameMissing) | ||
} | ||
|
||
projectsResponse, err := h.apiGrpcClient.GetProjectsByName(ctx, &zeropsApiProtocol.GetProjectsByNameRequest{ | ||
Name: config.ProjectName, | ||
}) | ||
if err := utils.HandleGrpcApiError(projectsResponse, err); err != nil { | ||
return nil, err | ||
} | ||
|
||
projects := projectsResponse.GetOutput().GetProjects() | ||
if len(projects) == 0 { | ||
return nil, errors.New(i18n.BuildDeployProjectNotFound) | ||
} | ||
if len(projects) > 1 { | ||
return nil, errors.New(i18n.BuildDeployProjectsWithSameName) | ||
} | ||
project := projects[0] | ||
|
||
serviceStackResponse, err := h.apiGrpcClient.GetServiceStackByName(ctx, &zeropsApiProtocol.GetServiceStackByNameRequest{ | ||
ProjectId: project.GetId(), | ||
Name: config.ServiceStackName, | ||
}) | ||
if err := utils.HandleGrpcApiError(serviceStackResponse, err); err != nil { | ||
return nil, err | ||
} | ||
serviceStack := serviceStackResponse.GetOutput() | ||
|
||
fmt.Println(i18n.BuildDeployServiceStatus + ": " + serviceStack.GetStatus().String()) | ||
|
||
return serviceStack, nil | ||
} |
2 changes: 1 addition & 1 deletion
2
src/cliAction/deploy/handler_checkProcess.go → ...ction/buildDeploy/handler_checkProcess.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package deploy | ||
package buildDeploy | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package buildDeploy | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/zerops-io/zcli/src/utils" | ||
"github.com/zerops-io/zcli/src/zeropsApiProtocol" | ||
) | ||
|
||
func (h *Handler) createAppVersion(ctx context.Context, config RunConfig, serviceStack *zeropsApiProtocol.GetServiceStackByNameResponseDto) (*zeropsApiProtocol.PostAppVersionResponseDto, error) { | ||
appVersionResponse, err := h.apiGrpcClient.PostAppVersion(ctx, &zeropsApiProtocol.PostAppVersionRequest{ | ||
ServiceStackId: serviceStack.GetId(), | ||
Name: func() *zeropsApiProtocol.StringNull { | ||
if config.VersionName != "" { | ||
return &zeropsApiProtocol.StringNull{ | ||
Value: config.VersionName, | ||
Valid: true, | ||
} | ||
} | ||
return &zeropsApiProtocol.StringNull{} | ||
}(), | ||
}) | ||
if err := utils.HandleGrpcApiError(appVersionResponse, err); err != nil { | ||
return nil, err | ||
} | ||
appVersion := appVersionResponse.GetOutput() | ||
|
||
return appVersion, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package buildDeploy | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/zerops-io/zcli/src/i18n" | ||
|
||
"github.com/zerops-io/zcli/src/utils" | ||
"github.com/zerops-io/zcli/src/zeropsApiProtocol" | ||
) | ||
|
||
func (h *Handler) Deploy(ctx context.Context, config RunConfig) error { | ||
|
||
serviceStack, err := h.checkInputValues(ctx, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployCreatingPackageStart) | ||
|
||
files, err := h.zipClient.FindFilesByRules(config.WorkingDir, config.PathsForPacking) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
packageBuf := &bytes.Buffer{} | ||
err = h.zipClient.ZipFiles(packageBuf, files) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = h.savePackage(config, packageBuf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
appVersion, err := h.createAppVersion(ctx, config, serviceStack) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = h.packageUpload(appVersion, packageBuf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployDeployingStart) | ||
|
||
temporaryShutdown := false | ||
if serviceStack.GetStatus() == zeropsApiProtocol.ServiceStackStatus_SERVICE_STACK_STATUS_READY_TO_DEPLOY || | ||
serviceStack.GetStatus() == zeropsApiProtocol.ServiceStackStatus_SERVICE_STACK_STATUS_UPGRADE_FAILED { | ||
temporaryShutdown = true | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployTemporaryShutdown+": ", temporaryShutdown) | ||
|
||
deployResponse, err := h.apiGrpcClient.PutAppVersionDeploy(ctx, &zeropsApiProtocol.PutAppVersionDeployRequest{ | ||
Id: appVersion.GetId(), | ||
TemporaryShutdown: temporaryShutdown, | ||
}) | ||
if err := utils.HandleGrpcApiError(deployResponse, err); err != nil { | ||
return err | ||
} | ||
|
||
deployProcessId := deployResponse.GetOutput().GetId() | ||
|
||
err = h.checkProcess(ctx, deployProcessId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeploySuccess) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package buildDeploy | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/zerops-io/zcli/src/i18n" | ||
"github.com/zerops-io/zcli/src/utils/httpClient" | ||
"github.com/zerops-io/zcli/src/zeropsApiProtocol" | ||
) | ||
|
||
func (h *Handler) packageUpload(appVersion *zeropsApiProtocol.PostAppVersionResponseDto, buff *bytes.Buffer) error { | ||
fmt.Println(i18n.BuildDeployUploadingPackageStart) | ||
|
||
cephResponse, err := h.httpClient.Put(appVersion.GetUploadUrl(), buff.Bytes(), httpClient.ContentType("application/zip")) | ||
if err != nil { | ||
return err | ||
} | ||
if cephResponse.StatusCode != http.StatusCreated { | ||
return errors.New(i18n.BuildDeployUploadPackageFailed) | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployUploadingPackageDone) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package buildDeploy | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/zerops-io/zcli/src/utils" | ||
"github.com/zerops-io/zcli/src/zeropsApiProtocol" | ||
|
||
"github.com/zerops-io/zcli/src/i18n" | ||
) | ||
|
||
func (h *Handler) Push(ctx context.Context, config RunConfig) error { | ||
serviceStack, err := h.checkInputValues(ctx, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployCreatingPackageStart) | ||
|
||
files, err := h.zipClient.FindGitFiles(config.WorkingDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buildConfigContent, err := func() ([]byte, error) { | ||
for _, file := range files { | ||
if file.ArchivePath == "zerops_build.yml" { | ||
buildConfigContent, err := ioutil.ReadFile(file.SourcePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(buildConfigContent) == 0 { | ||
return nil, errors.New(i18n.BuildDeployBuildConfigEmpty) | ||
} | ||
if len(buildConfigContent) > 10*1024*1024 { | ||
return nil, errors.New(i18n.BuildDeployBuildConfigTooLarge) | ||
} | ||
|
||
return buildConfigContent, nil | ||
} | ||
} | ||
|
||
return nil, errors.New(i18n.BuildDeployBuildConfigNotFound) | ||
}() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
packageBuf := &bytes.Buffer{} | ||
err = h.zipClient.ZipFiles(packageBuf, files) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployCreatingPackageDone) | ||
|
||
err = h.savePackage(config, packageBuf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
appVersion, err := h.createAppVersion(ctx, config, serviceStack) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = h.packageUpload(appVersion, packageBuf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployDeployingStart) | ||
|
||
deployResponse, err := h.apiGrpcClient.PutAppVersionBuildAndDeploy(ctx, &zeropsApiProtocol.PutAppVersionBuildAndDeployRequest{ | ||
Id: appVersion.GetId(), | ||
BuildConfigContent: base64.StdEncoding.EncodeToString(buildConfigContent), | ||
}) | ||
if err := utils.HandleGrpcApiError(deployResponse, err); err != nil { | ||
return err | ||
} | ||
|
||
deployProcessId := deployResponse.GetOutput().GetId() | ||
|
||
err = h.checkProcess(ctx, deployProcessId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeploySuccess) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package buildDeploy | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/zerops-io/zcli/src/i18n" | ||
) | ||
|
||
func (h *Handler) savePackage(config RunConfig, buff *bytes.Buffer) error { | ||
if config.ZipFilePath != "" { | ||
zipFilePath, err := filepath.Abs(config.ZipFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ioutil.WriteFile(zipFilePath, buff.Bytes(), 0660) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(i18n.BuildDeployPackageSavedInto+": ", zipFilePath) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.