Skip to content

Commit

Permalink
fix zip
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-hajek committed Jun 8, 2020
1 parent 6c6fd3c commit 6b35b17
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 75 deletions.
15 changes: 8 additions & 7 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

func deployCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "deploy projectName serviceName",
Use: "deploy projectName serviceName pathToFileOrDir [pathToFileOrDir]",
SilenceUsage: true,
Args: cobra.ExactArgs(2),
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
regSignals(cancel, logger)
Expand Down Expand Up @@ -49,15 +49,16 @@ func deployCmd() *cobra.Command {
logger,
apiGrpcClient,
).Run(ctx, deploy.RunConfig{
SourceDirectoryPath: params.GetString("sourceDirectory"),
ZipFilePath: params.GetString("zipFilePath"),
ProjectName: args[0],
ServiceStackName: args[1],
ZipFilePath: params.GetString("zipFilePath"),
WorkingDir: params.GetString("workingDir"),
ProjectName: args[0],
ServiceStackName: args[1],
PathsForPacking: args[2:],
})
},
}

params.RegisterString(cmd, "sourceDirectory", "./", "directory with source code, it will be zipped and will be send to zerops server for deploy")
params.RegisterString(cmd, "workingDir", "./", "working dir, all files path are relative to this directory")
params.RegisterString(cmd, "zipFilePath", "", "if it's set, save final zip file")

return cmd
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
module github.com/zerops-io/zcli


go 1.14

require (
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang/protobuf v1.3.1
github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/onsi/gomega v1.10.1
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.3
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 // indirect
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect
golang.org/x/text v0.3.2 // indirect
golang.zx2c4.com/wireguard v0.0.20200320 // indirect
google.golang.org/grpc v1.21.0
gopkg.in/ini.v1 v1.55.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
64 changes: 55 additions & 9 deletions go.sum

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/command/deploy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ type Config struct {
}

type RunConfig struct {
ProjectName string
ServiceStackName string
SourceDirectoryPath string
ZipFilePath string
ProjectName string
ServiceStackName string
PathsForPacking []string
WorkingDir string
ZipFilePath string
}

type Handler struct {
Expand Down
21 changes: 8 additions & 13 deletions src/command/deploy/handler_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (h *Handler) Run(ctx context.Context, config RunConfig) error {

h.logger.Info("temporaryShutdown: ", temporaryShutdown)

h.logger.Info("creating package")

appVersionResponse, err := h.apiGrpcClient.PostAppVersion(ctx, &zeropsApiProtocol.PostAppVersionRequest{
ServiceStackId: serviceStack.GetId(),
})
Expand All @@ -68,38 +70,31 @@ func (h *Handler) Run(ctx context.Context, config RunConfig) error {
}
appVersion := appVersionResponse.GetOutput()

sourceDirectoryPath, err := filepath.Abs(config.SourceDirectoryPath)
if err != nil {
return err
}

h.logger.Info("packing directory: ", sourceDirectoryPath)

data := &bytes.Buffer{}
err = h.zipClient.Zip(sourceDirectoryPath, data)
buff := &bytes.Buffer{}
err = h.zipClient.Zip(buff, config.WorkingDir, config.PathsForPacking...)
if err != nil {
return err
}

h.logger.Info("packing is done")
h.logger.Info("creating is done")

if config.ZipFilePath != "" {
zipFilePath, err := filepath.Abs(config.ZipFilePath)
if err != nil {
return err
}

err = ioutil.WriteFile(zipFilePath, data.Bytes(), 0660)
err = ioutil.WriteFile(zipFilePath, buff.Bytes(), 0660)
if err != nil {
return err
}

h.logger.Info("zip file saved into: ", zipFilePath)
}

h.logger.Info("uploading to zerops server")
h.logger.Info("uploading package to zerops server")

cephResponse, err := h.httpClient.Put(appVersion.GetUploadUrl(), data.Bytes(), httpClient.ContentType("application/zip"))
cephResponse, err := h.httpClient.Put(appVersion.GetUploadUrl(), buff.Bytes(), httpClient.ContentType("application/zip"))
if err != nil {
return err
}
Expand Down
113 changes: 76 additions & 37 deletions src/service/zipClient/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package zipClient

import (
"archive/zip"
"errors"
"io"
"os"
"path"
"path/filepath"
"strings"
)
Expand All @@ -21,57 +23,94 @@ func New(config Config) *Handler {
}
}

func (h *Handler) Zip(source string, w io.Writer) error {
func (h *Handler) Zip(w io.Writer, workingDir string, sources ...string) error {
archive := zip.NewWriter(w)
defer archive.Close()

info, err := os.Stat(source)
if err != nil {
return err
}

var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
for _, source := range sources {

err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
parts := strings.Split(source, "*")
if len(parts) > 2 {
return errors.New("only one *(asterisk) is allowed")
}
source := path.Join(parts...)

header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
source = path.Join(workingDir, source)

if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
err := filepath.Walk(source, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}

writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
header.Name = strings.TrimPrefix(func(filePath string) string {
if info.IsDir() {
filePath += "/"
}

filePath = strings.TrimPrefix(filePath, workingDir)

if len(parts) == 1 {
return filePath
} else {
if filePath == parts[0] {
return ""
}
return strings.TrimPrefix(filePath, parts[0])
}

}(filePath), "/")

if header.Name == "" {
return nil
}

if !info.IsDir() {
header.Method = zip.Deflate
}

writer, err := archive.CreateHeader(header)
if err != nil {
return err
}

if info.IsDir() {
return nil
}

if header.FileInfo().Mode()&os.ModeSymlink != 0 {
symlink, err := os.Readlink(filePath)
if err != nil {
return err
}

_, err = writer.Write([]byte(symlink))
if err != nil {
return err
}
} else {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
if err != nil {
return err
}
}

if info.IsDir() {
return nil
}
})

file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
}

return err
return nil
}
1 change: 1 addition & 0 deletions src/service/zipClient/test/var/www/dir/file2.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
symlink
Empty file.
Empty file.
Empty file.
Empty file.
Loading

0 comments on commit 6b35b17

Please sign in to comment.