<commit_before>package errors

import (
	"encoding/json"
	"fmt"
)

type (
	errorSource struct {
		*errorType
		source error
	}
)

// Error implements error interface.
func (e *errorSource) Error() string {
	return e.source.Error()
}

// NewAsSource returns a new error which is the source.
func NewAsSource(msg string) error {
	return newSource(nil, new(nil, msg, 1), 1)
}

// NewAsSourcef returns a new error which is the source.
func NewAsSourcef(format string, a ...interface{}) error {
	return newSource(nil, new(nil, fmt.Sprintf(format, a...), 1), 1)
}

// AsSource returns a new source error.
func AsSource(err error) error {
	return newSource(nil, err, 1)
}

// WrapBySourceError returns a new error.
// If the error is passed to errors.SourceOf function,
// returns the source.
func WrapBySourceError(inner error, source error) error {
	if inner == nil {
		return nil
	}
	return newSource(inner, source, 1)
}

// WrapBySourceMsg returns a new error.
func WrapBySourceMsg(inner error, msg string) error {
	if inner == nil {
		return nil
	}
	return newSource(inner, new(nil, msg, 1), 1)
}

// WrapBySourceMsgf returns a new error.
func WrapBySourceMsgf(inner error, format string, a ...interface{}) error {
	if inner == nil {
		return nil
	}
	return newSource(inner, new(nil, fmt.Sprintf(format, a...), 1), 1)
}

// SourceOf returns the source error of the err.
func SourceOf(err error) error {
	if e, ok := err.(*errorSource); ok {
		return e.source
	}
	if e, ok := err.(*collection); ok {
		return e.source()
	}
	if e, ok := err.(*errorType); ok {
		if e.inner != nil {
			return SourceOf(e.inner)
		}
		return e
	}
	return err
}

// ExplicitSourceOf returns an error if the error is
// explicitly specified Source by WrapBySourceXxx.
func ExplicitSourceOf(err error) error {
	if e, ok := err.(*errorSource); ok {
		return e.source
	}
	if e, ok := err.(*collection); ok {
		return e.explicitSource()
	}
	if e, ok := err.(*errorType); ok {
		if e.inner != nil {
			return ExplicitSourceOf(e.inner)
		}
		return nil
	}
	return nil
}

// MarshalJSON implements json.Marshaler interface.
func (e *errorSource) MarshalJSON() ([]byte, error) {
	obj := struct {
		Inner    *errMarshal `json:"inner"`
		Callers  *callerInfo `json:"callers"`
		Message  string      `json:"message"`
		IsSource bool        `json:"isSource"`
	}{
		Inner: &errMarshal{
			err:         e.inner,
			callerCount: e.callerCount,
		},
		Callers:  e.info,
		Message:  e.source.Error(),
		IsSource: true,
	}
	return json.Marshal(&obj)
}

func newSource(inner error, source error, skip int) error {
	return &errorSource{
		errorType: new(inner, "", skip+1).(*errorType),
		source:    source,
	}
}
<commit_msg>bug fix for nil source error.<commit_after>package errors

import (
	"encoding/json"
	"fmt"
)

type (
	errorSource struct {
		*errorType
		source error
	}
)

// Error implements error interface.
func (e *errorSource) Error() string {
	return e.source.Error()
}

// NewAsSource returns a new error which is the source.
func NewAsSource(msg string) error {
	return newSource(nil, new(nil, msg, 1), 1)
}

// NewAsSourcef returns a new error which is the source.
func NewAsSourcef(format string, a ...interface{}) error {
	return newSource(nil, new(nil, fmt.Sprintf(format, a...), 1), 1)
}

// AsSource returns a new source error.
func AsSource(err error) error {
	if err == nil {
		return nil
	}
	return newSource(nil, err, 1)
}

// WrapBySourceError returns a new error.
// If the error is passed to errors.SourceOf function,
// returns the source.
func WrapBySourceError(inner error, source error) error {
	if inner == nil {
		return nil
	}
	return newSource(inner, source, 1)
}

// WrapBySourceMsg returns a new error.
func WrapBySourceMsg(inner error, msg string) error {
	if inner == nil {
		return nil
	}
	return newSource(inner, new(nil, msg, 1), 1)
}

// WrapBySourceMsgf returns a new error.
func WrapBySourceMsgf(inner error, format string, a ...interface{}) error {
	if inner == nil {
		return nil
	}
	return newSource(inner, new(nil, fmt.Sprintf(format, a...), 1), 1)
}

// SourceOf returns the source error of the err.
func SourceOf(err error) error {
	if e, ok := err.(*errorSource); ok {
		return e.source
	}
	if e, ok := err.(*collection); ok {
		return e.source()
	}
	if e, ok := err.(*errorType); ok {
		if e.inner != nil {
			return SourceOf(e.inner)
		}
		return e
	}
	return err
}

// ExplicitSourceOf returns an error if the error is
// explicitly specified Source by WrapBySourceXxx.
func ExplicitSourceOf(err error) error {
	if e, ok := err.(*errorSource); ok {
		return e.source
	}
	if e, ok := err.(*collection); ok {
		return e.explicitSource()
	}
	if e, ok := err.(*errorType); ok {
		if e.inner != nil {
			return ExplicitSourceOf(e.inner)
		}
		return nil
	}
	return nil
}

// MarshalJSON implements json.Marshaler interface.
func (e *errorSource) MarshalJSON() ([]byte, error) {
	obj := struct {
		Inner    *errMarshal `json:"inner"`
		Callers  *callerInfo `json:"callers"`
		Message  string      `json:"message"`
		IsSource bool        `json:"isSource"`
	}{
		Inner: &errMarshal{
			err:         e.inner,
			callerCount: e.callerCount,
		},
		Callers:  e.info,
		Message:  e.source.Error(),
		IsSource: true,
	}
	return json.Marshal(&obj)
}

func newSource(inner error, source error, skip int) error {
	return &errorSource{
		errorType: new(inner, "", skip+1).(*errorType),
		source:    source,
	}
}
<|endoftext|>


<commit_before>package transports

const MarshalerNilTypeError string = "Marshaler can't handle a nil value."
const MarshalerTypeNotSupportedError string = "Marshaler doesn't support the type you're using:"

type Marshaler interface{
  Marshal(*interface{}) (error, interface{})
  Unmarshal()
}
<commit_msg>Defining MarshalerUnexpectedOutput<commit_after>package transports

const MarshalerNilTypeError string = "Marshaler can't handle a nil value."
const MarshalerTypeNotSupportedError string = "Marshaler doesn't support the type you're using:"

const MarshalerUnexpectedOutput string = "Unexpected Marshaler output"

type Marshaler interface{
  Marshal(*interface{}) (error, interface{})
  Unmarshal()
}
<|endoftext|>


<commit_before>package vsphere

import (
	"bytes"
	"fmt"
	"github.com/mitchellh/packer/common"
	"github.com/mitchellh/packer/packer"
	"os/exec"
	"strings"
)

var builtins = map[string]string{
	"mitchellh.vmware": "vmware",
}

type Config struct {
	common.PackerConfig `mapstructure:",squash"`

	Insecure           bool   `mapstructure:"insecure"`
	Cluster            string `mapstructure:"cluster"`
	Datacenter         string `mapstructure:"datacenter"`
	Datastore          string `mapstructure:"datastore"`
	Debug              bool   `mapstructure:"debug"`
	Host               string `mapstructure:"host"`
	Password           string `mapstructure:"password"`
	ResourcePool       string `mapstructure:"resource_pool"`
	Username           string `mapstructure:"username"`
	VMFolder           string `mapstructure:"vm_folder"`
	VMName             string `mapstructure:"vm_name"`
	VMNetwork          string `mapstructure:"vm_network"`

	tpl *packer.ConfigTemplate
}

type PostProcessor struct {
	config Config
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	_, err := common.DecodeConfig(&p.config, raws...)
	if err != nil {
		return err
	}

	p.config.tpl, err = packer.NewConfigTemplate()
	if err != nil {
		return err
	}
	p.config.tpl.UserVars = p.config.PackerUserVars

	// Accumulate any errors
	errs := new(packer.MultiError)

	if _, err := exec.LookPath("ovftool"); err != nil {
		errs = packer.MultiErrorAppend(
			errs, fmt.Errorf("ovftool not found: %s", err))
	}

	validates := map[string]*string{
		"cluster":      &p.config.Cluster,
		"datacenter":   &p.config.Datacenter,
		"datastore":    &p.config.Datastore,
		"host":         &p.config.Host,
		"vm_network":   &p.config.VMNetwork,
		"password":     &p.config.Password,
		"resource_pool": &p.config.ResourcePool,
		"username":     &p.config.Username,
		"vm_folder":    &p.config.VMFolder,
		"vm_name":      &p.config.VMName,
	}

	for n := range validates {
		if *validates[n] == "" {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("%s must be set", n))
		}
	}

	if len(errs.Errors) > 0 {
		return errs
	}

	return nil
}

func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
	if _, ok := builtins[artifact.BuilderId()]; !ok {
		return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
	}

	vmx := ""
	for _, path := range artifact.Files() {
		if strings.HasSuffix(path, ".vmx") {
			vmx = path
			break
		}
	}

	if vmx == "" {
		return nil, false, fmt.Errorf("VMX file not found")
	}

	// Get user variables from template
	vm_name, err := p.config.tpl.Process(p.config.VMName, p.config.PackerUserVars)
	if err != nil {
		return nil, false, fmt.Errorf("Failed: %s", err)
	}

	username, err := p.config.tpl.Process(p.config.Username, p.config.PackerUserVars)
	if err != nil {
		return nil, false, fmt.Errorf("Failed: %s", err)
	}

	password, err := p.config.tpl.Process(p.config.Password, p.config.PackerUserVars)
	if err != nil {
		return nil, false, fmt.Errorf("Failed: %s", err)
	}

	datastore, err := p.config.tpl.Process(p.config.Datastore, p.config.PackerUserVars)
	if err != nil {
		return nil, false, fmt.Errorf("Failed: %s", err)
	}

	ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))

	args := []string{
		fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
		"--acceptAllEulas",
		fmt.Sprintf("--name=%s", vm_name),
		fmt.Sprintf("--datastore=%s", datastore),
		fmt.Sprintf("--network=%s", p.config.VMNetwork),
		fmt.Sprintf("--vmFolder=%s", p.config.VMFolder),
		fmt.Sprintf("%s", vmx),
		fmt.Sprintf("vi://%s:%s@%s/%s/host/%s/Resources/%s",
			username,
			password,
			p.config.Host,
			p.config.Datacenter,
			p.config.Cluster,
			p.config.ResourcePool),
	}

	if p.config.Debug {
		ui.Message(fmt.Sprintf("DEBUG: %s", args))
	}

	var out bytes.Buffer
	cmd := exec.Command("ovftool", args...)
	cmd.Stdout = &out
	if err := cmd.Run(); err != nil {
		return nil, false, fmt.Errorf("Failed: %s\nStdout: %s", err, out.String())
	}

	ui.Message(fmt.Sprintf("%s", out.String()))

	return artifact, false, nil
}
<commit_msg>post-processor/vsphere: template process in prepare phase<commit_after>package vsphere

import (
	"bytes"
	"fmt"
	"github.com/mitchellh/packer/common"
	"github.com/mitchellh/packer/packer"
	"os/exec"
	"strings"
)

var builtins = map[string]string{
	"mitchellh.vmware": "vmware",
}

type Config struct {
	common.PackerConfig `mapstructure:",squash"`

	Insecure     bool   `mapstructure:"insecure"`
	Cluster      string `mapstructure:"cluster"`
	Datacenter   string `mapstructure:"datacenter"`
	Datastore    string `mapstructure:"datastore"`
	Host         string `mapstructure:"host"`
	Password     string `mapstructure:"password"`
	ResourcePool string `mapstructure:"resource_pool"`
	Username     string `mapstructure:"username"`
	VMFolder     string `mapstructure:"vm_folder"`
	VMName       string `mapstructure:"vm_name"`
	VMNetwork    string `mapstructure:"vm_network"`

	tpl *packer.ConfigTemplate
}

type PostProcessor struct {
	config Config
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	_, err := common.DecodeConfig(&p.config, raws...)
	if err != nil {
		return err
	}

	p.config.tpl, err = packer.NewConfigTemplate()
	if err != nil {
		return err
	}
	p.config.tpl.UserVars = p.config.PackerUserVars

	// Accumulate any errors
	errs := new(packer.MultiError)

	if _, err := exec.LookPath("ovftool"); err != nil {
		errs = packer.MultiErrorAppend(
			errs, fmt.Errorf("ovftool not found: %s", err))
	}

	templates := map[string]*string{
		"cluster":       &p.config.Cluster,
		"datacenter":    &p.config.Datacenter,
		"datastore":     &p.config.Datastore,
		"host":          &p.config.Host,
		"vm_network":    &p.config.VMNetwork,
		"password":      &p.config.Password,
		"resource_pool": &p.config.ResourcePool,
		"username":      &p.config.Username,
		"vm_folder":     &p.config.VMFolder,
		"vm_name":       &p.config.VMName,
	}

	for key, ptr := range templates {
		if *ptr == "" {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("%s must be set", key))
		}

		*ptr, err = p.config.tpl.Process(*ptr, nil)
		if err != nil {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("Error processing %s: %s", key, err))
		}
	}

	if len(errs.Errors) > 0 {
		return errs
	}

	return nil
}

func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
	if _, ok := builtins[artifact.BuilderId()]; !ok {
		return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
	}

	vmx := ""
	for _, path := range artifact.Files() {
		if strings.HasSuffix(path, ".vmx") {
			vmx = path
			break
		}
	}

	if vmx == "" {
		return nil, false, fmt.Errorf("VMX file not found")
	}

	args := []string{
		fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
		"--acceptAllEulas",
		fmt.Sprintf("--name=%s", p.config.VMName),
		fmt.Sprintf("--datastore=%s", p.config.Datastore),
		fmt.Sprintf("--network=%s", p.config.VMNetwork),
		fmt.Sprintf("--vmFolder=%s", p.config.VMFolder),
		fmt.Sprintf("%s", vmx),
		fmt.Sprintf("vi://%s:%s@%s/%s/host/%s/Resources/%s",
			p.config.Username,
			p.config.Password,
			p.config.Host,
			p.config.Datacenter,
			p.config.Cluster,
			p.config.ResourcePool),
	}

	ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
	var out bytes.Buffer
	cmd := exec.Command("ovftool", args...)
	cmd.Stdout = &out
	if err := cmd.Run(); err != nil {
		return nil, false, fmt.Errorf("Failed: %s\nStdout: %s", err, out.String())
	}

	ui.Message(fmt.Sprintf("%s", out.String()))

	return artifact, false, nil
}
<|endoftext|>


<commit_before>package middlewares

import (
	"encoding/json"
	"net/http"

	"github.com/sirupsen/logrus"
	"github.com/urfave/negroni"
)

type responsePanic struct {
	Code int    `json:"code"`
	Text string `json:"text"`
}

type textPanicFormatter struct {
	logger logrus.FieldLogger
}

// Implement PanicFormatter interface
func (t *textPanicFormatter) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *negroni.PanicInformation) {
	t.writeResponseJSON(rw, http.StatusInternalServerError, &responsePanic{
		Code: http.StatusInternalServerError,
		Text: "panic occurred",
	})
}

func (t *textPanicFormatter) writeResponseJSON(w http.ResponseWriter, statusCode int, response interface{}) {
	w.WriteHeader(statusCode)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	if err := json.NewEncoder(w).Encode(response); err != nil {
		t.logger.WithError(err).Error("cannot send response on panic")
	}
}

func NewRecovery(logger logrus.FieldLogger) negroni.Handler {
	middleware := negroni.NewRecovery()
	middleware.PrintStack = false
	middleware.Logger = logger
	middleware.Formatter = &textPanicFormatter{
		logger: logger,
	}
	return middleware
}
<commit_msg>Fix http status code sending in recovery middleware<commit_after>package middlewares

import (
	"encoding/json"
	"net/http"

	"github.com/sirupsen/logrus"
	"github.com/urfave/negroni"
)

type responsePanic struct {
	Code int    `json:"code"`
	Text string `json:"text"`
}

type textPanicFormatter struct {
	logger logrus.FieldLogger
}

// Implement PanicFormatter interface
func (t *textPanicFormatter) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *negroni.PanicInformation) {
	t.writeResponseJSON(rw, http.StatusInternalServerError, &responsePanic{
		Code: http.StatusInternalServerError,
		Text: "panic occurred",
	})
}

func (t *textPanicFormatter) writeResponseJSON(w http.ResponseWriter, statusCode int, response interface{}) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(statusCode)

	if err := json.NewEncoder(w).Encode(response); err != nil {
		t.logger.WithError(err).Error("cannot send response on panic")
	}
}

func NewRecovery(logger logrus.FieldLogger) negroni.Handler {
	middleware := negroni.NewRecovery()
	middleware.PrintStack = false
	middleware.Logger = logger
	middleware.Formatter = &textPanicFormatter{
		logger: logger,
	}
	return middleware
}
<|endoftext|>


<commit_before>	package main

	import (
		"flag"
		"time"
		"strings"

		"github.com/aws/aws-sdk-go/service/ec2"
		"github.com/aws/aws-sdk-go/aws"
		"github.com/mitchellh/cli"
		"strconv"
	)

	type CreateCommand struct {
		Ui 			cli.Ui
		InstanceId 	string
		Name 		string
		DryRun		bool
		NoReboot	bool
	}

	// descriptions for args
	var createDscrInstanceId = "The instance from which to create the AMI"
	var createDscrName = "The name of the AMI; the current timestamp will be automatically appended"
	var createDscrDryRun = "Execute a simulated run"
	var createDscrNoReboot = "If true, do not reboot the instance before creating the AMI. It is preferable to reboot the instance to guarantee a consistent filesystem when taking the snapshot, but the likelihood of an inconsistent snapshot is very low."

	func (c *CreateCommand) Help() string {
		return `ec2-snapper create <args> [--help]

	Create an AMI of the given EC2 instance.

	Available args are:
	--instance      ` + createDscrInstanceId + `
	--name          ` + createDscrName + `
	--dry-run       ` + createDscrDryRun + `
	--no-reboot     ` + createDscrNoReboot
	}

	func (c *CreateCommand) Synopsis() string {
		return "Create an AMI of the given EC2 instance"
	}

	func (c *CreateCommand) Run(args []string) int {

		// Handle the command-line args
		cmdFlags := flag.NewFlagSet("create", flag.ExitOnError)
		cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }

		cmdFlags.StringVar(&c.InstanceId, "instance", "", createDscrInstanceId)
		cmdFlags.StringVar(&c.Name, "name", "", createDscrName)
		cmdFlags.BoolVar(&c.DryRun, "dry-run", false, createDscrDryRun)
		cmdFlags.BoolVar(&c.NoReboot, "no-reboot", true, createDscrNoReboot)

		if err := cmdFlags.Parse(args); err != nil {
			return 1
		}

		// Check for required command-line args
		if c.InstanceId == "" {
			c.Ui.Error("ERROR: The argument '--instance' is required.")
			return 1
		}

		if c.Name == "" {
			c.Ui.Error("ERROR: The argument '--name' is required.")
			return 1
		}

		// Create an EC2 service object; AWS region is picked up from the "AWS_REGION" env var.
		svc := ec2.New(nil)

		// Generate a nicely formatted timestamp for right now
		const dateLayoutForAmiName = "2006-01-02 at 15_04_05 (MST)"
		t := time.Now()

		// Create the AMI Snapshot
		name := c.Name + " - " + t.Format(dateLayoutForAmiName)

		c.Ui.Output("==> Creating AMI for " + c.InstanceId + "...")

		resp, err := svc.CreateImage(&ec2.CreateImageInput{
			Name: &name,
			InstanceID: &c.InstanceId,
			DryRun: &c.DryRun,
			NoReboot: &c.NoReboot })
		if err != nil && strings.Contains(err.Error(), "NoCredentialProviders") {
			c.Ui.Error("ERROR: No AWS credentials were found.  Either set the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, or run this program on an EC2 instance that has an IAM Role with the appropriate permissions.")
			return 1
		} else if err != nil {
			panic(err)
		}

		// Sleep here to give time for AMI to get found
		time.Sleep(3000 * time.Millisecond)

		// Assign tags to this AMI.  We'll use these when it comes time to delete the AMI
		c.Ui.Output("==> Adding tags to AMI " + *resp.ImageID + "...")

		svc.CreateTags(&ec2.CreateTagsInput{
			Resources: []*string{resp.ImageID},
			Tags: []*ec2.Tag{
				&ec2.Tag{ Key: aws.String("ec2-snapper-instance-id"), Value: &c.InstanceId },
			},
		})

		// Check the status of the AMI
		respDscrImages, err := svc.DescribeImages(&ec2.DescribeImagesInput{
			Filters: []*ec2.Filter{
				&ec2.Filter{
					Name: aws.String("image-id"),
					Values: []*string{resp.ImageID},
				},
			},
		})
		if err != nil {
			panic(err)
		}

		// If no AMI at all was found, throw an error
		if len(respDscrImages.Images) == 0 {
			c.Ui.Error("ERROR: Could not find the AMI just created.")
			return 1
		}

		// If the AMI's status is failed throw an error
		if *respDscrImages.Images[0].State == "failed" {
			c.Ui.Error("ERROR: AMI was crexated but entered a state of 'failed'. This is an AWS issue. Please re-run this command.  Note that you will need to manually de-register the AMI in the AWS console or via the API.")
			return 1
		}

		// Announce success
		c.Ui.Info("==> Success! Created " + *resp.ImageID + " named \"" + name + "\"")
		return 0
	}

<commit_msg>Removed unused go library strconv<commit_after>	package main

	import (
		"flag"
		"time"
		"strings"

		"github.com/aws/aws-sdk-go/service/ec2"
		"github.com/aws/aws-sdk-go/aws"
		"github.com/mitchellh/cli"
	)

	type CreateCommand struct {
		Ui 			cli.Ui
		InstanceId 	string
		Name 		string
		DryRun		bool
		NoReboot	bool
	}

	// descriptions for args
	var createDscrInstanceId = "The instance from which to create the AMI"
	var createDscrName = "The name of the AMI; the current timestamp will be automatically appended"
	var createDscrDryRun = "Execute a simulated run"
	var createDscrNoReboot = "If true, do not reboot the instance before creating the AMI. It is preferable to reboot the instance to guarantee a consistent filesystem when taking the snapshot, but the likelihood of an inconsistent snapshot is very low."

	func (c *CreateCommand) Help() string {
		return `ec2-snapper create <args> [--help]

	Create an AMI of the given EC2 instance.

	Available args are:
	--instance      ` + createDscrInstanceId + `
	--name          ` + createDscrName + `
	--dry-run       ` + createDscrDryRun + `
	--no-reboot     ` + createDscrNoReboot
	}

	func (c *CreateCommand) Synopsis() string {
		return "Create an AMI of the given EC2 instance"
	}

	func (c *CreateCommand) Run(args []string) int {

		// Handle the command-line args
		cmdFlags := flag.NewFlagSet("create", flag.ExitOnError)
		cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }

		cmdFlags.StringVar(&c.InstanceId, "instance", "", createDscrInstanceId)
		cmdFlags.StringVar(&c.Name, "name", "", createDscrName)
		cmdFlags.BoolVar(&c.DryRun, "dry-run", false, createDscrDryRun)
		cmdFlags.BoolVar(&c.NoReboot, "no-reboot", true, createDscrNoReboot)

		if err := cmdFlags.Parse(args); err != nil {
			return 1
		}

		// Check for required command-line args
		if c.InstanceId == "" {
			c.Ui.Error("ERROR: The argument '--instance' is required.")
			return 1
		}

		if c.Name == "" {
			c.Ui.Error("ERROR: The argument '--name' is required.")
			return 1
		}

		// Create an EC2 service object; AWS region is picked up from the "AWS_REGION" env var.
		svc := ec2.New(nil)

		// Generate a nicely formatted timestamp for right now
		const dateLayoutForAmiName = "2006-01-02 at 15_04_05 (MST)"
		t := time.Now()

		// Create the AMI Snapshot
		name := c.Name + " - " + t.Format(dateLayoutForAmiName)

		c.Ui.Output("==> Creating AMI for " + c.InstanceId + "...")

		resp, err := svc.CreateImage(&ec2.CreateImageInput{
			Name: &name,
			InstanceID: &c.InstanceId,
			DryRun: &c.DryRun,
			NoReboot: &c.NoReboot })
		if err != nil && strings.Contains(err.Error(), "NoCredentialProviders") {
			c.Ui.Error("ERROR: No AWS credentials were found.  Either set the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, or run this program on an EC2 instance that has an IAM Role with the appropriate permissions.")
			return 1
		} else if err != nil {
			panic(err)
		}

		// Sleep here to give time for AMI to get found
		time.Sleep(3000 * time.Millisecond)

		// Assign tags to this AMI.  We'll use these when it comes time to delete the AMI
		c.Ui.Output("==> Adding tags to AMI " + *resp.ImageID + "...")

		svc.CreateTags(&ec2.CreateTagsInput{
			Resources: []*string{resp.ImageID},
			Tags: []*ec2.Tag{
				&ec2.Tag{ Key: aws.String("ec2-snapper-instance-id"), Value: &c.InstanceId },
			},
		})

		// Check the status of the AMI
		respDscrImages, err := svc.DescribeImages(&ec2.DescribeImagesInput{
			Filters: []*ec2.Filter{
				&ec2.Filter{
					Name: aws.String("image-id"),
					Values: []*string{resp.ImageID},
				},
			},
		})
		if err != nil {
			panic(err)
		}

		// If no AMI at all was found, throw an error
		if len(respDscrImages.Images) == 0 {
			c.Ui.Error("ERROR: Could not find the AMI just created.")
			return 1
		}

		// If the AMI's status is failed throw an error
		if *respDscrImages.Images[0].State == "failed" {
			c.Ui.Error("ERROR: AMI was crexated but entered a state of 'failed'. This is an AWS issue. Please re-run this command.  Note that you will need to manually de-register the AMI in the AWS console or via the API.")
			return 1
		}

		// Announce success
		c.Ui.Info("==> Success! Created " + *resp.ImageID + " named \"" + name + "\"")
		return 0
	}

<|endoftext|>


<commit_before>package builder

import (
	"errors"
	"io"
	"io/ioutil"
	"regexp"

	l "github.com/Sirupsen/logrus"
	"github.com/docker/docker/pkg/archive"
	"github.com/modcloth/go-fileutils"
	"github.com/onsi/gocleanup"
	"github.com/rafecolton/go-dockerclient-quick"

	"github.com/winchman/builder-core/communication"
	"github.com/winchman/builder-core/filecheck"
	"github.com/winchman/builder-core/parser"
)

var (
	// SkipPush will, when set to true, override any behavior set by a Bobfile and
	// will cause builders *NOT* to run `docker push` commands.  SkipPush is also set
	// by the `--skip-push` option when used on the command line.
	SkipPush bool

	imageWithTagRegex = regexp.MustCompile("^(.*):(.*)$")
)

/*
A Builder is the struct that actually does the work of moving files around and
executing the commands that do the docker build.
*/
type Builder struct {
	dockerClient    dockerclient.DockerClient
	workdir         string
	nextSubSequence *parser.SubSequence
	Stdout          io.Writer
	reporter        *comm.Reporter
	Builderfile     string
	contextDir      string
}

/*
SetNextSubSequence sets the next subsequence within bob to be processed. This
function is exported because it is used explicitly in tests, but in Build(), it
is intended to be used as a helper function.
*/
func (bob *Builder) SetNextSubSequence(subSeq *parser.SubSequence) {
	bob.nextSubSequence = subSeq
}

// NewBuilderOptions encapsulates all of the options necessary for creating a
// new builder
type NewBuilderOptions struct {
	Log          comm.LogChan
	Event        comm.EventChan
	ContextDir   string
	dockerClient dockerclient.DockerClient // default to nil for regular docker client
}

/*
NewBuilder returns an instance of a Builder struct.  The function exists in
case we want to initialize our Builders with something.
*/
func NewBuilder(opts NewBuilderOptions) *Builder {
	var ret = &Builder{
		reporter:   comm.NewReporter(opts.Log, opts.Event),
		contextDir: opts.ContextDir,
	}

	ret.dockerClient = opts.dockerClient

	if opts.Log != nil {
		ret.Stdout = comm.NewLogEntryWriter(opts.Log)
	} else {
		ret.Stdout = ioutil.Discard /* /dev/null */
	}

	return ret
}

// BuildCommandSequence performs a build from a parser-generated CommandSequence struct
func (bob *Builder) BuildCommandSequence(commandSequence *parser.CommandSequence) error {
	bob.reporter.Event(comm.EventOptions{EventType: comm.RequestedEvent})

	if bob.dockerClient == nil {
		client, err := dockerclient.NewDockerClient()
		if err != nil {
			return err
		}
		bob.dockerClient = client
	}

	for _, seq := range commandSequence.Commands {
		var imageID string
		var err error

		if err := bob.cleanWorkdir(); err != nil {
			return err
		}
		bob.SetNextSubSequence(seq)
		if err := bob.setup(); err != nil {
			return err
		}

		bob.reporter.Log(
			l.WithField("container_section", seq.Metadata.Name),
			"running commands for container section",
		)

		for _, cmd := range seq.SubCommand {
			opts := &parser.DockerCmdOpts{
				DockerClient: bob.dockerClient,
				Image:        imageID,
				ImageUUID:    seq.Metadata.UUID,
				SkipPush:     SkipPush,
				Stdout:       bob.Stdout,
				Workdir:      bob.workdir,
				Reporter:     bob.reporter,
			}
			cmd = cmd.WithOpts(opts)

			bob.reporter.Log(l.WithField("command", cmd.Message()), "running docker command")

			if imageID, err = cmd.Run(); err != nil {
				switch err.(type) {
				case parser.NilClientError:
					continue
				default:
					return err
				}
			}
		}

		bob.attemptToDeleteTemporaryUUIDTag(seq.Metadata.UUID)
	}

	bob.reporter.Event(comm.EventOptions{EventType: comm.CompletedEvent})

	return nil
}

func (bob *Builder) attemptToDeleteTemporaryUUIDTag(uuid string) {
	if bob.dockerClient == nil {
		return
	}

	regex := ":" + uuid + "$"
	image, err := bob.dockerClient.LatestImageByRegex(regex)
	if err != nil {
		bob.reporter.LogLevel(
			l.WithField("err", err),
			"error getting repo taggged with temporary tag",
			l.WarnLevel,
		)
	}

	for _, tag := range image.RepoTags {
		matched, err := regexp.MatchString(regex, tag)
		if err != nil {
			return
		}
		if matched {
			bob.reporter.LogLevel(
				l.WithFields(l.Fields{
					"image_id": image.ID,
					"tag":      tag,
				}),
				"deleting temporary tag",
				l.DebugLevel,
			)

			if err = bob.dockerClient.Client().RemoveImage(tag); err != nil {
				bob.reporter.LogLevel(
					l.WithField("err", err),
					"error deleting temporary tag",
					l.WarnLevel,
				)
			}
			return
		}
	}
}

/*
Setup moves all of the correct files into place in the temporary directory in
order to perform the docker build.
*/
func (bob *Builder) setup() error {
	var workdir = bob.workdir
	var pathToDockerfile *filecheck.TrustedFilePath
	var err error

	if bob.nextSubSequence == nil {
		return errors.New("no command sub sequence set, cannot perform setup")
	}

	meta := bob.nextSubSequence.Metadata
	dockerfile := meta.Dockerfile
	opts := filecheck.NewTrustedFilePathOptions{File: dockerfile, Top: bob.contextDir}
	pathToDockerfile, err = filecheck.NewTrustedFilePath(opts)
	if err != nil {
		return err
	}

	if pathToDockerfile.Sanitize(); pathToDockerfile.State != filecheck.OK {
		return pathToDockerfile.Error
	}

	contextDir := pathToDockerfile.Top()
	tarStream, err := archive.TarWithOptions(contextDir, &archive.TarOptions{
		Compression: archive.Uncompressed,
		Excludes:    []string{"Dockerfile"},
	})
	if err != nil {
		return err
	}

	defer tarStream.Close()
	if err := archive.Untar(tarStream, workdir, nil); err != nil {
		return err
	}

	if err := fileutils.CpWithArgs(
		contextDir+"/"+meta.Dockerfile,
		workdir+"/Dockerfile",
		fileutils.CpArgs{PreserveModTime: true},
	); err != nil {
		return err
	}

	return nil
}

func (bob *Builder) generateWorkDir() string {
	tmp, err := ioutil.TempDir("", "bob")
	if err != nil {
		return ""
	}

	gocleanup.Register(func() {
		fileutils.RmRF(tmp)
	})

	return tmp
}

/*
cleanWorkdir effectively does a rm -rf and mkdir -p on bob's workdir.  Intended
to be used before using the workdir (i.e. before new command groups).
*/
func (bob *Builder) cleanWorkdir() error {
	workdir := bob.generateWorkDir()
	bob.workdir = workdir

	if err := fileutils.RmRF(workdir); err != nil {
		return err
	}

	return fileutils.MkdirP(workdir, 0755)
}
<commit_msg>Add the image_id to the command logs after the command runs. The controller will need this information shortly.<commit_after>package builder

import (
	"errors"
	"io"
	"io/ioutil"
	"regexp"

	l "github.com/Sirupsen/logrus"
	"github.com/docker/docker/pkg/archive"
	"github.com/modcloth/go-fileutils"
	"github.com/onsi/gocleanup"
	"github.com/rafecolton/go-dockerclient-quick"

	"github.com/winchman/builder-core/communication"
	"github.com/winchman/builder-core/filecheck"
	"github.com/winchman/builder-core/parser"
)

var (
	// SkipPush will, when set to true, override any behavior set by a Bobfile and
	// will cause builders *NOT* to run `docker push` commands.  SkipPush is also set
	// by the `--skip-push` option when used on the command line.
	SkipPush bool

	imageWithTagRegex = regexp.MustCompile("^(.*):(.*)$")
)

/*
A Builder is the struct that actually does the work of moving files around and
executing the commands that do the docker build.
*/
type Builder struct {
	dockerClient    dockerclient.DockerClient
	workdir         string
	nextSubSequence *parser.SubSequence
	Stdout          io.Writer
	reporter        *comm.Reporter
	Builderfile     string
	contextDir      string
}

/*
SetNextSubSequence sets the next subsequence within bob to be processed. This
function is exported because it is used explicitly in tests, but in Build(), it
is intended to be used as a helper function.
*/
func (bob *Builder) SetNextSubSequence(subSeq *parser.SubSequence) {
	bob.nextSubSequence = subSeq
}

// NewBuilderOptions encapsulates all of the options necessary for creating a
// new builder
type NewBuilderOptions struct {
	Log          comm.LogChan
	Event        comm.EventChan
	ContextDir   string
	dockerClient dockerclient.DockerClient // default to nil for regular docker client
}

/*
NewBuilder returns an instance of a Builder struct.  The function exists in
case we want to initialize our Builders with something.
*/
func NewBuilder(opts NewBuilderOptions) *Builder {
	var ret = &Builder{
		reporter:   comm.NewReporter(opts.Log, opts.Event),
		contextDir: opts.ContextDir,
	}

	ret.dockerClient = opts.dockerClient

	if opts.Log != nil {
		ret.Stdout = comm.NewLogEntryWriter(opts.Log)
	} else {
		ret.Stdout = ioutil.Discard /* /dev/null */
	}

	return ret
}

// BuildCommandSequence performs a build from a parser-generated CommandSequence struct
func (bob *Builder) BuildCommandSequence(commandSequence *parser.CommandSequence) error {
	bob.reporter.Event(comm.EventOptions{EventType: comm.RequestedEvent})

	if bob.dockerClient == nil {
		client, err := dockerclient.NewDockerClient()
		if err != nil {
			return err
		}
		bob.dockerClient = client
	}

	for _, seq := range commandSequence.Commands {
		var imageID string
		var err error

		if err := bob.cleanWorkdir(); err != nil {
			return err
		}
		bob.SetNextSubSequence(seq)
		if err := bob.setup(); err != nil {
			return err
		}

		bob.reporter.Log(
			l.WithField("container_section", seq.Metadata.Name),
			"running commands for container section",
		)

		for _, cmd := range seq.SubCommand {
			opts := &parser.DockerCmdOpts{
				DockerClient: bob.dockerClient,
				Image:        imageID,
				ImageUUID:    seq.Metadata.UUID,
				SkipPush:     SkipPush,
				Stdout:       bob.Stdout,
				Workdir:      bob.workdir,
				Reporter:     bob.reporter,
			}
			cmd = cmd.WithOpts(opts)

			bob.reporter.Log(l.WithField("command", cmd.Message()), "running docker command")

			if imageID, err = cmd.Run(); err != nil {
				switch err.(type) {
				case parser.NilClientError:
					continue
				default:
					return err
				}
			}

			bob.reporter.Log(
				l.WithFields(l.Fields{
					"command":  cmd.Message(),
					"image_id": imageID,
				}),
				"finished running docker command")
		}

		bob.attemptToDeleteTemporaryUUIDTag(seq.Metadata.UUID)
	}

	bob.reporter.Event(comm.EventOptions{EventType: comm.CompletedEvent})

	return nil
}

func (bob *Builder) attemptToDeleteTemporaryUUIDTag(uuid string) {
	if bob.dockerClient == nil {
		return
	}

	regex := ":" + uuid + "$"
	image, err := bob.dockerClient.LatestImageByRegex(regex)
	if err != nil {
		bob.reporter.LogLevel(
			l.WithField("err", err),
			"error getting repo taggged with temporary tag",
			l.WarnLevel,
		)
	}

	for _, tag := range image.RepoTags {
		matched, err := regexp.MatchString(regex, tag)
		if err != nil {
			return
		}
		if matched {
			bob.reporter.LogLevel(
				l.WithFields(l.Fields{
					"image_id": image.ID,
					"tag":      tag,
				}),
				"deleting temporary tag",
				l.DebugLevel,
			)

			if err = bob.dockerClient.Client().RemoveImage(tag); err != nil {
				bob.reporter.LogLevel(
					l.WithField("err", err),
					"error deleting temporary tag",
					l.WarnLevel,
				)
			}
			return
		}
	}
}

/*
Setup moves all of the correct files into place in the temporary directory in
order to perform the docker build.
*/
func (bob *Builder) setup() error {
	var workdir = bob.workdir
	var pathToDockerfile *filecheck.TrustedFilePath
	var err error

	if bob.nextSubSequence == nil {
		return errors.New("no command sub sequence set, cannot perform setup")
	}

	meta := bob.nextSubSequence.Metadata
	dockerfile := meta.Dockerfile
	opts := filecheck.NewTrustedFilePathOptions{File: dockerfile, Top: bob.contextDir}
	pathToDockerfile, err = filecheck.NewTrustedFilePath(opts)
	if err != nil {
		return err
	}

	if pathToDockerfile.Sanitize(); pathToDockerfile.State != filecheck.OK {
		return pathToDockerfile.Error
	}

	contextDir := pathToDockerfile.Top()
	tarStream, err := archive.TarWithOptions(contextDir, &archive.TarOptions{
		Compression: archive.Uncompressed,
		Excludes:    []string{"Dockerfile"},
	})
	if err != nil {
		return err
	}

	defer tarStream.Close()
	if err := archive.Untar(tarStream, workdir, nil); err != nil {
		return err
	}

	if err := fileutils.CpWithArgs(
		contextDir+"/"+meta.Dockerfile,
		workdir+"/Dockerfile",
		fileutils.CpArgs{PreserveModTime: true},
	); err != nil {
		return err
	}

	return nil
}

func (bob *Builder) generateWorkDir() string {
	tmp, err := ioutil.TempDir("", "bob")
	if err != nil {
		return ""
	}

	gocleanup.Register(func() {
		fileutils.RmRF(tmp)
	})

	return tmp
}

/*
cleanWorkdir effectively does a rm -rf and mkdir -p on bob's workdir.  Intended
to be used before using the workdir (i.e. before new command groups).
*/
func (bob *Builder) cleanWorkdir() error {
	workdir := bob.generateWorkDir()
	bob.workdir = workdir

	if err := fileutils.RmRF(workdir); err != nil {
		return err
	}

	return fileutils.MkdirP(workdir, 0755)
}
<|endoftext|>


<commit_before>// Copyright 2015 The etcd Authors
//
// 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 v2store

import "github.com/prometheus/client_golang/prometheus"

// Set of raw Prometheus metrics.
// Labels
// * action = declared in event.go
// * outcome = Outcome
// Do not increment directly, use Report* methods.
var (
	readCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "reads_total",
			Help:      "Total number of reads action by (get/getRecursive), local to this member.",
		}, []string{"action"})

	writeCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "writes_total",
			Help:      "Total number of writes (e.g. set/compareAndDelete) seen by this member.",
		}, []string{"action"})

	readFailedCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "reads_failed_total",
			Help:      "Failed read actions by (get/getRecursive), local to this member.",
		}, []string{"action"})

	writeFailedCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "writes_failed_total",
			Help:      "Failed write actions (e.g. set/compareAndDelete), seen by this member.",
		}, []string{"action"})

	expireCounter = prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "expires_total",
			Help:      "Total number of expired keys.",
		})

	watchRequests = prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "watch_requests_total",
			Help:      "Total number of incoming watch requests (new or reestablished).",
		})

	watcherCount = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "watchers",
			Help:      "Count of currently active watchers.",
		})
)

const (
	GetRecursive = "getRecursive"
)

func init() {
	if prometheus.Register(readCounter) != nil {
		// Tests will try to double register since the tests use both
		// store and store_test packages; ignore second attempts.
		return
	}
	prometheus.MustRegister(writeCounter)
	prometheus.MustRegister(expireCounter)
	prometheus.MustRegister(watchRequests)
	prometheus.MustRegister(watcherCount)
}

func reportReadSuccess(read_action string) {
	readCounter.WithLabelValues(read_action).Inc()
}

func reportReadFailure(read_action string) {
	readCounter.WithLabelValues(read_action).Inc()
	readFailedCounter.WithLabelValues(read_action).Inc()
}

func reportWriteSuccess(write_action string) {
	writeCounter.WithLabelValues(write_action).Inc()
}

func reportWriteFailure(write_action string) {
	writeCounter.WithLabelValues(write_action).Inc()
	writeFailedCounter.WithLabelValues(write_action).Inc()
}

func reportExpiredKey() {
	expireCounter.Inc()
}

func reportWatchRequest() {
	watchRequests.Inc()
}

func reportWatcherAdded() {
	watcherCount.Inc()
}

func reportWatcherRemoved() {
	watcherCount.Dec()
}
<commit_msg>api/v2store:  use camel case instead of snake case.<commit_after>// Copyright 2015 The etcd Authors
//
// 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 v2store

import "github.com/prometheus/client_golang/prometheus"

// Set of raw Prometheus metrics.
// Labels
// * action = declared in event.go
// * outcome = Outcome
// Do not increment directly, use Report* methods.
var (
	readCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "reads_total",
			Help:      "Total number of reads action by (get/getRecursive), local to this member.",
		}, []string{"action"})

	writeCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "writes_total",
			Help:      "Total number of writes (e.g. set/compareAndDelete) seen by this member.",
		}, []string{"action"})

	readFailedCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "reads_failed_total",
			Help:      "Failed read actions by (get/getRecursive), local to this member.",
		}, []string{"action"})

	writeFailedCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "writes_failed_total",
			Help:      "Failed write actions (e.g. set/compareAndDelete), seen by this member.",
		}, []string{"action"})

	expireCounter = prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "expires_total",
			Help:      "Total number of expired keys.",
		})

	watchRequests = prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "watch_requests_total",
			Help:      "Total number of incoming watch requests (new or reestablished).",
		})

	watcherCount = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "etcd_debugging",
			Subsystem: "store",
			Name:      "watchers",
			Help:      "Count of currently active watchers.",
		})
)

const (
	GetRecursive = "getRecursive"
)

func init() {
	if prometheus.Register(readCounter) != nil {
		// Tests will try to double register since the tests use both
		// store and store_test packages; ignore second attempts.
		return
	}
	prometheus.MustRegister(writeCounter)
	prometheus.MustRegister(expireCounter)
	prometheus.MustRegister(watchRequests)
	prometheus.MustRegister(watcherCount)
}

func reportReadSuccess(readAction string) {
	readCounter.WithLabelValues(readAction).Inc()
}

func reportReadFailure(readAction string) {
	readCounter.WithLabelValues(readAction).Inc()
	readFailedCounter.WithLabelValues(readAction).Inc()
}

func reportWriteSuccess(writeAction string) {
	writeCounter.WithLabelValues(writeAction).Inc()
}

func reportWriteFailure(writeAction string) {
	writeCounter.WithLabelValues(writeAction).Inc()
	writeFailedCounter.WithLabelValues(writeAction).Inc()
}

func reportExpiredKey() {
	expireCounter.Inc()
}

func reportWatchRequest() {
	watchRequests.Inc()
}

func reportWatcherAdded() {
	watcherCount.Inc()
}

func reportWatcherRemoved() {
	watcherCount.Dec()
}
<|endoftext|>


<commit_before>package vbox

import (
	"bytes"
	"os/exec"
	"strings"
	"fmt"
	"regexp"
	"time"
	"errors"
	"strconv"

	"github.com/Sirupsen/logrus"

	// "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
	// "gitlab.com/gitlab-org/gitlab-ci-multi-runner/executors"
)

type StatusType string

const (
	NotFound  StatusType = "notfound"
	Invalid              = "invalid"
	Stopped              = "stopped"
	Suspended            = "suspended"
	Running              = "running"
	Saved                = "saved"
	// TODO: more statuses
)

func VboxManageOutput(exe string, args ...string) (string, error) {

	var stdout, stderr bytes.Buffer

	logrus.Debugf("Executing VBoxManageOutput: %#v", args)
	cmd := exec.Command(exe, args...)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()

	stderrString := strings.TrimSpace(stderr.String())

	if _, ok := err.(*exec.ExitError); ok {
		err = fmt.Errorf("VBoxManageOutput error: %s", stderrString)
	}

	return stdout.String(), err
}

func VBoxManage(args ...string) (string, error) {
	return VboxManageOutput("vboxmanage", args...)
}

func Version() (string, error) {
	version, err := VBoxManage("--version")
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(version), nil
}

func FindSshPort(vmName string) (string, error) {
	info, err := VBoxManage("showvminfo", vmName)
	portRe := regexp.MustCompile(`guestssh.*host port = (\d+)`)
	sshPort := portRe.FindStringSubmatch(info)
	return sshPort[1], err
}

func Exist(vmName string) bool {
	_, err := VBoxManage("showvminfo", vmName)
	if err != nil{
		return false
	}
	return true
}

func CreateOsVM(vmName string, templateName string) error {
	_, err := VBoxManage("clonevm", vmName, "--mode", "machine", "--name", templateName, "--register")
	return err
}

func FindNextPort(highport string, usedPorts [][]string) string {
	for _, port := range usedPorts {
		if highport == port[1] {
			var temp int
			temp,_ = strconv.Atoi(highport)
			temp = temp + 1
			highport = strconv.Itoa(temp)
			highport = FindNextPort(highport, usedPorts)
		}
	}
	return highport
}

func ConfigureSSH(vmName string) error {
	var localport string
	output, err := VBoxManage("list", "vms", "-l")
	allPortsRe := regexp.MustCompile(`host port = (\d+)`)
	usedPorts := allPortsRe.FindAllStringSubmatch(output, -1)
	logrus.Debugln(usedPorts)
	if usedPorts == nil {
		localport = "2222"
	} else {
		highport := "2222"
		localport = FindNextPort(highport, usedPorts)
	}

	rule := fmt.Sprintf("guestssh,tcp,127.0.0.1,%s,,22", localport)
	_, err = VBoxManage("modifyvm", vmName, "--natpf1", rule)
	return err
}

func CreateSnapshot(vmName string, snapshotName string) error {
	_, err := VBoxManage("snapshot", vmName, "take", snapshotName)
	return err
}

func RevertToSnapshot(vmName string) error {
	_, err := VBoxManage("snapshot", vmName, "restorecurrent")
	return err
}

func Start(vmName string) error {
	_, err := VBoxManage("startvm", vmName, "--type", "headless")
	return err
}

func Stop(vmName string) error {
	_, err := VBoxManage("controlvm", vmName, "poweroff")
	return err
}

func Kill(vmName string) error {
	_, err := VBoxManage("controlvm", vmName, "acpipowerbutton")
	return err
}

func Delete(vmName string) error {
	_, err := VBoxManage("unregistervm", vmName, "--delete")
	return err
}

func Status(vmName string) (StatusType, error) {
	output, err := VBoxManage("showvminfo", vmName, "--machinereadable")
	statusRe := regexp.MustCompile(`VMState="(\w+)"`)
	status := statusRe.FindStringSubmatch(output)
	if err != nil {
		return NotFound, err
	}
	return StatusType(status[1]), nil
}

func WaitForStatus(vmName string,  vmStatus StatusType, seconds int) error {
	var status StatusType
	var err error
	for i :=0; i < seconds; i++ {
		status, err = Status(vmName)
		if err != nil {
			return err
		}
		if status == vmStatus {
			return nil
		}
		time.Sleep(time.Second)
	}
	return errors.New("VM " + vmName + " is in " + string(status) + " where it should be in " + string(vmStatus))
}

func Unregister(vmName string) error {
	_, err := VBoxManage("unregistervm", vmName)
	return err
}
<commit_msg>added support for windows<commit_after>package vbox

import (
	"bytes"
	"os/exec"
	"strings"
	"fmt"
	"regexp"
	"time"
	"errors"
	"strconv"
	"runtime"

	"github.com/Sirupsen/logrus"

)

type StatusType string

const (
	NotFound  StatusType = "notfound"
	Invalid              = "invalid"
	Stopped              = "stopped"
	Suspended            = "suspended"
	Running              = "running"
	Saved                = "saved"
	// TODO: more statuses
)

func GetVboxPath() string {
	if runtime.GOOS == "windows" {
		return `c:\Program Files\Oracle\VirtualBox\VBoxManage.exe`
	} else {
		return `vboxmanage`
	}
}

func VboxManageOutput(exe string, args ...string) (string, error) {

	var stdout, stderr bytes.Buffer

	logrus.Debugf("Executing VBoxManageOutput: %#v", args)
	cmd := exec.Command(exe, args...)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()

	stderrString := strings.TrimSpace(stderr.String())

	if _, ok := err.(*exec.ExitError); ok {
		err = fmt.Errorf("VBoxManageOutput error: %s", stderrString)
	}

	return stdout.String(), err
}

func VBoxManage(args ...string) (string, error) {
	vboxPath := GetVboxPath()
	return VboxManageOutput(vboxPath, args...)
}

func Version() (string, error) {
	version, err := VBoxManage("--version")
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(version), nil
}

func FindSshPort(vmName string) (string, error) {
	info, err := VBoxManage("showvminfo", vmName)
	portRe := regexp.MustCompile(`guestssh.*host port = (\d+)`)
	sshPort := portRe.FindStringSubmatch(info)
	return sshPort[1], err
}

func Exist(vmName string) bool {
	_, err := VBoxManage("showvminfo", vmName)
	if err != nil{
		return false
	}
	return true
}

func CreateOsVM(vmName string, templateName string) error {
	_, err := VBoxManage("clonevm", vmName, "--mode", "machine", "--name", templateName, "--register")
	return err
}

func FindNextPort(highport string, usedPorts [][]string) string {
	for _, port := range usedPorts {
		if highport == port[1] {
			var temp int
			temp,_ = strconv.Atoi(highport)
			temp = temp + 1
			highport = strconv.Itoa(temp)
			highport = FindNextPort(highport, usedPorts)
		}
	}
	return highport
}

func ConfigureSSH(vmName string) error {
	var localport string
	output, err := VBoxManage("list", "vms", "-l")
	allPortsRe := regexp.MustCompile(`host port = (\d+)`)
	usedPorts := allPortsRe.FindAllStringSubmatch(output, -1)
	logrus.Debugln(usedPorts)
	if usedPorts == nil {
		localport = "2222"
	} else {
		highport := "2222"
		localport = FindNextPort(highport, usedPorts)
	}

	rule := fmt.Sprintf("guestssh,tcp,127.0.0.1,%s,,22", localport)
	_, err = VBoxManage("modifyvm", vmName, "--natpf1", rule)
	return err
}

func CreateSnapshot(vmName string, snapshotName string) error {
	_, err := VBoxManage("snapshot", vmName, "take", snapshotName)
	return err
}

func RevertToSnapshot(vmName string) error {
	_, err := VBoxManage("snapshot", vmName, "restorecurrent")
	return err
}

func Start(vmName string) error {
	_, err := VBoxManage("startvm", vmName, "--type", "headless")
	return err
}

func Stop(vmName string) error {
	_, err := VBoxManage("controlvm", vmName, "poweroff")
	return err
}

func Kill(vmName string) error {
	_, err := VBoxManage("controlvm", vmName, "acpipowerbutton")
	return err
}

func Delete(vmName string) error {
	_, err := VBoxManage("unregistervm", vmName, "--delete")
	return err
}

func Status(vmName string) (StatusType, error) {
	output, err := VBoxManage("showvminfo", vmName, "--machinereadable")
	statusRe := regexp.MustCompile(`VMState="(\w+)"`)
	status := statusRe.FindStringSubmatch(output)
	if err != nil {
		return NotFound, err
	}
	return StatusType(status[1]), nil
}

func WaitForStatus(vmName string,  vmStatus StatusType, seconds int) error {
	var status StatusType
	var err error
	for i :=0; i < seconds; i++ {
		status, err = Status(vmName)
		if err != nil {
			return err
		}
		if status == vmStatus {
			return nil
		}
		time.Sleep(time.Second)
	}
	return errors.New("VM " + vmName + " is in " + string(status) + " where it should be in " + string(vmStatus))
}

func Unregister(vmName string) error {
	_, err := VBoxManage("unregistervm", vmName)
	return err
}
<|endoftext|>


<commit_before>// Copyright 2015 - 2016 Square Inc.
//
// 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 filter

import (
	"math"
	"sort"
	"time"

	"github.com/square/metrics/api"
)

type filterList struct {
	index     []int
	value     []float64
	ascending bool
}

func (list filterList) Len() int {
	return len(list.index)
}
func (list filterList) Less(i, j int) bool {
	if math.IsNaN(list.value[j]) && !math.IsNaN(list.value[i]) {
		return true
	}
	if list.ascending {
		return list.value[i] < list.value[j]
	} else {
		return list.value[j] < list.value[i]
	}

}
func (list filterList) Swap(i, j int) {
	list.index[i], list.index[j] = list.index[j], list.index[i]
	list.value[i], list.value[j] = list.value[j], list.value[i]
}

func sortSeries(series []api.Timeseries, summary func([]float64) float64, lowest bool) ([]api.Timeseries, []float64) {
	array := filterList{
		index:     make([]int, len(series)),
		value:     make([]float64, len(series)),
		ascending: lowest,
	}
	for i := range array.index {
		array.index[i] = i
		array.value[i] = summary(series[i].Values)
	}
	sort.Sort(array)
	result := make([]api.Timeseries, len(series))
	weights := make([]float64, len(series))
	for i, index := range array.index {
		result[i] = series[index]
		weights[i] = array.value[index]
	}
	return result, weights
}

// FilterRecentBy reduces the number of things in the series `list` to at most the given `count`.
// However, it only considered recent points when evaluating their ordering.
func FilterByRecent(list api.SeriesList, count int, summary func([]float64) float64, lowest bool, duration time.Duration) api.SeriesList {
	slots := int(duration / list.Timerange.Resolution())
	if slots <= 0 {
		slots = 1
	}
	if slots > list.Timerange.Slots() {
		slots = list.Timerange.Slots()
	}
	sorted, _ := sortSeries(list.Series, func(values []float64) float64 {
		return summary(values[len(values)-slots:])
	}, lowest)

	if len(list.Series) < count {
		// Limit the count to the number of available series
		count = len(list.Series)
	}

	return api.SeriesList{
		Series:    sorted[:count],
		Timerange: list.Timerange,
	}
}

// FilterThresholdBy reduces the number of things in the series `list` to those whose `summar` is at at least/at most the threshold.
// However, it only considers the data points as recent as the duration permits.
func FilterThresholdByRecent(list api.SeriesList, threshold float64, summary func([]float64) float64, below bool, duration time.Duration) api.SeriesList {
	slots := int(duration / list.Timerange.Resolution())
	if slots > list.Timerange.Slots() {
		slots = list.Timerange.Slots()
	}
	sorted, values := sortSeries(list.Series, func(values []float64) float64 {
		return summary(values[len(values)-slots:])
	}, below)

	result := []api.Timeseries{}
	for i := range sorted {
		// Since the series are sorted, once one of them falls outside the threshold, we can stop.
		if (below && values[i] > threshold) || (!below && values[i] < threshold) {
			break
		}
		result = append(result, sorted[i])
	}

	return api.SeriesList{
		Series:    result,
		Timerange: list.Timerange,
	}
}
<commit_msg>simplify filtering functions<commit_after>// Copyright 2015 - 2016 Square Inc.
//
// 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 filter

import (
	"math"
	"sort"
	"time"

	"github.com/square/metrics/api"
)

type filterList struct {
	index     []int
	value     []float64
	ascending bool
}

func (list filterList) Len() int {
	return len(list.index)
}
func (list filterList) Less(i, j int) bool {
	if math.IsNaN(list.value[j]) && !math.IsNaN(list.value[i]) {
		return true
	}
	if list.ascending {
		return list.value[i] < list.value[j]
	} else {
		return list.value[j] < list.value[i]
	}

}
func (list filterList) Swap(i, j int) {
	list.index[i], list.index[j] = list.index[j], list.index[i]
	list.value[i], list.value[j] = list.value[j], list.value[i]
}

func sortSeries(series []api.Timeseries, summary func([]float64) float64, lowest bool) ([]api.Timeseries, []float64) {
	array := filterList{
		index:     make([]int, len(series)),
		value:     make([]float64, len(series)),
		ascending: lowest,
	}
	for i := range array.index {
		array.index[i] = i
		array.value[i] = summary(series[i].Values)
	}
	sort.Sort(array)
	result := make([]api.Timeseries, len(series))
	weights := make([]float64, len(series))
	for i, index := range array.index {
		result[i] = series[index]
		weights[i] = array.value[index]
	}
	return result, weights
}

func sortSeriesRecent(list api.SeriesList, summary func([]float64) float64, lowest bool, duration time.Duration) ([]api.Timeseries, []float64) {
	slots := int(duration/list.Timerange.Resolution()) + 1
	if slots < 1 {
		slots = 1
	}
	if slots > list.Timerange.Slots() {
		slots = list.Timerange.Slots()
	}
	return sortSeries(
		list.Series,
		func(values []float64) float64 {
			return summary(values[len(values)-slots:])
		},
		lowest,
	)
}

// FilterRecentBy reduces the number of things in the series `list` to at most the given `count`.
// However, it only considered recent points when evaluating their ordering.
func FilterByRecent(list api.SeriesList, count int, summary func([]float64) float64, lowest bool, duration time.Duration) api.SeriesList {
	// Sort them by their recent points.
	sorted, _ := sortSeriesRecent(list, summary, lowest, duration)

	if len(sorted) < count {
		// Limit the count to the number of available series
		count = len(sorted)
	}

	return api.SeriesList{
		Series:    sorted[:count],
		Timerange: list.Timerange,
	}
}

// FilterThresholdBy reduces the number of things in the series `list` to those whose `summar` is at at least/at most the threshold.
// However, it only considers the data points as recent as the duration permits.
func FilterThresholdByRecent(list api.SeriesList, threshold float64, summary func([]float64) float64, below bool, duration time.Duration) api.SeriesList {
	sorted, values := sortSeriesRecent(list, summary, below, duration)

	result := []api.Timeseries{}
	for i := range sorted {
		// Since the series are sorted, once one of them falls outside the threshold, we can stop.
		if (below && values[i] > threshold) || (!below && values[i] < threshold) {
			break
		}
		result = append(result, sorted[i])
	}

	return api.SeriesList{
		Series:    result,
		Timerange: list.Timerange,
	}
}
<|endoftext|>


<commit_before>package elkrem

import (
	"testing"

	"github.com/adiabat/btcd/chaincfg/chainhash"
)

// TestElkremBig tries 10K hashes
func TestElkremBig(t *testing.T) {
	sndr := NewElkremSender(chainhash.DoubleHashH([]byte("elktest")))
	var rcv ElkremReceiver
	//	SenderSerdesTest(t, sndr)
	for n := uint64(0); n < 10000; n++ {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		err = rcv.AddNext(sha)
		if err != nil {
			t.Fatal(err)
		}
		if n%1000 == 999 {
			t.Logf("stack with %d received hashes\n", n+1)
			for i, n := range rcv.s {
				t.Logf("Stack element %d: index %d height %d %s\n",
					i, n.i, n.h, n.sha.String())
			}
		}
	}
	//	SenderSerdesTest(t, sndr)
	ReceiverSerdesTest(t, &rcv)
	for n := uint64(0); n < 10000; n += 500 {
		sha, err := rcv.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("Retreived index %d %s\n", n, sha.String())
	}
}

// TestElkremLess tries 10K hashes
func TestElkremLess(t *testing.T) {
	sndr := NewElkremSender(chainhash.DoubleHashH([]byte("elktest2")))
	var rcv ElkremReceiver
	for n := uint64(0); n < 5000; n++ {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		err = rcv.AddNext(sha)
		if err != nil {
			t.Fatal(err)
		}
		if n%1000 == 999 {
			t.Logf("stack with %d received hashes\n", n+1)
			for i, n := range rcv.s {
				t.Logf("Stack element %d: index %d height %d %s\n",
					i, n.i, n.h, n.sha.String())
			}
		}
	}
	for n := uint64(0); n < 5000; n += 500 {
		sha, err := rcv.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("Retreived index %d %s\n",
			n, sha.String())
	}
}

func TestFixed(t *testing.T) {
	root, _ := chainhash.NewHashFromStr(
		"b43614f251760d689adf84211148a40d7dee13967b7109e13c8d1437a4966d58")

	sndr := NewElkremSender(*root)

	zero, _ := chainhash.NewHashFromStr(
		"2a124935e0713149b71ff17cb43465e9828bacd1e833f0dc08460783a6a42cb4")

	thousand, _ := chainhash.NewHashFromStr(
		"0151a39169940cdd8ccf1ba619f254ddbf16ce260a243528839b2634eaa63d0a")

	for n := uint64(0); n < 5000; n += 500 {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("elk %d: %s\n", n, sha.String())

		if n == 0 && !sha.IsEqual(zero) {
			t.Fatalf("Elk %d expected %s, got %s", n, zero.String(), sha.String())
		}
		if n == 1000 && !sha.IsEqual(thousand) {
			t.Fatalf("Elk %d expected %s, got %s", n, thousand.String(), sha.String())
		}

	}

}
<commit_msg>add more tests for elkrem<commit_after>package elkrem

import (
	"testing"

	"github.com/adiabat/btcd/chaincfg/chainhash"
)

// TestElkremBig tries 10K hashes
func TestElkremBig(t *testing.T) {
	sndr := NewElkremSender(chainhash.DoubleHashH([]byte("elktest")))
	var rcv ElkremReceiver
	//	SenderSerdesTest(t, sndr)
	for n := uint64(0); n < 10000; n++ {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		err = rcv.AddNext(sha)
		if err != nil {
			t.Fatal(err)
		}
		if n%1000 == 999 {
			t.Logf("stack with %d received hashes\n", n+1)
			for i, n := range rcv.s {
				t.Logf("Stack element %d: index %d height %d %s\n",
					i, n.i, n.h, n.sha.String())
			}
		}
	}
	//	SenderSerdesTest(t, sndr)
	ReceiverSerdesTest(t, &rcv)
	for n := uint64(0); n < 10000; n += 500 {
		sha, err := rcv.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("Retreived index %d %s\n", n, sha.String())
	}
}

// TestElkremLess tries 10K hashes
func TestElkremLess(t *testing.T) {
	sndr := NewElkremSender(chainhash.DoubleHashH([]byte("elktest2")))
	var rcv ElkremReceiver
	for n := uint64(0); n < 5000; n++ {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		err = rcv.AddNext(sha)
		if err != nil {
			t.Fatal(err)
		}
		if n%1000 == 999 {
			t.Logf("stack with %d received hashes\n", n+1)
			for i, n := range rcv.s {
				t.Logf("Stack element %d: index %d height %d %s\n",
					i, n.i, n.h, n.sha.String())
			}
		}
	}
	for n := uint64(0); n < 5000; n += 500 {
		sha, err := rcv.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("Retreived index %d %s\n",
			n, sha.String())
	}
}

// TestElkremIngestLeftFail puts a bed hash in such that the left child will fail
func TestElkremIngestLeftFail(t *testing.T) {
	sndr := NewElkremSender(chainhash.DoubleHashH([]byte("elkfailL")))
	var rcv ElkremReceiver
	for n := uint64(0); n < 31; n++ {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		err = rcv.AddNext(sha)
		if err != nil {
			t.Fatal(err)
		}
	}

	// This is correct but we can't check; anything will be accepted
	sha, err := sndr.AtIndex(31)
	if err != nil {
		t.Fatal(err)
	}
	// flip all the bits in the first byte
	sha[0] ^= 0xff
	err = rcv.AddNext(sha)
	if err != nil {
		t.Fatal(err)
	}
	// give the right thing here, but it's too late as 31 was wrong
	sha, err = sndr.AtIndex(32)
	if err != nil {
		t.Fatal(err)
	}

	err = rcv.AddNext(sha)
	if err != nil {
		t.Fatal(err)
	}
	sha, err = sndr.AtIndex(33)
	if err != nil {
		t.Fatal(err)
	}
	err = rcv.AddNext(sha)
	if err != nil {
		t.Fatalf("Should have a left child mismatch, but everything went OK!")
	}
}

// TestElkremIngestRightFail puts a bed hash in such that the left child will fail
func TestElkremIngestRightFail(t *testing.T) {
	sndr := NewElkremSender(chainhash.DoubleHashH([]byte("elkfailR")))
	var rcv ElkremReceiver
	for n := uint64(0); n < 31; n++ {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		err = rcv.AddNext(sha)
		if err != nil {
			t.Fatal(err)
		}
	}

	// This is correct but we can't check; anything will be accepted
	sha, err := sndr.AtIndex(31)
	if err != nil {
		t.Fatal(err)
	}
	err = rcv.AddNext(sha)
	if err != nil {
		t.Fatal(err)
	}
	sha, err = sndr.AtIndex(32)
	if err != nil {
		t.Fatal(err)
	}
	// flip all the bits in the first byte
	sha[0] ^= 0xff
	err = rcv.AddNext(sha)
	if err != nil {
		t.Fatal(err)
	}
	sha, err = sndr.AtIndex(33)
	if err != nil {
		t.Fatal(err)
	}
	err = rcv.AddNext(sha)
	if err == nil {
		t.Fatalf("Should have a right child mismatch, but everything went OK!")
	}
}

func TestFixed(t *testing.T) {
	root, _ := chainhash.NewHashFromStr(
		"b43614f251760d689adf84211148a40d7dee13967b7109e13c8d1437a4966d58")

	sndr := NewElkremSender(*root)

	zero, _ := chainhash.NewHashFromStr(
		"2a124935e0713149b71ff17cb43465e9828bacd1e833f0dc08460783a6a42cb4")

	thousand, _ := chainhash.NewHashFromStr(
		"0151a39169940cdd8ccf1ba619f254ddbf16ce260a243528839b2634eaa63d0a")

	for n := uint64(0); n < 5000; n += 500 {
		sha, err := sndr.AtIndex(n)
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("elk %d: %s\n", n, sha.String())

		if n == 0 && !sha.IsEqual(zero) {
			t.Fatalf("Elk %d expected %s, got %s", n, zero.String(), sha.String())
		}
		if n == 1000 && !sha.IsEqual(thousand) {
			t.Fatalf("Elk %d expected %s, got %s", n, thousand.String(), sha.String())
		}

	}

}
<|endoftext|>


<commit_before>package main_test

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/cloudfoundry-incubator/runtime-schema/bbs"
	"github.com/cloudfoundry-incubator/runtime-schema/models"
	"github.com/cloudfoundry/gunk/timeprovider"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-golang/lager/lagertest"
)

var _ = Describe("Veritas", func() {
	var (
		store  *bbs.BBS
		tmpDir string
		err    error
	)

	BeforeEach(func() {
		store = bbs.NewBBS(etcdRunner.Adapter(), timeprovider.NewTimeProvider(), lagertest.NewTestLogger("veritas"))

		err = store.DesireTask(models.Task{
			Guid:  "Task-Guid",
			Stack: "pancakes",
			Actions: []models.ExecutorAction{
				{models.RunAction{Path: "foo"}},
			},
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.DesireLRP(models.DesiredLRP{
			ProcessGuid: "Desired-Process-Guid",
			Stack:       "pancakes",
			Actions: []models.ExecutorAction{
				{models.RunAction{Path: "foo"}},
			},
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.ReportActualLRPAsRunning(models.ActualLRP{
			ProcessGuid:  "Actual-Process-Guid",
			InstanceGuid: "Instance-Guid",
			Index:        0,
		}, "Executor-ID")
		Ω(err).ShouldNot(HaveOccurred())

		err = store.ReportActualLRPAsRunning(models.ActualLRP{
			ProcessGuid:  "Actual-Process-Guid",
			InstanceGuid: "Instance-Guid-200",
			Index:        200,
		}, "Executor-ID")
		Ω(err).ShouldNot(HaveOccurred())

		err = store.RequestLRPStartAuction(models.LRPStartAuction{
			InstanceGuid: "InstanceGuid",
			DesiredLRP: models.DesiredLRP{
				ProcessGuid: "StartAuction-Process-Guid",
			},
			Index: 1,
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.RequestLRPStopAuction(models.LRPStopAuction{
			ProcessGuid: "StopAuction-Process-Guid",
			Index:       2,
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.RequestStopLRPInstance(models.StopLRPInstance{
			ProcessGuid:  "StopLRP-Process-Guid",
			Index:        3,
			InstanceGuid: "Instance-Guid",
		})
		Ω(err).ShouldNot(HaveOccurred())

		tmpDir, err = ioutil.TempDir("", "veritas")
		Ω(err).ShouldNot(HaveOccurred())
	})

	AfterEach(func() {
		os.RemoveAll(tmpDir)
	})

	It("should be able to print out the contents of the bbs", func() {
		dumpFile := filepath.Join(tmpDir, "dump")

		session, err := gexec.Start(exec.Command(veritas, "fetch-store", "-etcdCluster="+strings.Join(etcdRunner.NodeURLS(), ","), dumpFile), GinkgoWriter, GinkgoWriter)
		Ω(err).ShouldNot(HaveOccurred())

		Eventually(session).Should(gexec.Exit(0))

		dump, err := ioutil.ReadFile(dumpFile)
		Ω(err).ShouldNot(HaveOccurred())

		Ω(dump).Should(ContainSubstring("Desired-Process-Guid"))
		Ω(dump).Should(ContainSubstring("Actual-Process-Guid"))
		Ω(dump).Should(ContainSubstring("StartAuction-Process-Guid"))
		Ω(dump).Should(ContainSubstring("StopAuction-Process-Guid"))
		Ω(dump).Should(ContainSubstring("StopLRP-Process-Guid"))

		session, err = gexec.Start(exec.Command(veritas, "print-store", dumpFile), GinkgoWriter, GinkgoWriter)
		Eventually(session).Should(gexec.Exit(0))

		Ω(session.Out.Contents()).Should(ContainSubstring("Desired-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("Actual-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("StartAuction-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("StopAuction-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("StopLRP-Process-Guid"))
	})
})
<commit_msg>fix tests<commit_after>package main_test

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/cloudfoundry-incubator/runtime-schema/bbs"
	"github.com/cloudfoundry-incubator/runtime-schema/models"
	"github.com/cloudfoundry/gunk/timeprovider"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-golang/lager/lagertest"
)

var _ = Describe("Veritas", func() {
	var (
		store  *bbs.BBS
		tmpDir string
		err    error
	)

	BeforeEach(func() {
		store = bbs.NewBBS(etcdRunner.Adapter(), timeprovider.NewTimeProvider(), lagertest.NewTestLogger("veritas"))

		err = store.DesireTask(models.Task{
			Guid:   "Task-Guid",
			Stack:  "pancakes",
			Domain: "veritas",
			Actions: []models.ExecutorAction{
				{models.RunAction{Path: "foo"}},
			},
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.DesireLRP(models.DesiredLRP{
			ProcessGuid: "Desired-Process-Guid",
			Stack:       "pancakes",
			Domain:      "veritas",
			Actions: []models.ExecutorAction{
				{models.RunAction{Path: "foo"}},
			},
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.ReportActualLRPAsRunning(models.ActualLRP{
			ProcessGuid:  "Actual-Process-Guid",
			InstanceGuid: "Instance-Guid",
			Index:        0,
		}, "Executor-ID")
		Ω(err).ShouldNot(HaveOccurred())

		err = store.ReportActualLRPAsRunning(models.ActualLRP{
			ProcessGuid:  "Actual-Process-Guid",
			InstanceGuid: "Instance-Guid-200",
			Index:        200,
		}, "Executor-ID")
		Ω(err).ShouldNot(HaveOccurred())

		err = store.RequestLRPStartAuction(models.LRPStartAuction{
			InstanceGuid: "InstanceGuid",
			DesiredLRP: models.DesiredLRP{
				ProcessGuid: "StartAuction-Process-Guid",
			},
			Index: 1,
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.RequestLRPStopAuction(models.LRPStopAuction{
			ProcessGuid: "StopAuction-Process-Guid",
			Index:       2,
		})
		Ω(err).ShouldNot(HaveOccurred())

		err = store.RequestStopLRPInstance(models.StopLRPInstance{
			ProcessGuid:  "StopLRP-Process-Guid",
			Index:        3,
			InstanceGuid: "Instance-Guid",
		})
		Ω(err).ShouldNot(HaveOccurred())

		tmpDir, err = ioutil.TempDir("", "veritas")
		Ω(err).ShouldNot(HaveOccurred())
	})

	AfterEach(func() {
		os.RemoveAll(tmpDir)
	})

	It("should be able to print out the contents of the bbs", func() {
		dumpFile := filepath.Join(tmpDir, "dump")

		session, err := gexec.Start(exec.Command(veritas, "fetch-store", "-etcdCluster="+strings.Join(etcdRunner.NodeURLS(), ","), dumpFile), GinkgoWriter, GinkgoWriter)
		Ω(err).ShouldNot(HaveOccurred())

		Eventually(session).Should(gexec.Exit(0))

		dump, err := ioutil.ReadFile(dumpFile)
		Ω(err).ShouldNot(HaveOccurred())

		Ω(dump).Should(ContainSubstring("Desired-Process-Guid"))
		Ω(dump).Should(ContainSubstring("Actual-Process-Guid"))
		Ω(dump).Should(ContainSubstring("StartAuction-Process-Guid"))
		Ω(dump).Should(ContainSubstring("StopAuction-Process-Guid"))
		Ω(dump).Should(ContainSubstring("StopLRP-Process-Guid"))

		session, err = gexec.Start(exec.Command(veritas, "print-store", dumpFile), GinkgoWriter, GinkgoWriter)
		Eventually(session).Should(gexec.Exit(0))

		Ω(session.Out.Contents()).Should(ContainSubstring("Desired-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("Actual-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("StartAuction-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("StopAuction-Process-Guid"))
		Ω(session.Out.Contents()).Should(ContainSubstring("StopLRP-Process-Guid"))
	})
})
<|endoftext|>


<commit_before>package lfs

import (
	"fmt"
	"path/filepath"
	"sync"
	"sync/atomic"

	"github.com/github/git-lfs/git"
	"github.com/github/git-lfs/vendor/_nuts/github.com/cheggaaa/pb"
	"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
)

type Transferable interface {
	Check() (*objectResource, *WrappedError)
	Transfer(CopyCallback) *WrappedError
	Object() *objectResource
	Oid() string
	Size() int64
	SetObject(*objectResource)
}

// TransferQueue provides a queue that will allow concurrent transfers.
type TransferQueue struct {
	transferc        chan Transferable
	errorc           chan *WrappedError
	watchers         []chan string
	errors           []*WrappedError
	wg               sync.WaitGroup
	workers          int
	files            int
	finished         int64
	size             int64
	authCond         *sync.Cond
	transferables    map[string]Transferable
	bar              *pb.ProgressBar
	clientAuthorized int32
	transferKind     string
}

// newTransferQueue builds a TransferQueue, allowing `workers` concurrent transfers.
func newTransferQueue(workers, files int) *TransferQueue {
	return &TransferQueue{
		transferc:     make(chan Transferable, files),
		errorc:        make(chan *WrappedError),
		watchers:      make([]chan string, 0),
		workers:       workers,
		files:         files,
		authCond:      sync.NewCond(&sync.Mutex{}),
		transferables: make(map[string]Transferable),
	}
}

// Add adds a Transferable to the transfer queue.
func (q *TransferQueue) Add(t Transferable) {
	q.transferables[t.Oid()] = t
}

// Watch returns a channel where the queue will write the OID of each transfer
// as it completes. The channel will be closed when the queue finishes processing.
func (q *TransferQueue) Watch() chan string {
	c := make(chan string, q.files)
	q.watchers = append(q.watchers, c)
	return c
}

// processIndividual processes the queue of transfers one at a time by making
// a POST call for each object, feeding the results to the transfer workers.
// If configured, the object transfers can still happen concurrently, the
// sequential nature here is only for the meta POST calls.
func (q *TransferQueue) processIndividual() {
	apic := make(chan Transferable, q.files)
	workersReady := make(chan int, q.workers)
	var wg sync.WaitGroup

	for i := 0; i < q.workers; i++ {
		go func() {
			workersReady <- 1
			for t := range apic {
				// If an API authorization has not occured, we wait until we're woken up.
				q.authCond.L.Lock()
				if atomic.LoadInt32(&q.clientAuthorized) == 0 {
					q.authCond.Wait()
				}
				q.authCond.L.Unlock()

				obj, err := t.Check()
				if err != nil {
					q.errorc <- err
					wg.Done()
					continue
				}
				if obj != nil {
					q.wg.Add(1)
					t.SetObject(obj)
					q.transferc <- t
				}
				wg.Done()
			}
		}()
	}

	q.bar.Prefix(fmt.Sprintf("(%d of %d files) ", q.finished, len(q.transferables)))
	q.bar.Start()

	for _, t := range q.transferables {
		wg.Add(1)
		apic <- t
	}

	<-workersReady
	q.authCond.Signal() // Signal the first goroutine to run
	close(apic)
	wg.Wait()

	close(q.transferc)
}

// processBatch processes the queue of transfers using the batch endpoint,
// making only one POST call for all objects. The results are then handed
// off to the transfer workers.
func (q *TransferQueue) processBatch() error {
	transfers := make([]*objectResource, 0, len(q.transferables))
	for _, t := range q.transferables {
		transfers = append(transfers, &objectResource{Oid: t.Oid(), Size: t.Size()})
	}

	objects, err := Batch(transfers)
	if err != nil {
		if isNotImplError(err) {
			tracerx.Printf("queue: batch not implemented, disabling")
			configFile := filepath.Join(LocalGitDir, "config")
			git.Config.SetLocal(configFile, "lfs.batch", "false")
		}

		return err
	}

	q.files = 0

	for _, o := range objects {
		if _, ok := o.Links[q.transferKind]; ok {
			// This object needs to be transfered
			if transfer, ok := q.transferables[o.Oid]; ok {
				q.files++
				q.wg.Add(1)
				transfer.SetObject(o)
				q.transferc <- transfer
			}
		}
	}

	close(q.transferc)
	q.bar.Prefix(fmt.Sprintf("(%d of %d files) ", q.finished, q.files))
	q.bar.Start()
	sendApiEvent(apiEventSuccess) // Wake up transfer workers
	return nil
}

// Process starts the transfer queue and displays a progress bar. Process will
// do individual or batch transfers depending on the Config.BatchTransfer() value.
// Process will transfer files sequentially or concurrently depending on the
// Concig.ConcurrentTransfers() value.
func (q *TransferQueue) Process() {
	q.bar = pb.New64(q.size)
	q.bar.SetUnits(pb.U_BYTES)
	q.bar.ShowBar = false

	// This goroutine collects errors returned from transfers
	go func() {
		for err := range q.errorc {
			q.errors = append(q.errors, err)
		}
	}()

	// This goroutine watches for apiEvents. In order to prevent multiple
	// credential requests from happening, the queue is processed sequentially
	// until an API request succeeds (meaning authenication has happened successfully).
	// Once the an API request succeeds, all worker goroutines are woken up and allowed
	// to process transfers. Once a success happens, this goroutine exits.
	go func() {
		for {
			event := <-apiEvent
			switch event {
			case apiEventSuccess:
				atomic.StoreInt32(&q.clientAuthorized, 1)
				q.authCond.Broadcast() // Wake all remaining goroutines
				return
			case apiEventFail:
				q.authCond.Signal() // Wake the next goroutine
			}
		}
	}()

	for i := 0; i < q.workers; i++ {
		// These are the worker goroutines that process transfers
		go func() {
			for transfer := range q.transferc {
				cb := func(total, read int64, current int) error {
					q.bar.Add(current)
					return nil
				}

				if err := transfer.Transfer(cb); err != nil {
					q.errorc <- err
				} else {
					oid := transfer.Oid()
					for _, c := range q.watchers {
						c <- oid
					}
				}

				f := atomic.AddInt64(&q.finished, 1)
				q.bar.Prefix(fmt.Sprintf("(%d of %d files) ", f, q.files))
				q.wg.Done()
			}
		}()
	}

	if Config.BatchTransfer() {
		if err := q.processBatch(); err != nil {
			q.processIndividual()
		}
	} else {
		q.processIndividual()
	}

	q.wg.Wait()
	close(q.errorc)
	for _, watcher := range q.watchers {
		close(watcher)
	}

	q.bar.Finish()
}

// Errors returns any errors encountered during transfer.
func (q *TransferQueue) Errors() []*WrappedError {
	return q.errors
}
<commit_msg>ラララララ ラー ララララー<commit_after>package lfs

import (
	"fmt"
	"path/filepath"
	"sync"
	"sync/atomic"

	"github.com/github/git-lfs/git"
	"github.com/github/git-lfs/vendor/_nuts/github.com/cheggaaa/pb"
	"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
)

type Transferable interface {
	Check() (*objectResource, *WrappedError)
	Transfer(CopyCallback) *WrappedError
	Object() *objectResource
	Oid() string
	Size() int64
	SetObject(*objectResource)
}

// TransferQueue provides a queue that will allow concurrent transfers.
type TransferQueue struct {
	transferc        chan Transferable
	errorc           chan *WrappedError
	watchers         []chan string
	errors           []*WrappedError
	wg               sync.WaitGroup
	workers          int
	files            int
	finished         int64
	size             int64
	authCond         *sync.Cond
	transferables    map[string]Transferable
	bar              *pb.ProgressBar
	clientAuthorized int32
	transferKind     string
}

// newTransferQueue builds a TransferQueue, allowing `workers` concurrent transfers.
func newTransferQueue(workers, files int) *TransferQueue {
	return &TransferQueue{
		transferc:     make(chan Transferable, files),
		errorc:        make(chan *WrappedError),
		watchers:      make([]chan string, 0),
		workers:       workers,
		files:         files,
		authCond:      sync.NewCond(&sync.Mutex{}),
		transferables: make(map[string]Transferable),
	}
}

// Add adds a Transferable to the transfer queue.
func (q *TransferQueue) Add(t Transferable) {
	q.transferables[t.Oid()] = t
}

// Watch returns a channel where the queue will write the OID of each transfer
// as it completes. The channel will be closed when the queue finishes processing.
func (q *TransferQueue) Watch() chan string {
	c := make(chan string, q.files)
	q.watchers = append(q.watchers, c)
	return c
}

// processIndividual processes the queue of transfers one at a time by making
// a POST call for each object, feeding the results to the transfer workers.
// If configured, the object transfers can still happen concurrently, the
// sequential nature here is only for the meta POST calls.
func (q *TransferQueue) processIndividual() {
	apic := make(chan Transferable, q.files)
	workersReady := make(chan int, q.workers)
	var wg sync.WaitGroup

	for i := 0; i < q.workers; i++ {
		go func() {
			for t := range apic {
				// If an API authorization has not occured, we wait until we're woken up.
				q.authCond.L.Lock()
				if atomic.LoadInt32(&q.clientAuthorized) == 0 {
					workersReady <- 1
					q.authCond.Wait()
				}
				q.authCond.L.Unlock()

				obj, err := t.Check()
				if err != nil {
					q.errorc <- err
					wg.Done()
					continue
				}
				if obj != nil {
					q.wg.Add(1)
					t.SetObject(obj)
					q.transferc <- t
				}
				wg.Done()
			}
		}()
	}

	q.bar.Prefix(fmt.Sprintf("(%d of %d files) ", q.finished, len(q.transferables)))
	q.bar.Start()

	for _, t := range q.transferables {
		wg.Add(1)
		apic <- t
	}

	go func() {
		wg.Wait()
		close(workersReady)
	}()

	<-workersReady
	q.authCond.L.Lock()
	q.authCond.Signal() // Signal the first goroutine to run
	q.authCond.L.Unlock()

	close(apic)
	for _ = range workersReady {
	}

	close(q.transferc)
}

// processBatch processes the queue of transfers using the batch endpoint,
// making only one POST call for all objects. The results are then handed
// off to the transfer workers.
func (q *TransferQueue) processBatch() error {
	transfers := make([]*objectResource, 0, len(q.transferables))
	for _, t := range q.transferables {
		transfers = append(transfers, &objectResource{Oid: t.Oid(), Size: t.Size()})
	}

	objects, err := Batch(transfers)
	if err != nil {
		if isNotImplError(err) {
			tracerx.Printf("queue: batch not implemented, disabling")
			configFile := filepath.Join(LocalGitDir, "config")
			git.Config.SetLocal(configFile, "lfs.batch", "false")
		}

		return err
	}

	q.files = 0

	for _, o := range objects {
		if _, ok := o.Links[q.transferKind]; ok {
			// This object needs to be transfered
			if transfer, ok := q.transferables[o.Oid]; ok {
				q.files++
				q.wg.Add(1)
				transfer.SetObject(o)
				q.transferc <- transfer
			}
		}
	}

	close(q.transferc)
	q.bar.Prefix(fmt.Sprintf("(%d of %d files) ", q.finished, q.files))
	q.bar.Start()
	sendApiEvent(apiEventSuccess) // Wake up transfer workers
	return nil
}

// Process starts the transfer queue and displays a progress bar. Process will
// do individual or batch transfers depending on the Config.BatchTransfer() value.
// Process will transfer files sequentially or concurrently depending on the
// Concig.ConcurrentTransfers() value.
func (q *TransferQueue) Process() {
	q.bar = pb.New64(q.size)
	q.bar.SetUnits(pb.U_BYTES)
	q.bar.ShowBar = false

	// This goroutine collects errors returned from transfers
	go func() {
		for err := range q.errorc {
			q.errors = append(q.errors, err)
		}
	}()

	// This goroutine watches for apiEvents. In order to prevent multiple
	// credential requests from happening, the queue is processed sequentially
	// until an API request succeeds (meaning authenication has happened successfully).
	// Once the an API request succeeds, all worker goroutines are woken up and allowed
	// to process transfers. Once a success happens, this goroutine exits.
	go func() {
		for {
			event := <-apiEvent
			switch event {
			case apiEventSuccess:
				atomic.StoreInt32(&q.clientAuthorized, 1)
				q.authCond.Broadcast() // Wake all remaining goroutines
				return
			case apiEventFail:
				q.authCond.Signal() // Wake the next goroutine
			}
		}
	}()

	for i := 0; i < q.workers; i++ {
		// These are the worker goroutines that process transfers
		go func() {
			for transfer := range q.transferc {
				cb := func(total, read int64, current int) error {
					q.bar.Add(current)
					return nil
				}

				if err := transfer.Transfer(cb); err != nil {
					q.errorc <- err
				} else {
					oid := transfer.Oid()
					for _, c := range q.watchers {
						c <- oid
					}
				}

				f := atomic.AddInt64(&q.finished, 1)
				q.bar.Prefix(fmt.Sprintf("(%d of %d files) ", f, q.files))
				q.wg.Done()
			}
		}()
	}

	if Config.BatchTransfer() {
		if err := q.processBatch(); err != nil {
			q.processIndividual()
		}
	} else {
		q.processIndividual()
	}

	q.wg.Wait()
	close(q.errorc)
	for _, watcher := range q.watchers {
		close(watcher)
	}

	q.bar.Finish()
}

// Errors returns any errors encountered during transfer.
func (q *TransferQueue) Errors() []*WrappedError {
	return q.errors
}
<|endoftext|>


<commit_before>package main

import (
	"../../go-baudio"
	"math"
)

func main() {
	//n := float64(0)
	b := baudio.New(nil)
	b.AddChannel(8, func(t float64, i int) float64 {
		return float64((i & 0x71) * int(math.Floor(float64(i/1000))))
	})
	b.Play(nil)
	//b.Record("./sine.wav", nil)
}
<commit_msg>fix comments.<commit_after>package main

import (
	"../../go-baudio"
	"math"
)

func main() {
	b := baudio.New(nil)
	b.AddChannel(8, func(t float64, i int) float64 {
		return float64((i & 0x71) * int(math.Floor(float64(i/1000))))
	})
	b.Play(nil)
	//b.Record("./mix.wav", nil)
}
<|endoftext|>


<commit_before>// Copyright 2012 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import "github.com/cpmech/gosl/plt"

func main() {
	Nf := []float64{5, 7, 10, 13, 15, 20}
	Eave := []float64{3.5998e-12, 2.9629e-10, 6.0300e-8, 3.3686e-6, 2.5914e-5, 1.1966e-3}
	plt.SetForEps(0.75, 200)
	plt.Plot(Nf, Eave, "'b-', marker='.', clip_on=0")
	plt.SetYlog()
	plt.Gll("$N_f$", "$E_{ave}$", "")
	plt.SaveD("/tmp/goga", "multierror.eps")
}
<commit_msg>Fix code to plot multi-obj errors<commit_after>// Copyright 2012 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import "github.com/cpmech/gosl/plt"

func main() {
	Nf := []float64{5, 7, 10, 13, 15, 20}
	Eave := []float64{2.33e-12, 2.39e-10, 5.76e-8, 2.39e-6, 2.58e-5, 1.12e-3}
	plt.Reset(true, &plt.A{Eps: true, Prop: 0.75, WidthPt: 220})
	plt.HideBorders(&plt.A{HideR: true, HideT: true})
	plt.Plot(Nf, Eave, &plt.A{C: "r", M: ".", Lw: 1.2, NoClip: true})
	plt.SetYlog()
	plt.Gll("$N_f$", "$E_{ave}$", nil)
	plt.Save("/tmp/goga", "multierror")
}
<|endoftext|>


<commit_before>package command

import (
	"github.com/jroimartin/gocui"
	"github.com/mephux/komanda-cli/komanda/client"
	"github.com/mephux/komanda-cli/komanda/ui"
)

// JoinCmd strcut
type JoinCmd struct {
	*MetadataTmpl
}

// Metadata for join command
func (e *JoinCmd) Metadata() CommandMetadata {
	return e
}

// Exec join command
func (e *JoinCmd) Exec(args []string) error {
	Server.Exec(client.StatusChannel, func(c *client.Channel, g *gocui.Gui, v *gocui.View, s *client.Server) error {

		if !s.Client.Connected() {
			client.StatusMessage(v, "Not connected")
			return nil
		}

		if len(args) >= 2 && len(args[1]) > 0 {

			if channel, _, has := Server.HasChannel(args[1]); has {
				CurrentChannel = args[1]
				s.CurrentChannel = args[1]

				Server.Gui.SetViewOnTop(Server.CurrentChannel)

				if _, err := g.SetCurrentView(channel.Name); err != nil {
					return err
				}

				channel.Unread = false
				channel.Highlight = false

				if _, err := g.SetCurrentView("input"); err != nil {
					return err
				}

				ui.UpdateMenuView(g)

				return nil
			} else {
				s.Client.Join(args[1])
				CurrentChannel = args[1]
				s.CurrentChannel = args[1]

				return s.NewChannel(args[1], false)
			}
		}

		return nil
	})

	return nil
}

func joinCmd() Command {
	return &JoinCmd{
		MetadataTmpl: &MetadataTmpl{
			name: "join",
			args: "<channel>",
			aliases: []string{
				"j",
			},
			description: "join irc channel",
		},
	}
}
<commit_msg>format fix<commit_after>package command

import (
	"github.com/jroimartin/gocui"
	"github.com/mephux/komanda-cli/komanda/client"
	"github.com/mephux/komanda-cli/komanda/ui"
)

// JoinCmd strcut
type JoinCmd struct {
	*MetadataTmpl
}

// Metadata for join command
func (e *JoinCmd) Metadata() CommandMetadata {
	return e
}

// Exec join command
func (e *JoinCmd) Exec(args []string) error {
	Server.Exec(client.StatusChannel, func(c *client.Channel, g *gocui.Gui, v *gocui.View, s *client.Server) error {

		if !s.Client.Connected() {
			client.StatusMessage(v, "Not connected")
			return nil
		}

		if len(args) >= 2 && len(args[1]) > 0 {

			if channel, _, has := Server.HasChannel(args[1]); has {
				CurrentChannel = args[1]
				s.CurrentChannel = args[1]

				Server.Gui.SetViewOnTop(Server.CurrentChannel)

				if _, err := g.SetCurrentView(channel.Name); err != nil {
					return err
				}

				channel.Unread = false
				channel.Highlight = false

				if _, err := g.SetCurrentView("input"); err != nil {
					return err
				}

				ui.UpdateMenuView(g)

				return nil
			}

			s.Client.Join(args[1])
			CurrentChannel = args[1]
			s.CurrentChannel = args[1]

			return s.NewChannel(args[1], false)
		}

		return nil
	})

	return nil
}

func joinCmd() Command {
	return &JoinCmd{
		MetadataTmpl: &MetadataTmpl{
			name: "join",
			args: "<channel>",
			aliases: []string{
				"j",
			},
			description: "join irc channel",
		},
	}
}
<|endoftext|>


<commit_before>package guard

import (
	"net/url"
	"reflect"

	"github.com/Nivl/go-rest-tools/router/params"
	"github.com/Nivl/go-rest-tools/security/auth"
)

// Guard represents a security access system for routes
type Guard struct {
	// ParamStruct is an instance of a struct that describes the http params
	// accepted by an endpoint
	ParamStruct interface{}

	// Auth is used to add a auth middleware
	Auth RouteAuth
}

// ParseParams parses and returns the list of params needed
// Returns an error if a required param is missing, or if a type is wrong
func (g *Guard) ParseParams(sources map[string]url.Values) (interface{}, error) {
	// It's ok not to have a guard provided, as well as not having params
	if g == nil || g.ParamStruct == nil {
		return nil, nil
	}

	// We give p the same type as g.ParamStruct
	p := reflect.New(reflect.TypeOf(g.ParamStruct).Elem()).Interface()
	err := params.NewParams(p).Parse(sources)
	if err != nil {
		return nil, err
	}
	return p, nil
}

// HasAccess check if a given user has access to the
func (g *Guard) HasAccess(u *auth.User) (bool, error) {
	// It's ok not to have a guard provided, as well as not having an auth check
	if g == nil || g.Auth == nil {
		return true, nil
	}

	err := g.Auth(u)
	return err == nil, err
}
<commit_msg>refactor: Make Guard.HasAccess return an httperr.Error<commit_after>package guard

import (
	"net/url"
	"reflect"

	"github.com/Nivl/go-rest-tools/network/http/httperr"
	"github.com/Nivl/go-rest-tools/router/params"
	"github.com/Nivl/go-rest-tools/security/auth"
)

// Guard represents a security access system for routes
type Guard struct {
	// ParamStruct is an instance of a struct that describes the http params
	// accepted by an endpoint
	ParamStruct interface{}

	// Auth is used to add a auth middleware
	Auth RouteAuth
}

// ParseParams parses and returns the list of params needed
// Returns an error if a required param is missing, or if a type is wrong
func (g *Guard) ParseParams(sources map[string]url.Values) (interface{}, error) {
	// It's ok not to have a guard provided, as well as not having params
	if g == nil || g.ParamStruct == nil {
		return nil, nil
	}

	// We give p the same type as g.ParamStruct
	p := reflect.New(reflect.TypeOf(g.ParamStruct).Elem()).Interface()
	err := params.NewParams(p).Parse(sources)
	if err != nil {
		return nil, err
	}
	return p, nil
}

// HasAccess check if a given user has access to the
func (g *Guard) HasAccess(u *auth.User) (bool, httperr.Error) {
	// It's ok not to have a guard provided, as well as not having an auth check
	if g == nil || g.Auth == nil {
		return true, nil
	}

	err := g.Auth(u)
	return err == nil, err
}
<|endoftext|>