Example #1
0
File: fetch.go Project: cfregly/Nut
func (command *FetchCommand) Run(args []string) int {
	flagSet := flag.NewFlagSet("fetch", flag.ExitOnError)
	flagSet.Usage = func() { fmt.Println(command.Help()) }
	name := flagSet.String("name", "", "Name of the container (Default: random UUID)")
	bucket := flagSet.String("bucket", "", "S3 bucket")
	key := flagSet.String("key", "", "S3 key")
	region := flagSet.String("region", "us-west-1", "S3 region")
	sudo := flagSet.Bool("sudo", false, "Use sudo during decompression of image")
	flagSet.Parse(args)
	if *bucket == "" {
		log.Errorf("Must provide the s3 bucket name")
		return -1
	}
	if *key == "" {
		log.Errorf("Must provide the s3 key name")
		return -1
	}
	svc := s3.New(session.New(), aws.NewConfig().WithRegion(*region))
	fo, err := ioutil.TempFile(os.TempDir(), "nut")
	if err != nil {
		log.Error(err)
		return -1
	}
	params := &s3.GetObjectInput{
		Bucket: aws.String(*bucket),
		Key:    aws.String(*key),
	}

	resp, downloadErr := svc.GetObject(params)
	if downloadErr != nil {
		log.Error(downloadErr)
		return -1
	}
	defer resp.Body.Close()
	if _, copyError := io.Copy(fo, resp.Body); copyError != nil {
		log.Errorln(copyError)
		return -1
	}
	log.Infof("Image written to: %s\n", fo.Name())
	fo.Close()
	if *name == "" {
		uuid, err := specification.UUID()
		if err != nil {
			log.Errorln(err)
			return -1
		}
		name = &uuid
	}
	if err := specification.DecompressImage(*name, fo.Name(), *sudo); err != nil {
		log.Errorln(err)
		return -1
	}
	if err := specification.UpdateUTS(*name); err != nil {
		log.Errorln(err)
		return -1
	}
	return 0
}
Example #2
0
File: build.go Project: cfregly/Nut
func (command *BuildCommand) Run(args []string) int {

	flagSet := flag.NewFlagSet("build", flag.ExitOnError)
	flagSet.Usage = func() { fmt.Println(command.Help()) }

	file := flagSet.String("specfile", "Dockerfile", "Container build specification file")
	stopAfterBuild := flagSet.Bool("stop", false, "Stop container after build")
	ephemeral := flagSet.Bool("ephemeral", false, "Destroy the container after creating it")
	name := flagSet.String("name", "", "Name of the resulting container (defaults to randomly generated UUID)")
	export := flagSet.String("export", "", "File path for the container tarball")
	exportSudo := flagSet.Bool("export-sudo", false, "Use sudo while invoking tar")
	volume := flagSet.String("volume", "", "Mount host directory inside container. Format: '[host_directory:]container_directory[:mount options]")
	publish := flagSet.String("publish", "", "Publish tarball in s3 (assumes -stop, -export)")

	flagSet.Parse(args)
	if *publish != "" {
		if *export == "" {
			uuid, err := specification.UUID()
			if err != nil {
				log.Errorln(err)
				return -1
			}
			*export = uuid
		}
	}

	if *export != "" {
		*stopAfterBuild = true
	}
	if *name == "" {
		uuid, err := specification.UUID()
		if err != nil {
			log.Errorln(err)
			return -1
		}
		name = &uuid
	}

	spec := specification.New(*name, *file)

	if err := spec.Parse(); err != nil {
		log.Errorf("Failed to parse dockerfile. Error: %s\n", err)
		return -1
	}

	if err := spec.Build(*volume); err != nil {
		log.Errorf("Failed to build container from dockerfile. Error: %s\n", err)
		return -1
	}

	if *stopAfterBuild {
		log.Infof("Stopping container")
		if err := spec.Stop(); err != nil {
			log.Errorf("Failed to stop container. Error: %s\n", err)
			return -1
		}
	}

	if *export != "" {
		log.Infof("Exporting container")
		if err := spec.Export(*export, *exportSudo); err != nil {
			log.Errorf("Failed to export container. Error: %s\n", err)
			return -1
		}
	}

	if *ephemeral {
		log.Infof("Ephemeral mode. Destroying the container")
		if err := spec.Destroy(); err != nil {
			log.Errorf("Failed to destroy container. Error: %s\n", err)
			return -1
		}
	}
	if *publish != "" {
		parts := strings.Split(*publish, "/")
		bucket := parts[0]
		key := strings.Join(parts[1:], "/")
		if err := specification.Publish(*export, "us-west-1", bucket, key); err != nil {
			log.Errorln(err)
			return -1
		}
	}
	return 0
}