<commit_before>package handler

import (
	"errors"
	"fmt"
	"net/http"
	"regexp"
	"runtime"
	"runtime/debug"

	"github.com/kataras/iris"
	"github.com/mlanin/go-apierr"
)

// Config for the Handler.
type Config struct {
	EnvGetter   func() string
	DebugGetter func() bool
}

// Handler for APIErrors.
type Handler struct {
	Config Config
}

// New restores the server on internal server errors (panics)
// returns the middleware
//
// is here for compatiblity
func New(cfg Config) *Handler {
	return &Handler{Config: cfg}
}

// Serve the middleware.
func (h *Handler) Serve(ctx *iris.Context) {
	defer func() {
		messages := make([]interface{}, 0)

		if err := recover(); err != nil {
			fail := h.convertToAPIError(err)

			if h.needToReport(fail) {
				messages = append(messages, fmt.Sprintf("[apierr.APIError] %+v [%+v]\n", err, fail.Context))
				if h.needToAddTrace(fail) {
					messages = append(messages, fmt.Sprintf("--> %+v\n", h.thrower()))
					messages = append(messages, string(debug.Stack()))
				}
				ctx.Log(fmt.Sprintln(messages...))
			}

			ctx.JSON(fail.HTTPCode, fail)
		}
	}()

	ctx.Next()
}

// Converts catched error to internal apierr.APIError instance.
func (h *Handler) convertToAPIError(err interface{}) *apierr.APIError {
	var fail *apierr.APIError

	switch err := err.(type) {
	case *apierr.APIError:
		fail = err
	case error:
		fail = h.NewAPIError(err.(error))
	case string:
		fail = h.NewAPIError(errors.New(err))
	default:
		fail = h.NewAPIError(fmt.Errorf("%v", err))
	}

	return fail
}

// NewAPIError makes new API error.
func (h *Handler) NewAPIError(err error) *apierr.APIError {
	// Don't show unknown error text to user when in production.
	if h.isProduction() {
		return apierr.InternalServerError
	}

	return &apierr.APIError{
		Body: apierr.Body{
			ID:      "internal_server_error",
			Message: err.Error(),
		},
		HTTPCode:     http.StatusInternalServerError,
		ShouldReport: true,
	}
}

// Check if we neer to report the error.
func (h *Handler) needToReport(fail *apierr.APIError) bool {
	return fail.WantsToBeReported() || !h.isProduction()
}

// Check if we neer to report the error.
func (h *Handler) needToAddTrace(fail *apierr.APIError) bool {
	return fail.WantsToShowTrace() || !h.isDebugOn()
}

// Check if app in production mode.
func (h *Handler) isProduction() bool {
	return h.Config.EnvGetter() == "production"
}

// Check if app in debug mode is on.
func (h *Handler) isDebugOn() bool {
	return h.Config.DebugGetter()
}

// Return a possible line, where panic was thrown.
func (h *Handler) thrower() string {
	var goSrcRegexp = regexp.MustCompile(`(mlanin/apierr-handler/.*.go)|(libexec/src/runtime)`)
	var goTestRegexp = regexp.MustCompile(`mlanin/apierr-handler/.*test.go`)

	for i := 2; i < 15; i++ {
		_, file, line, ok := runtime.Caller(i)
		if ok && (!goSrcRegexp.MatchString(file) || goTestRegexp.MatchString(file)) {
			return fmt.Sprintf("%v:%v", file, line)
		}
	}

	return ""
}
<commit_msg>Fix newline bug<commit_after>package handler

import (
	"errors"
	"fmt"
	"net/http"
	"regexp"
	"runtime"
	"runtime/debug"
	"strings"

	"github.com/kataras/iris"
	"github.com/mlanin/go-apierr"
)

// Config for the Handler.
type Config struct {
	EnvGetter   func() string
	DebugGetter func() bool
}

// Handler for APIErrors.
type Handler struct {
	Config Config
}

// New restores the server on internal server errors (panics)
// returns the middleware
//
// is here for compatiblity
func New(cfg Config) *Handler {
	return &Handler{Config: cfg}
}

// Serve the middleware.
func (h *Handler) Serve(ctx *iris.Context) {
	defer func() {
		messages := make([]string, 0)

		if err := recover(); err != nil {
			fail := h.convertToAPIError(err)

			if h.needToReport(fail) {
				messages = append(messages, fmt.Sprintf("[apierr.APIError] %+v [%+v]", err, fail.Context))
				if h.needToAddTrace(fail) {
					messages = append(messages, fmt.Sprintf("--> %+v", h.thrower()))
					messages = append(messages, string(debug.Stack()))
				}
				ctx.Log(strings.Join(messages, "\n"))
			}

			ctx.JSON(fail.HTTPCode, fail)
		}
	}()

	ctx.Next()
}

// Converts catched error to internal apierr.APIError instance.
func (h *Handler) convertToAPIError(err interface{}) *apierr.APIError {
	var fail *apierr.APIError

	switch err := err.(type) {
	case *apierr.APIError:
		fail = err
	case error:
		fail = h.NewAPIError(err.(error))
	case string:
		fail = h.NewAPIError(errors.New(err))
	default:
		fail = h.NewAPIError(fmt.Errorf("%v", err))
	}

	return fail
}

// NewAPIError makes new API error.
func (h *Handler) NewAPIError(err error) *apierr.APIError {
	// Don't show unknown error text to user when in production.
	if h.isProduction() {
		return apierr.InternalServerError
	}

	return &apierr.APIError{
		Body: apierr.Body{
			ID:      "internal_server_error",
			Message: err.Error(),
		},
		HTTPCode:     http.StatusInternalServerError,
		ShouldReport: true,
	}
}

// Check if we neer to report the error.
func (h *Handler) needToReport(fail *apierr.APIError) bool {
	return fail.WantsToBeReported() || !h.isProduction()
}

// Check if we neer to report the error.
func (h *Handler) needToAddTrace(fail *apierr.APIError) bool {
	return fail.WantsToShowTrace() || !h.isDebugOn()
}

// Check if app in production mode.
func (h *Handler) isProduction() bool {
	return h.Config.EnvGetter() == "production"
}

// Check if app in debug mode is on.
func (h *Handler) isDebugOn() bool {
	return h.Config.DebugGetter()
}

// Return a possible line, where panic was thrown.
func (h *Handler) thrower() string {
	var goSrcRegexp = regexp.MustCompile(`(mlanin/apierr-handler/.*.go)|(libexec/src/runtime)`)
	var goTestRegexp = regexp.MustCompile(`mlanin/apierr-handler/.*test.go`)

	for i := 2; i < 15; i++ {
		_, file, line, ok := runtime.Caller(i)
		if ok && (!goSrcRegexp.MatchString(file) || goTestRegexp.MatchString(file)) {
			return fmt.Sprintf("%v:%v", file, line)
		}
	}

	return ""
}
<|endoftext|>


<commit_before>// apns.go
//
// Documentation related to the JSON alert message:
// http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
//
// Documentation related to the binary protocol:
// http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
package apns

import (
	"bytes"
	"encoding/binary"
	"encoding/hex"
	"encoding/json"
	"errors"
	"strconv"
)

// The first byte of the push notification payload is the command value,
// which at this time is fixed to 1 by Apple.
const PUSH_COMMAND_VALUE = 1

// The total length of the payload cannot exceed this amount.
const MAX_PAYLOAD_SIZE_BYTES = 256

// The Envelope is the overall wrapper for the various push notification fields.
// It contains a Message, which may either contain a BasicAlert (which is a simple string
// message) or a DictionaryAlert (which contains localization fields). Apple recommends using
// the basic alert structure if at all possible.
//
// You can also set CustomProperties ( "acme": "foo" ) if desired. See the documentation
// listed at the top of this file for more information on how to best use APNs.
type Envelope struct {
	DeviceToken      string                 `json:"-"`
	Identifier       int32                  `json:"-"`
	Expiry           uint32                 `json:"-"`
	CustomProperties map[string]interface{} `json:"-"`
}

type Message struct {
	BasicAlert      string           `json:"alert,omitempty"`
	DictionaryAlert *DictionaryAlert `json:"alert,omitempty"`
	Badge           int              `json:"badge,omitempty"`
	Sound           string           `json:"sound,omitempty"`
}

type DictionaryAlert struct {
	Body         string   `json:"body,omitempty"`
	ActionLocKey string   `json:"action-loc-key,omitempty"`
	LocKey       string   `json:"loc-key,omitempty"`
	LocArgs      []string `json:"loc-args,omitempty"`
	LaunchImage  string   `json:"launch-image,omitempty"`
}

// Inserts the message into the envelope.
func (this *Envelope) SetMessage(m *Message) {
	this.Set("aps", m)
}

// The push notification requests support arbitrary custom metadata.
// This method requires a string key and any type for the value.
func (this *Envelope) Set(key string, value interface{}) {
	if this.CustomProperties == nil {
		this.CustomProperties = make(map[string]interface{})
	}
	this.CustomProperties[key] = value
}

// This method looks up a string key and returns the value, or nil.
func (this *Envelope) Get(key string) (value interface{}) {
	if this.CustomProperties == nil {
		value = nil
	} else {
		value = this.CustomProperties[key]
	}
	return
}

// Returns the JSON structure as a byte array.
func (this *Envelope) ToJSON() (j []byte, err error) {
	j, err = json.Marshal(this.CustomProperties)
	return
}

// Returns the JSON structure as a string.
func (this *Envelope) ToString() (s string, err error) {
	j, err := this.ToJSON()
	if err != nil {
		return "", err
	}
	s = string(j)
	return
}

// Returns a byte array of the complete Envelope struct. This array
// is what should be transmitted to the APN Service.
func (this *Envelope) ToBytes() (b []byte, err error) {
	token, err := hex.DecodeString(this.DeviceToken)
	if err != nil {
		return nil, err
	}
	payload, err := this.ToJSON()
	if err != nil {
		return nil, err
	}
	if len(payload) > MAX_PAYLOAD_SIZE_BYTES {
		return nil, errors.New("payload is larger than the " + strconv.Itoa(MAX_PAYLOAD_SIZE_BYTES) + " byte limit")
	}

	buffer := bytes.NewBuffer([]byte{})
	binary.Write(buffer, binary.BigEndian, uint8(PUSH_COMMAND_VALUE))
	binary.Write(buffer, binary.BigEndian, uint32(this.Identifier))
	binary.Write(buffer, binary.BigEndian, uint32(this.Expiry))
	binary.Write(buffer, binary.BigEndian, uint16(len(token)))
	binary.Write(buffer, binary.BigEndian, token)
	binary.Write(buffer, binary.BigEndian, uint16(len(payload)))
	binary.Write(buffer, binary.BigEndian, payload)
	b = buffer.Bytes()

	return b, nil
}
<commit_msg>Minor signature cleanup<commit_after>// apns.go
//
// Documentation related to the JSON alert message:
// http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
//
// Documentation related to the binary protocol:
// http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
package apns

import (
	"bytes"
	"encoding/binary"
	"encoding/hex"
	"encoding/json"
	"errors"
	"strconv"
)

// The first byte of the push notification payload is the command value,
// which at this time is fixed to 1 by Apple.
const PUSH_COMMAND_VALUE = 1

// The total length of the payload cannot exceed this amount.
const MAX_PAYLOAD_SIZE_BYTES = 256

// The Envelope is the overall wrapper for the various push notification fields.
// It contains a Message, which may either contain a BasicAlert (which is a simple string
// message) or a DictionaryAlert (which contains localization fields). Apple recommends using
// the basic alert structure if at all possible.
//
// You can also set CustomProperties ( "acme": "foo" ) if desired. See the documentation
// listed at the top of this file for more information on how to best use APNs.
type Envelope struct {
	DeviceToken      string                 `json:"-"`
	Identifier       int32                  `json:"-"`
	Expiry           uint32                 `json:"-"`
	CustomProperties map[string]interface{} `json:"-"`
}

type Message struct {
	BasicAlert      string           `json:"alert,omitempty"`
	DictionaryAlert *DictionaryAlert `json:"alert,omitempty"`
	Badge           int              `json:"badge,omitempty"`
	Sound           string           `json:"sound,omitempty"`
}

type DictionaryAlert struct {
	Body         string   `json:"body,omitempty"`
	ActionLocKey string   `json:"action-loc-key,omitempty"`
	LocKey       string   `json:"loc-key,omitempty"`
	LocArgs      []string `json:"loc-args,omitempty"`
	LaunchImage  string   `json:"launch-image,omitempty"`
}

// Inserts the message into the envelope.
func (this *Envelope) SetMessage(m *Message) {
	this.Set("aps", m)
}

// The push notification requests support arbitrary custom metadata.
// This method requires a string key and any type for the value.
func (this *Envelope) Set(key string, value interface{}) {
	if this.CustomProperties == nil {
		this.CustomProperties = make(map[string]interface{})
	}
	this.CustomProperties[key] = value
}

// This method looks up a string key and returns the value, or nil.
func (this *Envelope) Get(key string) interface{} {
	if this.CustomProperties == nil {
		return nil
	}
	return this.CustomProperties[key]
}

// Returns the JSON structure as a byte array.
func (this *Envelope) ToJSON() ([]byte, error) {
	return json.Marshal(this.CustomProperties)
}

// Returns the JSON structure as a string.
func (this *Envelope) ToString() string {
	j, err := this.ToJSON()
	if err != nil {
		return ""
	}
	return string(j)
}

// Returns a byte array of the complete Envelope struct. This array
// is what should be transmitted to the APN Service.
func (this *Envelope) ToBytes() ([]byte, error) {
	token, err := hex.DecodeString(this.DeviceToken)
	if err != nil {
		return nil, err
	}
	payload, err := this.ToJSON()
	if err != nil {
		return nil, err
	}
	if len(payload) > MAX_PAYLOAD_SIZE_BYTES {
		return nil, errors.New("payload is larger than the " + strconv.Itoa(MAX_PAYLOAD_SIZE_BYTES) + " byte limit")
	}

	buffer := bytes.NewBuffer([]byte{})
	binary.Write(buffer, binary.BigEndian, uint8(PUSH_COMMAND_VALUE))
	binary.Write(buffer, binary.BigEndian, uint32(this.Identifier))
	binary.Write(buffer, binary.BigEndian, uint32(this.Expiry))
	binary.Write(buffer, binary.BigEndian, uint16(len(token)))
	binary.Write(buffer, binary.BigEndian, token)
	binary.Write(buffer, binary.BigEndian, uint16(len(payload)))
	binary.Write(buffer, binary.BigEndian, payload)
	return buffer.Bytes(), nil
}
<|endoftext|>


<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Ray Zhang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package client

import (
	"bufio"
	"crypto/tls"
	"errors"
	"io"
	"net"
	"os"
	"strconv"
	"sync/atomic"
	"time"

	"github.com/123hurray/netroxy/common"
	"github.com/123hurray/netroxy/utils/logger"
	"github.com/123hurray/netroxy/utils/security"
)

const defaultBufferSize = 16 * 1024

type Client struct {
	common.ProtocolReader
	conn       net.Conn
	targets    map[int]*common.Mapping
	ip         string
	port       int
	config     *ClientConfig
	exitChan   chan bool
	expireTime int32
	timeout    int
	name       string
	token      string
}

func NewClient(config *ClientConfig) *Client {
	client := new(Client)
	client.ip = config.Ip
	client.port = config.Port
	client.config = config
	client.targets = make(map[int]*common.Mapping)
	client.exitChan = make(chan bool)
	client.expireTime = 0
	name, err := os.Hostname()
	if err != nil {
		logger.Fatal("Cannot get Hostname.")
	}
	client.name = name + "-" + security.GenerateUID(8)
	return client
}

func (self *Client) send(content string) {
	self.conn.Write([]byte(content))
}

func (self *Client) Login() error {
	var conn net.Conn
	var err error
	if self.config.TLS.Enabled == true {
		tlsConfig := tls.Config{InsecureSkipVerify: !self.config.TLS.Verify}
		logger.Debug("Using TLS.")
		conn, err = tls.Dial("tcp", self.ip+":"+strconv.Itoa(self.port), &tlsConfig)
	} else {
		conn, err = net.Dial("tcp", self.ip+":"+strconv.Itoa(self.port))
	}
	if err != nil {
		return err
	}
	self.conn = conn
	logger.Debug("Client name:", self.name)
	self.auth(self.name, self.config.Username, self.config.Password)
	self.SetReader(bufio.NewReaderSize(conn, defaultBufferSize))
	ars, err := self.GetString()
	if err != nil {
		return err
	}
	if ars != "ARS" {
		logger.Warn("Illegal command, expected ARS but receive", ars)
		return errors.New("Illegal command")
	}
	isOK, err := self.GetBool()
	if err != nil {
		return err
	}
	if isOK == true {
		timeout, err := self.GetInt()
		if err != nil {
			logger.Warn("Illegal parameter")
			return err
		}
		token, err := self.GetString()
		if err != nil {
			logger.Warn("Illegal parameter")
			return err
		}
		self.timeout = timeout
		self.token = token
		logger.Info("Login to server success.")
		go self.supervise()
		go self.handle()
		return nil
	} else {
		return errors.New("Auth failed")
	}
}

func (self *Client) supervise() {
	ticker := time.NewTicker(time.Duration(self.timeout) * time.Second)
	for {
		select {
		case <-ticker.C:
			if atomic.AddInt32(&self.expireTime, 1) == 3 {
				logger.Warn("Supervise failed.")
				ticker.Stop()
				self.Close()
				return
			} else {
				self.superviseRequest()
			}
		case <-self.exitChan:
			ticker.Stop()
			logger.Info("Supervise stop.")
			return
		}
	}
}

func (self *Client) Close() {
	err := self.conn.Close()
	if err == nil {
		logger.Debug("Close self.exitChan")
		close(self.exitChan)
	}
}

func (self *Client) handle() {
	defer self.Close()
	for {
		command, err := self.GetString()
		if err != nil {
			logger.Warn("Connection closed.", err)
			return
		}
		switch {
		case command == "SRS":
			atomic.StoreInt32(&self.expireTime, 0)
		case command == "MRS":
			remotePort, err := self.GetInt()
			if err != nil {
				logger.Warn("Illegal parament.", err)
				return
			}
			isOk, err := self.GetBool()
			if err != nil {
				logger.Warn("Illegal parament.", err)
				return
			}
			t := self.targets[remotePort]
			if t != nil {

				if isOk == false {
					logger.Warn("Map", t.Addr(), "port failed.")
					break
				}
				logger.Info("Mapping", self.conn.RemoteAddr(), "<->", t.Addr(), "accepted.")
			}
		case command == "TRQ":
			logger.Info("Tunnel request.")
			addr := self.ip + ":" + strconv.Itoa(self.port)
			remotePort, err := self.GetInt()
			if err != nil {
				logger.Warn("Illegal parament.", err)
				return
			}
			t := self.targets[remotePort]
			if t != nil {
				logger.Info("New tunnel", self.ip+":"+strconv.Itoa(remotePort), "<->", t.Addr(), "Establishing...")
				conn1, err := net.Dial("tcp", addr)
				if err != nil {
					logger.Warn("Cannot connect to", addr, err)
					break
				}
				conn2, err := net.Dial("tcp", t.Addr())
				if err != nil {
					logger.Warn("Cannot connect to", t.Addr(), err)
					break
				}
				self.channelResponse(conn1, remotePort, self.token)
				logger.Info("Dial " + t.Addr() + " OK")
				logger.Info("New tunnel", self.ip+":"+strconv.Itoa(remotePort), "<->", t.Addr(), "created.")
				go func() {
					io.Copy(conn1, conn2)
					logger.Debug("Proxy conn1 closed.")
					defer conn1.Close()
				}()
				go func() {
					io.Copy(conn2, conn1)
					logger.Debug("Proxy conn2 closed.")
					defer conn2.Close()
				}()
			}
		default:
			logger.Warn("Illegal command:", command)
			return
		}
	}
}

func (self *Client) Wait() {
	<-self.exitChan
}
func (self *Client) Connect(mapConfig *ConnectionConfig) (*common.Mapping, error) {
	addr := mapConfig.Ip + ":" + strconv.Itoa(mapConfig.Port)
	t := common.NewMapping(mapConfig.Ip, mapConfig.Port, mapConfig.RemotePort, mapConfig.IsOpen)
	logger.Info("Send new mapping", addr, ":", t.Addr(), "request...")
	self.mapRequest(mapConfig.RemotePort, addr, mapConfig.IsOpen)
	self.targets[mapConfig.RemotePort] = t
	return t, nil
}
<commit_msg>Fix bug: Client should use TLS in TRS<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Ray Zhang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package client

import (
	"bufio"
	"crypto/tls"
	"errors"
	"io"
	"net"
	"os"
	"strconv"
	"sync/atomic"
	"time"

	"github.com/123hurray/netroxy/common"
	"github.com/123hurray/netroxy/utils/logger"
	"github.com/123hurray/netroxy/utils/security"
)

const defaultBufferSize = 16 * 1024

type Client struct {
	common.ProtocolReader
	conn       net.Conn
	targets    map[int]*common.Mapping
	ip         string
	port       int
	config     *ClientConfig
	exitChan   chan bool
	expireTime int32
	timeout    int
	name       string
	token      string
}

func NewClient(config *ClientConfig) *Client {
	client := new(Client)
	client.ip = config.Ip
	client.port = config.Port
	client.config = config
	client.targets = make(map[int]*common.Mapping)
	client.exitChan = make(chan bool)
	client.expireTime = 0
	name, err := os.Hostname()
	if err != nil {
		logger.Fatal("Cannot get Hostname.")
	}
	client.name = name + "-" + security.GenerateUID(8)
	return client
}

func (self *Client) send(content string) {
	self.conn.Write([]byte(content))
}

func (self *Client) Login() error {
	var conn net.Conn
	var err error
	if self.config.TLS.Enabled == true {
		tlsConfig := tls.Config{InsecureSkipVerify: !self.config.TLS.Verify}
		logger.Debug("Using TLS.")
		conn, err = tls.Dial("tcp", self.ip+":"+strconv.Itoa(self.port), &tlsConfig)
	} else {
		conn, err = net.Dial("tcp", self.ip+":"+strconv.Itoa(self.port))
	}
	if err != nil {
		return err
	}
	self.conn = conn
	logger.Debug("Client name:", self.name)
	self.auth(self.name, self.config.Username, self.config.Password)
	self.SetReader(bufio.NewReaderSize(conn, defaultBufferSize))
	ars, err := self.GetString()
	if err != nil {
		return err
	}
	if ars != "ARS" {
		logger.Warn("Illegal command, expected ARS but receive", ars)
		return errors.New("Illegal command")
	}
	isOK, err := self.GetBool()
	if err != nil {
		return err
	}
	if isOK == true {
		timeout, err := self.GetInt()
		if err != nil {
			logger.Warn("Illegal parameter")
			return err
		}
		token, err := self.GetString()
		if err != nil {
			logger.Warn("Illegal parameter")
			return err
		}
		self.timeout = timeout
		self.token = token
		logger.Info("Login to server success.")
		go self.supervise()
		go self.handle()
		return nil
	} else {
		return errors.New("Auth failed")
	}
}

func (self *Client) supervise() {
	ticker := time.NewTicker(time.Duration(self.timeout) * time.Second)
	for {
		select {
		case <-ticker.C:
			if atomic.AddInt32(&self.expireTime, 1) == 3 {
				logger.Warn("Supervise failed.")
				ticker.Stop()
				self.Close()
				return
			} else {
				self.superviseRequest()
			}
		case <-self.exitChan:
			ticker.Stop()
			logger.Info("Supervise stop.")
			return
		}
	}
}

func (self *Client) Close() {
	err := self.conn.Close()
	if err == nil {
		logger.Debug("Close self.exitChan")
		close(self.exitChan)
	}
}

func (self *Client) handle() {
	defer self.Close()
	for {
		command, err := self.GetString()
		if err != nil {
			logger.Warn("Connection closed.", err)
			return
		}
		switch {
		case command == "SRS":
			atomic.StoreInt32(&self.expireTime, 0)
		case command == "MRS":
			remotePort, err := self.GetInt()
			if err != nil {
				logger.Warn("Illegal parament.", err)
				return
			}
			isOk, err := self.GetBool()
			if err != nil {
				logger.Warn("Illegal parament.", err)
				return
			}
			t := self.targets[remotePort]
			if t != nil {

				if isOk == false {
					logger.Warn("Map", t.Addr(), "port failed.")
					break
				}
				logger.Info("Mapping", self.conn.RemoteAddr(), "<->", t.Addr(), "accepted.")
			}
		case command == "TRQ":
			logger.Info("Tunnel request.")
			addr := self.ip + ":" + strconv.Itoa(self.port)
			remotePort, err := self.GetInt()
			if err != nil {
				logger.Warn("Illegal parament.", err)
				return
			}
			t := self.targets[remotePort]
			if t != nil {
				logger.Info("New tunnel", self.ip+":"+strconv.Itoa(remotePort), "<->", t.Addr(), "Establishing...")
				var conn1 net.Conn
				if self.config.TLS.Enabled == true {
					tlsConfig := tls.Config{InsecureSkipVerify: !self.config.TLS.Verify}
					conn1, err = tls.Dial("tcp", addr, &tlsConfig)
				} else {
					conn1, err = net.Dial("tcp", addr)
				}
				if err != nil {
					logger.Warn("Cannot connect to", addr, err)
					break
				}
				conn2, err := net.Dial("tcp", t.Addr())
				if err != nil {
					logger.Warn("Cannot connect to", t.Addr(), err)
					break
				}
				self.channelResponse(conn1, remotePort, self.token)
				logger.Info("Dial " + t.Addr() + " OK")
				logger.Info("New tunnel", self.ip+":"+strconv.Itoa(remotePort), "<->", t.Addr(), "created.")
				go func() {
					io.Copy(conn1, conn2)
					logger.Debug("Proxy conn1 closed.")
					defer conn1.Close()
				}()
				go func() {
					io.Copy(conn2, conn1)
					logger.Debug("Proxy conn2 closed.")
					defer conn2.Close()
				}()
			}
		default:
			logger.Warn("Illegal command:", command)
			return
		}
	}
}

func (self *Client) Wait() {
	<-self.exitChan
}
func (self *Client) Connect(mapConfig *ConnectionConfig) (*common.Mapping, error) {
	addr := mapConfig.Ip + ":" + strconv.Itoa(mapConfig.Port)
	t := common.NewMapping(mapConfig.Ip, mapConfig.Port, mapConfig.RemotePort, mapConfig.IsOpen)
	logger.Info("Send new mapping", addr, ":", t.Addr(), "request...")
	self.mapRequest(mapConfig.RemotePort, addr, mapConfig.IsOpen)
	self.targets[mapConfig.RemotePort] = t
	return t, nil
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/koding/kite"
	"os"
)

func main() {
	k := kite.New("client", "1.0.0")
        k.Config.DisableAuthentication = true

	fmt.Println(k.Config)

	client := k.NewClient("http://" + os.Getenv("SQUARE_SERVICE_HOST")  + ":" + os.Getenv("SQUARE_SERVICE_PORT") + "/kite")
	connected, err := client.DialForever()
	if err != nil {
		k.Log.Fatal(err.Error())
	}

	// Wait until connected
	<-connected

	response, err := client.Tell("square", 4)
	if err != nil {
		panic(err)
	}
	fmt.Println(response.MustFloat64())
}
<commit_msg>Added loop<commit_after>package main

import (
	"fmt"
	"github.com/koding/kite"
	"os"
)

func main() {
	k := kite.New("client", "1.0.0")
        k.Config.DisableAuthentication = true

	fmt.Println(k.Config)

	client := k.NewClient("http://" + os.Getenv("SQUARE_SERVICE_HOST")  + ":" + os.Getenv("SQUARE_SERVICE_PORT") + "/kite")
	connected, err := client.DialForever()
	if err != nil {
		k.Log.Fatal(err.Error())
	}

	// Wait until connected
	<-connected
	for true {
		response, err := client.Tell("square", 4)
		if err != nil {
			panic(err)
		}
		fmt.Println(response.MustFloat64())
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"flag"
	"fmt"
	"github.com/golang/glog"
	"github.com/heidi-ann/hydra/config"
	"github.com/heidi-ann/hydra/test"
	"io"
	"net"
	"os"
	"time"
)

var config_file = flag.String("config", "example.config", "Client configuration file")
var auto_file = flag.String("auto", "", "If workload is automatically generated, configure file for workload")

func connect(addrs []string, tries int) (net.Conn, error) {
	var conn net.Conn
	var err error

	for i := range addrs {
		for t := tries; t > 0; t-- {
			conn, err = net.Dial("tcp", addrs[i])

			// if successful
			if err == nil {
				glog.Infof("Connected to %s", addrs[i])
				return conn, err
			}

			//if unsuccessful
			glog.Warning(err)
			time.Sleep(2 * time.Second)
		}
	}

	return conn, err
}

func main() {
	// set up logging
	flag.Parse()
	defer glog.Flush()

	// parse config files
	conf := config.Parse(*config_file)
	interactive_mode := (*auto_file == "")
	var gen test.Generator
	if !interactive_mode {
		gen = test.Generate(test.ParseAuto(*auto_file))
	}

	// set up stats collection
	filename := "latency.csv"
	glog.Info("Opening file: ", filename)
	file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		glog.Fatal(err)
	}
	stats := bufio.NewWriter(file)
	defer stats.Flush()

	// connecting to server
	conn, err := connect(conf.Addresses.Address, 3)
	if err != nil {
		glog.Fatal(err)
	}

	// mian mian
	term_reader := bufio.NewReader(os.Stdin)
	net_reader := bufio.NewReader(conn)

	for {
		text := ""

		if interactive_mode {
			// reading from terminal
			fmt.Print("Enter command: ")
			text, err = term_reader.ReadString('\n')
			if err != nil {
				glog.Fatal(err)
			}
			glog.Info("User entered", text)
		} else {
			// use generator
			text = gen.Next()
			glog.Info("Generator produced ", text)
		}

		// send to server
		startTime := time.Now()
		_, err = conn.Write([]byte(text))
		if err != nil {
			if err == io.EOF {
				continue
			}
			glog.Warning(err)
			// reconnecting to server
			conn, err = connect(conf.Addresses.Address, 3)
			if err != nil {
				glog.Fatal(err)
			}
		}

		glog.Info("Sent ", text)

		// read response
		reply, err := net_reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				continue
			}
			glog.Warning(err)
			// reconnecting to server
			conn, err = connect(conf.Addresses.Address, 3)
			if err != nil {
				glog.Fatal(err)
			}
		}

		// write to latency to log
		str := fmt.Sprintf("%d\n", time.Since(startTime).Nanoseconds())
		n, err := stats.WriteString(str)
		if err != nil {
			glog.Fatal(err)
		}
		glog.Warningf("Written %d bytes to log", n)
		_ = stats.Flush()

		// writing result to user
		if interactive_mode {
			fmt.Print(reply, "request took ", time.Since(startTime))
		}

	}

}
<commit_msg>adding stat file option<commit_after>package main

import (
	"bufio"
	"flag"
	"fmt"
	"github.com/golang/glog"
	"github.com/heidi-ann/hydra/config"
	"github.com/heidi-ann/hydra/test"
	"io"
	"net"
	"os"
	"time"
)

var config_file = flag.String("config", "example.config", "Client configuration file")
var auto_file = flag.String("auto", "", "If workload is automatically generated, configure file for workload")
var stat_file = flag.String("config", "latency.csv", "File to write stats to")

func connect(addrs []string, tries int) (net.Conn, error) {
	var conn net.Conn
	var err error

	for i := range addrs {
		for t := tries; t > 0; t-- {
			conn, err = net.Dial("tcp", addrs[i])

			// if successful
			if err == nil {
				glog.Infof("Connected to %s", addrs[i])
				return conn, err
			}

			//if unsuccessful
			glog.Warning(err)
			time.Sleep(2 * time.Second)
		}
	}

	return conn, err
}

func main() {
	// set up logging
	flag.Parse()
	defer glog.Flush()

	// parse config files
	conf := config.Parse(*config_file)
	interactive_mode := (*auto_file == "")
	var gen test.Generator
	if !interactive_mode {
		gen = test.Generate(test.ParseAuto(*auto_file))
	}

	// set up stats collection
	filename := *stat_file
	glog.Info("Opening file: ", filename)
	file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		glog.Fatal(err)
	}
	stats := bufio.NewWriter(file)
	defer stats.Flush()

	// connecting to server
	conn, err := connect(conf.Addresses.Address, 3)
	if err != nil {
		glog.Fatal(err)
	}

	// mian mian
	term_reader := bufio.NewReader(os.Stdin)
	net_reader := bufio.NewReader(conn)

	for {
		text := ""

		if interactive_mode {
			// reading from terminal
			fmt.Print("Enter command: ")
			text, err = term_reader.ReadString('\n')
			if err != nil {
				glog.Fatal(err)
			}
			glog.Info("User entered", text)
		} else {
			// use generator
			text = gen.Next()
			glog.Info("Generator produced ", text)
		}

		// send to server
		startTime := time.Now()
		_, err = conn.Write([]byte(text))
		if err != nil {
			if err == io.EOF {
				continue
			}
			glog.Warning(err)
			// reconnecting to server
			conn, err = connect(conf.Addresses.Address, 3)
			if err != nil {
				glog.Fatal(err)
			}
		}

		glog.Info("Sent ", text)

		// read response
		reply, err := net_reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				continue
			}
			glog.Warning(err)
			// reconnecting to server
			conn, err = connect(conf.Addresses.Address, 3)
			if err != nil {
				glog.Fatal(err)
			}
		}

		// write to latency to log
		str := fmt.Sprintf("%d\n", time.Since(startTime).Nanoseconds())
		n, err := stats.WriteString(str)
		if err != nil {
			glog.Fatal(err)
		}
		glog.Warningf("Written %d bytes to log", n)
		_ = stats.Flush()

		// writing result to user
		if interactive_mode {
			fmt.Print(reply, "request took ", time.Since(startTime))
		}

	}

}
<|endoftext|>


<commit_before>package client

import (
	"bytes"
	"encoding/json"
	"errors"
	"io"
	"net/http"
	"sync"
	"sync/atomic"
	"time"

	"github.com/cozy/cozy-stack/client/auth"
	"github.com/cozy/cozy-stack/client/request"
)

// ErrWrongPassphrase is used when the passphrase is wrong
var ErrWrongPassphrase = errors.New("Unauthorized: wrong passphrase")

// jsonAPIErrors is a group of errors. It is the error type returned by the
// API.
type jsonAPIErrors struct {
	Errors []*request.Error `json:"errors"`
}

// jsonAPIDocument is a simple JSONAPI document used to un-serialized
type jsonAPIDocument struct {
	Data     json.RawMessage `json:"data"`
	Included json.RawMessage `json:"included"`
}

// Client encapsulates the element representing a typical connection to the
// HTTP api of the cozy-stack.
//
// It holds the elements to authenticate a user, as well as the transport layer
// used for all the calls to the stack.
type Client struct {
	Domain string
	Client *http.Client

	AuthClient  *auth.Client
	AuthScopes  []string
	AuthAccept  auth.UserAcceptFunc
	AuthStorage auth.Storage
	Authorizer  request.Authorizer

	UserAgent     string
	Retries       int
	Timeout       time.Duration
	Transport     http.RoundTripper
	DisableSecure bool

	authed bool
	inited int32
	authMu sync.Mutex
	auth   *auth.Request
}

func (c *Client) init() {
	if !atomic.CompareAndSwapInt32(&c.inited, 0, 1) {
		return
	}
	if c.Retries == 0 {
		c.Retries = 3
	}
	if c.Timeout == 0 {
		c.Timeout = 30 * time.Second
	}
	if c.Transport == nil {
		c.Transport = &http.Transport{
			Proxy: http.ProxyFromEnvironment,
		}
	}
	if c.AuthStorage == nil {
		c.AuthStorage = auth.NewFileStorage()
	}
	if c.Client == nil {
		c.Client = &http.Client{
			Transport: c.Transport,
			Timeout:   c.Timeout,
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				return http.ErrUseLastResponse
			},
		}
	}
}

// Authenticate is used to authenticate a client via OAuth.
func (c *Client) Authenticate() (request.Authorizer, error) {
	c.init()
	c.authMu.Lock()
	defer c.authMu.Unlock()
	if c.authed {
		return c.auth, nil
	}
	if c.auth == nil {
		c.auth = &auth.Request{
			ClientParams:  c.AuthClient,
			Scopes:        c.AuthScopes,
			Domain:        c.Domain,
			DisableSecure: c.DisableSecure,
			HTTPClient:    c.Client,
			UserAgent:     c.UserAgent,
			UserAccept:    c.AuthAccept,
			Storage:       c.AuthStorage,
		}
	}
	if err := c.auth.Authenticate(); err != nil {
		return nil, err
	}
	c.authed = true
	return c.auth, nil
}

// Req is used to perform a request to the stack given the ReqOptions passed.
func (c *Client) Req(opts *request.Options) (*http.Response, error) {
	c.init()
	var err error
	if c.Authorizer != nil {
		opts.Authorizer = c.Authorizer
	} else {
		opts.Authorizer, err = c.Authenticate()
	}
	if err != nil {
		return nil, err
	}
	opts.Domain = c.Domain
	opts.Client = c.Client
	opts.UserAgent = c.UserAgent
	opts.DisableSecure = c.DisableSecure
	opts.ParseError = parseJSONAPIError
	return request.Req(opts)
}

func parseJSONAPIError(res *http.Response, b []byte) error {
	var errs jsonAPIErrors
	if err := json.Unmarshal(b, &errs); err != nil || errs.Errors == nil || len(errs.Errors) == 0 {
		return &request.Error{
			Status: http.StatusText(res.StatusCode),
			Title:  http.StatusText(res.StatusCode),
			Detail: string(b),
		}
	}
	// TODO: handle multi-error
	return errs.Errors[0]
}

func readJSONAPI(r io.ReadCloser, data interface{}, included interface{}) (err error) {
	defer func() {
		if cerr := r.Close(); err == nil && cerr != nil {
			err = cerr
		}
	}()
	var doc jsonAPIDocument
	decoder := json.NewDecoder(r)
	if err = decoder.Decode(&doc); err != nil {
		return err
	}
	if data != nil {
		if err = json.Unmarshal(doc.Data, &data); err != nil {
			return err
		}
	}
	if included != nil && doc.Included != nil {
		if err = json.Unmarshal(doc.Included, &included); err != nil {
			return err
		}
	}
	return nil
}

func writeJSONAPI(data interface{}) (io.Reader, error) {
	buf, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}

	doc := jsonAPIDocument{Data: buf}
	buf, err = json.Marshal(doc)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(buf), nil
}
<commit_msg>Make readJSONApi accept simple io.Reader<commit_after>package client

import (
	"bytes"
	"encoding/json"
	"errors"
	"io"
	"net/http"
	"sync"
	"sync/atomic"
	"time"

	"github.com/cozy/cozy-stack/client/auth"
	"github.com/cozy/cozy-stack/client/request"
)

// ErrWrongPassphrase is used when the passphrase is wrong
var ErrWrongPassphrase = errors.New("Unauthorized: wrong passphrase")

// jsonAPIErrors is a group of errors. It is the error type returned by the
// API.
type jsonAPIErrors struct {
	Errors []*request.Error `json:"errors"`
}

// jsonAPIDocument is a simple JSONAPI document used to un-serialized
type jsonAPIDocument struct {
	Data     json.RawMessage `json:"data"`
	Included json.RawMessage `json:"included"`
}

// Client encapsulates the element representing a typical connection to the
// HTTP api of the cozy-stack.
//
// It holds the elements to authenticate a user, as well as the transport layer
// used for all the calls to the stack.
type Client struct {
	Domain string
	Client *http.Client

	AuthClient  *auth.Client
	AuthScopes  []string
	AuthAccept  auth.UserAcceptFunc
	AuthStorage auth.Storage
	Authorizer  request.Authorizer

	UserAgent     string
	Retries       int
	Timeout       time.Duration
	Transport     http.RoundTripper
	DisableSecure bool

	authed bool
	inited int32
	authMu sync.Mutex
	auth   *auth.Request
}

func (c *Client) init() {
	if !atomic.CompareAndSwapInt32(&c.inited, 0, 1) {
		return
	}
	if c.Retries == 0 {
		c.Retries = 3
	}
	if c.Timeout == 0 {
		c.Timeout = 30 * time.Second
	}
	if c.Transport == nil {
		c.Transport = &http.Transport{
			Proxy: http.ProxyFromEnvironment,
		}
	}
	if c.AuthStorage == nil {
		c.AuthStorage = auth.NewFileStorage()
	}
	if c.Client == nil {
		c.Client = &http.Client{
			Transport: c.Transport,
			Timeout:   c.Timeout,
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				return http.ErrUseLastResponse
			},
		}
	}
}

// Authenticate is used to authenticate a client via OAuth.
func (c *Client) Authenticate() (request.Authorizer, error) {
	c.init()
	c.authMu.Lock()
	defer c.authMu.Unlock()
	if c.authed {
		return c.auth, nil
	}
	if c.auth == nil {
		c.auth = &auth.Request{
			ClientParams:  c.AuthClient,
			Scopes:        c.AuthScopes,
			Domain:        c.Domain,
			DisableSecure: c.DisableSecure,
			HTTPClient:    c.Client,
			UserAgent:     c.UserAgent,
			UserAccept:    c.AuthAccept,
			Storage:       c.AuthStorage,
		}
	}
	if err := c.auth.Authenticate(); err != nil {
		return nil, err
	}
	c.authed = true
	return c.auth, nil
}

// Req is used to perform a request to the stack given the ReqOptions passed.
func (c *Client) Req(opts *request.Options) (*http.Response, error) {
	c.init()
	var err error
	if c.Authorizer != nil {
		opts.Authorizer = c.Authorizer
	} else {
		opts.Authorizer, err = c.Authenticate()
	}
	if err != nil {
		return nil, err
	}
	opts.Domain = c.Domain
	opts.Client = c.Client
	opts.UserAgent = c.UserAgent
	opts.DisableSecure = c.DisableSecure
	opts.ParseError = parseJSONAPIError
	return request.Req(opts)
}

func parseJSONAPIError(res *http.Response, b []byte) error {
	var errs jsonAPIErrors
	if err := json.Unmarshal(b, &errs); err != nil || errs.Errors == nil || len(errs.Errors) == 0 {
		return &request.Error{
			Status: http.StatusText(res.StatusCode),
			Title:  http.StatusText(res.StatusCode),
			Detail: string(b),
		}
	}
	// TODO: handle multi-error
	return errs.Errors[0]
}

func readJSONAPI(r io.Reader, data interface{}, included interface{}) (err error) {
	defer func() {
		if rc, ok := r.(io.ReadCloser); ok {
			if cerr := rc.Close(); err == nil && cerr != nil {
				err = cerr
			}
		}
	}()
	var doc jsonAPIDocument
	decoder := json.NewDecoder(r)
	if err = decoder.Decode(&doc); err != nil {
		return err
	}
	if data != nil {
		if err = json.Unmarshal(doc.Data, &data); err != nil {
			return err
		}
	}
	if included != nil && doc.Included != nil {
		if err = json.Unmarshal(doc.Included, &included); err != nil {
			return err
		}
	}
	return nil
}

func writeJSONAPI(data interface{}) (io.Reader, error) {
	buf, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}

	doc := jsonAPIDocument{Data: buf}
	buf, err = json.Marshal(doc)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(buf), nil
}
<|endoftext|>


<commit_before>package gitmediaclient

import (
	".."
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
)

func Put(filename string) error {
	oid := filepath.Base(filename)
	stat, err := os.Stat(filename)
	if err != nil {
		return err
	}

	file, err := os.Open(filename)
	if err != nil {
		return err
	}

	req, creds, err := clientRequest("PUT", oid)
	if err != nil {
		return err
	}

	req.Body = file
	req.ContentLength = stat.Size()

	res, err := doRequest(req, creds)
	if err != nil {
		return err
	}

	fmt.Printf("Sending %s from %s: %d\n", oid, filename, res.StatusCode)
	return nil
}

func Get(filename string) (io.ReadCloser, error) {
	oid := filepath.Base(filename)
	if stat, err := os.Stat(filename); err != nil || stat == nil {
		req, creds, err := clientRequest("GET", oid)
		if err != nil {
			return nil, err
		}

		req.Header.Set("Accept", "application/vnd.git-media")
		res, err := doRequest(req, creds)

		if err != nil {
			return nil, err
		}

		return res.Body, nil
	}

	return os.Open(filename)
}

func doRequest(req *http.Request, creds Creds) (*http.Response, error) {
	res, err := http.DefaultClient.Do(req)

	if err == nil {
		if res.StatusCode > 299 {
			execCreds(creds, "reject")

			apierr := &Error{}
			dec := json.NewDecoder(res.Body)
			if err := dec.Decode(apierr); err != nil {
				return res, err
			}

			return res, apierr
		}
	} else {
		execCreds(creds, "approve")
	}

	return res, err
}

func clientRequest(method, oid string) (*http.Request, Creds, error) {
	u := ObjectUrl(oid)
	req, err := http.NewRequest(method, u.String(), nil)
	if err == nil {
		creds, err := credentials(u)
		if err != nil {
			return req, nil, err
		}

		token := fmt.Sprintf("%s:%s", creds["username"], creds["password"])
		auth := "Basic " + base64.URLEncoding.EncodeToString([]byte(token))
		req.Header.Set("Authorization", auth)
		return req, creds, nil
	}

	return req, nil, err
}

func ObjectUrl(oid string) *url.URL {
	c := gitmedia.Config()
	u, _ := url.Parse(c.Endpoint)
	u.Path = filepath.Join(u.Path, "/objects/"+oid)
	return u
}

type Error struct {
	Message   string `json:"message"`
	RequestId string `json:"request_id,omitempty"`
}

func (e *Error) Error() string {
	return e.Message
}
<commit_msg>send accept/content-type headers on git-media PUT requests<commit_after>package gitmediaclient

import (
	".."
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
)

const (
	gitMediaType     = "application/vnd.git-media"
	gitMediaMetaType = gitMediaType + "+json; charset=utf-8"
)

func Put(filename string) error {
	oid := filepath.Base(filename)
	stat, err := os.Stat(filename)
	if err != nil {
		return err
	}

	file, err := os.Open(filename)
	if err != nil {
		return err
	}

	req, creds, err := clientRequest("PUT", oid)
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", gitMediaType)
	req.Header.Set("Accept", gitMediaMetaType)
	req.Body = file
	req.ContentLength = stat.Size()

	res, err := doRequest(req, creds)
	if err != nil {
		return err
	}

	fmt.Printf("Sending %s from %s: %d\n", oid, filename, res.StatusCode)
	return nil
}

func Get(filename string) (io.ReadCloser, error) {
	oid := filepath.Base(filename)
	if stat, err := os.Stat(filename); err != nil || stat == nil {
		req, creds, err := clientRequest("GET", oid)
		if err != nil {
			return nil, err
		}

		req.Header.Set("Accept", gitMediaType)
		res, err := doRequest(req, creds)

		if err != nil {
			return nil, err
		}

		return res.Body, nil
	}

	return os.Open(filename)
}

func doRequest(req *http.Request, creds Creds) (*http.Response, error) {
	res, err := http.DefaultClient.Do(req)

	if err == nil {
		if res.StatusCode > 299 {
			execCreds(creds, "reject")

			apierr := &Error{}
			dec := json.NewDecoder(res.Body)
			if err := dec.Decode(apierr); err != nil {
				return res, err
			}

			return res, apierr
		}
	} else {
		execCreds(creds, "approve")
	}

	return res, err
}

func clientRequest(method, oid string) (*http.Request, Creds, error) {
	u := ObjectUrl(oid)
	req, err := http.NewRequest(method, u.String(), nil)
	if err == nil {
		creds, err := credentials(u)
		if err != nil {
			return req, nil, err
		}

		token := fmt.Sprintf("%s:%s", creds["username"], creds["password"])
		auth := "Basic " + base64.URLEncoding.EncodeToString([]byte(token))
		req.Header.Set("Authorization", auth)
		return req, creds, nil
	}

	return req, nil, err
}

func ObjectUrl(oid string) *url.URL {
	c := gitmedia.Config()
	u, _ := url.Parse(c.Endpoint)
	u.Path = filepath.Join(u.Path, "/objects/"+oid)
	return u
}

type Error struct {
	Message   string `json:"message"`
	RequestId string `json:"request_id,omitempty"`
}

func (e *Error) Error() string {
	return e.Message
}
<|endoftext|>


<commit_before>package main

import ini "gopkg.in/ini.v1"
import "log"

const configFile = "client.ini"

type config struct {
	debug               bool
	debugScript         bool
	rpcListenAddr       string
	addr                string
	dataFile            string
	rpcSrvAddr          string
	logPath             string
	defaultRPCPath      string
	defaultRPCDebugPath string
	pprofAddr           string
	mailUser            string
	mailPass            string
	mailHost            string
	mailTo              string
}

func newConfig() *config {
	cf := loadConfig()

	rpc := cf.Section("rpc")
	logc := cf.Section("log")
	base := cf.Section("base")
	server := cf.Section("server")
	mail := cf.Section("mail")

	c := &config{
		debug:         base.Key("debug").MustBool(false),
		debugScript:   base.Key("debugScript").MustBool(false),
		rpcListenAddr: rpc.Key("listen").MustString(":20001"),
		pprofAddr:     server.Key("pprof_addr").MustString(":20002"),
		addr:          rpc.Key("local_addr").MustString("localhost:20002"),
		rpcSrvAddr:    rpc.Key("server_addr").MustString("localhost:20003"),
		dataFile:      base.Key("data_file").MustString("data.json"),
		logPath:       logc.Key("dir").MustString("./logs"),
		mailTo:        mail.Key("to").MustString(""),
	}

	log.Printf("config:%v", *c)
	return c
}

func loadConfig() *ini.File {
	f, err := ini.Load(configFile)
	if err != nil {
		panic(err)
	}
	return f
}
<commit_msg>update<commit_after>package main

import ini "gopkg.in/ini.v1"
import "log"

const configFile = "client.ini"

type config struct {
	debug               bool
	debugScript         bool
	rpcListenAddr       string
	addr                string
	dataFile            string
	rpcSrvAddr          string
	logPath             string
	defaultRPCPath      string
	defaultRPCDebugPath string
	pprofAddr           string
	mailUser            string
	mailPass            string
	mailHost            string
	mailTo              string
}

func newConfig() *config {
	cf := loadConfig()

	rpc := cf.Section("rpc")
	logc := cf.Section("log")
	base := cf.Section("base")
	server := cf.Section("server")
	mail := cf.Section("mail")

	c := &config{
		debug:         base.Key("debug").MustBool(false),
		debugScript:   base.Key("debugScript").MustBool(false),
		rpcListenAddr: rpc.Key("listen").MustString(":20001"),
		pprofAddr:     server.Key("pprof_addr").MustString(":20002"),
		addr:          rpc.Key("local_addr").MustString("localhost:20002"),
		rpcSrvAddr:    rpc.Key("server_addr").MustString("localhost:20003"),
		dataFile:      base.Key("data_file").MustString("data.json"),
		logPath:       logc.Key("dir").MustString("./logs"),
		mailTo:        mail.Key("to").MustString(""),
	}
	log.Printf("config:%v", *c)
	return c
}

func loadConfig() *ini.File {
	f, err := ini.Load(configFile)
	if err != nil {
		panic(err)
	}
	return f
}
<|endoftext|>


<commit_before>package test

import (
	"archive/tar"
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"io"
	"io/ioutil"
	"strconv"

	"github.com/coduno/api/model"
	"github.com/coduno/api/util"
	"golang.org/x/net/context"
)

func turnLeft(d model.RobotDirection) model.RobotDirection {
	d--
	if d < 0 {
		d = model.Left
	}
	return d
}

func turnRight(d model.RobotDirection) model.RobotDirection {
	d++
	if d > 3 {
		d = model.Up
	}
	return d
}

func init() {
	RegisterTester(Robot, robot)
}

func robot(ctx context.Context, t model.KeyedTest, sub model.KeyedSubmission, ball io.Reader) (err error) {
	testrc, err := util.Load(util.CloudContext(ctx), util.TemplateBucket, t.Params["tests"])
	if err != nil {
		return
	}
	defer testrc.Close()

	in := tar.NewReader(ball)
	if _, err = in.Next(); err != nil {
		return err
	}

	var tmap []byte
	if tmap, err = ioutil.ReadAll(testrc); err != nil {
		return
	}

	var m model.Map
	if err = json.Unmarshal(tmap, &m); err != nil {
		return
	}

	tr, err := testRobot(&m, in)
	if err != nil {
		// TODO(victorbalan): Pass the error to the ws so the client knows what he's doing wrong
		return
	}

	if _, err = tr.PutWithParent(ctx, sub.Key); err != nil {
		return
	}
	return marshalJSON(&sub, tr.Moves)
}

func testRobot(m *model.Map, in io.Reader) (tr model.RobotTestResults, err error) {
	tr.Moves = append(tr.Moves, model.RobotLogEntry{Position: m.Start, Event: model.Move})
	direction := model.Right
	r := bufio.NewReader(in)
	var picks []int
	for {
		current, err := r.ReadBytes('\n')
		if len(current) == 0 {
			break
		} else if err != nil && err != io.EOF {
			return tr, err
		}

		current = bytes.ToLower(current)
		current = bytes.TrimSuffix(current, []byte("\n"))

		c := bytes.Split(current, []byte(" "))
		switch string(c[0]) {
		case "move":
			if len(c) < 2 {
				return tr, errors.New("move missing parameter")
			}

			n, err := strconv.Atoi(string(c[1]))
			if err != nil {
				return tr, err
			}

			if ok := move(&tr, n, direction, m); !ok {
				return tr, nil
			}
		case "right":
			direction = turnRight(direction)
		case "left":
			direction = turnLeft(direction)
		case "pick":
			tr.Moves = append(tr.Moves, tr.Moves[len(tr.Moves)-1])
			picks = append(picks, len(tr.Moves)-1)
		default:
			return tr, errors.New("bad command")
		}
	}
	if m.Finish == tr.Moves[len(tr.Moves)-1].Position {
		tr.ReachedFinish = true
	}

	picked := 0
	for _, pick := range picks {
		if m.CoinAt(tr.Moves[pick].Position) {
			picked++
			tr.Moves[pick].Event = model.Picked
		} else {
			tr.Failed = true
			tr.Moves[pick].Event = model.WrongPick
		}
	}
	if picked != len(m.Coins) {
		tr.Failed = true
	}
	return tr, nil
}

func move(tr *model.RobotTestResults, n int, direction model.RobotDirection, m *model.Map) bool {
	for n > 0 {
		n--

		i := len(tr.Moves) - 1
		tr.Moves = append(tr.Moves, tr.Moves[i].Move(direction))
		i++

		if tr.Moves[i].Validate(m) {
			continue
		}

		tr.Failed = true
		tr.ReachedFinish = false
		return false
	}
	return true
}
<commit_msg>Pass the error for the robot tester<commit_after>package test

import (
	"archive/tar"
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"io"
	"io/ioutil"
	"strconv"

	"github.com/coduno/api/model"
	"github.com/coduno/api/util"
	"golang.org/x/net/context"
)

func turnLeft(d model.RobotDirection) model.RobotDirection {
	d--
	if d < 0 {
		d = model.Left
	}
	return d
}

func turnRight(d model.RobotDirection) model.RobotDirection {
	d++
	if d > 3 {
		d = model.Up
	}
	return d
}

func init() {
	RegisterTester(Robot, robot)
}

func robot(ctx context.Context, t model.KeyedTest, sub model.KeyedSubmission, ball io.Reader) (err error) {
	testrc, err := util.Load(util.CloudContext(ctx), util.TemplateBucket, t.Params["tests"])
	if err != nil {
		return
	}
	defer testrc.Close()

	in := tar.NewReader(ball)
	if _, err = in.Next(); err != nil {
		return err
	}

	var tmap []byte
	if tmap, err = ioutil.ReadAll(testrc); err != nil {
		return
	}

	var m model.Map
	if err = json.Unmarshal(tmap, &m); err != nil {
		return
	}

	tr, err := testRobot(&m, in)
	if err != nil {
		return marshalJSON(&sub, struct {
			Error string
		}{
			Error: err.Error(),
		})
	}

	if _, err = tr.PutWithParent(ctx, sub.Key); err != nil {
		return
	}
	return marshalJSON(&sub, tr.Moves)
}

func testRobot(m *model.Map, in io.Reader) (tr model.RobotTestResults, err error) {
	tr.Moves = append(tr.Moves, model.RobotLogEntry{Position: m.Start, Event: model.Move})
	direction := model.Right
	r := bufio.NewReader(in)
	var picks []int
	for {
		current, err := r.ReadBytes('\n')
		if len(current) == 0 {
			break
		} else if err != nil && err != io.EOF {
			return tr, err
		}

		current = bytes.ToLower(current)
		current = bytes.TrimSuffix(current, []byte("\n"))

		c := bytes.Split(current, []byte(" "))
		switch string(c[0]) {
		case "move":
			if len(c) < 2 {
				return tr, errors.New("move missing parameter")
			}

			n, err := strconv.Atoi(string(c[1]))
			if err != nil {
				return tr, err
			}

			if ok := move(&tr, n, direction, m); !ok {
				return tr, nil
			}
		case "right":
			direction = turnRight(direction)
		case "left":
			direction = turnLeft(direction)
		case "pick":
			tr.Moves = append(tr.Moves, tr.Moves[len(tr.Moves)-1])
			picks = append(picks, len(tr.Moves)-1)
		default:
			return tr, errors.New("bad command")
		}
	}
	if m.Finish == tr.Moves[len(tr.Moves)-1].Position {
		tr.ReachedFinish = true
	}

	picked := 0
	for _, pick := range picks {
		if m.CoinAt(tr.Moves[pick].Position) {
			picked++
			tr.Moves[pick].Event = model.Picked
		} else {
			tr.Failed = true
			tr.Moves[pick].Event = model.WrongPick
		}
	}
	if picked != len(m.Coins) {
		tr.Failed = true
	}
	return tr, nil
}

func move(tr *model.RobotTestResults, n int, direction model.RobotDirection, m *model.Map) bool {
	for n > 0 {
		n--

		i := len(tr.Moves) - 1
		tr.Moves = append(tr.Moves, tr.Moves[i].Move(direction))
		i++

		if tr.Moves[i].Validate(m) {
			continue
		}

		tr.Failed = true
		tr.ReachedFinish = false
		return false
	}
	return true
}
<|endoftext|>


<commit_before>package client

import (
	"fmt"
	"github.com/mitchellh/mapstructure"
	"github.com/nsf/termbox-go"
	"github.com/pkg/errors"
	"github.com/zrepl/zrepl/config"
	"github.com/zrepl/zrepl/daemon"
	"github.com/zrepl/zrepl/replication"
	"github.com/zrepl/zrepl/replication/fsrep"
	"sort"
	"sync"
	"time"
)

type tui struct {
	x, y   int
	indent int

	lock   sync.Mutex //For report and error
	report map[string]interface{}
	err    error
}

func newTui() tui {
	return tui{}
}

func (t *tui) moveCursor(x, y int) {
	t.x += x
	t.y += y
}

func (t *tui) moveLine(dl int, col int) {
	t.y += dl
	t.x = t.indent*4 + col
}

func (t *tui) write(text string) {
	for _, c := range text {
		termbox.SetCell(t.x, t.y, c, termbox.ColorDefault, termbox.ColorDefault)
		t.x += 1
	}
}

func (t *tui) printf(text string, a ...interface{}) {
	t.write(fmt.Sprintf(text, a...))
}

func (t *tui) newline() {
	t.moveLine(1, 0)
}

func (t *tui) setIndent(indent int) {
	t.indent = indent
	t.moveLine(0, 0)
}

func (t *tui) addIndent(indent int) {
	t.indent += indent
	t.moveLine(0, 0)
}

func RunStatus(config *config.Config, args []string) error {
	httpc, err := controlHttpClient(config.Global.Control.SockPath)
	if err != nil {
		return err
	}

	t := newTui()
	t.lock.Lock()
	t.err = errors.New("Got no report yet")
	t.lock.Unlock()

	err = termbox.Init()
	if err != nil {
		return err
	}
	defer termbox.Close()

	update := func() {
		m := make(map[string]interface{})

		err2 := jsonRequestResponse(httpc, daemon.ControlJobEndpointStatus,
			struct{}{},
			&m,
		)

		t.lock.Lock()
		t.err = err2
		t.report = m
		t.lock.Unlock()
		t.draw()
	}
	update()

	ticker := time.NewTicker(500 * time.Millisecond)
	defer ticker.Stop()
	go func() {
		for _ = range ticker.C {
			update()
		}
	}()

	termbox.HideCursor()
	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

loop:
	for {
		switch ev := termbox.PollEvent(); ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc:
				break loop
			case termbox.KeyCtrlC:
				break loop
			}
		case termbox.EventResize:
			t.draw()
		}
	}

	return nil

}

func (t *tui) draw() {
	t.lock.Lock()
	defer t.lock.Unlock()

	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
	t.x = 0
	t.y = 0
	t.indent = 0

	if t.err != nil {
		t.write(t.err.Error())
	} else {
		//Iterate over map in alphabetical order
		keys := make([]string, len(t.report))
		i := 0
		for k, _ := range t.report {
			keys[i] = k
			i++
		}
		sort.Strings(keys)

		for _, k := range keys {
			v := t.report[k]
			if len(k) == 0 || k[0] == '_' { //Internal job
				continue
			}
			t.setIndent(0)

			t.printf("Job: %s", k)
			t.setIndent(1)
			t.newline()

			if v == nil {
				t.printf("No report generated yet")
				t.newline()
				continue
			}
			rep := replication.Report{}
			err := mapstructure.Decode(v, &rep)
			if err != nil {
				t.printf("Failed to decode report: %s", err.Error())
				t.newline()
				continue
			}

			all := make([]*fsrep.Report, 0, len(rep.Completed)+len(rep.Pending) + 1)
			all = append(all, rep.Completed...)
			all = append(all, rep.Pending...)
			if rep.Active != nil {
				all = append(all, rep.Active)
			}
			sort.Slice(all, func(i, j int) bool {
				return all[i].Filesystem < all[j].Filesystem
			})

			t.printf("Status:  %s", rep.Status)
			t.newline()
			if rep.Problem != "" {
				t.printf("Problem: %s", rep.Problem)
				t.newline()
			}
			{ // Progress: [---------------]
				sumUpFSRep := func(rep *fsrep.Report) (transferred, total int64) {
					for _, s := range rep.Pending {
						transferred += s.Bytes
						total  += s.ExpectedBytes
					}
					for _, s := range rep.Completed {
						transferred += s.Bytes
						total += s.ExpectedBytes
					}
					return
				}
				var transferred, total int64
				for _, fs := range all {
					fstx, fstotal := sumUpFSRep(fs)
					transferred += fstx
					total += fstotal
				}
				t.write("Progress: ")
				t.drawBar(80, transferred, total)
				t.write(fmt.Sprintf(" %s / %s", ByteCountBinary(transferred), ByteCountBinary(total)))
				t.newline()
			}

			maxFS, maxStatus := calculateMaxFilesystemAndVersionNameLength(all)
			for _, fs := range all {
				printFilesystemStatus(fs, t, fs == rep.Active, maxFS, maxStatus)
			}
		}
	}
	termbox.Flush()
}

const snapshotIndent = 1
func calculateMaxFilesystemAndVersionNameLength(all []*fsrep.Report) (maxFS, maxStatus int) {
	for _, e := range all {
		if len(e.Filesystem) > maxFS {
			maxFS = len(e.Filesystem)
		}
		all2 := make([]*fsrep.StepReport, 0, len(e.Pending) + len(e.Completed))
		all2 = append(all2, e.Pending...)
		all2 = append(all2, e.Completed...)
		for _, e2 := range all2 {
			elen := len(e2.Problem) + len(e2.From) + len(e2.To) + 60 // random spacing, units, labels, etc
			if elen > maxStatus {
				maxStatus = elen
			}
		}
	}
	return
}

func times(str string, n int) (out string) {
	for i := 0; i < n; i++ {
		out += str
	}
	return
}

func rightPad(str string, length int, pad string) string {
	if len(str) > length {
		return str[:length]
	}
	return str + times(pad, length-len(str))
}

func leftPad(str string, length int, pad string) string {
	if len(str) > length {
		return str[len(str)-length:]
	}
	return times(pad, length-len(str)) + str
}

func (t *tui) drawBar(length int, bytes, totalBytes int64) {
	var completedLength int
	if totalBytes > 0 {
		completedLength = int(int64(length) * bytes / totalBytes)
		if completedLength > length {
			completedLength = length
		}
	}

	t.write("[")
	t.write(times("=", completedLength))
	t.write(">")
	t.write(times("-", length-completedLength))
	t.write("]")
}

func StringStepState(s fsrep.StepState) string {
	switch s {
	case fsrep.StepReplicationReady: return "Ready"
	case fsrep.StepReplicationRetry: return "Retry"
	case fsrep.StepMarkReplicatedReady: return "MarkReady"
	case fsrep.StepMarkReplicatedRetry: return "MarkRetry"
	case fsrep.StepPermanentError: return "PermanentError"
	case fsrep.StepCompleted: return "Completed"
	default:
		return fmt.Sprintf("UNKNOWN %d", s)
	}
}

func filesystemStatusString(rep *fsrep.Report, active bool, maxFS, maxStatus int) (totalStatus string, bytes, totalBytes int64) {
	bytes = int64(0)
	totalBytes = int64(0)
	for _, s := range rep.Pending {
		bytes += s.Bytes
		totalBytes += s.ExpectedBytes
	}
	for _, s := range rep.Completed {
		bytes += s.Bytes
		totalBytes += s.ExpectedBytes
	}

	nextStep := ""
	if len(rep.Pending) > 0 {
		if rep.Pending[0].From != "" {
			nextStep = fmt.Sprintf("%s => %s", rep.Pending[0].From, rep.Pending[0].To)
		} else {
			nextStep = fmt.Sprintf("%s (full)", rep.Pending[0].To)
		}
	}
	status := fmt.Sprintf("%s (step %d/%d, %s/%s)",
		rep.Status,
		len(rep.Completed), len(rep.Pending) + len(rep.Completed),
		ByteCountBinary(bytes), ByteCountBinary(totalBytes),
	)
	if rep.Problem == "" && nextStep != "" {
		status += fmt.Sprintf(" next: %s", nextStep)
	} else if rep.Problem != "" {
		status += fmt.Sprintf(" problem: %s", rep.Problem)
	}
	activeIndicator := " "
	if active {
		activeIndicator = "*"
	}
	totalStatus = fmt.Sprintf("%s %s %s",
		activeIndicator,
		rightPad(rep.Filesystem, maxFS, " "),
		rightPad(status, maxStatus, " "))
	return totalStatus, bytes, totalBytes
}

func printFilesystemStatus(rep *fsrep.Report, t *tui, active bool, maxFS, maxTotal int) {
	totalStatus, _, _ := filesystemStatusString(rep, active, maxFS, maxTotal)
	t.write(totalStatus)
	t.newline()
}

func ByteCountBinary(b int64) string {
	const unit = 1024
	if b < unit {
		return fmt.Sprintf("%d B", b)
	}
	div, exp := int64(unit), 0
	for n := b / unit; n >= unit; n /= unit {
		div *= unit
		exp++
	}
	return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
<commit_msg>status command: better handling of 'nothing to do' Complete state<commit_after>package client

import (
	"fmt"
	"github.com/mitchellh/mapstructure"
	"github.com/nsf/termbox-go"
	"github.com/pkg/errors"
	"github.com/zrepl/zrepl/config"
	"github.com/zrepl/zrepl/daemon"
	"github.com/zrepl/zrepl/replication"
	"github.com/zrepl/zrepl/replication/fsrep"
	"sort"
	"sync"
	"time"
)

type tui struct {
	x, y   int
	indent int

	lock   sync.Mutex //For report and error
	report map[string]interface{}
	err    error
}

func newTui() tui {
	return tui{}
}

func (t *tui) moveCursor(x, y int) {
	t.x += x
	t.y += y
}

func (t *tui) moveLine(dl int, col int) {
	t.y += dl
	t.x = t.indent*4 + col
}

func (t *tui) write(text string) {
	for _, c := range text {
		termbox.SetCell(t.x, t.y, c, termbox.ColorDefault, termbox.ColorDefault)
		t.x += 1
	}
}

func (t *tui) printf(text string, a ...interface{}) {
	t.write(fmt.Sprintf(text, a...))
}

func (t *tui) newline() {
	t.moveLine(1, 0)
}

func (t *tui) setIndent(indent int) {
	t.indent = indent
	t.moveLine(0, 0)
}

func (t *tui) addIndent(indent int) {
	t.indent += indent
	t.moveLine(0, 0)
}

func RunStatus(config *config.Config, args []string) error {
	httpc, err := controlHttpClient(config.Global.Control.SockPath)
	if err != nil {
		return err
	}

	t := newTui()
	t.lock.Lock()
	t.err = errors.New("Got no report yet")
	t.lock.Unlock()

	err = termbox.Init()
	if err != nil {
		return err
	}
	defer termbox.Close()

	update := func() {
		m := make(map[string]interface{})

		err2 := jsonRequestResponse(httpc, daemon.ControlJobEndpointStatus,
			struct{}{},
			&m,
		)

		t.lock.Lock()
		t.err = err2
		t.report = m
		t.lock.Unlock()
		t.draw()
	}
	update()

	ticker := time.NewTicker(500 * time.Millisecond)
	defer ticker.Stop()
	go func() {
		for _ = range ticker.C {
			update()
		}
	}()

	termbox.HideCursor()
	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

loop:
	for {
		switch ev := termbox.PollEvent(); ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc:
				break loop
			case termbox.KeyCtrlC:
				break loop
			}
		case termbox.EventResize:
			t.draw()
		}
	}

	return nil

}

func (t *tui) draw() {
	t.lock.Lock()
	defer t.lock.Unlock()

	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
	t.x = 0
	t.y = 0
	t.indent = 0

	if t.err != nil {
		t.write(t.err.Error())
	} else {
		//Iterate over map in alphabetical order
		keys := make([]string, len(t.report))
		i := 0
		for k, _ := range t.report {
			keys[i] = k
			i++
		}
		sort.Strings(keys)

		for _, k := range keys {
			v := t.report[k]
			if len(k) == 0 || k[0] == '_' { //Internal job
				continue
			}
			t.setIndent(0)

			t.printf("Job: %s", k)
			t.setIndent(1)
			t.newline()

			if v == nil {
				t.printf("No report generated yet")
				t.newline()
				continue
			}
			rep := replication.Report{}
			err := mapstructure.Decode(v, &rep)
			if err != nil {
				t.printf("Failed to decode report: %s", err.Error())
				t.newline()
				continue
			}

			all := make([]*fsrep.Report, 0, len(rep.Completed)+len(rep.Pending) + 1)
			all = append(all, rep.Completed...)
			all = append(all, rep.Pending...)
			if rep.Active != nil {
				all = append(all, rep.Active)
			}
			sort.Slice(all, func(i, j int) bool {
				return all[i].Filesystem < all[j].Filesystem
			})

			t.printf("Status:  %s", rep.Status)
			t.newline()
			if rep.Problem != "" {
				t.printf("Problem: %s", rep.Problem)
				t.newline()
			}
			{ // Progress: [---------------]
				sumUpFSRep := func(rep *fsrep.Report) (transferred, total int64) {
					for _, s := range rep.Pending {
						transferred += s.Bytes
						total  += s.ExpectedBytes
					}
					for _, s := range rep.Completed {
						transferred += s.Bytes
						total += s.ExpectedBytes
					}
					return
				}
				var transferred, total int64
				for _, fs := range all {
					fstx, fstotal := sumUpFSRep(fs)
					transferred += fstx
					total += fstotal
				}
				t.write("Progress: ")
				t.drawBar(80, transferred, total)
				t.write(fmt.Sprintf(" %s / %s", ByteCountBinary(transferred), ByteCountBinary(total)))
				t.newline()
			}

			var maxFSLen int
			for _, fs := range all {
				if len(fs.Filesystem) > maxFSLen {
					maxFSLen = len(fs.Filesystem)
				}
			}
			for _, fs := range all {
				printFilesystemStatus(fs, t, fs == rep.Active, maxFSLen)
			}
		}
	}
	termbox.Flush()
}

const snapshotIndent = 1
func calculateMaxFSLength(all []*fsrep.Report) (maxFS, maxStatus int) {
	for _, e := range all {
		if len(e.Filesystem) > maxFS {
			maxFS = len(e.Filesystem)
		}
		all2 := make([]*fsrep.StepReport, 0, len(e.Pending) + len(e.Completed))
		all2 = append(all2, e.Pending...)
		all2 = append(all2, e.Completed...)
		for _, e2 := range all2 {
			elen := len(e2.Problem) + len(e2.From) + len(e2.To) + 60 // random spacing, units, labels, etc
			if elen > maxStatus {
				maxStatus = elen
			}
		}
	}
	return
}

func times(str string, n int) (out string) {
	for i := 0; i < n; i++ {
		out += str
	}
	return
}

func rightPad(str string, length int, pad string) string {
	if len(str) > length {
		return str[:length]
	}
	return str + times(pad, length-len(str))
}

func leftPad(str string, length int, pad string) string {
	if len(str) > length {
		return str[len(str)-length:]
	}
	return times(pad, length-len(str)) + str
}

func (t *tui) drawBar(length int, bytes, totalBytes int64) {
	var completedLength int
	if totalBytes > 0 {
		completedLength = int(int64(length) * bytes / totalBytes)
		if completedLength > length {
			completedLength = length
		}
	} else if totalBytes == bytes {
		completedLength = length
	}

	t.write("[")
	t.write(times("=", completedLength))
	t.write(">")
	t.write(times("-", length-completedLength))
	t.write("]")
}

func StringStepState(s fsrep.StepState) string {
	switch s {
	case fsrep.StepReplicationReady: return "Ready"
	case fsrep.StepReplicationRetry: return "Retry"
	case fsrep.StepMarkReplicatedReady: return "MarkReady"
	case fsrep.StepMarkReplicatedRetry: return "MarkRetry"
	case fsrep.StepPermanentError: return "PermanentError"
	case fsrep.StepCompleted: return "Completed"
	default:
		return fmt.Sprintf("UNKNOWN %d", s)
	}
}

func filesystemStatusString(rep *fsrep.Report, active bool, fsWidth int) (line string, bytes, totalBytes int64) {
	bytes = int64(0)
	totalBytes = int64(0)
	for _, s := range rep.Pending {
		bytes += s.Bytes
		totalBytes += s.ExpectedBytes
	}
	for _, s := range rep.Completed {
		bytes += s.Bytes
		totalBytes += s.ExpectedBytes
	}

	next := ""
	if rep.Problem != "" {
		next = " problem: " + rep.Problem
	} else if len(rep.Pending) > 0 {
		if rep.Pending[0].From != "" {
			next = fmt.Sprintf("next: %s => %s", rep.Pending[0].From, rep.Pending[0].To)
		} else {
			next = fmt.Sprintf("next: %s (full)", rep.Pending[0].To)
		}
	}
	status := fmt.Sprintf("%s (step %d/%d, %s/%s)%s",
		rep.Status,
		len(rep.Completed), len(rep.Pending) + len(rep.Completed),
		ByteCountBinary(bytes), ByteCountBinary(totalBytes),
		next,
	)
	activeIndicator := " "
	if active {
		activeIndicator = "*"
	}
	line = fmt.Sprintf("%s %s %s",
		activeIndicator,
		rightPad(rep.Filesystem, fsWidth, " "),
		status)
	return line, bytes, totalBytes
}

func printFilesystemStatus(rep *fsrep.Report, t *tui, active bool, maxFS int) {
	totalStatus, _, _ := filesystemStatusString(rep, active, maxFS)
	t.write(totalStatus)
	t.newline()
}

func ByteCountBinary(b int64) string {
	const unit = 1024
	if b < unit {
		return fmt.Sprintf("%d B", b)
	}
	div, exp := int64(unit), 0
	for n := b / unit; n >= unit; n /= unit {
		div *= unit
		exp++
	}
	return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
<|endoftext|>


<commit_before>/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api

import (
	"github.com/fsouza/go-dockerclient"
)

// Common string formats
// ---------------------
// Many fields in this API have formatting requirements.  The commonly used
// formats are defined here.
//
// C_IDENTIFIER:  This is a string that conforms the definition of an "identifier"
//     in the C language.  This is captured by the following regex:
//         [A-Za-z_][A-Za-z0-9_]*
//     This defines the format, but not the length restriction, which should be
//     specified at the definition of any field of this type.
//
// DNS_LABEL:  This is a string, no more than 63 characters long, that conforms
//     to the definition of a "label" in RFCs 1035 and 1123.  This is captured
//     by the following regex:
//         [a-z0-9]([-a-z0-9]*[a-z0-9])?
//
//  DNS_SUBDOMAIN:  This is a string, no more than 253 characters long, that conforms
//      to the definition of a "subdomain" in RFCs 1035 and 1123.  This is captured
//      by the following regex:
//         [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*
//     or more simply:
//         DNS_LABEL(\.DNS_LABEL)*

// ContainerManifest corresponds to the Container Manifest format, documented at:
// https://developers.google.com/compute/docs/containers/container_vms#container_manifest
// This is used as the representation of Kubernetes workloads.
type ContainerManifest struct {
	// Required: This must be a supported version string, such as "v1beta1".
	Version string `yaml:"version" json:"version"`
	// Required: This must be a DNS_SUBDOMAIN.
	ID         string      `yaml:"id" json:"id"`
	Volumes    []Volume    `yaml:"volumes" json:"volumes"`
	Containers []Container `yaml:"containers" json:"containers"`
}

// Volume represents a named volume in a pod that may be accessed by any containers in the pod.
type Volume struct {
	// Required: This must be a DNS_LABEL.  Each volume in a pod must have
	// a unique name.
	Name string `yaml:"name" json:"name"`
}

// Port represents a network port in a single container
type Port struct {
	// Optional: If specified, this must be a DNS_LABEL.  Each named port
	// in a pod must have a unique name.
	Name string `yaml:"name,omitempty" json:"name,omitempty"`
	// Optional: Defaults to ContainerPort.  If specified, this must be a
	// valid port number, 0 < x < 65536.
	HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
	// Required: This must be a valid port number, 0 < x < 65536.
	ContainerPort int `yaml:"containerPort" json:"containerPort"`
	// Optional: Defaults to "TCP".
	Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"`
	// Optional: What host IP to bind the external port to.
	HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
}

// VolumeMount describes a mounting of a Volume within a container
type VolumeMount struct {
	// Required: This must match the Name of a Volume [above].
	Name string `yaml:"name" json:"name"`
	// Optional: Defaults to false (read-write).
	ReadOnly bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
	// Required.
	// Exactly one of the following must be set.  If both are set, prefer MountPath.
	// DEPRECATED: Path will be removed in a future version of the API.
	MountPath string `yaml:"mountPath,omitempty" json:"mountPath,omitempty"`
	Path      string `yaml:"path,omitempty" json:"path,omitempty"`
	// One of: "LOCAL" (local volume) or "HOST" (external mount from the host). Default: LOCAL.
	MountType string `yaml:"mountType,omitempty" json:"mountType,omitempty"`
}

// EnvVar represents an environment variable present in a Container
type EnvVar struct {
	// Required: This must be a C_IDENTIFIER.
	// Exactly one of the following must be set.  If both are set, prefer Name.
	// DEPRECATED: EnvVar.Key will be removed in a future version of the API.
	Name string `yaml:"name" json:"name"`
	Key  string `yaml:"key,omitempty" json:"key,omitempty"`
	// Optional: defaults to "".
	Value string `yaml:"value,omitempty" json:"value,omitempty"`
}

// HTTPGetProbe describes a liveness probe based on HTTP Get requests.
type HTTPGetProbe struct {
	// Path to access on the http server
	Path string `yaml:"path,omitempty" json:"path,omitempty"`
	// Name or number of the port to access on the container
	Port string `yaml:"port,omitempty" json:"port,omitempty"`
	// Host name to connect to.  Optional, default: "localhost"
	Host string `yaml:"host,omitempty" json:"host,omitempty"`
}

// LivenessProbe describes a liveness probe to be examined to the container.
type LivenessProbe struct {
	Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
	// Type of liveness probe.  Current legal values "http"
	Type string `yaml:"type,omitempty" json:"type,omitempty"`
	// HTTPGetProbe parameters, required if Type == 'http'
	HTTPGet HTTPGetProbe `yaml:"httpGet,omitempty" json:"httpGet,omitempty"`
	// Length of time before health checking is activated.  In seconds.
	InitialDelaySeconds int64 `yaml:"initialDelaySeconds,omitempty" json:"initialDelaySeconds,omitempty"`
}

// Container represents a single container that is expected to be run on the host.
type Container struct {
	// Required: This must be a DNS_LABEL.  Each container in a pod must
	// have a unique name.
	Name string `yaml:"name" json:"name"`
	// Required.
	Image string `yaml:"image" json:"image"`
	// Optional: Defaults to whatever is defined in the image.
	Command []string `yaml:"command,omitempty" json:"command,omitempty"`
	// Optional: Defaults to Docker's default.
	WorkingDir string   `yaml:"workingDir,omitempty" json:"workingDir,omitempty"`
	Ports      []Port   `yaml:"ports,omitempty" json:"ports,omitempty"`
	Env        []EnvVar `yaml:"env,omitempty" json:"env,omitempty"`
	// Optional: Defaults to unlimited.
	Memory int `yaml:"memory,omitempty" json:"memory,omitempty"`
	// Optional: Defaults to unlimited.
	CPU           int           `yaml:"cpu,omitempty" json:"cpu,omitempty"`
	VolumeMounts  []VolumeMount `yaml:"volumeMounts,omitempty" json:"volumeMounts,omitempty"`
	LivenessProbe LivenessProbe `yaml:"livenessProbe,omitempty" json:"livenessProbe,omitempty"`
}

// Percentile represents a pair which contains a percentage from 0 to 100 and
// its corresponding value.
type Percentile struct {
	Percentage int    `json:"percentage,omitempty"`
	Value      uint64 `json:"value,omitempty"`
}

// ContainerStats represents statistical information of a container
type ContainerStats struct {
	CpuUsagePercentiles    []Percentile `json:"cpu_usage_percentiles,omitempty"`
	MemoryUsagePercentiles []Percentile `json:"memory_usage_percentiles,omitempty"`
	MaxMemoryUsage         uint64       `json:"max_memory_usage,omitempty"`
}

// Event is the representation of an event logged to etcd backends
type Event struct {
	Event     string             `json:"event,omitempty"`
	Manifest  *ContainerManifest `json:"manifest,omitempty"`
	Container *Container         `json:"container,omitempty"`
	Timestamp int64              `json:"timestamp"`
}

// The below types are used by kube_client and api_server.

// JSONBase is shared by all objects sent to, or returned from the client
type JSONBase struct {
	Kind              string `json:"kind,omitempty" yaml:"kind,omitempty"`
	ID                string `json:"id,omitempty" yaml:"id,omitempty"`
	CreationTimestamp string `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
	SelfLink          string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"`
	ResourceVersion   uint64 `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
}

// PodStatus represents a status of a pod.
type PodStatus string

// These are the valid statuses of pods.
const (
	PodRunning PodStatus = "Running"
	PodPending PodStatus = "Pending"
	PodStopped PodStatus = "Stopped"
)

// PodInfo contains one entry for every container with available info.
type PodInfo map[string]docker.Container

// PodState is the state of a pod, used as either input (desired state) or output (current state)
type PodState struct {
	Manifest ContainerManifest `json:"manifest,omitempty" yaml:"manifest,omitempty"`
	Status   PodStatus         `json:"status,omitempty" yaml:"status,omitempty"`
	Host     string            `json:"host,omitempty" yaml:"host,omitempty"`
	HostIP   string            `json:"hostIP,omitempty" yaml:"hostIP,omitempty"`
	PodIP    string            `json:"podIP,omitempty" yaml:"podIP,omitempty"`

	// The key of this map is the *name* of the container within the manifest; it has one
	// entry per container in the manifest. The value of this map is currently the output
	// of `docker inspect`. This output format is *not* final and should not be relied
	// upon.
	// TODO: Make real decisions about what our info should look like.
	Info PodInfo `json:"info,omitempty" yaml:"info,omitempty"`
}

// PodList is a list of Pods.
type PodList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []Pod `json:"items" yaml:"items,omitempty"`
}

// Pod is a collection of containers, used as either input (create, update) or as output (list, get)
type Pod struct {
	JSONBase     `json:",inline" yaml:",inline"`
	Labels       map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
	DesiredState PodState          `json:"desiredState,omitempty" yaml:"desiredState,omitempty"`
	CurrentState PodState          `json:"currentState,omitempty" yaml:"currentState,omitempty"`
}

// ReplicationControllerState is the state of a replication controller, either input (create, update) or as output (list, get)
type ReplicationControllerState struct {
	Replicas        int               `json:"replicas" yaml:"replicas"`
	ReplicaSelector map[string]string `json:"replicaSelector,omitempty" yaml:"replicaSelector,omitempty"`
	PodTemplate     PodTemplate       `json:"podTemplate,omitempty" yaml:"podTemplate,omitempty"`
}

// ReplicationControllerList is a collection of replication controllers.
type ReplicationControllerList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []ReplicationController `json:"items,omitempty" yaml:"items,omitempty"`
}

// ReplicationController represents the configuration of a replication controller
type ReplicationController struct {
	JSONBase     `json:",inline" yaml:",inline"`
	DesiredState ReplicationControllerState `json:"desiredState,omitempty" yaml:"desiredState,omitempty"`
	Labels       map[string]string          `json:"labels,omitempty" yaml:"labels,omitempty"`
}

// PodTemplate holds the information used for creating pods
type PodTemplate struct {
	DesiredState PodState          `json:"desiredState,omitempty" yaml:"desiredState,omitempty"`
	Labels       map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
}

// ServiceList holds a list of services
type ServiceList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []Service `json:"items" yaml:"items"`
}

// Service is a named abstraction of software service (for example, mysql) consisting of local port
// (for example 3306) that the proxy listens on, and the selector that determines which pods
// will answer requests sent through the proxy.
type Service struct {
	JSONBase `json:",inline" yaml:",inline"`
	Port     int `json:"port,omitempty" yaml:"port,omitempty"`

	// This service's labels.
	Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

	// This service will route traffic to pods having labels matching this selector.
	Selector                   map[string]string `json:"selector,omitempty" yaml:"selector,omitempty"`
	CreateExternalLoadBalancer bool              `json:"createExternalLoadBalancer,omitempty" yaml:"createExternalLoadBalancer,omitempty"`
}

// Endpoints is a collection of endpoints that implement the actual service, for example:
// Name: "mysql", Endpoints: ["10.10.1.1:1909", "10.10.2.2:8834"]
type Endpoints struct {
	Name      string
	Endpoints []string
}

// Minion is a worker node in Kubernetenes.
// The name of the minion according to etcd is in JSONBase.ID.
type Minion struct {
	JSONBase `json:",inline" yaml:",inline"`
	// Queried from cloud provider, if available.
	HostIP string `json:"hostIP,omitempty" yaml:"hostIP,omitempty"`
}

// MinionList is a list of minions.
type MinionList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []Minion `json:"minions,omitempty" yaml:"minions,omitempty"`
}

// Status is a return value for calls that don't return other objects.
// Arguably, this could go in apiserver, but I'm including it here so clients needn't
// import both.
type Status struct {
	JSONBase `json:",inline" yaml:",inline"`
	// One of: "success", "failure", "working" (for operations not yet completed)
	// TODO: if "working", include an operation identifier so final status can be
	// checked.
	Status string `json:"status,omitempty" yaml:"status,omitempty"`
	// Details about the status. May be an error description or an
	// operation number for later polling.
	Details string `json:"details,omitempty" yaml:"details,omitempty"`
	// Suggested HTTP return code for this status, 0 if not set.
	Code int `json:"code,omitempty" yaml:"code,omitempty"`
}

// Values of Status.Status
const (
	StatusSuccess = "success"
	StatusFailure = "failure"
	StatusWorking = "working"
)

// ServerOp is an operation delivered to API clients.
type ServerOp struct {
	JSONBase `yaml:",inline" json:",inline"`
}

// ServerOpList is a list of operations, as delivered to API clients.
type ServerOpList struct {
	JSONBase `yaml:",inline" json:",inline"`
	Items    []ServerOp `yaml:"items,omitempty" json:"items,omitempty"`
}
<commit_msg>Address some comments from thockin@<commit_after>/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api

import (
	"github.com/fsouza/go-dockerclient"
)

// Common string formats
// ---------------------
// Many fields in this API have formatting requirements.  The commonly used
// formats are defined here.
//
// C_IDENTIFIER:  This is a string that conforms the definition of an "identifier"
//     in the C language.  This is captured by the following regex:
//         [A-Za-z_][A-Za-z0-9_]*
//     This defines the format, but not the length restriction, which should be
//     specified at the definition of any field of this type.
//
// DNS_LABEL:  This is a string, no more than 63 characters long, that conforms
//     to the definition of a "label" in RFCs 1035 and 1123.  This is captured
//     by the following regex:
//         [a-z0-9]([-a-z0-9]*[a-z0-9])?
//
//  DNS_SUBDOMAIN:  This is a string, no more than 253 characters long, that conforms
//      to the definition of a "subdomain" in RFCs 1035 and 1123.  This is captured
//      by the following regex:
//         [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*
//     or more simply:
//         DNS_LABEL(\.DNS_LABEL)*

// ContainerManifest corresponds to the Container Manifest format, documented at:
// https://developers.google.com/compute/docs/containers/container_vms#container_manifest
// This is used as the representation of Kubernetes workloads.
type ContainerManifest struct {
	// Required: This must be a supported version string, such as "v1beta1".
	Version string `yaml:"version" json:"version"`
	// Required: This must be a DNS_SUBDOMAIN.
	ID         string      `yaml:"id" json:"id"`
	Volumes    []Volume    `yaml:"volumes" json:"volumes"`
	Containers []Container `yaml:"containers" json:"containers"`
}

// Volume represents a named volume in a pod that may be accessed by any containers in the pod.
type Volume struct {
	// Required: This must be a DNS_LABEL.  Each volume in a pod must have
	// a unique name.
	Name string `yaml:"name" json:"name"`
}

// Port represents a network port in a single container
type Port struct {
	// Optional: If specified, this must be a DNS_LABEL.  Each named port
	// in a pod must have a unique name.
	Name string `yaml:"name,omitempty" json:"name,omitempty"`
	// Optional: Defaults to ContainerPort.  If specified, this must be a
	// valid port number, 0 < x < 65536.
	HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
	// Required: This must be a valid port number, 0 < x < 65536.
	ContainerPort int `yaml:"containerPort" json:"containerPort"`
	// Optional: Defaults to "TCP".
	Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"`
	// Optional: What host IP to bind the external port to.
	HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
}

// VolumeMount describes a mounting of a Volume within a container
type VolumeMount struct {
	// Required: This must match the Name of a Volume [above].
	Name string `yaml:"name" json:"name"`
	// Optional: Defaults to false (read-write).
	ReadOnly bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
	// Required.
	// Exactly one of the following must be set.  If both are set, prefer MountPath.
	// DEPRECATED: Path will be removed in a future version of the API.
	MountPath string `yaml:"mountPath,omitempty" json:"mountPath,omitempty"`
	Path      string `yaml:"path,omitempty" json:"path,omitempty"`
	// One of: "LOCAL" (local volume) or "HOST" (external mount from the host). Default: LOCAL.
	MountType string `yaml:"mountType,omitempty" json:"mountType,omitempty"`
}

// EnvVar represents an environment variable present in a Container
type EnvVar struct {
	// Required: This must be a C_IDENTIFIER.
	// Exactly one of the following must be set.  If both are set, prefer Name.
	// DEPRECATED: EnvVar.Key will be removed in a future version of the API.
	Name string `yaml:"name" json:"name"`
	Key  string `yaml:"key,omitempty" json:"key,omitempty"`
	// Optional: defaults to "".
	Value string `yaml:"value,omitempty" json:"value,omitempty"`
}

// HTTPGetProbe describes a liveness probe based on HTTP Get requests.
type HTTPGetProbe struct {
	// Path to access on the http server
	Path string `yaml:"path,omitempty" json:"path,omitempty"`
	// Name or number of the port to access on the container
	Port string `yaml:"port,omitempty" json:"port,omitempty"`
	// Host name to connect to.  Optional, default: "localhost"
	Host string `yaml:"host,omitempty" json:"host,omitempty"`
}

// LivenessProbe describes a liveness probe to be examined to the container.
type LivenessProbe struct {
	// Type of liveness probe.  Current legal values "http"
	Type string `yaml:"type,omitempty" json:"type,omitempty"`
	// HTTPGetProbe parameters, required if Type == 'http'
	HTTPGet *HTTPGetProbe `yaml:"httpGet,omitempty" json:"httpGet,omitempty"`
	// Length of time before health checking is activated.  In seconds.
	InitialDelaySeconds int64 `yaml:"initialDelaySeconds,omitempty" json:"initialDelaySeconds,omitempty"`
}

// Container represents a single container that is expected to be run on the host.
type Container struct {
	// Required: This must be a DNS_LABEL.  Each container in a pod must
	// have a unique name.
	Name string `yaml:"name" json:"name"`
	// Required.
	Image string `yaml:"image" json:"image"`
	// Optional: Defaults to whatever is defined in the image.
	Command []string `yaml:"command,omitempty" json:"command,omitempty"`
	// Optional: Defaults to Docker's default.
	WorkingDir string   `yaml:"workingDir,omitempty" json:"workingDir,omitempty"`
	Ports      []Port   `yaml:"ports,omitempty" json:"ports,omitempty"`
	Env        []EnvVar `yaml:"env,omitempty" json:"env,omitempty"`
	// Optional: Defaults to unlimited.
	Memory int `yaml:"memory,omitempty" json:"memory,omitempty"`
	// Optional: Defaults to unlimited.
	CPU           int            `yaml:"cpu,omitempty" json:"cpu,omitempty"`
	VolumeMounts  []VolumeMount  `yaml:"volumeMounts,omitempty" json:"volumeMounts,omitempty"`
	LivenessProbe *LivenessProbe `yaml:"livenessProbe,omitempty" json:"livenessProbe,omitempty"`
}

// Percentile represents a pair which contains a percentage from 0 to 100 and
// its corresponding value.
type Percentile struct {
	Percentage int    `json:"percentage,omitempty"`
	Value      uint64 `json:"value,omitempty"`
}

// ContainerStats represents statistical information of a container
type ContainerStats struct {
	CpuUsagePercentiles    []Percentile `json:"cpu_usage_percentiles,omitempty"`
	MemoryUsagePercentiles []Percentile `json:"memory_usage_percentiles,omitempty"`
	MaxMemoryUsage         uint64       `json:"max_memory_usage,omitempty"`
}

// Event is the representation of an event logged to etcd backends
type Event struct {
	Event     string             `json:"event,omitempty"`
	Manifest  *ContainerManifest `json:"manifest,omitempty"`
	Container *Container         `json:"container,omitempty"`
	Timestamp int64              `json:"timestamp"`
}

// The below types are used by kube_client and api_server.

// JSONBase is shared by all objects sent to, or returned from the client
type JSONBase struct {
	Kind              string `json:"kind,omitempty" yaml:"kind,omitempty"`
	ID                string `json:"id,omitempty" yaml:"id,omitempty"`
	CreationTimestamp string `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
	SelfLink          string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"`
	ResourceVersion   uint64 `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
}

// PodStatus represents a status of a pod.
type PodStatus string

// These are the valid statuses of pods.
const (
	PodRunning PodStatus = "Running"
	PodPending PodStatus = "Pending"
	PodStopped PodStatus = "Stopped"
)

// PodInfo contains one entry for every container with available info.
type PodInfo map[string]docker.Container

// PodState is the state of a pod, used as either input (desired state) or output (current state)
type PodState struct {
	Manifest ContainerManifest `json:"manifest,omitempty" yaml:"manifest,omitempty"`
	Status   PodStatus         `json:"status,omitempty" yaml:"status,omitempty"`
	Host     string            `json:"host,omitempty" yaml:"host,omitempty"`
	HostIP   string            `json:"hostIP,omitempty" yaml:"hostIP,omitempty"`
	PodIP    string            `json:"podIP,omitempty" yaml:"podIP,omitempty"`

	// The key of this map is the *name* of the container within the manifest; it has one
	// entry per container in the manifest. The value of this map is currently the output
	// of `docker inspect`. This output format is *not* final and should not be relied
	// upon.
	// TODO: Make real decisions about what our info should look like.
	Info PodInfo `json:"info,omitempty" yaml:"info,omitempty"`
}

// PodList is a list of Pods.
type PodList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []Pod `json:"items" yaml:"items,omitempty"`
}

// Pod is a collection of containers, used as either input (create, update) or as output (list, get)
type Pod struct {
	JSONBase     `json:",inline" yaml:",inline"`
	Labels       map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
	DesiredState PodState          `json:"desiredState,omitempty" yaml:"desiredState,omitempty"`
	CurrentState PodState          `json:"currentState,omitempty" yaml:"currentState,omitempty"`
}

// ReplicationControllerState is the state of a replication controller, either input (create, update) or as output (list, get)
type ReplicationControllerState struct {
	Replicas        int               `json:"replicas" yaml:"replicas"`
	ReplicaSelector map[string]string `json:"replicaSelector,omitempty" yaml:"replicaSelector,omitempty"`
	PodTemplate     PodTemplate       `json:"podTemplate,omitempty" yaml:"podTemplate,omitempty"`
}

// ReplicationControllerList is a collection of replication controllers.
type ReplicationControllerList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []ReplicationController `json:"items,omitempty" yaml:"items,omitempty"`
}

// ReplicationController represents the configuration of a replication controller
type ReplicationController struct {
	JSONBase     `json:",inline" yaml:",inline"`
	DesiredState ReplicationControllerState `json:"desiredState,omitempty" yaml:"desiredState,omitempty"`
	Labels       map[string]string          `json:"labels,omitempty" yaml:"labels,omitempty"`
}

// PodTemplate holds the information used for creating pods
type PodTemplate struct {
	DesiredState PodState          `json:"desiredState,omitempty" yaml:"desiredState,omitempty"`
	Labels       map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
}

// ServiceList holds a list of services
type ServiceList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []Service `json:"items" yaml:"items"`
}

// Service is a named abstraction of software service (for example, mysql) consisting of local port
// (for example 3306) that the proxy listens on, and the selector that determines which pods
// will answer requests sent through the proxy.
type Service struct {
	JSONBase `json:",inline" yaml:",inline"`
	Port     int `json:"port,omitempty" yaml:"port,omitempty"`

	// This service's labels.
	Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

	// This service will route traffic to pods having labels matching this selector.
	Selector                   map[string]string `json:"selector,omitempty" yaml:"selector,omitempty"`
	CreateExternalLoadBalancer bool              `json:"createExternalLoadBalancer,omitempty" yaml:"createExternalLoadBalancer,omitempty"`
}

// Endpoints is a collection of endpoints that implement the actual service, for example:
// Name: "mysql", Endpoints: ["10.10.1.1:1909", "10.10.2.2:8834"]
type Endpoints struct {
	Name      string
	Endpoints []string
}

// Minion is a worker node in Kubernetenes.
// The name of the minion according to etcd is in JSONBase.ID.
type Minion struct {
	JSONBase `json:",inline" yaml:",inline"`
	// Queried from cloud provider, if available.
	HostIP string `json:"hostIP,omitempty" yaml:"hostIP,omitempty"`
}

// MinionList is a list of minions.
type MinionList struct {
	JSONBase `json:",inline" yaml:",inline"`
	Items    []Minion `json:"minions,omitempty" yaml:"minions,omitempty"`
}

// Status is a return value for calls that don't return other objects.
// Arguably, this could go in apiserver, but I'm including it here so clients needn't
// import both.
type Status struct {
	JSONBase `json:",inline" yaml:",inline"`
	// One of: "success", "failure", "working" (for operations not yet completed)
	// TODO: if "working", include an operation identifier so final status can be
	// checked.
	Status string `json:"status,omitempty" yaml:"status,omitempty"`
	// Details about the status. May be an error description or an
	// operation number for later polling.
	Details string `json:"details,omitempty" yaml:"details,omitempty"`
	// Suggested HTTP return code for this status, 0 if not set.
	Code int `json:"code,omitempty" yaml:"code,omitempty"`
}

// Values of Status.Status
const (
	StatusSuccess = "success"
	StatusFailure = "failure"
	StatusWorking = "working"
)

// ServerOp is an operation delivered to API clients.
type ServerOp struct {
	JSONBase `yaml:",inline" json:",inline"`
}

// ServerOpList is a list of operations, as delivered to API clients.
type ServerOpList struct {
	JSONBase `yaml:",inline" json:",inline"`
	Items    []ServerOp `yaml:"items,omitempty" json:"items,omitempty"`
}
<|endoftext|>