Skip to content

Commit

Permalink
Merge pull request #16 from zeropsio/18525
Browse files Browse the repository at this point in the history
#18525 - better notifications
  • Loading branch information
jan-hajek authored Jan 5, 2021
2 parents 516f165 + 5435a0c commit cc42784
Show file tree
Hide file tree
Showing 33 changed files with 3,769 additions and 2,182 deletions.
2 changes: 1 addition & 1 deletion src/cliAction/buildDeploy/handler_checkInputValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *Handler) checkInputValues(ctx context.Context, config RunConfig) (*zero
}
serviceStack := serviceStackResponse.GetOutput()

fmt.Println(i18n.BuildDeployServiceStatus + ": " + serviceStack.GetStatus().String())
fmt.Printf(i18n.BuildDeployServiceStatus+"\n", serviceStack.GetStatus().String())

return serviceStack, nil
}
6 changes: 3 additions & 3 deletions src/cliAction/buildDeploy/handler_checkProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package buildDeploy

import (
"context"
"errors"
"github.com/zerops-io/zcli/src/i18n"
"fmt"
"time"

"github.com/zerops-io/zcli/src/i18n"
"github.com/zerops-io/zcli/src/utils"
"github.com/zerops-io/zcli/src/zeropsApiProtocol"
)
Expand All @@ -31,7 +31,7 @@ func (h *Handler) checkProcess(ctx context.Context, processId string) error {

if !(processStatus == zeropsApiProtocol.ProcessStatus_PROCESS_STATUS_RUNNING ||
processStatus == zeropsApiProtocol.ProcessStatus_PROCESS_STATUS_PENDING) {
return errors.New(i18n.ProcessInvalidState + ": " + zeropsApiProtocol.ProcessStatus_name[int32(processStatus)])
return fmt.Errorf(i18n.ProcessInvalidState, getProcessResponse.GetOutput().GetId())
}
time.Sleep(time.Second)
}
Expand Down
5 changes: 2 additions & 3 deletions src/cliAction/buildDeploy/handler_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/zerops-io/zcli/src/i18n"

"github.com/zerops-io/zcli/src/utils"
"github.com/zerops-io/zcli/src/zeropsApiProtocol"
)
Expand Down Expand Up @@ -50,11 +49,11 @@ func (h *Handler) Deploy(ctx context.Context, config RunConfig) error {

temporaryShutdown := false
if serviceStack.GetStatus() == zeropsApiProtocol.ServiceStackStatus_SERVICE_STACK_STATUS_READY_TO_DEPLOY ||
serviceStack.GetStatus() == zeropsApiProtocol.ServiceStackStatus_SERVICE_STACK_STATUS_UPGRADE_FAILED {
serviceStack.GetStatus() == zeropsApiProtocol.ServiceStackStatus_SERVICE_STACK_STATUS_ACTION_FAILED {
temporaryShutdown = true
}

fmt.Println(i18n.BuildDeployTemporaryShutdown+": ", temporaryShutdown)
fmt.Printf(i18n.BuildDeployTemporaryShutdown+"\n", temporaryShutdown)

deployResponse, err := h.apiGrpcClient.PutAppVersionDeploy(ctx, &zeropsApiProtocol.PutAppVersionDeployRequest{
Id: appVersion.GetId(),
Expand Down
2 changes: 1 addition & 1 deletion src/cliAction/buildDeploy/handler_savePackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (h *Handler) savePackage(config RunConfig, buff *bytes.Buffer) error {
return err
}

fmt.Println(i18n.BuildDeployPackageSavedInto+": ", zipFilePath)
fmt.Printf(i18n.BuildDeployPackageSavedInto+"\n", zipFilePath)
}
return nil
}
2 changes: 1 addition & 1 deletion src/cliAction/login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (h *Handler) Run(ctx context.Context, runConfig RunConfig) error {
return nil
}

func (h *Handler) loginWithPassword(ctx context.Context, login, password string) error {
func (h *Handler) loginWithPassword(_ context.Context, login, password string) error {

if login == "" {
return errors.New(i18n.LoginZeropsLoginMissing)
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/daemonRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func daemonRunCmd() *cobra.Command {
}
}()

grpcServer := createDaemonGrpcServer(vpn)
grpcServer, err := createDaemonGrpcServer(vpn)
if err != nil {
return err
}

wg.Add(1)
go func() {
defer wg.Done()
Expand Down
12 changes: 10 additions & 2 deletions src/cmd/di.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"
"path"
"time"

"github.com/zerops-io/zcli/src/cliStorage"
Expand Down Expand Up @@ -64,10 +66,16 @@ func createVpn(
)
}

func createDaemonGrpcServer(vpn *vpn.Handler) *grpcDaemonServer.Handler {
func createDaemonGrpcServer(vpn *vpn.Handler) (*grpcDaemonServer.Handler, error) {
socketDir := path.Dir(constants.SocketFilePath)
err := os.MkdirAll(socketDir, 0755)
if err != nil {
return nil, err
}

return grpcDaemonServer.New(grpcDaemonServer.Config{
Socket: constants.SocketFilePath,
},
vpn,
)
), nil
}
2 changes: 1 addition & 1 deletion src/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func loginCmd() *cobra.Command {
}

httpClient := httpClient.New(httpClient.Config{
HttpTimeout: time.Second * 10,
HttpTimeout: time.Second * 5,
})

apiClientFactory := grpcApiClientFactory.New(grpcApiClientFactory.Config{
Expand Down
18 changes: 15 additions & 3 deletions src/daemonInstaller/handler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
package daemonInstaller

import (
"errors"
"os"
"os/exec"
"path"
"regexp"
"text/template"

"github.com/zerops-io/zcli/src/constants"
"github.com/zerops-io/zcli/src/i18n"
)

type darwinRecord struct {
Expand All @@ -31,6 +33,16 @@ func (daemon *darwinRecord) Install() error {
return ErrAlreadyInstalled
}

_, err := exec.LookPath("wg")
if err != nil {
return errors.New(i18n.DaemonInstallWireguardNotFoundDarwin)
}

_, err = exec.LookPath("wireguard-go")
if err != nil {
return errors.New(i18n.DaemonInstallWireguardNotFoundDarwin)
}

cliBinaryPath, err := os.Executable()
if err != nil {
return err
Expand All @@ -43,8 +55,8 @@ func (daemon *darwinRecord) Install() error {
}
defer file.Close()

logDir, _ := path.Split(constants.LogFilePath)
daemonStorageDir, _ := path.Split(constants.DaemonStorageFilePath)
logDir := path.Dir(constants.LogFilePath)
daemonStorageDir := path.Dir(constants.DaemonStorageFilePath)

templ, err := template.New("propertyList").Parse(propertyList)
if err != nil {
Expand Down Expand Up @@ -99,7 +111,7 @@ func (daemon *darwinRecord) Remove() error {
}
}

daemonStorageDir, _ := path.Split(constants.DaemonStorageFilePath)
daemonStorageDir := path.Dir(constants.DaemonStorageFilePath)

{
err := sudoCommands(
Expand Down
25 changes: 15 additions & 10 deletions src/daemonInstaller/handler_linux_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
package daemonInstaller

import (
"errors"
"os"
"os/exec"
"path"
"regexp"
"strings"
"text/template"

"github.com/zerops-io/zcli/src/dns"

"github.com/zerops-io/zcli/src/constants"
"github.com/zerops-io/zcli/src/dns"
"github.com/zerops-io/zcli/src/i18n"
)

type systemDRecord struct {
Expand All @@ -26,6 +27,11 @@ func (daemon *systemDRecord) Install() error {
return ErrAlreadyInstalled
}

_, err := exec.LookPath("wg")
if err != nil {
return errors.New(i18n.DaemonInstallWireguardNotFound)
}

tmpServiceFilePath := path.Join(os.TempDir(), daemon.serviceName())
file, err := os.Create(tmpServiceFilePath)
if err != nil {
Expand All @@ -39,8 +45,8 @@ func (daemon *systemDRecord) Install() error {
}

// create read writes paths
logDir, _ := path.Split(constants.LogFilePath)
daemonStorageDir, _ := path.Split(constants.DaemonStorageFilePath)
logDir := path.Dir(constants.LogFilePath)
daemonStorageDir := path.Dir(constants.DaemonStorageFilePath)
readWritePaths := []string{
logDir,
daemonStorageDir,
Expand All @@ -51,17 +57,16 @@ func (daemon *systemDRecord) Install() error {
return err
}
if dnsManagement == dns.LocalDnsManagementResolveConf {
dir, _ := path.Split(constants.ResolvconfOrderFilePath)
dir := path.Dir(constants.ResolvconfOrderFilePath)
readWritePaths = append(readWritePaths, dir)
readWritePaths = append(readWritePaths, "/run/resolvconf/")
}
if dnsManagement == dns.LocalDnsManagementFile {
dir, _ := path.Split(constants.ResolvFilePath)
dir := path.Dir(constants.ResolvFilePath)
readWritePaths = append(readWritePaths, dir)
}

runtimeDirectory, _ := path.Split(constants.SocketFilePath)
runtimeDirectoryName := path.Base(runtimeDirectory)
runtimeDirectoryName := path.Base(path.Dir(constants.SocketFilePath))

if err := tmpl.Execute(
file,
Expand Down Expand Up @@ -122,8 +127,8 @@ func (daemon *systemDRecord) Remove() error {
}
}

logDir, _ := path.Split(constants.LogFilePath)
DaemonStorageDir, _ := path.Split(constants.DaemonStorageFilePath)
logDir := path.Dir(constants.LogFilePath)
DaemonStorageDir := path.Dir(constants.DaemonStorageFilePath)

{
err := sudoCommands(
Expand Down
13 changes: 10 additions & 3 deletions src/dns/handler_detectDns.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ const (

func DetectDns() (LocalDnsManagement, error) {

if utils.FileExists(scutil.BinaryLocation) {
binaryLocationExists, err := utils.FileExists(scutil.BinaryLocation)
if err != nil {
return "", err
}
if binaryLocationExists {
return LocalDnsManagementScutil, nil
}

resolvExists := utils.FileExists(constants.ResolvFilePath)
resolvExists, err := utils.FileExists(constants.ResolvFilePath)
if err != nil {
return "", err
}

if resolvExists {
valid, err := isValidSystemdResolve(constants.ResolvFilePath)
Expand All @@ -42,7 +49,7 @@ func DetectDns() (LocalDnsManagement, error) {
}
}

_, err := exec.LookPath("resolvconf")
_, err = exec.LookPath("resolvconf")
if err == nil {
return LocalDnsManagementResolveConf, nil
}
Expand Down
43 changes: 43 additions & 0 deletions src/dns/handler_isAlive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dns

import (
"context"
"errors"
"os/exec"
"time"

"github.com/zerops-io/zcli/src/i18n"
)

func IsAlive() (bool, error) {

_, err := exec.LookPath("dig")
if err == nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
ipAddress, err := exec.CommandContext(ctx, "dig", "+short", "core-master.zerops", "AAAA").Output()
if err != nil {
return false, err
}
if string(ipAddress) == "" {
return false, nil
}

return true, nil
}

// ping6 fallback
_, err = exec.LookPath("ping6")
if err == nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
_, err = exec.CommandContext(ctx, "ping6", "-c", "1", "core-master.zerops").Output()
if err != nil {
return false, nil
}

return true, nil
}

return false, errors.New(i18n.VpnStatusDnsNoCheckFunction)
}
7 changes: 7 additions & 0 deletions src/grpcApiClientFactory/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

"golang.org/x/oauth2"
"google.golang.org/grpc/credentials/oauth"
Expand Down Expand Up @@ -44,7 +45,13 @@ func (h *Handler) CreateClient(ctx context.Context, grpcApiAddress string, token
grpcApiAddress,
grpc.WithPerRPCCredentials(h.createBearerCredentials(token)),
grpc.WithTransportCredentials(tlsCreds),
grpc.WithChainUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
return invoker(timeoutCtx, method, req, reply, cc, opts...)
}),
)

if err != nil {
return
}
Expand Down
Loading

0 comments on commit cc42784

Please sign in to comment.