Ejemplo n.º 1
0
func (s *S) TestDestroyBucket(c *gocheck.C) {
	h := testHandler{}
	ts := s.t.StartGandalfTestServer(&h)
	defer ts.Close()
	app := App{
		Name:     "battery",
		Platform: "python",
		Units:    []Unit{{Machine: 1}},
	}
	bucket := fmt.Sprintf("battery%x", patchRandomReader())
	defer unpatchRandomReader()
	err := CreateApp(&app, 1, []auth.Team{s.team})
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": app.Name})
	defer s.provisioner.Destroy(&app)
	app.Get()
	err = destroyBucket(&app)
	c.Assert(err, gocheck.IsNil)
	s3 := getS3Endpoint()
	_, err = s3.Bucket(bucket).List("", "/", "", 100)
	c.Assert(err, gocheck.NotNil)
	iam := getIAMEndpoint()
	_, err = iam.GetUserPolicy("app-battery-bucket", "battery")
	c.Assert(err, gocheck.NotNil)
	_, err = iam.DeleteAccessKey(app.Env["TSURU_S3_ACCESS_KEY_ID"].Value, "")
	c.Assert(err, gocheck.NotNil)
	msg, err := aqueue().Get(1e6)
	c.Assert(err, gocheck.IsNil)
	c.Assert(msg.Args, gocheck.DeepEquals, []string{app.Name})
	msg.Delete()
}
Ejemplo n.º 2
0
func lookupBucket(bucketName string, auth aws.Auth) (*s3.Bucket, error) {
	var bucket s3.Bucket

	// Looking in each region for bucket
	// To do, make this less crusty and ghetto
	for r, _ := range aws.Regions {
		s3 := s3.New(auth, aws.Regions[r])
		b := s3.Bucket(bucketName)

		// If list return, bucket is valid in this region.
		_, err := b.List("", "", "", 0)
		if err == nil {
			bucket = *b
		} else if err.Error() == "Get : 301 response missing Location header" {
			continue
		} else {
			fmt.Printf("Invalid bucket.\n")
			return nil, err
		}
	}
	fmt.Printf("Found bucket in %s.\n", bucket.S3.Region.Name)
	return &bucket, nil
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()
	if len(outbucket) == 0 || len(outdir) == 0 {
		flag.Usage()
		os.Exit(255)
	}

	r, ok := regions[region]
	if !ok {
		flag.Usage()
		fmt.Fprintf(os.Stderr, "Unrecognised region: %s\n\n", region)
		os.Exit(255)
	}

	cmd := flag.Arg(0)
	if len(cmd) == 0 {
		flag.Usage()
		os.Exit(255)
	}
	args := flag.Args()[1:]

	log.SetFlags(log.LstdFlags | log.Lshortfile)

	auth, err := aws.EnvAuth()
	if err != nil {
		log.Fatal(err)
	}

	s3 := s3.New(auth, r)
	outb := s3.Bucket(outbucket)
	s3io, err := s3io.New(outb, outdir, cmd, args...)
	if err != nil {
		log.Fatal(err)
	}

	if len(inbucket) != 0 {
		if len(inpath) == 0 {
			flag.Usage()
			fmt.Fprintf(os.Stderr, "Need input path with input bucket\n\n")
			os.Exit(255)
		}
		stdin, err := s3io.StdinPipe()
		if err != nil {
			log.Fatal(err)
		}
		go func() {
			inb := s3.Bucket(inbucket)
			input, err := inb.GetReader(inpath)
			if err != nil {
				log.Fatal(err)
			}
			if _, err := io.Copy(stdin, input); err != nil {
				log.Fatal(err)
			}
			input.Close()
		}()
	} else {
		s3io.SetStdin(os.Stdin)
	}

	meta, err := s3io.Run()
	if err != nil {
		log.Fatal(err)
	}

	os.Stdout.Write(meta)
}