<commit_before>package command

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"

	"github.com/Sirupsen/logrus"
	"github.com/jessevdk/go-flags"
	"github.com/mitchellh/cli"
	"github.com/nerdalize/nerd/nerd/aws"
	"github.com/pkg/errors"
)

const (
	//DatasetFilename is the filename of the file that contains the dataset ID in the data folder.
	DatasetFilename = ".dataset"
	//DatasetPermissions are the permissions for DatasetFilename
	DatasetPermissions = 0644
)

//UploadOpts describes command options
type UploadOpts struct {
	NerdOpts
}

//Upload command
type Upload struct {
	*command

	opts   *UploadOpts
	parser *flags.Parser
}

//UploadFactory returns a factory method for the join command
func UploadFactory() func() (cmd cli.Command, err error) {
	cmd := &Upload{
		command: &command{
			help:     "",
			synopsis: "Upload a dataset to the cloud storage.",
			parser:   flags.NewNamedParser("nerd upload <path>", flags.Default),
			ui: &cli.BasicUi{
				Reader: os.Stdin,
				Writer: os.Stderr,
			},
		},

		opts: &UploadOpts{},
	}

	cmd.runFunc = cmd.DoRun
	_, err := cmd.command.parser.AddGroup("options", "options", cmd.opts)
	if err != nil {
		panic(err)
	}

	return func() (cli.Command, error) {
		return cmd, nil
	}
}

//DoRun is called by run and allows an error to be returned
func (cmd *Upload) DoRun(args []string) (err error) {
	if len(args) < 1 {
		return fmt.Errorf("not enough arguments, see --help")
	}

	dataPath := args[0]

	nerdclient, err := NewClient(cmd.ui)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}
	ds, err := nerdclient.CreateDataset()
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	client, err := aws.NewDataClient(&aws.DataClientConfig{
		Credentials: aws.NewNerdalizeCredentials(nerdclient),
		Bucket:      ds.Bucket,
	})
	if err != nil {
		HandleError(errors.Wrap(err, "could not create data client"), cmd.opts.VerboseOutput)
	}

	fi, err := os.Stat(dataPath)
	if err != nil {
		HandleError(errors.Errorf("argument '%v' is not a valid file or directory", dataPath), cmd.opts.VerboseOutput)
	} else if !fi.IsDir() {
		HandleError(errors.Errorf("provided path '%s' is not a directory", dataPath), cmd.opts.VerboseOutput)
	}

	err = ioutil.WriteFile(path.Join(dataPath, DatasetFilename), []byte(ds.DatasetID), DatasetPermissions)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	err = client.UploadDir(dataPath, ds.Root, &stdoutkw{}, 64)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	logrus.Infof("Uploaded data to dataset with ID: %v", ds.DatasetID)

	return nil
}
<commit_msg>Add chunked upload synopsis<commit_after>package command

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"

	"github.com/Sirupsen/logrus"
	"github.com/jessevdk/go-flags"
	"github.com/mitchellh/cli"
	"github.com/nerdalize/nerd/nerd/aws"
	"github.com/pkg/errors"
)

const (
	//DatasetFilename is the filename of the file that contains the dataset ID in the data folder.
	DatasetFilename = ".dataset"
	//DatasetPermissions are the permissions for DatasetFilename
	DatasetPermissions = 0644
)

//UploadOpts describes command options
type UploadOpts struct {
	NerdOpts
}

//Upload command
type Upload struct {
	*command

	opts   *UploadOpts
	parser *flags.Parser
}

//UploadFactory returns a factory method for the join command
func UploadFactory() func() (cmd cli.Command, err error) {
	cmd := &Upload{
		command: &command{
			help:     "",
			synopsis: "Upload a dataset to cloud storage.\nOptionally you can specify a dataset-ID to append files to that dataset. This is also useful to continue an upload in case a previous try failed.",
			parser:   flags.NewNamedParser("nerd upload <path> [dataset-ID]", flags.Default),
			ui: &cli.BasicUi{
				Reader: os.Stdin,
				Writer: os.Stderr,
			},
		},

		opts: &UploadOpts{},
	}

	cmd.runFunc = cmd.DoRun
	_, err := cmd.command.parser.AddGroup("options", "options", cmd.opts)
	if err != nil {
		panic(err)
	}

	return func() (cli.Command, error) {
		return cmd, nil
	}
}

//DoRun is called by run and allows an error to be returned
func (cmd *Upload) DoRun(args []string) (err error) {
	if len(args) < 1 {
		return fmt.Errorf("not enough arguments, see --help")
	}

	dataPath := args[0]

	nerdclient, err := NewClient(cmd.ui)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}
	ds, err := nerdclient.CreateDataset()
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	client, err := aws.NewDataClient(&aws.DataClientConfig{
		Credentials: aws.NewNerdalizeCredentials(nerdclient),
		Bucket:      ds.Bucket,
	})
	if err != nil {
		HandleError(errors.Wrap(err, "could not create data client"), cmd.opts.VerboseOutput)
	}

	fi, err := os.Stat(dataPath)
	if err != nil {
		HandleError(errors.Errorf("argument '%v' is not a valid file or directory", dataPath), cmd.opts.VerboseOutput)
	} else if !fi.IsDir() {
		HandleError(errors.Errorf("provided path '%s' is not a directory", dataPath), cmd.opts.VerboseOutput)
	}

	err = ioutil.WriteFile(path.Join(dataPath, DatasetFilename), []byte(ds.DatasetID), DatasetPermissions)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	err = client.UploadDir(dataPath, ds.Root, &stdoutkw{}, 64)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	logrus.Infof("Uploaded data to dataset with ID: %v", ds.DatasetID)

	return nil
}
<|endoftext|>


<commit_before>package commands

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"sync"

	"github.com/cbegin/graven/domain"
	"github.com/cbegin/graven/util"
	"github.com/hashicorp/go-multierror"
	"github.com/urfave/cli"
)

var BuildCommand = cli.Command{
	Name:   "build",
	Usage:  "Builds the current project",
	Action: build,
}

func build(c *cli.Context) error {
	if err := clean(c); err != nil {
		return err
	}

	project, err := domain.FindProject()
	if err != nil {
		return err
	}

	var merr error
	mutex := sync.Mutex{}
	wg := sync.WaitGroup{}
	for _, artifact := range project.Artifacts {
		a := artifact
		wg.Add(len(artifact.Targets))
		for _, target := range artifact.Targets {
			t := target
			go func() {
				defer wg.Done()
				err := buildTarget(project, &a, &t)
				if err != nil {
					mutex.Lock()
					merr = multierror.Append(merr, err)
					mutex.Unlock()
				}
			}()
		}
	}
	wg.Wait()

	return merr
}

func buildTarget(project *domain.Project, artifact *domain.Artifact, target *domain.Target) error {
	classifiedPath := project.TargetPath(artifact.Classifier)
	if _, err := os.Stat(classifiedPath); os.IsNotExist(err) {
		os.Mkdir(classifiedPath, 0755)
	}

	for _, resource := range append(project.Resources, artifact.Resources...) {
		resourcePath := project.ProjectPath(resource)
		baseProjectPath := project.ProjectPath()
		if len(resourcePath[len(baseProjectPath):]) < 1 {
			return fmt.Errorf("Resource path cannot be the entire project folder: %s", resourcePath)
		}
		err := domain.CopyDir(resourcePath, classifiedPath)
		if err != nil {
			return err
		}
	}

	return runBuildCommand(classifiedPath, project, artifact, target)
}

func runBuildCommand(classifiedPath string, project *domain.Project, artifact *domain.Artifact, target *domain.Target) error {
	var c *exec.Cmd
	if target.Flags == "" {
		c = exec.Command("go", "build", "-o", path.Join(classifiedPath, target.Executable), target.Package)
	} else {
		c = exec.Command("go", "build", "-o", path.Join(classifiedPath, target.Executable), target.Flags, target.Package)
	}
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	c.Stdin = os.Stdin
	c.Dir = project.ProjectPath()

	environment := []string{}
	for k, v := range util.MergeMaps(artifact.Environment, target.Environment) {
		environment = append(environment, fmt.Sprintf("%s=%s", k, v))
	}
	gopath, _ := os.LookupEnv("GOPATH")
	environment = append(environment, fmt.Sprintf("%s=%s", "GOPATH", gopath))
	c.Env = environment

	err := c.Run()
	if err != nil {
		return fmt.Errorf("Build error. %v", err)
	}

	if !c.ProcessState.Success() {
		return fmt.Errorf("Build command exited in an error state. %v", c)
	}
	return err
}
<commit_msg>Removed bug reference that was fixed with previous commit.<commit_after>package commands

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"sync"

	"github.com/cbegin/graven/domain"
	"github.com/cbegin/graven/util"
	"github.com/hashicorp/go-multierror"
	"github.com/urfave/cli"
)

var BuildCommand = cli.Command{
	Name:   "build",
	Usage:  "Builds the current project",
	Action: build,
}

func build(c *cli.Context) error {
	if err := clean(c); err != nil {
		return err
	}

	project, err := domain.FindProject()
	if err != nil {
		return err
	}

	var merr error
	mutex := sync.Mutex{}
	wg := sync.WaitGroup{}
	for _, artifact := range project.Artifacts {
		a := artifact
		wg.Add(len(artifact.Targets))
		for _, target := range artifact.Targets {
			t := target
			go func() {
				defer wg.Done()
				err := buildTarget(project, &a, &t)
				if err != nil {
					mutex.Lock()
					merr = multierror.Append(merr, err)
					mutex.Unlock()
				}
			}()
		}
	}
	wg.Wait()

	return merr
}

func buildTarget(project *domain.Project, artifact *domain.Artifact, target *domain.Target) error {
	classifiedPath := project.TargetPath(artifact.Classifier)
	if _, err := os.Stat(classifiedPath); os.IsNotExist(err) {
		os.Mkdir(classifiedPath, 0755)
	}

	for _, resource := range append(project.Resources, artifact.Resources...) {
		resourcePath := project.ProjectPath(resource)
		baseProjectPath := project.ProjectPath()
		if len(resourcePath[len(baseProjectPath):]) < 1 {
			return fmt.Errorf("Resource path cannot be the entire project folder: %s", resourcePath)
		}
		err := domain.CopyDir(resourcePath, classifiedPath)
		if err != nil {
			return err
		}
	}

	return runBuildCommand(classifiedPath, project, artifact, target)
}

func runBuildCommand(classifiedPath string, project *domain.Project, artifact *domain.Artifact, target *domain.Target) error {
	fmt.Printf("Building %v-%v-%v-%v\n", project.Name, project.Version, artifact.Classifier, target.Executable)
	defer fmt.Printf("Done %v-%v-%v-%v\n", project.Name, project.Version, artifact.Classifier, target.Executable)
	var c *exec.Cmd
	if target.Flags == "" {
		c = exec.Command("go", "build", "-o", path.Join(classifiedPath, target.Executable), target.Package)
	} else {
		c = exec.Command("go", "build", "-o", path.Join(classifiedPath, target.Executable), target.Flags, target.Package)
	}
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	c.Stdin = os.Stdin
	c.Dir = project.ProjectPath()

	environment := []string{}
	for k, v := range util.MergeMaps(artifact.Environment, target.Environment) {
		environment = append(environment, fmt.Sprintf("%s=%s", k, v))
	}
	gopath, _ := os.LookupEnv("GOPATH")
	environment = append(environment, fmt.Sprintf("%s=%s", "GOPATH", gopath))
	c.Env = environment

	err := c.Run()
	if err != nil {
		return fmt.Errorf("Build error. %v", err)
	}

	if !c.ProcessState.Success() {
		return fmt.Errorf("Build command exited in an error state. %v", c)
	}
	return err
}
<|endoftext|>


<commit_before>package commands

import (
	"fmt"

	"github.com/spf13/cobra"
)

var startCmd = &cobra.Command{
	Use:   "start",
	Short: "Start kAdvisor",
	Long:  "kAdvisor can start all-in-one as a single node",
}

func init() {
	startCmd.Run = start
}

func start(cmd *cobra.Command, args []string) {
	InitializeConfig()
	InitializeKubeClient()
	fmt.Println("Running")
}
<commit_msg>Print discovered nodes<commit_after>package commands

import (
	"encoding/json"

	log "github.com/Sirupsen/logrus"
	"github.com/spf13/cobra"
)

var startCmd = &cobra.Command{
	Use:   "start",
	Short: "Start kAdvisor",
	Long:  "kAdvisor can start all-in-one as a single node",
}

func init() {
	startCmd.Run = start
}

func start(cmd *cobra.Command, args []string) {
	InitializeConfig()
	kubernetesClient := InitializeKubeClient()
	log.Info("Running")
	nodeList, err := kubernetesClient.Nodes().List()
	if err != nil {
		log.Fatal(err)
	}
	marshalledNodes, err := json.MarshalIndent(nodeList, "", "  ")
	if err != nil {
		log.Fatal(err)
	}
	log.Debug(string(marshalledNodes[:]))
}
<|endoftext|>


<commit_before>package builder

import (
	"github.com/appc/spec/discovery"
	"github.com/blablacar/cnt/config"
	"github.com/blablacar/cnt/log"
	"github.com/blablacar/cnt/utils"
	"io/ioutil"
	"os"
	"strings"
)

func (cnt *Img) Build() error {
	log.Get().Info("Building Image : ", cnt.manifest.NameAndVersion)

	os.MkdirAll(cnt.rootfs, 0777)

	cnt.processFrom()
	cnt.copyRunlevelsScripts()

	cnt.runlevelBuildSetup()

	cnt.writeImgManifest()
	cnt.writeCntManifest() // TODO move that, here because we update the version number to generated version

	cnt.runBuild()
	cnt.copyRunlevelsPrestart()
	cnt.copyAttributes()
	cnt.copyConfd()
	cnt.copyFiles()
	cnt.runBuildLate()

	cnt.tarAci()
	//	ExecCmd("chown " + os.Getenv("SUDO_USER") + ": " + target + "/*") //TODO chown
	return nil
}

///////////////////////////////////////////////////////

func (cnt *Img) writeCntManifest() {
	utils.CopyFile(cnt.path+IMG_MANIFEST, cnt.target+IMG_MANIFEST)
}

func (cnt *Img) runBuildLate() {
	res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD_LATE)
	res2, err2 := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD_LATE)
	if res && res2 && err != nil && err2 != nil {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT_LATE, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build-late.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "--share-system", "target/build-late.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Panic("systemd-nspawn is required")
	}
}

func (cnt *Img) runBuild() {
	if res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD); res || err != nil {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "--share-system", "target/build.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Panic("systemd-nspawn is required")
		//		log.Get().Info("Run with docker")
		//
		//		//
		//		log.Get().Info("Prepare Docker")
		//		first := exec.Command("bash", "-c", "cd "+cnt.rootfs+" && tar cf - .")
		//		second := exec.Command("docker", "import", "-", "")
		//
		//		reader, writer := io.Pipe()
		//		first.Stdout = writer
		//		second.Stdin = reader
		//
		//		var buff bytes.Buffer
		//		second.Stdout = &buff
		//
		//		first.Start()
		//		second.Start()
		//		first.Wait()
		//		writer.Close()
		//		second.Wait()
		//		imgId := strings.TrimSpace(buff.String())
		//
		//		//
		//		log.Get().Info("Run Docker\n")
		//		cmd := []string{"run", "--name=" + cnt.manifest.NameAndVersion.ShortName(), "-v", cnt.target + ":/target", imgId, "/target/build.sh"}
		//		utils.ExecCmd("docker", "rm", cnt.manifest.NameAndVersion.ShortName())
		//		if err := utils.ExecCmd("docker", cmd...); err != nil {
		//			panic(err)
		//		}
		//
		//		//
		//		log.Get().Info("Release Docker")
		//		if cnt.manifest.Build.NoBuildImage() {
		//			os.RemoveAll(cnt.rootfs)
		//			os.Mkdir(cnt.rootfs, 0777)
		//
		//			if err := utils.ExecCmd("docker", "export", "-o", cnt.target+"/dockerfs.tar", cnt.manifest.NameAndVersion.ShortName()); err != nil {
		//				panic(err)
		//			}
		//
		//			utils.ExecCmd("tar", "xpf", cnt.target+"/dockerfs.tar", "-C", cnt.rootfs)
		//		}
		//		if err := utils.ExecCmd("docker", "rm", cnt.manifest.NameAndVersion.ShortName()); err != nil {
		//			panic(err)
		//		}
		//		if err := utils.ExecCmd("docker", "rmi", imgId); err != nil {
		//			panic(err)
		//		}
	}
}

func (cnt *Img) processFrom() {
	if cnt.manifest.From != "" {
		log.Get().Info("Prepare rootfs from " + cnt.manifest.From)

		app, err := discovery.NewAppFromString(string(cnt.manifest.From))
		if app.Labels["os"] == "" {
			app.Labels["os"] = "linux"
		}
		if app.Labels["arch"] == "" {
			app.Labels["arch"] = "amd64"
		}

		endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
		if err != nil {
			panic(err)
		}

		url := endpoint.ACIEndpoints[0].ACI

		aciPath := config.GetConfig().AciPath + "/" + string(cnt.manifest.From)
		if _, err := os.Stat(aciPath + "/image.aci"); cnt.args.ForceUpdate || os.IsNotExist(err) {
			if err := os.MkdirAll(aciPath, 0755); err != nil {
				log.Get().Panic(err)
			}
			if err = utils.ExecCmd("wget", "-O", aciPath+"/image.aci", url); err != nil {
				os.Remove(aciPath + "/image.aci")
				log.Get().Panic("Cannot download from image", err)
			}
		} else {
			log.Get().Info("Image " + cnt.manifest.From + " Already exists locally, will not be downloaded")
		}

		utils.ExecCmd("tar", "xpf", aciPath+"/image.aci", "-C", cnt.target)

		//		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.manifest.From)
		//		utils.ExecCmd("rkt", "image", "export", "--overwrite", cnt.manifest.From, cnt.target + "/from.aci")
		//		utils.ExecCmd("tar", "xf", cnt.target + "/from.aci", "-C", cnt.target)
		//		os.Remove(cnt.target + "/from.aci")
	}
}

func (cnt *Img) copyRunlevelsScripts() {
	if err := os.MkdirAll(cnt.target+RUNLEVELS, 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+RUNLEVELS, cnt.target+RUNLEVELS)
}

func (cnt *Img) runlevelBuildSetup() {
	files, err := ioutil.ReadDir(cnt.path + RUNLEVELS_BUILD_SETUP)
	if err != nil {
		return
	}

	os.Setenv("BASEDIR", cnt.path)
	os.Setenv("TARGET", cnt.target)
	for _, f := range files {
		if !f.IsDir() {
			log.Get().Info("Running Build setup level : ", f.Name())
			if err := utils.ExecCmd(cnt.path + RUNLEVELS_BUILD_SETUP + "/" + f.Name()); err != nil {
				log.Get().Panic(err)
			}
		}
	}
}

func (cnt *Img) tarAci() {
	dir, _ := os.Getwd()
	log.Get().Debug("chdir to", cnt.target)
	os.Chdir(cnt.target)

	args := []string{"manifest", "rootfs/"}

	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_EARLY); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_EARLY, "/"))
	}
	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_LATE); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_LATE, "/"))
	}

	utils.Tar(cnt.args.Zip, "image.aci", args...)
	log.Get().Debug("chdir to", dir)
	os.Chdir(dir)
}

//func (cnt *Cnt) copyInstallAndCreatePacker() {
//	if _, err := os.Stat(cnt.path + "/install.sh"); err == nil {
//		utils.CopyFile(cnt.path + "/install.sh", cnt.target + "/install.sh")
//		sum, _ := utils.ChecksumFile(cnt.target + "/install.sh")
//		lastSum, err := ioutil.ReadFile(cnt.target + "/install.sh.SUM")
//		if err != nil || !bytes.Equal(lastSum, sum) {
//			utils.WritePackerFiles(cnt.target)
//			ioutil.WriteFile(cnt.target + "/install.sh.SUM", sum, 0755)
//			return
//		}
//	}
//	utils.RemovePackerFiles(cnt.target)
//}

func (cnt *Img) copyRunlevelsPrestart() {
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/late-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/early-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+RUNLEVELS_PRESTART, cnt.rootfs+"/etc/prestart/early-prestart.d")
	utils.CopyDir(cnt.path+RUNLEVELS_LATESTART, cnt.rootfs+"/etc/prestart/late-prestart.d")
}

func (cnt *Img) copyConfd() {
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+CONFD_CONFIG, cnt.rootfs+"/etc/prestart/conf.d")
	utils.CopyDir(cnt.path+CONFD_TEMPLATE, cnt.rootfs+"/etc/prestart/templates")
}

func (cnt *Img) copyFiles() {
	utils.CopyDir(cnt.path+FILES_PATH, cnt.rootfs)
}

func (cnt *Img) copyAttributes() {
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/attributes/"+cnt.manifest.NameAndVersion.ShortName(), 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+ATTRIBUTES, cnt.rootfs+"/etc/prestart/attributes/"+cnt.manifest.NameAndVersion.ShortName())
}

func (cnt *Img) writeImgManifest() {
	log.Get().Debug("Writing aci manifest")
	version := cnt.manifest.NameAndVersion.Version()
	if version == "" {
		version = utils.GenerateVersion()
	}
	utils.WriteImageManifest(&cnt.manifest, cnt.target+"/manifest", cnt.manifest.NameAndVersion.Name(), version)
}
<commit_msg>fix build late detection<commit_after>package builder

import (
	"github.com/appc/spec/discovery"
	"github.com/blablacar/cnt/config"
	"github.com/blablacar/cnt/log"
	"github.com/blablacar/cnt/utils"
	"io/ioutil"
	"os"
	"strings"
)

func (cnt *Img) Build() error {
	log.Get().Info("Building Image : ", cnt.manifest.NameAndVersion)

	os.MkdirAll(cnt.rootfs, 0777)

	cnt.processFrom()
	cnt.copyRunlevelsScripts()

	cnt.runlevelBuildSetup()

	cnt.writeImgManifest()
	cnt.writeCntManifest() // TODO move that, here because we update the version number to generated version

	cnt.runBuild()
	cnt.copyRunlevelsPrestart()
	cnt.copyAttributes()
	cnt.copyConfd()
	cnt.copyFiles()
	cnt.runBuildLate()

	cnt.tarAci()
	//	ExecCmd("chown " + os.Getenv("SUDO_USER") + ": " + target + "/*") //TODO chown
	return nil
}

///////////////////////////////////////////////////////

func (cnt *Img) writeCntManifest() {
	utils.CopyFile(cnt.path+IMG_MANIFEST, cnt.target+IMG_MANIFEST)
}

func (cnt *Img) runBuildLate() {
	res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD_LATE)
	res2, err2 := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD_INHERIT_LATE)
	if (res && res2) || (err != nil && err2 != nil) {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT_LATE, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build-late.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "--share-system", "target/build-late.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Panic("systemd-nspawn is required")
	}
}

func (cnt *Img) runBuild() {
	if res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD); res || err != nil {
		return
	}

	{
		rootfs := "${TARGET}/rootfs"
		if cnt.manifest.Build.NoBuildImage() {
			rootfs = ""
		}
		build := strings.Replace(BUILD_SCRIPT, "%%ROOTFS%%", rootfs, 1)
		ioutil.WriteFile(cnt.target+"/build.sh", []byte(build), 0777)
	}

	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory="+cnt.rootfs, "--capability=all",
			"--bind="+cnt.target+"/:/target", "--share-system", "target/build.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Panic("systemd-nspawn is required")
		//		log.Get().Info("Run with docker")
		//
		//		//
		//		log.Get().Info("Prepare Docker")
		//		first := exec.Command("bash", "-c", "cd "+cnt.rootfs+" && tar cf - .")
		//		second := exec.Command("docker", "import", "-", "")
		//
		//		reader, writer := io.Pipe()
		//		first.Stdout = writer
		//		second.Stdin = reader
		//
		//		var buff bytes.Buffer
		//		second.Stdout = &buff
		//
		//		first.Start()
		//		second.Start()
		//		first.Wait()
		//		writer.Close()
		//		second.Wait()
		//		imgId := strings.TrimSpace(buff.String())
		//
		//		//
		//		log.Get().Info("Run Docker\n")
		//		cmd := []string{"run", "--name=" + cnt.manifest.NameAndVersion.ShortName(), "-v", cnt.target + ":/target", imgId, "/target/build.sh"}
		//		utils.ExecCmd("docker", "rm", cnt.manifest.NameAndVersion.ShortName())
		//		if err := utils.ExecCmd("docker", cmd...); err != nil {
		//			panic(err)
		//		}
		//
		//		//
		//		log.Get().Info("Release Docker")
		//		if cnt.manifest.Build.NoBuildImage() {
		//			os.RemoveAll(cnt.rootfs)
		//			os.Mkdir(cnt.rootfs, 0777)
		//
		//			if err := utils.ExecCmd("docker", "export", "-o", cnt.target+"/dockerfs.tar", cnt.manifest.NameAndVersion.ShortName()); err != nil {
		//				panic(err)
		//			}
		//
		//			utils.ExecCmd("tar", "xpf", cnt.target+"/dockerfs.tar", "-C", cnt.rootfs)
		//		}
		//		if err := utils.ExecCmd("docker", "rm", cnt.manifest.NameAndVersion.ShortName()); err != nil {
		//			panic(err)
		//		}
		//		if err := utils.ExecCmd("docker", "rmi", imgId); err != nil {
		//			panic(err)
		//		}
	}
}

func (cnt *Img) processFrom() {
	if cnt.manifest.From != "" {
		log.Get().Info("Prepare rootfs from " + cnt.manifest.From)

		app, err := discovery.NewAppFromString(string(cnt.manifest.From))
		if app.Labels["os"] == "" {
			app.Labels["os"] = "linux"
		}
		if app.Labels["arch"] == "" {
			app.Labels["arch"] = "amd64"
		}

		endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
		if err != nil {
			panic(err)
		}

		url := endpoint.ACIEndpoints[0].ACI

		aciPath := config.GetConfig().AciPath + "/" + string(cnt.manifest.From)
		if _, err := os.Stat(aciPath + "/image.aci"); cnt.args.ForceUpdate || os.IsNotExist(err) {
			if err := os.MkdirAll(aciPath, 0755); err != nil {
				log.Get().Panic(err)
			}
			if err = utils.ExecCmd("wget", "-O", aciPath+"/image.aci", url); err != nil {
				os.Remove(aciPath + "/image.aci")
				log.Get().Panic("Cannot download from image", err)
			}
		} else {
			log.Get().Info("Image " + cnt.manifest.From + " Already exists locally, will not be downloaded")
		}

		utils.ExecCmd("tar", "xpf", aciPath+"/image.aci", "-C", cnt.target)

		//		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.manifest.From)
		//		utils.ExecCmd("rkt", "image", "export", "--overwrite", cnt.manifest.From, cnt.target + "/from.aci")
		//		utils.ExecCmd("tar", "xf", cnt.target + "/from.aci", "-C", cnt.target)
		//		os.Remove(cnt.target + "/from.aci")
	}
}

func (cnt *Img) copyRunlevelsScripts() {
	if err := os.MkdirAll(cnt.target+RUNLEVELS, 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+RUNLEVELS, cnt.target+RUNLEVELS)
}

func (cnt *Img) runlevelBuildSetup() {
	files, err := ioutil.ReadDir(cnt.path + RUNLEVELS_BUILD_SETUP)
	if err != nil {
		return
	}

	os.Setenv("BASEDIR", cnt.path)
	os.Setenv("TARGET", cnt.target)
	for _, f := range files {
		if !f.IsDir() {
			log.Get().Info("Running Build setup level : ", f.Name())
			if err := utils.ExecCmd(cnt.path + RUNLEVELS_BUILD_SETUP + "/" + f.Name()); err != nil {
				log.Get().Panic(err)
			}
		}
	}
}

func (cnt *Img) tarAci() {
	dir, _ := os.Getwd()
	log.Get().Debug("chdir to", cnt.target)
	os.Chdir(cnt.target)

	args := []string{"manifest", "rootfs/"}

	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_EARLY); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_EARLY, "/"))
	}
	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_LATE); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_LATE, "/"))
	}

	utils.Tar(cnt.args.Zip, "image.aci", args...)
	log.Get().Debug("chdir to", dir)
	os.Chdir(dir)
}

//func (cnt *Cnt) copyInstallAndCreatePacker() {
//	if _, err := os.Stat(cnt.path + "/install.sh"); err == nil {
//		utils.CopyFile(cnt.path + "/install.sh", cnt.target + "/install.sh")
//		sum, _ := utils.ChecksumFile(cnt.target + "/install.sh")
//		lastSum, err := ioutil.ReadFile(cnt.target + "/install.sh.SUM")
//		if err != nil || !bytes.Equal(lastSum, sum) {
//			utils.WritePackerFiles(cnt.target)
//			ioutil.WriteFile(cnt.target + "/install.sh.SUM", sum, 0755)
//			return
//		}
//	}
//	utils.RemovePackerFiles(cnt.target)
//}

func (cnt *Img) copyRunlevelsPrestart() {
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/late-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/early-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+RUNLEVELS_PRESTART, cnt.rootfs+"/etc/prestart/early-prestart.d")
	utils.CopyDir(cnt.path+RUNLEVELS_LATESTART, cnt.rootfs+"/etc/prestart/late-prestart.d")
}

func (cnt *Img) copyConfd() {
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+CONFD_CONFIG, cnt.rootfs+"/etc/prestart/conf.d")
	utils.CopyDir(cnt.path+CONFD_TEMPLATE, cnt.rootfs+"/etc/prestart/templates")
}

func (cnt *Img) copyFiles() {
	utils.CopyDir(cnt.path+FILES_PATH, cnt.rootfs)
}

func (cnt *Img) copyAttributes() {
	if err := os.MkdirAll(cnt.rootfs+"/etc/prestart/attributes/"+cnt.manifest.NameAndVersion.ShortName(), 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path+ATTRIBUTES, cnt.rootfs+"/etc/prestart/attributes/"+cnt.manifest.NameAndVersion.ShortName())
}

func (cnt *Img) writeImgManifest() {
	log.Get().Debug("Writing aci manifest")
	version := cnt.manifest.NameAndVersion.Version()
	if version == "" {
		version = utils.GenerateVersion()
	}
	utils.WriteImageManifest(&cnt.manifest, cnt.target+"/manifest", cnt.manifest.NameAndVersion.Name(), version)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"runtime"
	"time"
)

// State of a virtual machine.
type vmState string

const (
	vmRunning      vmState = "running"
	vmPoweroff             = "poweroff"
	vmPaused               = "paused"
	vmSaved                = "saved"
	vmAborted              = "aborted"
	vmUnregistered         = "(unregistered)" // not actually reported by VirtualBox
	vmUnknown              = "(unknown)"      // not actually reported by VirtualBox
)

// Call the external SSH command to login into boot2docker VM.
func cmdSSH() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := cmd(B2D.SSH,
			"-o", "StrictHostKeyChecking=no",
			"-o", "UserKnownHostsFile=/dev/null",
			"-p", fmt.Sprintf("%d", B2D.SSHPort),
			"docker@localhost"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
		return 1
	}
	return 0
}

// Start the VM from all possible states.
func cmdStart() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		logf("%s is already running.", B2D.VM)
	case vmPaused:
		logf("Resuming %s", B2D.VM)
		if err := vbm("controlvm", B2D.VM, "resume"); err != nil {
			logf("Failed to resume vm: %s", err)
			return 1
		}
		addr := fmt.Sprintf("localhost:%d", B2D.SSHPort)
		if err := read(addr); err != nil {
			logf("Failed to connect to SSH port at %s: %s", addr, err)
			return 1
		}
		logf("Resumed.")
	case vmSaved, vmPoweroff, vmAborted:
		logf("Starting %s...", B2D.VM)
		if err := vbm("startvm", B2D.VM, "--type", "headless"); err != nil {
			logf("Failed to start vm: %s", err)
			return 1
		}
		logf("Waiting for SSH server to start...")
		addr := fmt.Sprintf("localhost:%d", B2D.SSHPort)
		if err := read(addr); err != nil {
			logf("Failed to connect to SSH port at %s: %s", addr, err)
			return 1
		}
		logf("Started.")
	default:
		logf("Cannot start %s from state %.", B2D.VM, state)
		return 1
	}

	// Check if $DOCKER_HOST ENV var is properly configured.
	DockerHost := getenv("DOCKER_HOST", "")
	if DockerHost != fmt.Sprintf("tcp://localhost:%d", B2D.DockerPort) {
		fmt.Printf("\nTo connect the docker client to the Docker daemon, please set:\n")
		fmt.Printf("export DOCKER_HOST=tcp://localhost:%d\n\n", B2D.DockerPort)
	}
	return 0
}

// Save the current state of VM on disk.
func cmdSave() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		logf("Suspending %s", B2D.VM)
		if err := vbm("controlvm", B2D.VM, "savestate"); err != nil {
			logf("Failed to suspend vm: %s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Pause the VM.
func cmdPause() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := vbm("controlvm", B2D.VM, "pause"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Gracefully stop the VM by sending ACPI shutdown signal.
func cmdStop() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		logf("Shutting down %s...", B2D.VM)
		if err := vbm("controlvm", B2D.VM, "acpipowerbutton"); err != nil {
			logf("Failed to shutdown vm: %s", err)
			return 1
		}
		for status(B2D.VM) == vmRunning {
			time.Sleep(1 * time.Second)
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Forcefully power off the VM (equivalent to unplug power). Could potentially
// result in corrupted disk. Use with care.
func cmdPoweroff() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := vbm("controlvm", B2D.VM, "poweroff"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Gracefully stop and then start the VM.
func cmdRestart() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if exitcode := cmdStop(); exitcode != 0 {
			return exitcode
		}
		fallthrough
	default:
		return cmdStart()
	}
	return 0
}

// Forcefully reset the VM. Could potentially result in corrupted disk. Use
// with care.
func cmdReset() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := vbm("controlvm", B2D.VM, "reset"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Delete the VM and remove associated files.
func cmdDelete() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)

	case vmPoweroff, vmAborted:
		if err := vbm("unregistervm", "--delete", B2D.VM); err != nil {
			logf("Failed to delete vm: %s", err)
			return 1
		}
	default:
		logf("%s needs to be stopped to delete it.", B2D.VM)
		return 1
	}
	return 0
}

// Show detailed info of the VM.
func cmdInfo() int {
	if err := vbm("showvminfo", B2D.VM); err != nil {
		logf("%s", err)
		return 1
	}
	return 0
}

// Show the current state of the VM.
func cmdStatus() int {
	fmt.Printf("%s\n", status(B2D.VM))
	return 0
}

// Initialize the boot2docker VM from scratch.
func cmdInit() int {
	if state := status(B2D.VM); state != vmUnregistered {
		logf("%s already exists.\n")
		return 1
	}

	if ping(fmt.Sprintf("localhost:%d", B2D.DockerPort)) {
		logf("DOCKER_PORT=%d on localhost is occupied. Please choose another none.", B2D.DockerPort)
		return 1
	}

	if ping(fmt.Sprintf("localhost:%d", B2D.SSHPort)) {
		logf("SSH_HOST_PORT=%d on localhost is occupied. Please choose another one.", B2D.SSHPort)
		return 1
	}

	logf("Creating VM %s...", B2D.VM)
	if err := vbm("createvm", "--name", B2D.VM, "--register"); err != nil {
		logf("Failed to create vm: %s", err)
		return 1
	}

	if err := vbm("modifyvm", B2D.VM,
		"--ostype", "Linux26_64",
		"--cpus", fmt.Sprintf("%d", runtime.NumCPU()),
		"--memory", fmt.Sprintf("%d", B2D.Memory),
		"--rtcuseutc", "on",
		"--acpi", "on",
		"--ioapic", "on",
		"--hpet", "on",
		"--hwvirtex", "on",
		"--vtxvpid", "on",
		"--largepages", "on",
		"--nestedpaging", "on",
		"--firmware", "bios",
		"--bioslogofadein", "off",
		"--bioslogofadeout", "off",
		"--bioslogodisplaytime", "0",
		"--biosbootmenu", "disabled",
		"--boot1", "dvd"); err != nil {
		logf("Failed to modify %s: %s", B2D.VM, err)
		return 1
	}

	logf("Setting VM networking...")
	if err := vbm("modifyvm", B2D.VM, "--nic1", "nat", "--nictype1", "virtio", "--cableconnected1", "on"); err != nil {
		logf("Failed to modify %s: %s", B2D.VM, err)
		return 1
	}

	if err := vbm("modifyvm", B2D.VM,
		"--natpf1", fmt.Sprintf("ssh,tcp,127.0.0.1,%d,,22", B2D.SSHPort),
		"--natpf1", fmt.Sprintf("docker,tcp,127.0.0.1,%d,,4243", B2D.DockerPort)); err != nil {
		logf("Failed to modify %s: %s", B2D.VM, err)
		return 1
	}
	logf("Port forwarding [ssh]: host tcp://127.0.0.1:%d --> guest tcp://0.0.0.0:22", B2D.SSHPort)
	logf("Port forwarding [docker]: host tcp://127.0.0.1:%d --> guest tcp://0.0.0.0:4243", B2D.DockerPort)

	if _, err := os.Stat(B2D.ISO); err != nil {
		if os.IsNotExist(err) {
			if exitcode := cmdDownload(); exitcode != 0 {
				return exitcode
			}
		} else {
			logf("Failed to open ISO image: %s", err)
			return 1
		}
	}

	if _, err := os.Stat(B2D.Disk); err != nil {
		if os.IsNotExist(err) {
			if err := makeDiskImage(B2D.Disk, B2D.DiskSize); err != nil {
				logf("Failed to create disk image: %s", err)
				return 1
			}
		} else {
			logf("Failed to open disk image: %s", err)
			return 1
		}
	}

	logf("Setting VM disks...")
	if err := vbm("storagectl", B2D.VM, "--name", "SATA", "--add", "sata", "--hostiocache", "on"); err != nil {
		logf("Failed to add storage controller: %s", err)
		return 1
	}

	if err := vbm("storageattach", B2D.VM, "--storagectl", "SATA", "--port", "0", "--device", "0", "--type", "dvddrive", "--medium", B2D.ISO); err != nil {
		logf("Failed to attach storage device %s: %s", B2D.ISO, err)
		return 1
	}

	if err := vbm("storageattach", B2D.VM, "--storagectl", "SATA", "--port", "1", "--device", "0", "--type", "hdd", "--medium", B2D.Disk); err != nil {
		logf("Failed to attach storage device %s: %s", B2D.Disk, err)
		return 1
	}

	logf("Done. Type `%s up` to start the VM.", os.Args[0])
	return 0
}

// Download the boot2docker ISO image.
func cmdDownload() int {
	logf("Downloading boot2docker ISO image...")
	url := "https://api.github.com/repos/boot2docker/boot2docker/releases"
	tag, err := getLatestReleaseName(url)
	if err != nil {
		logf("Failed to get latest release: %s", err)
		return 1
	}
	logf("Latest release is %s", tag)

	url = fmt.Sprintf("https://github.com/boot2docker/boot2docker/releases/download/%s/boot2docker.iso", tag)
	if err := download(B2D.ISO, url); err != nil {
		logf("Failed to download ISO image: %s", err)
		return 1
	}
	return 0
}
<commit_msg>Fixed an error message and wrapped excessive long lines<commit_after>package main

import (
	"fmt"
	"os"
	"runtime"
	"time"
)

// State of a virtual machine.
type vmState string

const (
	vmRunning      vmState = "running"
	vmPoweroff             = "poweroff"
	vmPaused               = "paused"
	vmSaved                = "saved"
	vmAborted              = "aborted"
	vmUnregistered         = "(unregistered)" // not actually reported by VirtualBox
	vmUnknown              = "(unknown)"      // not actually reported by VirtualBox
)

// Call the external SSH command to login into boot2docker VM.
func cmdSSH() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := cmd(B2D.SSH,
			"-o", "StrictHostKeyChecking=no",
			"-o", "UserKnownHostsFile=/dev/null",
			"-p", fmt.Sprintf("%d", B2D.SSHPort),
			"docker@localhost"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
		return 1
	}
	return 0
}

// Start the VM from all possible states.
func cmdStart() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		logf("%s is already running.", B2D.VM)
	case vmPaused:
		logf("Resuming %s", B2D.VM)
		if err := vbm("controlvm", B2D.VM, "resume"); err != nil {
			logf("Failed to resume vm: %s", err)
			return 1
		}
		addr := fmt.Sprintf("localhost:%d", B2D.SSHPort)
		if err := read(addr); err != nil {
			logf("Failed to connect to SSH port at %s: %s", addr, err)
			return 1
		}
		logf("Resumed.")
	case vmSaved, vmPoweroff, vmAborted:
		logf("Starting %s...", B2D.VM)
		if err := vbm("startvm", B2D.VM, "--type", "headless"); err != nil {
			logf("Failed to start vm: %s", err)
			return 1
		}
		logf("Waiting for SSH server to start...")
		addr := fmt.Sprintf("localhost:%d", B2D.SSHPort)
		if err := read(addr); err != nil {
			logf("Failed to connect to SSH port at %s: %s", addr, err)
			return 1
		}
		logf("Started.")
	default:
		logf("Cannot start %s from state %.", B2D.VM, state)
		return 1
	}

	// Check if $DOCKER_HOST ENV var is properly configured.
	DockerHost := getenv("DOCKER_HOST", "")
	if DockerHost != fmt.Sprintf("tcp://localhost:%d", B2D.DockerPort) {
		fmt.Printf("\nTo connect the docker client to the Docker daemon, please set:\n")
		fmt.Printf("export DOCKER_HOST=tcp://localhost:%d\n\n", B2D.DockerPort)
	}
	return 0
}

// Save the current state of VM on disk.
func cmdSave() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		logf("Suspending %s", B2D.VM)
		if err := vbm("controlvm", B2D.VM, "savestate"); err != nil {
			logf("Failed to suspend vm: %s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Pause the VM.
func cmdPause() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := vbm("controlvm", B2D.VM, "pause"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Gracefully stop the VM by sending ACPI shutdown signal.
func cmdStop() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		logf("Shutting down %s...", B2D.VM)
		if err := vbm("controlvm", B2D.VM, "acpipowerbutton"); err != nil {
			logf("Failed to shutdown vm: %s", err)
			return 1
		}
		for status(B2D.VM) == vmRunning {
			time.Sleep(1 * time.Second)
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Forcefully power off the VM (equivalent to unplug power). Could potentially
// result in corrupted disk. Use with care.
func cmdPoweroff() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := vbm("controlvm", B2D.VM, "poweroff"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Gracefully stop and then start the VM.
func cmdRestart() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if exitcode := cmdStop(); exitcode != 0 {
			return exitcode
		}
		fallthrough
	default:
		return cmdStart()
	}
	return 0
}

// Forcefully reset the VM. Could potentially result in corrupted disk. Use
// with care.
func cmdReset() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)
		return 1
	case vmRunning:
		if err := vbm("controlvm", B2D.VM, "reset"); err != nil {
			logf("%s", err)
			return 1
		}
	default:
		logf("%s is not running.", B2D.VM)
	}
	return 0
}

// Delete the VM and remove associated files.
func cmdDelete() int {
	switch state := status(B2D.VM); state {
	case vmUnregistered:
		logf("%s is not registered.", B2D.VM)

	case vmPoweroff, vmAborted:
		if err := vbm("unregistervm", "--delete", B2D.VM); err != nil {
			logf("Failed to delete vm: %s", err)
			return 1
		}
	default:
		logf("%s needs to be stopped to delete it.", B2D.VM)
		return 1
	}
	return 0
}

// Show detailed info of the VM.
func cmdInfo() int {
	if err := vbm("showvminfo", B2D.VM); err != nil {
		logf("%s", err)
		return 1
	}
	return 0
}

// Show the current state of the VM.
func cmdStatus() int {
	fmt.Printf("%s\n", status(B2D.VM))
	return 0
}

// Initialize the boot2docker VM from scratch.
func cmdInit() int {
	if state := status(B2D.VM); state != vmUnregistered {
		logf("%s already exists.\n")
		return 1
	}

	if ping(fmt.Sprintf("localhost:%d", B2D.DockerPort)) {
		logf("DOCKER_PORT=%d on localhost is occupied. Please choose another none.", B2D.DockerPort)
		return 1
	}

	if ping(fmt.Sprintf("localhost:%d", B2D.SSHPort)) {
		logf("SSH_PORT=%d on localhost is occupied. Please choose another one.", B2D.SSHPort)
		return 1
	}

	logf("Creating VM %s...", B2D.VM)
	if err := vbm("createvm", "--name", B2D.VM, "--register"); err != nil {
		logf("Failed to create vm: %s", err)
		return 1
	}

	if err := vbm("modifyvm", B2D.VM,
		"--ostype", "Linux26_64",
		"--cpus", fmt.Sprintf("%d", runtime.NumCPU()),
		"--memory", fmt.Sprintf("%d", B2D.Memory),
		"--rtcuseutc", "on",
		"--acpi", "on",
		"--ioapic", "on",
		"--hpet", "on",
		"--hwvirtex", "on",
		"--vtxvpid", "on",
		"--largepages", "on",
		"--nestedpaging", "on",
		"--firmware", "bios",
		"--bioslogofadein", "off",
		"--bioslogofadeout", "off",
		"--bioslogodisplaytime", "0",
		"--biosbootmenu", "disabled",
		"--boot1", "dvd",
	); err != nil {
		logf("Failed to modify %s: %s", B2D.VM, err)
		return 1
	}

	logf("Setting VM networking...")
	if err := vbm("modifyvm", B2D.VM,
		"--nic1", "nat",
		"--nictype1", "virtio",
		"--cableconnected1", "on",
	); err != nil {
		logf("Failed to modify %s: %s", B2D.VM, err)
		return 1
	}

	if err := vbm("modifyvm", B2D.VM,
		"--natpf1", fmt.Sprintf("ssh,tcp,127.0.0.1,%d,,22", B2D.SSHPort),
		"--natpf1", fmt.Sprintf("docker,tcp,127.0.0.1,%d,,4243", B2D.DockerPort),
	); err != nil {
		logf("Failed to modify %s: %s", B2D.VM, err)
		return 1
	}
	logf("Port forwarding [ssh]: host tcp://127.0.0.1:%d --> guest tcp://0.0.0.0:22", B2D.SSHPort)
	logf("Port forwarding [docker]: host tcp://127.0.0.1:%d --> guest tcp://0.0.0.0:4243", B2D.DockerPort)

	if _, err := os.Stat(B2D.ISO); err != nil {
		if os.IsNotExist(err) {
			if exitcode := cmdDownload(); exitcode != 0 {
				return exitcode
			}
		} else {
			logf("Failed to open ISO image: %s", err)
			return 1
		}
	}

	if _, err := os.Stat(B2D.Disk); err != nil {
		if os.IsNotExist(err) {
			if err := makeDiskImage(B2D.Disk, B2D.DiskSize); err != nil {
				logf("Failed to create disk image: %s", err)
				return 1
			}
		} else {
			logf("Failed to open disk image: %s", err)
			return 1
		}
	}

	logf("Setting VM disks...")
	if err := vbm("storagectl", B2D.VM,
		"--name", "SATA",
		"--add", "sata",
		"--hostiocache", "on",
	); err != nil {
		logf("Failed to add storage controller: %s", err)
		return 1
	}

	if err := vbm("storageattach", B2D.VM,
		"--storagectl", "SATA",
		"--port", "0",
		"--device", "0",
		"--type", "dvddrive",
		"--medium", B2D.ISO,
	); err != nil {
		logf("Failed to attach storage device %s: %s", B2D.ISO, err)
		return 1
	}

	if err := vbm("storageattach", B2D.VM,
		"--storagectl", "SATA",
		"--port", "1",
		"--device", "0",
		"--type", "hdd",
		"--medium", B2D.Disk,
	); err != nil {
		logf("Failed to attach storage device %s: %s", B2D.Disk, err)
		return 1
	}

	logf("Done. Type `%s up` to start the VM.", os.Args[0])
	return 0
}

// Download the boot2docker ISO image.
func cmdDownload() int {
	logf("Downloading boot2docker ISO image...")
	url := "https://api.github.com/repos/boot2docker/boot2docker/releases"
	tag, err := getLatestReleaseName(url)
	if err != nil {
		logf("Failed to get latest release: %s", err)
		return 1
	}
	logf("Latest release is %s", tag)

	url = fmt.Sprintf("https://github.com/boot2docker/boot2docker/releases/download/%s/boot2docker.iso", tag)
	if err := download(B2D.ISO, url); err != nil {
		logf("Failed to download ISO image: %s", err)
		return 1
	}
	return 0
}
<|endoftext|>


<commit_before>package dockerinfra

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"strings"
	"time"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/client"
	"github.com/goadapp/goad/api"
	goadtypes "github.com/goadapp/goad/goad/types"
	"github.com/goadapp/goad/infrastructure"
	"github.com/goadapp/goad/result"
	"github.com/spf13/afero"
	"github.com/streadway/amqp"
	try "gopkg.in/matryer/try.v1"
)

const (
	rabbitPort    = "5672"
	rabbitRetries = 30
)

type dockerInfrastructure struct {
	Cli                 *client.Client
	NetworkID           string
	RabbitMQContainerID string
	RabbitMQContainerIP string
	config              *goadtypes.TestConfig
}

func handleErr(err error) {
	if err != nil {
		panic(err)
	}
}

func New(config *goadtypes.TestConfig) infrastructure.Infrastructure {
	cli, err := client.NewEnvClient()
	handleErr(err)
	infra := &dockerInfrastructure{
		Cli:    cli,
		config: config,
	}
	DockerPullLambdaImage()
	DockerPullRabbitMQImage()
	return infra
}

func (i *dockerInfrastructure) Run(args infrastructure.InvokeArgs) {
	i.runAsDockerContainer(args)
}

func (i *dockerInfrastructure) GetSettings() *goadtypes.TestConfig {
	return i.config
}
func (i *dockerInfrastructure) Setup() (func(), error) {
	ctx := context.Background()
	cli := i.Cli

	list, err := cli.NetworkList(ctx, types.NetworkListOptions{})
	for _, network := range list {
		if network.Name == "goad-bridge" {
			i.NetworkID = network.ID
		}
	}
	handleErr(err)

	if i.NetworkID == "" {
		netw, nerr := cli.NetworkCreate(ctx, "goad-bridge", types.NetworkCreate{
			CheckDuplicate: true,
		})
		handleErr(nerr)
		i.NetworkID = netw.ID
	}

	running := false
	containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
		All: true,
	})
	handleErr(err)
	for _, container := range containers {
		if strings.Contains(container.Image, "rabbitmq:") {
			i.RabbitMQContainerID = container.ID
			if container.State == "running" {
				i.RabbitMQContainerIP = i.getRabbitContainerIP()
				running = true
			}
		}
	}

	if i.RabbitMQContainerID == "" {
		// Create container to execute lambda
		resp, cerr := cli.ContainerCreate(ctx, &container.Config{
			Image: "rabbitmq:3",
		}, &container.HostConfig{
			AutoRemove: true,
		}, &network.NetworkingConfig{
			EndpointsConfig: map[string]*network.EndpointSettings{
				"goad-bridge": &network.EndpointSettings{},
			},
		}, "rabbitmq")
		handleErr(cerr)
		i.RabbitMQContainerID = resp.ID
	}

	if !running {
		// run container
		err = cli.ContainerStart(ctx, i.RabbitMQContainerID, types.ContainerStartOptions{})
		handleErr(err)

		ip := i.getRabbitContainerIP()
		i.RabbitMQContainerIP = ip
		fmt.Print("Waiting for queue to get ready")
		try.MaxRetries = rabbitRetries
		err = try.Do(func(attempt int) (bool, error) {
			conn, derr := net.Dial("tcp", fmt.Sprintf("%s:%s", ip, rabbitPort))
			if derr != nil {
				fmt.Print(".")
				time.Sleep(1 * time.Second)
				return true, derr
			}
			defer conn.Close()
			return true, nil
		})
		handleErr(err)
	}

	return i.Teardown, nil
}

func DockerPullLambdaImage() {
	DockerPullImage("lambci/lambda")
}

func DockerPullRabbitMQImage() {
	DockerPullImage("rabbitmq:3")
}

func DockerPullImage(imageName string) {
	ctx := context.Background()
	cli, err := client.NewEnvClient()
	handleErr(err)

	// Pull the image from dockerhub.
	out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
	handleErr(err)
	defer out.Close()
	io.Copy(os.Stdout, out)
}

func (i *dockerInfrastructure) getRabbitContainerIP() string {
	ctx := context.Background()
	cli := i.Cli
	ip := ""
	try.MaxRetries = rabbitRetries
	err := try.Do(func(attempt int) (bool, error) {
		data, derr := cli.ContainerInspect(ctx, i.RabbitMQContainerID)
		if derr != nil {
			time.Sleep(1 * time.Second)
			return true, derr
		}
		networks := data.NetworkSettings.Networks
		ip = networks["goad-bridge"].IPAddress
		return true, nil
	})
	handleErr(err)
	return ip
}

func (i *dockerInfrastructure) runAsDockerContainer(args interface{}) {
	ctx := context.Background()
	cli, err := client.NewEnvClient()
	handleErr(err)
	rabbitmqURL := fmt.Sprintf("RABBITMQ=%s", i.GetQueueURL())

	var runnerPath string
	if i.config.RunnerPath == "" {
		runnerPath = createTempDefaultRunner()
	} else {
		runnerPath = os.ExpandEnv(fmt.Sprintf("${PWD}/%s", i.config.RunnerPath))
	}
	// Create container to execute lambda
	resp, err := cli.ContainerCreate(ctx, &container.Config{
		Image: "lambci/lambda",
		Cmd:   append([]string{"index.handler"}, toJSONString(args)),
		Volumes: map[string]struct{}{
			"/var/task": struct{}{},
		},
		Env: []string{rabbitmqURL},
	}, &container.HostConfig{
		AutoRemove: true,
		Binds: []string{
			fmt.Sprintf("%s:/var/task:ro", runnerPath),
		},
	}, &network.NetworkingConfig{
		EndpointsConfig: map[string]*network.EndpointSettings{
			"goad-bridge": &network.EndpointSettings{},
		},
	}, "")
	handleErr(err)

	// run container
	err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
	handleErr(err)
}

func toJSONString(args interface{}) string {
	b, err := json.Marshal(args)
	handleErr(err)
	return string(b[:])
}

func (i *dockerInfrastructure) GetQueueURL() string {
	return fmt.Sprintf("amqp://guest:guest@%s:%s/", i.RabbitMQContainerIP, rabbitPort)
}

func (i *dockerInfrastructure) Receive(results chan *result.LambdaResults) {
	defer close(results)
	fmt.Println("RECEIVING DOCKER")
	data := result.SetupRegionsAggData(i.config.Lambdas)

	// log.Printf("trying to connecto to: %s", queueURL)
	conn, err := amqp.Dial(i.GetQueueURL())
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"goad", // name
		false,  // durable
		false,  // delete when unused
		false,  // exclusive
		false,  // no-wait
		nil,    // arguments
	)
	failOnError(err, "Failed to declare a queue")

	msgs, err := ch.Consume(
		q.Name, // queue
		"cli",  // consumer
		true,   // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	failOnError(err, "Failed to register a consumer")
	// timeoutStart := time.Now()
	for {
		select {
		case msg := <-msgs:
			lambdaResult := &api.RunnerResult{}
			json.Unmarshal(msg.Body, lambdaResult)
			lambdaAggregate := data.Lambdas[lambdaResult.RunnerID]
			result.AddResult(&lambdaAggregate, lambdaResult)
			results <- data
		}
		if data.AllLambdasFinished() {
			break
		}
	}
}

func (i *dockerInfrastructure) Teardown() {
	ctx := context.Background()
	cli, err := client.NewEnvClient()
	handleErr(err)

	timeout := time.Second * 1

	err = cli.ContainerStop(ctx, i.RabbitMQContainerID, &timeout)
	handleErr(err)

	containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
		All: true,
	})
	handleErr(err)
	for _, container := range containers {
		if strings.Contains(container.Image, "lambci/lambda") {
			cli.ContainerStop(ctx, container.ID, &timeout)
		}
	}

	err = cli.NetworkRemove(ctx, i.NetworkID)
	handleErr(err)
}

func createTempDefaultRunner() string {
	var fs afero.Fs = afero.NewOsFs()
	dir := afero.GetTempDir(fs, "defaultRunner")
	defaultRunnerZip, _ := infrastructure.Asset(infrastructure.DefaultRunnerAsset)
	infrastructure.Unzip(defaultRunnerZip, dir)

	return dir
}

func failOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %s", msg, err)
	}
}
<commit_msg>Fix run-docker mode for goad<commit_after>package dockerinfra

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"strings"
	"time"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/client"
	"github.com/goadapp/goad/api"
	goadtypes "github.com/goadapp/goad/goad/types"
	"github.com/goadapp/goad/infrastructure"
	"github.com/goadapp/goad/result"
	"github.com/spf13/afero"
	"github.com/streadway/amqp"
	try "gopkg.in/matryer/try.v1"
)

const (
	rabbitPort    = "5672"
	rabbitRetries = 30
)

type dockerInfrastructure struct {
	Cli                 *client.Client
	NetworkID           string
	RabbitMQContainerID string
	RabbitMQContainerIP string
	config              *goadtypes.TestConfig
}

func handleErr(err error) {
	if err != nil {
		panic(err)
	}
}

func New(config *goadtypes.TestConfig) infrastructure.Infrastructure {
	cli, err := client.NewEnvClient()
	handleErr(err)
	infra := &dockerInfrastructure{
		Cli:    cli,
		config: config,
	}
	DockerPullLambdaImage()
	DockerPullRabbitMQImage()
	return infra
}

func (i *dockerInfrastructure) Run(args infrastructure.InvokeArgs) {
	i.runAsDockerContainer(args)
}

func (i *dockerInfrastructure) GetSettings() *goadtypes.TestConfig {
	return i.config
}
func (i *dockerInfrastructure) Setup() (func(), error) {
	ctx := context.Background()
	cli := i.Cli

	list, err := cli.NetworkList(ctx, types.NetworkListOptions{})
	for _, network := range list {
		if network.Name == "goad-bridge" {
			i.NetworkID = network.ID
		}
	}
	handleErr(err)

	if i.NetworkID == "" {
		netw, nerr := cli.NetworkCreate(ctx, "goad-bridge", types.NetworkCreate{
			CheckDuplicate: true,
		})
		handleErr(nerr)
		i.NetworkID = netw.ID
	}

	running := false
	containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
		All: true,
	})
	handleErr(err)
	for _, container := range containers {
		if strings.Contains(container.Image, "rabbitmq:") {
			i.RabbitMQContainerID = container.ID
			if container.State == "running" {
				i.RabbitMQContainerIP = i.getRabbitContainerIP()
				running = true
			}
		}
	}

	if i.RabbitMQContainerID == "" {
		// Create container to execute lambda
		resp, cerr := cli.ContainerCreate(ctx, &container.Config{
			Image: "rabbitmq:3",
		}, &container.HostConfig{
			AutoRemove: true,
		}, &network.NetworkingConfig{
			EndpointsConfig: map[string]*network.EndpointSettings{
				"goad-bridge": &network.EndpointSettings{},
			},
		}, "rabbitmq")
		handleErr(cerr)
		i.RabbitMQContainerID = resp.ID
	}

	if !running {
		// run container
		err = cli.ContainerStart(ctx, i.RabbitMQContainerID, types.ContainerStartOptions{})
		handleErr(err)

		ip := i.getRabbitContainerIP()
		i.RabbitMQContainerIP = ip
		fmt.Print("Waiting for queue to get ready")
		try.MaxRetries = rabbitRetries
		err = try.Do(func(attempt int) (bool, error) {
			conn, derr := net.Dial("tcp", fmt.Sprintf("%s:%s", ip, rabbitPort))
			if derr != nil {
				fmt.Print(".")
				time.Sleep(1 * time.Second)
				return true, derr
			}
			defer conn.Close()
			return true, nil
		})
		handleErr(err)
	}

	return i.Teardown, nil
}

func DockerPullLambdaImage() {
	DockerPullImage("lambci/lambda")
}

func DockerPullRabbitMQImage() {
	DockerPullImage("rabbitmq:3")
}

func DockerPullImage(imageName string) {
	ctx := context.Background()
	cli, err := client.NewEnvClient()
	handleErr(err)

	// Pull the image from dockerhub.
	out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
	handleErr(err)
	defer out.Close()
	io.Copy(os.Stdout, out)
}

func (i *dockerInfrastructure) getRabbitContainerIP() string {
	ctx := context.Background()
	cli := i.Cli
	ip := ""
	try.MaxRetries = rabbitRetries
	err := try.Do(func(attempt int) (bool, error) {
		data, derr := cli.ContainerInspect(ctx, i.RabbitMQContainerID)
		if derr != nil {
			time.Sleep(1 * time.Second)
			return true, derr
		}
		networks := data.NetworkSettings.Networks
		ip = networks["goad-bridge"].IPAddress
		return true, nil
	})
	handleErr(err)
	return ip
}

func (i *dockerInfrastructure) runAsDockerContainer(args interface{}) {
	ctx := context.Background()
	cli, err := client.NewEnvClient()
	handleErr(err)
	rabbitmqURL := fmt.Sprintf("RABBITMQ=%s", i.GetQueueURL())

	var runnerPath string
	if i.config.RunnerPath == "" {
		runnerPath = createTempDefaultRunner()
	} else {
		runnerPath = os.ExpandEnv(fmt.Sprintf("${PWD}/%s", i.config.RunnerPath))
	}
	// Create container to execute lambda
	resp, err := cli.ContainerCreate(ctx, &container.Config{
		Image: "lambci/lambda",
		Cmd:   append([]string{"index.handler"}, toJSONString(args)),
		Volumes: map[string]struct{}{
			"/var/task": struct{}{},
		},
		Env: []string{rabbitmqURL},
	}, &container.HostConfig{
		AutoRemove: true,
		Binds: []string{
			fmt.Sprintf("%s:/var/task:ro", runnerPath),
		},
	}, &network.NetworkingConfig{
		EndpointsConfig: map[string]*network.EndpointSettings{
			"goad-bridge": &network.EndpointSettings{},
		},
	}, "")
	handleErr(err)

	// run container
	err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
	handleErr(err)
}

func toJSONString(args interface{}) string {
	b, err := json.Marshal(args)
	handleErr(err)
	return string(b[:])
}

func (i *dockerInfrastructure) GetQueueURL() string {
	return fmt.Sprintf("amqp://guest:guest@%s:%s/", i.RabbitMQContainerIP, rabbitPort)
}

func (i *dockerInfrastructure) Receive(results chan *result.LambdaResults) {
	defer close(results)
	fmt.Println("RECEIVING DOCKER")
	data := result.SetupRegionsAggData(i.config.Lambdas)

	// log.Printf("trying to connecto to: %s", queueURL)
	conn, err := amqp.Dial(i.GetQueueURL())
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"goad", // name
		false,  // durable
		false,  // delete when unused
		false,  // exclusive
		false,  // no-wait
		nil,    // arguments
	)
	failOnError(err, "Failed to declare a queue")

	msgs, err := ch.Consume(
		q.Name, // queue
		"cli",  // consumer
		true,   // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	failOnError(err, "Failed to register a consumer")
	// timeoutStart := time.Now()
	for {
		select {
		case msg := <-msgs:
			lambdaResult := &api.RunnerResult{}
			json.Unmarshal(msg.Body, lambdaResult)
			lambdaAggregate := &data.Lambdas[lambdaResult.RunnerID]
			result.AddResult(lambdaAggregate, lambdaResult)
			results <- data
		}
		if data.AllLambdasFinished() {
			break
		}
	}
}

func (i *dockerInfrastructure) Teardown() {
	ctx := context.Background()
	cli, err := client.NewEnvClient()
	handleErr(err)

	timeout := time.Second * 1

	err = cli.ContainerStop(ctx, i.RabbitMQContainerID, &timeout)
	handleErr(err)

	containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
		All: true,
	})
	handleErr(err)
	for _, container := range containers {
		if strings.Contains(container.Image, "lambci/lambda") {
			cli.ContainerStop(ctx, container.ID, &timeout)
		}
	}

	err = cli.NetworkRemove(ctx, i.NetworkID)
	handleErr(err)
}

func createTempDefaultRunner() string {
	var fs afero.Fs = afero.NewOsFs()
	dir := afero.GetTempDir(fs, "defaultRunner")
	defaultRunnerZip, _ := infrastructure.Asset(infrastructure.DefaultRunnerAsset)
	infrastructure.Unzip(defaultRunnerZip, dir)

	return dir
}

func failOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %s", msg, err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/clockworkcoding/goodreads"
)

var config Configuration

type Configuration struct {
	GoodReadsKey    string `json:"goodReadsKey"`
	GoodReadsSecret string `json:"goodReadsSecret"`
	SlackToken      string `json:"slackToken"`
	GoodReadsHost   string `json:"goodReadsHost"`
	GoodReadsPort   string `json:"goodReadsPort"`
	SlackHost       string `json:"slackHost"`
	SlackHostHTTP   string `json:"slackHostHttp"`
	IsHTTPS         string `json:"isHTTPS"`
}

func main() {
	gr := goodreads.NewClient(config.GoodReadsKey, config.GoodReadsSecret)
	results, err := gr.GetSearch("Collapsing Empire")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	book, err := gr.GetBook(results.Search_work[0].Search_best_book.Search_id.Text)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(book.Book_title[0].Text)
	fmt.Println(book.Book_description.Text)
}

func init() {
	config = readConfig()
}

func readConfig() Configuration {

	file, _ := os.Open("conf.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}
	err := decoder.Decode(&configuration)
	if err != nil {
		fmt.Println("error:", err)
	}

	return configuration
}
<commit_msg>read configuration from environment when available<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/clockworkcoding/goodreads"
)

var config Configuration

type Configuration struct {
	GoodReadsKey    string `json:"goodReadsKey"`
	GoodReadsSecret string `json:"goodReadsSecret"`
	SlackToken      string `json:"slackToken"`
	GoodReadsHost   string `json:"goodReadsHost"`
	GoodReadsPort   string `json:"goodReadsPort"`
	SlackHost       string `json:"slackHost"`
	SlackHostHTTP   string `json:"slackHostHttp"`
	IsHTTPS         string `json:"isHTTPS"`
}

func main() {
	gr := goodreads.NewClient(config.GoodReadsKey, config.GoodReadsSecret)
	results, err := gr.GetSearch("Collapsing Empire")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	book, err := gr.GetBook(results.Search_work[0].Search_best_book.Search_id.Text)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(book.Book_title[0].Text)
	fmt.Println(book.Book_description.Text)
}

func init() {
	config = readConfig()
}

func readConfig() Configuration {

	file, _ := os.Open("conf.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}

	if configuration.GoodReadsKey = os.Getenv("goodReadsKey"); configuration.GoodReadsKey != "" {
		configuration.GoodReadsHost = os.Getenv("goodReadsHost")
		configuration.GoodReadsPort = os.Getenv("goodReadsPort")
		configuration.GoodReadsSecret = os.Getenv("goodReadsSecret")
		configuration.IsHTTPS = os.Getenv("isHTTPS")
		configuration.SlackHostHTTP = os.Getenv("slackHostHTTP")
		configuration.SlackHost = os.Getenv("slackHost")
		configuration.SlackToken = os.Getenv("slackToken")
	} else {
		err := decoder.Decode(&configuration)
		if err != nil {
			fmt.Println("error:", err)
		}
	}

	return configuration
}
<|endoftext|>


<commit_before>package bootstrap

import (
	"fmt"
	"gnd.la/assets"
	"strings"
)

const (
	bootstrapCssFmt              = "//netdna.bootstrapcdn.com/bootstrap/%s/css/bootstrap.min.css"
	bootstrapCssNoIconsFmt       = "//netdna.bootstrapcdn.com/bootstrap/%s/css/bootstrap.no-icons.min.css"
	bootstrapCssNoIconsLegacyFmt = "//netdna.bootstrapcdn.com/bootstrap/%s/css/bootstrap-combined.no-icons.min.css"
	bootstrapJsFmt               = "//netdna.bootstrapcdn.com/bootstrap/%s/js/bootstrap.min.js"
	fontAwesomeFmt               = "//netdna.bootstrapcdn.com/font-awesome/%s/css/font-awesome.min.css"
)

func bootstrapParser(m assets.Manager, names []string, options assets.Options) ([]assets.Asset, error) {
	if len(names) > 1 {
		return nil, fmt.Errorf("invalid bootstrap declaration \"%s\": must include only a version number", names)
	}
	version := names[0]
	if !strings.HasPrefix(version, "3.") && !strings.HasPrefix(version, "2.") {
		return nil, fmt.Errorf("invalid bootstrap version %q", version)
	}
	var as []assets.Asset
	if options.BoolOpt("fontawesome", m) {
		format := bootstrapCssNoIconsFmt
		if strings.HasPrefix(version, "2.") {
			format = bootstrapCssNoIconsLegacyFmt
		}
		as = append(as, &assets.Css{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("bootstrap-noicons-%s.css", version),
			},
			Href: fmt.Sprintf(format, version),
		})
		faVersion := "4.0.1"
		if v := options.StringOpt("fontawesome", m); v != "" {
			faVersion = v
		}
		as = append(as, &assets.Css{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("fontawesome-%s.css", faVersion),
			},
			Href: fmt.Sprintf(fontAwesomeFmt, faVersion),
		})
	} else {
		as = append(as, &assets.Css{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("bootstrap-%s.css", version),
			},
			Href: fmt.Sprintf(bootstrapCssFmt, version),
		})
	}
	if !options.BoolOpt("nojs", m) {
		as = append(as, &assets.Script{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("bootstrap-%s.js", version),
			},
			Src: fmt.Sprintf(bootstrapJsFmt, version),
		})
	}
	return as, nil
}

func init() {
	assets.Register("bootstrap", bootstrapParser)
}
<commit_msg>Fix import<commit_after>package bootstrap

import (
	"fmt"
	"gnd.la/template/assets"
	"strings"
)

const (
	bootstrapCssFmt              = "//netdna.bootstrapcdn.com/bootstrap/%s/css/bootstrap.min.css"
	bootstrapCssNoIconsFmt       = "//netdna.bootstrapcdn.com/bootstrap/%s/css/bootstrap.no-icons.min.css"
	bootstrapCssNoIconsLegacyFmt = "//netdna.bootstrapcdn.com/bootstrap/%s/css/bootstrap-combined.no-icons.min.css"
	bootstrapJsFmt               = "//netdna.bootstrapcdn.com/bootstrap/%s/js/bootstrap.min.js"
	fontAwesomeFmt               = "//netdna.bootstrapcdn.com/font-awesome/%s/css/font-awesome.min.css"
)

func bootstrapParser(m assets.Manager, names []string, options assets.Options) ([]assets.Asset, error) {
	if len(names) > 1 {
		return nil, fmt.Errorf("invalid bootstrap declaration \"%s\": must include only a version number", names)
	}
	version := names[0]
	if !strings.HasPrefix(version, "3.") && !strings.HasPrefix(version, "2.") {
		return nil, fmt.Errorf("invalid bootstrap version %q", version)
	}
	var as []assets.Asset
	if options.BoolOpt("fontawesome", m) {
		format := bootstrapCssNoIconsFmt
		if strings.HasPrefix(version, "2.") {
			format = bootstrapCssNoIconsLegacyFmt
		}
		as = append(as, &assets.Css{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("bootstrap-noicons-%s.css", version),
			},
			Href: fmt.Sprintf(format, version),
		})
		faVersion := "4.0.1"
		if v := options.StringOpt("fontawesome", m); v != "" {
			faVersion = v
		}
		as = append(as, &assets.Css{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("fontawesome-%s.css", faVersion),
			},
			Href: fmt.Sprintf(fontAwesomeFmt, faVersion),
		})
	} else {
		as = append(as, &assets.Css{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("bootstrap-%s.css", version),
			},
			Href: fmt.Sprintf(bootstrapCssFmt, version),
		})
	}
	if !options.BoolOpt("nojs", m) {
		as = append(as, &assets.Script{
			Common: assets.Common{
				Manager: m,
				Name:    fmt.Sprintf("bootstrap-%s.js", version),
			},
			Src: fmt.Sprintf(bootstrapJsFmt, version),
		})
	}
	return as, nil
}

func init() {
	assets.Register("bootstrap", bootstrapParser)
}
<|endoftext|>


<commit_before>package bluejaymain

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
	"os"
)

const dataFileName string = "data.json"

func init() {
	// Create empty data file if it does not exist already
	_, err := os.Stat(dataFileName)
	if err != nil {
		if err = ioutil.WriteFile(dataFileName, []byte(`[]`), 0600); err != nil {
			log.Fatal("File could not be written. Cannot continue.")
		}
	}
	log.Printf("%s did not exist and it was created", dataFileName)
}

func getAll() Marks {
	var marks Marks
	fileData, err := ioutil.ReadFile(dataFileName)
	if err != nil {
		// return make(Marks, 0), err
		log.Fatal("Data file is supposed to exist. It should have been created in init func")
	}
	if err := json.Unmarshal(fileData, &marks); err != nil {
		log.Fatal("Error: could not unmarshall data file to JSON") //TODO investigate proper logging
		// return nil, err
	}
	return marks
}

func addMark(m Mark) Mark {
	calculateNextId := func(marks Marks) int {
		nextId := 0
		for _, m := range marks {
			if m.ID > nextId {
				nextId = m.ID
			}
		}
		return nextId + 1
	}

	allMarks := getAll()
	m.ID = calculateNextId(allMarks)
	allMarks = append(allMarks, m)
	persistMarks(allMarks)

	return m
}

func deleteMark(id int) error {
	allMarks := getAll()

	removed := false
	// exclude the mark if exists
	for i, m := range allMarks {
		if m.ID == id {
			allMarks = append(allMarks[:i], allMarks[i+1:]...)
			removed = true
		}
	}
	if !removed {
		log.Printf("Bookrmark with id %d not found", id)
		return errors.New("not found")
	}
	persistMarks(allMarks)

	return nil
}

func persistMarks(marks Marks) {
	jsonData, err := json.Marshal(marks)                 //TODO check err with log level
	err = ioutil.WriteFile(dataFileName, jsonData, 0600) //TODO should be err :=
	check(err)
}
<commit_msg>Remove commented code<commit_after>package bluejaymain

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
	"os"
)

const dataFileName string = "data.json"

func init() {
	// Create empty data file if it does not exist already
	_, err := os.Stat(dataFileName)
	if err != nil {
		if err = ioutil.WriteFile(dataFileName, []byte(`[]`), 0600); err != nil {
			log.Fatal("File could not be written. Cannot continue.")
		}
	}
	log.Printf("%s did not exist and it was created", dataFileName)
}

func getAll() Marks {
	var marks Marks
	fileData, err := ioutil.ReadFile(dataFileName)
	if err != nil {
		log.Fatal("Data file is supposed to exist. It should have been created in init func")
	}
	if err := json.Unmarshal(fileData, &marks); err != nil {
		log.Fatal("Error: could not unmarshall data file to JSON") //TODO investigate proper logging
	}
	return marks
}

func addMark(m Mark) Mark {
	calculateNextId := func(marks Marks) int {
		nextId := 0
		for _, m := range marks {
			if m.ID > nextId {
				nextId = m.ID
			}
		}
		return nextId + 1
	}

	allMarks := getAll()
	m.ID = calculateNextId(allMarks)
	allMarks = append(allMarks, m)
	persistMarks(allMarks)

	return m
}

func deleteMark(id int) error {
	allMarks := getAll()

	removed := false
	// exclude the mark if exists
	for i, m := range allMarks {
		if m.ID == id {
			allMarks = append(allMarks[:i], allMarks[i+1:]...)
			removed = true
		}
	}
	if !removed {
		log.Printf("Bookrmark with id %d not found", id)
		return errors.New("not found")
	}
	persistMarks(allMarks)

	return nil
}

func persistMarks(marks Marks) {
	jsonData, err := json.Marshal(marks)                 //TODO check err with log level
	err = ioutil.WriteFile(dataFileName, jsonData, 0600) //TODO should be err :=
	check(err)
}
<|endoftext|>


<commit_before>// Copyright 2015 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gochip8

import (
	"testing"

	"math/rand"

	"github.com/bmizerany/assert"
)

// dumbSound - Only for testing purpuses
type dumbSound struct {
	BeepCalled bool
}

func (s *dumbSound) Beep() {
	(*s).BeepCalled = true
}

// newCpuAt creates a new cpu and write the ins to the pc memory location
// Only for testing purpuse
func newCpuAt(ins uint16) cpu {
	s := &dumbSound{}
	c := newCpu(s)
	c.memory.WriteWord(uint16(c.pc), ins)

	return c
}

func TestTimers(t *testing.T) {
	s := &dumbSound{}
	c := newCpu(s)

	// With Beep
	c.memory.WriteWord(uint16(c.pc), 0x0E0)

	c.dt = 1
	c.st = 1

	assert.Equal(t, s.BeepCalled, false)

	c.Step()

	assert.Equal(t, c.dt, byte(0))
	assert.Equal(t, c.st, byte(0))
	assert.Equal(t, s.BeepCalled, true)

	// No Beep
	c.memory.WriteWord(uint16(c.pc), 0x0E0)
	s.BeepCalled = false

	c.st = 2
	c.Step()

	assert.Equal(t, s.BeepCalled, false)
	assert.Equal(t, c.st, byte(1))
}

// 00E0 - CLS
func Test00E0(t *testing.T) {

	c := newCpuAt(0x00E0)
	c.gfx.SetPixel(0, 0)
	c.Step()

	for i := 0; i < 64; i++ {
		for j := 0; j < 32; j++ {
			assert.Equal(t, byte(COLOR_BLACK), byte(c.gfx.GetPixel(i, j)))
		}
	}
}

// 00EE - RET
func Test00EE(t *testing.T) {
	c := newCpuAt(0x00EE)

	c.stack.Push(pc(1))
	c.Step()

	assert.Equal(t, pc(1), c.pc)
}

// 1nnn - JP addr
func Test1000(t *testing.T) {
	c := newCpuAt(0x1FFF)
	c.Step()

	assert.Equal(t, pc(0x0FFF), c.pc)
}

// 2nnn - CALL addr
func Test2000(t *testing.T) {
	c := newCpuAt(0x2FFF)
	oldPC := c.pc + 2 // The increment happen before the Step

	c.Step()

	assert.Equal(t, oldPC, c.stack.Pop())
	assert.Equal(t, pc(0x0FFF), c.pc)
}

// 3xkk - SE Vx, byte
func Test3000Equal(t *testing.T) {
	c := newCpuAt(0x31FF)
	c.regs[1] = 0xFF

	oldPC := c.pc + 4
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 3xkk - SE Vx, byte
func Test3000NotEqual(t *testing.T) {
	c := newCpuAt(0x31FF)
	c.regs[1] = 0xAA

	oldPC := c.pc + 2
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 4xkk - SNE Vx, byte
func Test4000Equal(t *testing.T) {
	c := newCpuAt(0x41FF)
	c.regs[1] = 0xFF

	oldPC := c.pc + 2
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 4xkk - SNE Vx, byte
func Test4000NotEqual(t *testing.T) {
	c := newCpuAt(0x41FF)
	c.regs[1] = 0xAA

	oldPC := c.pc + 4
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 5xy0 - SE Vx, Vy
func Test5000Equal(t *testing.T) {
	c := newCpuAt(0x512F)
	c.regs[1] = 0xFF
	c.regs[2] = 0xFF

	oldPC := c.pc + 4
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 5xy0 - SE Vx, Vy
func Test5000NotEqual(t *testing.T) {
	c := newCpuAt(0x512F)
	c.regs[1] = 0xFF
	c.regs[2] = 0xAA

	oldPC := c.pc + 2
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 6xkk - LD Vx, byte
func Test6000(t *testing.T) {
	c := newCpuAt(0x61FF)
	c.Step()
	assert.Equal(t, c.regs[1], byte(0xFF))
}

// 7xkk - ADD Vx, byte
func Test7000(t *testing.T) {
	c := newCpuAt(0x7101)
	c.regs[1] = 1
	c.Step()
	assert.Equal(t, c.regs[1], byte(2))
}

// 8xy0 - LD Vx, Vy
func Test8xy0(t *testing.T) {
	c := newCpuAt(0x8120)
	c.regs[1] = 1
	c.regs[2] = 0
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(0))
	c.Step()
	assert.Equal(t, c.regs[1], byte(0))
	assert.Equal(t, c.regs[2], byte(0))
}

// 8xy1 - OR Vx, Vy
func Test8xy1(t *testing.T) {
	c := newCpuAt(0x8121)
	c.regs[1] = 1
	c.regs[2] = 2
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(2))
	c.Step()
	assert.Equal(t, c.regs[1], byte(1|2))
	assert.Equal(t, c.regs[2], byte(2))
}

// 8xy2 - OR Vx, Vy
func Test8xy2(t *testing.T) {
	c := newCpuAt(0x8122)
	c.regs[1] = 1
	c.regs[2] = 2
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(2))
	c.Step()
	assert.Equal(t, c.regs[1], byte(1&2))
	assert.Equal(t, c.regs[2], byte(2))
}

// 8xy3 - OR Vx, Vy
func Test8xy3(t *testing.T) {
	c := newCpuAt(0x8123)
	c.regs[1] = 1
	c.regs[2] = 2
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(2))
	c.Step()
	assert.Equal(t, c.regs[1], byte(1^2))
	assert.Equal(t, c.regs[2], byte(2))
}

// 8xy4 - ADD Vx, Vy
func Test8xy4Overflow(t *testing.T) {
	c := newCpuAt(0x8124)
	c.regs[1] = 0xFF
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0))
	assert.Equal(t, c.regs[0xF], byte(1))
}

// 8xy4 - ADD Vx, Vy
func Test8xy4ONotverflow(t *testing.T) {
	c := newCpuAt(0x8124)
	c.regs[1] = 0xF0
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0xF0+1))
	assert.Equal(t, c.regs[0xF], byte(0))
}

// 8xy5 - SUB Vx, Vy
func Test8xy5Greater(t *testing.T) {
	c := newCpuAt(0x8125)
	c.regs[1] = 2
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[0xF], byte(1))
}

// 8xy5 - SUB Vx, Vy
func Test8xy5NotGreater(t *testing.T) {
	c := newCpuAt(0x8125)
	c.regs[1] = 1
	c.regs[2] = 2

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0xFF))
	assert.Equal(t, c.regs[0xF], byte(0))
}

// 8xy6 - SHR Vx {, Vy}
func Test8xy6(t *testing.T) {
	c := newCpuAt(0x8126)
	c.regs[1] = 1

	c.Step()

	assert.Equal(t, c.regs[0xF], byte(1&0x0001))
	assert.Equal(t, c.regs[1], byte(1/2))
}

// 8xy7 - SUBN Vx, Vy
func Test8xy7Greater(t *testing.T) {
	c := newCpuAt(0x8127)
	c.regs[1] = 1
	c.regs[2] = 2

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[0xF], byte(1))
}

// 8xy7 - SUBN Vx, Vy
func Test8xy7NotGreater(t *testing.T) {
	c := newCpuAt(0x8127)
	c.regs[1] = 2
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0xFF))
	assert.Equal(t, c.regs[0xF], byte(0))
}

// 8xyE - SHL Vx {, Vy}
func Test8xyE(t *testing.T) {
	c := newCpuAt(0x812E)
	c.regs[1] = 1

	c.Step()

	assert.Equal(t, c.regs[0xF], byte(1&0x0001))
	assert.Equal(t, c.regs[1], byte(1*2))
}

// 9xy0 - SNE Vx, Vy
func Test9xy0NotEqual(t *testing.T) {
	c := newCpuAt(0x9120)
	c.regs[1] = 1
	c.regs[2] = 2

	oldPC := c.pc

	c.Step()

	assert.Equal(t, c.pc, oldPC+4)
}

// 9xy0 - SNE Vx, Vy
func Test9xy0Equal(t *testing.T) {
	c := newCpuAt(0x9120)
	c.regs[1] = 1
	c.regs[2] = 1

	oldPC := c.pc

	c.Step()

	assert.Equal(t, c.pc, oldPC+2)
}

// Annn - LD I, addr
func TestA000(t *testing.T) {
	c := newCpuAt(0xAFFF)

	c.Step()

	assert.Equal(t, c.i, uint16(0xFFF))
}

// Bnnn - JP V0, addr
func TestB000(t *testing.T) {
	c := newCpuAt(0xBFFF)
	c.regs[0] = 1

	c.Step()

	assert.Equal(t, c.pc, pc(0xFFF+1))
}

// Cxkk - RND Vx, byte
func TestC000(t *testing.T) {
	c := newCpuAt(0xC1FF)

	rand.Seed(1)         // Seed with a Known value
	expectedRandom := 33 // With the expected seed, 33 is the first random.

	c.Step()

	assert.Equal(t, c.regs[1], byte(expectedRandom&0xFF))
}
<commit_msg>Added Tests for 0xEx9E and 0xExA1<commit_after>// Copyright 2015 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gochip8

import (
	"testing"

	"math/rand"

	"github.com/bmizerany/assert"
)

// dumbSound - Only for testing purpuses
type dumbSound struct {
	BeepCalled bool
}

func (s *dumbSound) Beep() {
	(*s).BeepCalled = true
}

// newCpuAt creates a new cpu and write the ins to the pc memory location
// Only for testing purpuse
func newCpuAt(ins uint16) cpu {
	s := &dumbSound{}
	c := newCpu(s)
	c.memory.WriteWord(uint16(c.pc), ins)

	return c
}

func TestTimers(t *testing.T) {
	s := &dumbSound{}
	c := newCpu(s)

	// With Beep
	c.memory.WriteWord(uint16(c.pc), 0x0E0)

	c.dt = 1
	c.st = 1

	assert.Equal(t, s.BeepCalled, false)

	c.Step()

	assert.Equal(t, c.dt, byte(0))
	assert.Equal(t, c.st, byte(0))
	assert.Equal(t, s.BeepCalled, true)

	// No Beep
	c.memory.WriteWord(uint16(c.pc), 0x0E0)
	s.BeepCalled = false

	c.st = 2
	c.Step()

	assert.Equal(t, s.BeepCalled, false)
	assert.Equal(t, c.st, byte(1))
}

// 00E0 - CLS
func Test00E0(t *testing.T) {

	c := newCpuAt(0x00E0)
	c.gfx.SetPixel(0, 0)
	c.Step()

	for i := 0; i < 64; i++ {
		for j := 0; j < 32; j++ {
			assert.Equal(t, byte(COLOR_BLACK), byte(c.gfx.GetPixel(i, j)))
		}
	}
}

// 00EE - RET
func Test00EE(t *testing.T) {
	c := newCpuAt(0x00EE)

	c.stack.Push(pc(1))
	c.Step()

	assert.Equal(t, pc(1), c.pc)
}

// 1nnn - JP addr
func Test1000(t *testing.T) {
	c := newCpuAt(0x1FFF)
	c.Step()

	assert.Equal(t, pc(0x0FFF), c.pc)
}

// 2nnn - CALL addr
func Test2000(t *testing.T) {
	c := newCpuAt(0x2FFF)
	oldPC := c.pc + 2 // The increment happen before the Step

	c.Step()

	assert.Equal(t, oldPC, c.stack.Pop())
	assert.Equal(t, pc(0x0FFF), c.pc)
}

// 3xkk - SE Vx, byte
func Test3000Equal(t *testing.T) {
	c := newCpuAt(0x31FF)
	c.regs[1] = 0xFF

	oldPC := c.pc + 4
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 3xkk - SE Vx, byte
func Test3000NotEqual(t *testing.T) {
	c := newCpuAt(0x31FF)
	c.regs[1] = 0xAA

	oldPC := c.pc + 2
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 4xkk - SNE Vx, byte
func Test4000Equal(t *testing.T) {
	c := newCpuAt(0x41FF)
	c.regs[1] = 0xFF

	oldPC := c.pc + 2
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 4xkk - SNE Vx, byte
func Test4000NotEqual(t *testing.T) {
	c := newCpuAt(0x41FF)
	c.regs[1] = 0xAA

	oldPC := c.pc + 4
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 5xy0 - SE Vx, Vy
func Test5000Equal(t *testing.T) {
	c := newCpuAt(0x512F)
	c.regs[1] = 0xFF
	c.regs[2] = 0xFF

	oldPC := c.pc + 4
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 5xy0 - SE Vx, Vy
func Test5000NotEqual(t *testing.T) {
	c := newCpuAt(0x512F)
	c.regs[1] = 0xFF
	c.regs[2] = 0xAA

	oldPC := c.pc + 2
	c.Step()

	assert.Equal(t, oldPC, c.pc)
}

// 6xkk - LD Vx, byte
func Test6000(t *testing.T) {
	c := newCpuAt(0x61FF)
	c.Step()
	assert.Equal(t, c.regs[1], byte(0xFF))
}

// 7xkk - ADD Vx, byte
func Test7000(t *testing.T) {
	c := newCpuAt(0x7101)
	c.regs[1] = 1
	c.Step()
	assert.Equal(t, c.regs[1], byte(2))
}

// 8xy0 - LD Vx, Vy
func Test8xy0(t *testing.T) {
	c := newCpuAt(0x8120)
	c.regs[1] = 1
	c.regs[2] = 0
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(0))
	c.Step()
	assert.Equal(t, c.regs[1], byte(0))
	assert.Equal(t, c.regs[2], byte(0))
}

// 8xy1 - OR Vx, Vy
func Test8xy1(t *testing.T) {
	c := newCpuAt(0x8121)
	c.regs[1] = 1
	c.regs[2] = 2
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(2))
	c.Step()
	assert.Equal(t, c.regs[1], byte(1|2))
	assert.Equal(t, c.regs[2], byte(2))
}

// 8xy2 - OR Vx, Vy
func Test8xy2(t *testing.T) {
	c := newCpuAt(0x8122)
	c.regs[1] = 1
	c.regs[2] = 2
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(2))
	c.Step()
	assert.Equal(t, c.regs[1], byte(1&2))
	assert.Equal(t, c.regs[2], byte(2))
}

// 8xy3 - OR Vx, Vy
func Test8xy3(t *testing.T) {
	c := newCpuAt(0x8123)
	c.regs[1] = 1
	c.regs[2] = 2
	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[2], byte(2))
	c.Step()
	assert.Equal(t, c.regs[1], byte(1^2))
	assert.Equal(t, c.regs[2], byte(2))
}

// 8xy4 - ADD Vx, Vy
func Test8xy4Overflow(t *testing.T) {
	c := newCpuAt(0x8124)
	c.regs[1] = 0xFF
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0))
	assert.Equal(t, c.regs[0xF], byte(1))
}

// 8xy4 - ADD Vx, Vy
func Test8xy4ONotverflow(t *testing.T) {
	c := newCpuAt(0x8124)
	c.regs[1] = 0xF0
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0xF0+1))
	assert.Equal(t, c.regs[0xF], byte(0))
}

// 8xy5 - SUB Vx, Vy
func Test8xy5Greater(t *testing.T) {
	c := newCpuAt(0x8125)
	c.regs[1] = 2
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[0xF], byte(1))
}

// 8xy5 - SUB Vx, Vy
func Test8xy5NotGreater(t *testing.T) {
	c := newCpuAt(0x8125)
	c.regs[1] = 1
	c.regs[2] = 2

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0xFF))
	assert.Equal(t, c.regs[0xF], byte(0))
}

// 8xy6 - SHR Vx {, Vy}
func Test8xy6(t *testing.T) {
	c := newCpuAt(0x8126)
	c.regs[1] = 1

	c.Step()

	assert.Equal(t, c.regs[0xF], byte(1&0x0001))
	assert.Equal(t, c.regs[1], byte(1/2))
}

// 8xy7 - SUBN Vx, Vy
func Test8xy7Greater(t *testing.T) {
	c := newCpuAt(0x8127)
	c.regs[1] = 1
	c.regs[2] = 2

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(1))
	assert.Equal(t, c.regs[0xF], byte(1))
}

// 8xy7 - SUBN Vx, Vy
func Test8xy7NotGreater(t *testing.T) {
	c := newCpuAt(0x8127)
	c.regs[1] = 2
	c.regs[2] = 1

	assert.Equal(t, c.regs[0xF], byte(0))

	c.Step()

	assert.Equal(t, c.regs[1], byte(0xFF))
	assert.Equal(t, c.regs[0xF], byte(0))
}

// 8xyE - SHL Vx {, Vy}
func Test8xyE(t *testing.T) {
	c := newCpuAt(0x812E)
	c.regs[1] = 1

	c.Step()

	assert.Equal(t, c.regs[0xF], byte(1&0x0001))
	assert.Equal(t, c.regs[1], byte(1*2))
}

// 9xy0 - SNE Vx, Vy
func Test9xy0NotEqual(t *testing.T) {
	c := newCpuAt(0x9120)
	c.regs[1] = 1
	c.regs[2] = 2

	oldPC := c.pc

	c.Step()

	assert.Equal(t, c.pc, oldPC+4)
}

// 9xy0 - SNE Vx, Vy
func Test9xy0Equal(t *testing.T) {
	c := newCpuAt(0x9120)
	c.regs[1] = 1
	c.regs[2] = 1

	oldPC := c.pc

	c.Step()

	assert.Equal(t, c.pc, oldPC+2)
}

// Annn - LD I, addr
func TestA000(t *testing.T) {
	c := newCpuAt(0xAFFF)

	c.Step()

	assert.Equal(t, c.i, uint16(0xFFF))
}

// Bnnn - JP V0, addr
func TestB000(t *testing.T) {
	c := newCpuAt(0xBFFF)
	c.regs[0] = 1

	c.Step()

	assert.Equal(t, c.pc, pc(0xFFF+1))
}

// Cxkk - RND Vx, byte
func TestC000(t *testing.T) {
	c := newCpuAt(0xC1FF)

	rand.Seed(1)         // Seed with a Known value
	expectedRandom := 33 // With the expected seed, 33 is the first random.

	c.Step()

	assert.Equal(t, c.regs[1], byte(expectedRandom&0xFF))
}

// TODO: Dxyn - DRW Vx, Vy, nibble

// Ex9E - SKP Vx
func TestEx9E_True(t *testing.T) {
	c := newCpuAt(0xE19E)
	c.keys[1] = true

	oldPC := c.pc + 4

	c.Step()

	assert.Equal(t, pc(oldPC), c.pc)
}

// Ex9E - SKP Vx
func TestEx9E_False(t *testing.T) {
	c := newCpuAt(0xE19E)
	c.keys[1] = false

	oldPC := c.pc + 2

	c.Step()

	assert.Equal(t, pc(oldPC), c.pc)
}

// ExA1 - SKNP Vx
func TestExA1_True(t *testing.T) {
	c := newCpuAt(0xE1A1)
	c.keys[1] = true

	oldPC := c.pc + 2

	c.Step()

	assert.Equal(t, pc(oldPC), c.pc)
}

// ExA1 - SKNP Vx
func TestExA1_False(t *testing.T) {
	c := newCpuAt(0xE1A1)
	c.keys[1] = false

	oldPC := c.pc + 4

	c.Step()

	assert.Equal(t, pc(oldPC), c.pc)
}
<|endoftext|>


<commit_before>// +build acceptance

package v2

import (
	"os"
	"testing"

	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/acceptance/tools"
	"github.com/rackspace/gophercloud/rackspace"
	"github.com/rackspace/gophercloud/rackspace/identity/v2/tokens"
	th "github.com/rackspace/gophercloud/testhelper"
)

func rackspaceAuthOptions(t *testing.T) gophercloud.AuthOptions {
	// Obtain credentials from the environment.
	options, err := rackspace.AuthOptionsFromEnv()
	th.AssertNoErr(t, err)
	options = tools.OnlyRS(options)

	if options.Username == "" {
		t.Fatal("Please provide a Rackspace username as RS_USERNAME.")
	}
	if options.APIKey == "" {
		t.Fatal("Please provide a Rackspace API key as RS_API_KEY.")
	}

	return options
}

func createClient(t *testing.T, auth bool) *gophercloud.ServiceClient {
	ao := rackspaceAuthOptions(t)

	provider, err := rackspace.NewClient(ao.IdentityEndpoint)
	th.AssertNoErr(t, err)

	if auth {
		err = rackspace.Authenticate(provider, ao)
		th.AssertNoErr(t, err)
	}

	return rackspace.NewIdentityV2(provider)
}

func TestTokenAuth(t *testing.T) {
	authedClient := createClient(t, true)
	token := authedClient.TokenID

	tenantID := os.Getenv("RS_TENANT_ID")
	if tenantID == "" {
		t.Skip("You must set RS_TENANT_ID environment variable to run this test")
	}

	authOpts := tokens.AuthOptions{}
	authOpts.TenantID = tenantID
	authOpts.Token = token

	_, err := tokens.Create(authedClient, authOpts).ExtractToken()
	th.AssertNoErr(t, err)
}
<commit_msg>fix duplicate var in tokens v2 acceptance test<commit_after>// +build acceptance

package v2

import (
	"os"
	"testing"

	"github.com/rackspace/gophercloud/rackspace/identity/v2/tokens"
	th "github.com/rackspace/gophercloud/testhelper"
)

func TestTokenAuth(t *testing.T) {
	authedClient := createClient(t, true)
	token := authedClient.TokenID

	tenantID := os.Getenv("RS_TENANT_ID")
	if tenantID == "" {
		t.Skip("You must set RS_TENANT_ID environment variable to run this test")
	}

	authOpts := tokens.AuthOptions{}
	authOpts.TenantID = tenantID
	authOpts.TokenID = token

	_, err := tokens.Create(authedClient, authOpts).ExtractToken()
	th.AssertNoErr(t, err)
}
<|endoftext|>