moved to central copy function

This commit is contained in:
Brad Rydzewski 2015-05-17 19:25:53 -07:00
parent c828c706cb
commit 73ed4f08d5
9 changed files with 144 additions and 230 deletions

7
.gitignore vendored
View file

@ -23,11 +23,12 @@ drone
*_bindata.go
# generate binaries
cmd/drone/drone
cmd/drone-build/drone-build
cmd/drone-agent/drone-agent
cmd/drone-build/drone-build
cmd/drone-agent/drone-server
# generated binaries
bin/drone
bin/drone-build
bin/drone-agent
bin/drone-build
bin/drone-server

View file

@ -28,6 +28,11 @@ concat:
cmd/drone-server/static/scripts/controllers/*.js \
cmd/drone-server/static/scripts/term.js > cmd/drone-server/static/scripts/drone.min.js
# installs the drone binaries into bin
install:
install -t /usr/local/bin bin/drone
install -t /usr/local/bin bin/drone-agent
# embeds all the static files directly
# into the drone binary file
bindata:

View file

@ -7,4 +7,3 @@ FROM gliderlabs/alpine:3.1
RUN apk-install ca-certificates
ADD drone-build /bin/
ENTRYPOINT ["/bin/drone-build"]

View file

@ -5,6 +5,7 @@ import (
"os"
log "github.com/Sirupsen/logrus"
"github.com/drone/drone/pkg/docker"
"github.com/samalba/dockerclient"
)
@ -154,7 +155,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo
return
}
defer rc.Close()
StdCopy(os.Stdout, os.Stdout, rc)
docker.StdCopy(os.Stdout, os.Stdout, rc)
// fetches the container information
info, err := client.InspectContainer(id)

View file

@ -1,98 +0,0 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Drone API",
"contact": {
"name": "Team Drone",
"url": "https://github.com/drone/drone"
},
"license": {
"name": "Creative Commons 4.0 International",
"url": "http://creativecommons.org/licenses/by/4.0/"
}
},
"host": "localhost:8080",
"basePath": "/api",
"schemes": [
"http"
],
"paths": {
"/user": {
"get": {
"description": "Returns the currently authenticated user.",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "The currently authenticated user.",
"schema": {
"$ref": "#/definitions/User"
}
}
}
},
"patch": {
"description": "Updates the currently authenticated user.",
"produces": [
"application/json"
],
"parameters": [
{
"name": "pet",
"in": "body",
"description": "Updates to the user.",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"200": {
"description": "The updated user.",
"schema": {
"$ref": "#/definitions/User"
}
}
}
}
}
},
"definitions": {
"User": {
"required": [
"login"
],
"properties": {
"login": {
"type": "string"
},
"name": {
"type": "string"
},
"email": {
"type": "string"
},
"gravatar_id": {
"type": "string"
},
"admin": {
"type": "boolean"
},
"active": {
"type": "boolean"
},
"created_at": {
"type": "integer",
"format": "int64"
},
"updated_at": {
"type": "integer",
"format": "int64"
}
}
}
}
}

129
doc/swagger.yml Normal file
View file

@ -0,0 +1,129 @@
swagger: "2.0"
info:
version: 1.0.0
title: Drone API
contact:
name: Team Drone
url: "https://github.com/drone/drone"
license:
name: Creative Commons 4.0 International
url: "http://creativecommons.org/licenses/by/4.0/"
host: "localhost:8080"
basePath: /api
schemes:
- http
paths:
/user:
get:
description: Returns the currently authenticated user.
produces:
- application/json
responses:
"200":
description: The currently authenticated user.
schema:
$ref: "#/definitions/User"
patch:
description: Updates the currently authenticated user.
produces:
- application/json
parameters:
- name: user
in: body
description: Updates to the user.
required: true
schema:
$ref: "#/definitions/User"
responses:
"200":
description: The updated user.
schema:
$ref: "#/definitions/User"
definitions:
User:
properties:
login:
type: string
name:
type: string
email:
type: string
gravatar_id:
type: string
admin:
type: boolean
active:
type: boolean
created_at:
type: integer
format: int64
updated_at:
type: integer
format: int64
Repo:
properties:
owner:
type: string
name:
type: string
created_at:
type: integer
format: int64
updated_at:
type: integer
format: int64
Commit:
properties:
sequence:
type: integer
state:
type: string
sha:
type: string
ref:
type: string
branch:
type: string
author:
type: string
gravatar_id:
type: string
message:
type: string
pull_request:
type: boolean
started_at:
type: integer
format: int64
finished_at:
type: integer
format: int64
created_at:
type: integer
format: int64
updated_at:
type: integer
format: int64
Build:
properties:
sequence:
type: integer
state:
type: string
exit_code:
type: integer
duration:
type: integer
format: int64
started_at:
type: integer
format: int64
finished_at:
type: integer
format: int64
created_at:
type: integer
format: int64
updated_at:
type: integer
format: int64

View file

@ -1,4 +1,4 @@
package main
package docker
import (
"encoding/binary"

View file

@ -1,124 +0,0 @@
package builtin
import (
"encoding/binary"
"errors"
"io"
)
const (
StdWriterPrefixLen = 8
StdWriterFdIndex = 0
StdWriterSizeIndex = 4
)
type StdType [StdWriterPrefixLen]byte
var (
Stdin StdType = StdType{0: 0}
Stdout StdType = StdType{0: 1}
Stderr StdType = StdType{0: 2}
)
type StdWriter struct {
io.Writer
prefix StdType
sizeBuf []byte
}
var ErrInvalidStdHeader = errors.New("Unrecognized input header")
// StdCopy is a modified version of io.Copy.
//
// StdCopy will demultiplex `src`, assuming that it contains two streams,
// previously multiplexed together using a StdWriter instance.
// As it reads from `src`, StdCopy will write to `dstout` and `dsterr`.
//
// StdCopy will read until it hits EOF on `src`. It will then return a nil error.
// In other words: if `err` is non nil, it indicates a real underlying error.
//
// `written` will hold the total number of bytes written to `dstout` and `dsterr`.
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
var (
buf = make([]byte, 32*1024+StdWriterPrefixLen+1)
bufLen = len(buf)
nr, nw int
er, ew error
out io.Writer
frameSize int
)
for {
// Make sure we have at least a full header
for nr < StdWriterPrefixLen {
var nr2 int
nr2, er = src.Read(buf[nr:])
nr += nr2
if er == io.EOF {
if nr < StdWriterPrefixLen {
return written, nil
}
break
}
if er != nil {
return 0, er
}
}
// Check the first byte to know where to write
switch buf[StdWriterFdIndex] {
case 0:
fallthrough
case 1:
// Write on stdout
out = dstout
case 2:
// Write on stderr
out = dsterr
default:
return 0, ErrInvalidStdHeader
}
// Retrieve the size of the frame
frameSize = int(binary.BigEndian.Uint32(buf[StdWriterSizeIndex : StdWriterSizeIndex+4]))
// Check if the buffer is big enough to read the frame.
// Extend it if necessary.
if frameSize+StdWriterPrefixLen > bufLen {
buf = append(buf, make([]byte, frameSize+StdWriterPrefixLen-bufLen+1)...)
bufLen = len(buf)
}
// While the amount of bytes read is less than the size of the frame + header, we keep reading
for nr < frameSize+StdWriterPrefixLen {
var nr2 int
nr2, er = src.Read(buf[nr:])
nr += nr2
if er == io.EOF {
if nr < frameSize+StdWriterPrefixLen {
return written, nil
}
break
}
if er != nil {
return 0, er
}
}
// Write the retrieved frame (without header)
nw, ew = out.Write(buf[StdWriterPrefixLen : frameSize+StdWriterPrefixLen])
if ew != nil {
return 0, ew
}
// If the frame has not been fully written: error
if nw != frameSize {
return 0, io.ErrShortWrite
}
written += int64(nw)
// Move the rest of the buffer to the beginning
copy(buf, buf[frameSize+StdWriterPrefixLen:])
// Move the index
nr -= frameSize + StdWriterPrefixLen
}
}

View file

@ -9,6 +9,7 @@ import (
"os"
"time"
"github.com/drone/drone/pkg/docker"
"github.com/drone/drone/pkg/queue"
common "github.com/drone/drone/pkg/types"
"github.com/samalba/dockerclient"
@ -153,7 +154,7 @@ func (r *Runner) Run(w *queue.Work) error {
return err
} else {
defer rc.Close()
StdCopy(&buf, &buf, rc)
docker.StdCopy(&buf, &buf, rc)
}
err = r.SetLogs(w.Repo, w.Commit, task, ioutil.NopCloser(&buf))
if err != nil {
@ -248,7 +249,7 @@ func (r *Runner) Logs(build *common.Build) (io.ReadCloser, error) {
pr, pw := io.Pipe()
go func() {
defer rc.Close()
StdCopy(pw, pw, rc)
docker.StdCopy(pw, pw, rc)
}()
return pr, nil
}