Skip to content

Commit

Permalink
uploads gzip to main container
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 3404b14 commit 35fa525
Show file tree
Hide file tree
Showing 38 changed files with 813 additions and 279 deletions.
8 changes: 4 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package main

import (
"os"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/exec"
)
Expand All @@ -14,7 +16,7 @@ func build(c tsuru.Client, appName string, cmd []string, fs Filesystem, executor
if err != nil {
return err
}
return execScript(cmd, envs, nil, fs, executor)
return execScript(cmd, envs, os.Stdout, fs, executor)
}

func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executor) error {
Expand All @@ -24,7 +26,7 @@ func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executo
return err
}
diff, firstDeploy, err := readDiffDeploy(fs)
if !firstDeploy || err != nil {
if !firstDeploy && err == nil {
err = c.SendDiffDeploy(diff, appName)
if err != nil {
return err
Expand Down
26 changes: 18 additions & 8 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (f *executorFS) ReadFile(name string) ([]byte, error) {
out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
opts := exec.ExecuteOptions{
Cmd: "cat",
Args: []string{name},
Cmd: "/bin/sh",
Args: []string{"-c", fmt.Sprintf("cat %s", name)},
Stdout: out,
Stderr: errOut,
}
Expand All @@ -67,22 +67,32 @@ func (f *executorFS) ReadFile(name string) ([]byte, error) {
}

func (f *executorFS) CheckFile(name string) (bool, error) {
outErr := new(bytes.Buffer)
opts := exec.ExecuteOptions{
Cmd: "stat",
Args: []string{name},
Cmd: "/bin/sh",
Args: []string{"-c", fmt.Sprintf("stat %s", name)},
Stderr: outErr,
}
if err := f.executor.Execute(opts); err != nil {
if strings.Contains(strings.ToLower(err.Error()), "no such") {
errOut := outErr.String()
if strings.Contains(strings.ToLower(errOut), "no such") {
return false, nil
}
return false, err
return false, fmt.Errorf("error checking file %v: %v. Output: %v", name, err, errOut)
}
return true, nil
}

func (f *executorFS) RemoveFile(name string) error {
return f.executor.Execute(exec.ExecuteOptions{
Cmd: "rm",
Args: []string{name},
Cmd: "/bin/sh",
Args: []string{"-c", fmt.Sprintf("rm %s", name)},
})
}

func (f *executorFS) CreateDir(dirname string) error {
return f.executor.Execute(exec.ExecuteOptions{
Cmd: "/bin/sh",
Args: []string{"-c", fmt.Sprintf("mkdir -p %s", dirname)},
})
}
12 changes: 11 additions & 1 deletion internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package docker
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
Expand Down Expand Up @@ -106,13 +107,22 @@ func (c *Client) Push(ctx context.Context, authConfig AuthConfig, img Image) err
opts := docker.PushImageOptions{
Name: img.Name(),
Tag: img.tag,
OutputStream: os.Stdout,
OutputStream: &errorCheckWriter{W: os.Stdout},
Context: ctx,
InactivityTimeout: streamInactivityTimeout,
RawJSONStream: true,
}
return c.api.PushImage(opts, docker.AuthConfiguration(authConfig))
}

func (c *Client) Upload(ctx context.Context, containerID, path string, inputStream io.Reader) error {
opts := docker.UploadToContainerOptions{
Path: path,
InputStream: inputStream,
}
return c.api.UploadToContainer(containerID, opts)
}

func splitImageName(imageName string) (registry, repo, tag string) {
imgNameSplit := strings.Split(imageName, ":")
switch len(imgNameSplit) {
Expand Down
19 changes: 7 additions & 12 deletions internal/docker/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package docker

import (
"fmt"
"os"
"strings"

"github.com/fsouza/go-dockerclient"
"github.com/tsuru/tsuru/exec"
Expand All @@ -19,18 +19,15 @@ type Executor struct {
}

func (d *Executor) Execute(opts exec.ExecuteOptions) error {
if opts.Stdout == nil {
opts.Stdout = os.Stdout
}
if opts.Stderr == nil {
opts.Stderr = os.Stderr
}
cmd := append([]string{opts.Cmd}, opts.Args...)
if cmd[0] != "/bin/sh" && (opts.Dir != "" || len(opts.Envs) > 0) {
cmd = append([]string{"/bin/sh", "-c"}, strings.Join(cmd, " "))
}
if opts.Dir != "" {
cmd = append([]string{"/bin/sh", "-c", "cd", opts.Dir, "&&"}, cmd...)
cmd = append(cmd[:2], fmt.Sprintf("cd %s && %s", opts.Dir, strings.Join(cmd[2:], " ")))
}
if len(opts.Envs) > 0 {
cmd = append([]string{"/bin/sh", "-c", "export"}, append(opts.Envs, cmd...)...)
cmd = append(cmd[:2], fmt.Sprintf("env %s && %s", strings.Join(opts.Envs, " "), strings.Join(cmd[2:], " ")))
}
e, err := d.Client.api.CreateExec(docker.CreateExecOptions{
Container: d.ContainerID,
Expand All @@ -46,8 +43,6 @@ func (d *Executor) Execute(opts exec.ExecuteOptions) error {
OutputStream: opts.Stdout,
InputStream: opts.Stdin,
ErrorStream: opts.Stderr,
Tty: true,
RawTerminal: true,
})
if err != nil {
return err
Expand All @@ -57,7 +52,7 @@ func (d *Executor) Execute(opts exec.ExecuteOptions) error {
return err
}
if execData.ExitCode != 0 {
return fmt.Errorf("container exited with error: %v", execData.ExitCode)
return fmt.Errorf("unexpected exit code %#+v while running %v", execData.ExitCode, cmd)
}
return nil
}
48 changes: 48 additions & 0 deletions internal/docker/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package docker

import (
"bytes"
"encoding/json"
"errors"
"io"
)

type errorCheckWriter struct {
W io.Writer
b []byte
}

type dockerJSONMessage struct {
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"errorDetail,omitempty"`
ErrorMessage string `json:"error"`
}

func (w *errorCheckWriter) Write(data []byte) (n int, err error) {
n, err = w.W.Write(data)
if err != nil {
return
}
w.b = append(w.b, data...)
if len(w.b) == 0 {
return
}
parts := bytes.Split(w.b, []byte("\n"))
w.b = parts[len(parts)-1]
var msg dockerJSONMessage
for _, part := range parts {
jsonErr := json.Unmarshal(part, &msg)
if jsonErr != nil {
continue
}
if msg.Error != nil {
return 0, errors.New(msg.Error.Message)
}
if msg.ErrorMessage != "" {
return 0, errors.New(msg.ErrorMessage)
}
}
return
}
74 changes: 74 additions & 0 deletions internal/docker/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package docker

import (
"bytes"

"gopkg.in/check.v1"
)

func (s *S) TestErrorCheckWriter(c *check.C) {
tests := []struct {
msg []string
err string
}{
{
msg: []string{`
{"status":"Pulling from tsuru/static","id":"latest"}
something
{invalid},
{"other": "other"}
`}},
{
msg: []string{`
{"status":"Pulling from tsuru/static","id":"latest"}
something
{"errorDetail": {"message": "my err msg"}}
{"other": "other"}
`},
err: `my err msg`,
},
{
msg: []string{`
{"status":"Pulling from tsuru/static","id":"latest"}
something
{"errorDetail": {"`, `message": "my err msg"}}
{"other": "other"}
`},
err: `my err msg`,
},
{
msg: []string{`
{"status":"Pulling from tsuru/static","id":"latest"}
something`, `
{"errorDetail": {"message": "my err msg"}}`, `
{"other": "other"}
`},
err: `my err msg`,
},
{
msg: []string{`{"errorDetail": {"message"`, `: "my err msg"}}`},
err: `my err msg`,
},
{
msg: []string{`
{"error":`, ` "my err msg"}`},
err: `my err msg`,
},
}
for _, tt := range tests {
buf := bytes.NewBuffer(nil)
writer := errorCheckWriter{W: buf}
var err error
for _, msg := range tt.msg {
_, err = writer.Write([]byte(msg))
if err != nil {
break
}
}
if tt.err != "" {
c.Assert(err, check.ErrorMatches, tt.err)
} else {
c.Assert(err, check.IsNil)
}
}
}
Loading

0 comments on commit 35fa525

Please sign in to comment.