Exemple #1
0
func main() {
	flag.Parse()
	if flag.Arg(0) == "" {
		errorf("no command")
		os.Exit(2)
	}
	auth, err := aws.EnvAuth()
	if err != nil {
		fatalf("envauth: %v", err)
	}
	conn := ec2.New(auth, aws.USEast)

	if flag.Arg(0) == "help" {
		for _, c := range cmds {
			c.printUsage()
		}
		return
	}

	for _, c := range cmds {
		if flag.Arg(0) == c.name {
			c.run(conn, flag.Args()[1:])
			return
		}
	}
	errorf("unknown command %q", flag.Arg(0))
	os.Exit(2)
}
func getInstances() []*Instance {
	auth := aws.Auth{aws_access_key, aws_secret_key}
	c = ec2.New(auth, aws.Regions[ec2_region])

	filter := ec2.NewFilter()
	filter.Add("instance-state-name", "running")
	resp, err := c.Instances(nil, filter)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	instances := []*Instance{}
	for _, reservation := range resp.Reservations {
		for _, instance := range reservation.Instances {
			name := ""
			for _, tag := range instance.Tags {
				if tag.Key == "Name" {
					name = tag.Value
					break
				}
			}
			if name != "" {
				i := &Instance{instance.DNSName, name}
				instances = append(instances, i)
			}
		}
	}

	return instances
}
Exemple #3
0
func (s *AmazonClientSuite) SetUpSuite(c *C) {
	if !testutil.Amazon {
		c.Skip("AmazonClientSuite tests not enabled")
	}
	s.srv.SetUp(c)
	s.ec2 = ec2.New(s.srv.auth, aws.USEast)
}
Exemple #4
0
func (s *AmazonServerSuite) SetUpSuite(c *C) {
	if !*amazon {
		c.Skip("AmazonServerSuite tests not enabled")
	}
	s.srv.SetUp(c)
	s.ServerTests.ec2 = ec2.New(s.srv.auth, aws.USEast)
}
Exemple #5
0
// Spawn instance will spawn an instance to whatever stack is specified in the environment
func (pool AmazonSpawnPool) Grow(conf string) (err error) {

	e := ec2.New(pool.auth, aws.USEast)

	options := ec2.RunInstances{
		ImageId:        "ami-5b632e32", // CoreOS
		InstanceType:   "t1.micro",
		KeyName:        "fiddler", // TODO: We should add this
		SecurityGroups: []ec2.SecurityGroup{pool.sg},
		UserData:       []byte(fmt.Sprintf("https://fiddler/%s/%s", pool.sg.Id, pool.name)), // We need something consistent here
	}

	resp, err := e.RunInstances(&options)
	if err != nil {
		return
	}

	for _, instance := range resp.Instances {
		log.Println("Now running", instance.InstanceId)
		// Next, we'll need to install Fiddler
		// It's kind of annoying, but we can ssh into our new box

	}

	return
}
Exemple #6
0
func (s *S) TestBootstrapPrivateDns(c *gocheck.C) {
	server, err := ec2test.NewServer()
	c.Assert(err, gocheck.IsNil)
	defer server.Quit()
	h := instanceAgentsConfigHealer{}
	region := aws.SAEast
	region.EC2Endpoint = server.URL()
	h.e = ec2.New(aws.Auth{AccessKey: "some", SecretKey: "thing"}, region)
	resp, err := h.ec2().RunInstances(&ec2.RunInstances{MaxCount: 1})
	c.Assert(err, gocheck.IsNil)
	instance := resp.Instances[0]
	p := JujuProvisioner{}
	m := machine{
		AgentState:    "running",
		IpAddress:     "localhost",
		InstanceId:    instance.InstanceId,
		InstanceState: "running",
	}
	p.saveBootstrapMachine(m)
	conn, collection := p.bootstrapCollection()
	defer conn.Close()
	defer collection.Remove(m)
	dns, err := h.bootstrapPrivateDns()
	c.Assert(err, gocheck.IsNil)
	c.Assert(dns, gocheck.Equals, instance.PrivateDNSName)
}
Exemple #7
0
func (s *S) TestBootstrapInstanceIdHealerHeal(c *gocheck.C) {
	ec2Server, err := ec2test.NewServer()
	c.Assert(err, gocheck.IsNil)
	defer ec2Server.Quit()
	s3Server, err := s3test.NewServer(nil)
	c.Assert(err, gocheck.IsNil)
	defer s3Server.Quit()
	h := bootstrapInstanceIdHealer{}
	region := aws.SAEast
	region.EC2Endpoint = ec2Server.URL()
	region.S3Endpoint = s3Server.URL()
	h.e = ec2.New(aws.Auth{AccessKey: "some", SecretKey: "thing"}, region)
	sg, err := h.ec2().CreateSecurityGroup("juju-delta-0", "")
	c.Assert(err, gocheck.IsNil)
	h.s = s3.New(aws.Auth{AccessKey: "some", SecretKey: "thing"}, region)
	jujuBucket := "ble"
	config.Set("juju:bucket", jujuBucket)
	bucket := h.s3().Bucket(jujuBucket)
	err = bucket.PutBucket(s3.PublicReadWrite)
	c.Assert(err, gocheck.IsNil)
	resp, err := h.ec2().RunInstances(&ec2.RunInstances{MaxCount: 1, SecurityGroups: []ec2.SecurityGroup{sg.SecurityGroup}})
	c.Assert(err, gocheck.IsNil)
	err = bucket.Put("provider-state", []byte("doesnotexist"), "binary/octet-stream", s3.PublicReadWrite)
	c.Assert(err, gocheck.IsNil)
	c.Assert(h.needsHeal(), gocheck.Equals, true)
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	data, err := bucket.Get("provider-state")
	expected := "zookeeper-instances: [" + resp.Instances[0].InstanceId + "]"
	c.Assert(string(data), gocheck.Equals, expected)
}
Exemple #8
0
// Communicate with all EC2 endpoints to see if they are alive.
func (s *ClientTests) TestRegions(c *C) {
	name := sessionName("goamz-region-test")
	perms := []ec2.IPPerm{{
		Protocol:  "tcp",
		FromPort:  80,
		ToPort:    80,
		SourceIPs: []string{"127.0.0.1/32"},
	}}
	errs := make(chan error, len(allRegions))
	for _, region := range allRegions {
		go func(r aws.Region) {
			e := ec2.New(s.ec2.Auth, r)
			_, err := e.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, perms)
			errs <- err
		}(region)
	}
	for _ = range allRegions {
		err := <-errs
		if err != nil {
			ec2_err, ok := err.(*ec2.Error)
			if ok {
				c.Check(ec2_err.Code, Matches, "InvalidGroup.NotFound")
			} else {
				c.Errorf("Non-EC2 error: %s", err)
			}
		} else {
			c.Errorf("Test should have errored but it seems to have succeeded")
		}
	}
}
Exemple #9
0
func (e *environ) SetConfig(cfg *config.Config) error {
	ecfg, err := providerInstance.newConfig(cfg)
	if err != nil {
		return err
	}
	e.ecfgMutex.Lock()
	defer e.ecfgMutex.Unlock()
	e.ecfgUnlocked = ecfg

	auth := aws.Auth{ecfg.accessKey(), ecfg.secretKey()}
	region := aws.Regions[ecfg.region()]
	publicBucketRegion := aws.Regions[ecfg.publicBucketRegion()]
	e.ec2Unlocked = ec2.New(auth, region)
	e.s3Unlocked = s3.New(auth, region)

	// create new storage instances, existing instances continue
	// to reference their existing configuration.
	e.storageUnlocked = &storage{
		bucket: e.s3Unlocked.Bucket(ecfg.controlBucket()),
	}
	if ecfg.publicBucket() != "" {
		e.publicStorageUnlocked = &storage{
			bucket: s3.New(auth, publicBucketRegion).Bucket(ecfg.publicBucket()),
		}
	} else {
		e.publicStorageUnlocked = nil
	}
	return nil
}
Exemple #10
0
func awsInit(config *ec2Config) (ec2Conn *ec2.EC2, err error) {
	auth, err := aws.EnvAuth()

	if err != nil {
		return nil, err
	}

	return ec2.New(auth, config.region), nil
}
Exemple #11
0
func createEC2Handler(region aws.Region) (*ec2.EC2, error) {
	keyId, err := config.GetString("iaas:ec2:key-id")
	if err != nil {
		return nil, err
	}
	secretKey, err := config.GetString("iaas:ec2:secret-key")
	if err != nil {
		return nil, err
	}
	auth := aws.Auth{AccessKey: keyId, SecretKey: secretKey}
	return ec2.New(auth, region), nil
}
func (s *S) TestSignatureWithEndpointPath(c *C) {
	ec2.FakeTime(true)
	defer ec2.FakeTime(false)

	testServer.Response(200, nil, RebootInstancesExample)

	// https://bugs.launchpad.net/goamz/+bug/1022749
	ec2 := ec2.New(s.ec2.Auth, aws.Region{EC2Endpoint: testServer.URL + "/services/Cloud"})

	_, err := ec2.RebootInstances("i-10a64379")
	c.Assert(err, IsNil)

	req := testServer.WaitRequest()
	c.Assert(req.Form["Signature"], DeepEquals, []string{"gdG/vEm+c6ehhhfkrJy3+wuVzw/rzKR42TYelMwti7M="})
}
Exemple #13
0
func (s *S) TestGetPrivateDns(c *gocheck.C) {
	server, err := ec2test.NewServer()
	c.Assert(err, gocheck.IsNil)
	defer server.Quit()
	h := instanceAgentsConfigHealer{}
	region := aws.SAEast
	region.EC2Endpoint = server.URL()
	h.e = ec2.New(aws.Auth{AccessKey: "some", SecretKey: "thing"}, region)
	resp, err := h.ec2().RunInstances(&ec2.RunInstances{MaxCount: 1})
	c.Assert(err, gocheck.IsNil)
	instance := resp.Instances[0]
	dns, err := h.getPrivateDns(instance.InstanceId)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dns, gocheck.Equals, instance.PrivateDNSName)
}
Exemple #14
0
func getEC2Endpoint() *ec2.EC2 {
	access, err := config.GetString("aws:access-key-id")
	if err != nil {
		log.Fatal(err.Error())
	}
	secret, err := config.GetString("aws:secret-access-key")
	if err != nil {
		log.Fatal(err.Error())
	}
	endpoint, err := config.GetString("aws:ec2:endpoint")
	if err != nil {
		log.Fatal(err.Error())
	}
	auth := aws.Auth{AccessKey: access, SecretKey: secret}
	return ec2.New(auth, aws.Region{EC2Endpoint: endpoint})
}
Exemple #15
0
func getEC2Instances(config map[string]string) (instances Instances) {
	instances = make(Instances)

	if _, ok := config["access_key"]; !ok {
		log.Fatal("Missing access_key for ", config["name"], " AWS cloud")
	}

	if _, ok := config["secret_key"]; !ok {
		log.Fatal("Missing secret_key for ", config["name"], " AWS cloud")
	}

	if _, ok := config["region"]; !ok {
		config["region"] = "us-east-1"
	}

	auth := aws.Auth{AccessKey: config["access_key"], SecretKey: config["secret_key"]}

	e := ec2.New(auth, aws.Regions[config["region"]])
	resp, err := e.Instances(nil, nil)

	if err != nil {
		log.Println(err)
		return
	}

	for _, res := range resp.Reservations {
		for _, inst := range res.Instances {

			if inst.DNSName != "" {
				var tags []Tag

				for _, tag := range inst.Tags {
					tags = append(tags, Tag{tag.Key, tag.Value})
				}

				for _, sg := range inst.SecurityGroups {
					tags = append(tags, Tag{"Security group", sg.Name})
				}

				instances[inst.DNSName] = tags
			}
		}
	}

	return
}
Exemple #16
0
func (c *Config) runInstance(s *Server) error {
	e := ec2.New(c.auth, s.Region())
	options := &ec2.RunInstances{
		ImageId:      s.ImageID,
		InstanceType: s.Size,
		KeyName:      c.KeyName,
	}
	resp, err := e.RunInstances(options)
	if err != nil {
		return err
	}
	if len(resp.Instances) != 1 {
		return fmt.Errorf("want 1 instance, got %d", len(resp.Instances))
	}
	s.Instance = &resp.Instances[0]
	return nil
}
Exemple #17
0
func (c *Config) getInstance(s *Server) (*ec2.Instance, error) {
	e := ec2.New(c.auth, s.Region())
	instIds := []string{s.Instance.InstanceId}
	resp, err := e.Instances(instIds, nil)
	if err != nil {
		return nil, err
	}
	if len(resp.Reservations) != 1 {
		return nil, fmt.Errorf("getInstance: want 1 reservation, got %d", len(resp.Reservations))
	}
	r := resp.Reservations[0]
	if len(r.Instances) != 1 {
		return nil, fmt.Errorf("getInstance: want 1 instance, got %d", len(r.Instances))
	}
	s.Instance = &r.Instances[0]
	return &r.Instances[0], nil
}
Exemple #18
0
func (c *Config) Shutdown() error {
	ok := true
	for _, s := range c.Server {
		if s.Instance == nil {
			continue
		}
		e := ec2.New(c.auth, s.Region())
		instIds := []string{s.Instance.InstanceId}
		_, err := e.TerminateInstances(instIds)
		if err != nil {
			log.Println(s.Instance.InstanceId, err)
			ok = false
		}
	}
	if !ok {
		return errors.New("some instances didn't shut down cleanly")
	}
	return nil
}
Exemple #19
0
func (s *S) TestBootstrapInstanceIdHealerNeedsHeal(c *gocheck.C) {
	ec2Server, err := ec2test.NewServer()
	c.Assert(err, gocheck.IsNil)
	defer ec2Server.Quit()
	s3Server, err := s3test.NewServer(nil)
	c.Assert(err, gocheck.IsNil)
	defer s3Server.Quit()
	h := bootstrapInstanceIdHealer{}
	region := aws.SAEast
	region.EC2Endpoint = ec2Server.URL()
	region.S3Endpoint = s3Server.URL()
	h.e = ec2.New(aws.Auth{AccessKey: "some", SecretKey: "thing"}, region)
	h.s = s3.New(aws.Auth{AccessKey: "some", SecretKey: "thing"}, region)
	jujuBucket := "ble"
	config.Set("juju:bucket", jujuBucket)
	bucket := h.s3().Bucket(jujuBucket)
	err = bucket.PutBucket(s3.PublicReadWrite)
	c.Assert(err, gocheck.IsNil)
	err = bucket.Put("provider-state", []byte("doesnotexist"), "binary/octet-stream", s3.PublicReadWrite)
	c.Assert(err, gocheck.IsNil)
	c.Assert(h.needsHeal(), gocheck.Equals, true)
}
Exemple #20
0
func awsInit(config *ec2Config) (ec2Conn *ec2.EC2, err error) {
	auth, err := aws.EnvAuth()

	if err != nil {
		return nil, err
	}

	conn := ec2.New(auth, config.region)

	if config.ami == "" {
		fmt.Println("No AMI specified.  Retrieving default ami")

		amiId, err := defaultAMI(conn)
		if err != nil {
			return nil, fmt.Errorf("Please manually specify an ami.")
		}

		config.ami = amiId
	}

	return conn, nil
}
Exemple #21
0
func (ac *ansibleConfig) findEC2Instance() (*ec2.Instance, error) {
	var (
		err      error
		instance *ec2.Instance
	)

	auth := aws.Auth{
		ac.settings.AccessKeyID,
		ac.settings.SecretAccessKey,
	}
	region := aws.Regions[ac.settings.Region]
	e := ec2.New(auth, region)

	instance, err = searchByFilter(e, "private-ip-address", ac.Host)
	if err != nil {
		instance, err = searchByFilter(e, "ip-address", ac.Host)
	}
	if err != nil {
		return nil, err
	}
	return instance, nil
}
Exemple #22
0
// This will set-up a new spawn pool, if it doesn't exist
// For AWS, we'll track this by security groups
func (pool *AmazonSpawnPool) findOrCreateSecurityGroup() (ec2.SecurityGroup, error) {
	sgName := fmt.Sprintf("fiddler-%s", pool.name)
	groups := []ec2.SecurityGroup{ec2.SecurityGroup{Name: sgName}}

	e := ec2.New(pool.auth, aws.USEast)

	sgResp, sgErr := e.SecurityGroups(groups, nil)
	if sgErr == nil { // We found the security group
		return sgResp.Groups[0].SecurityGroup, nil
	} else {
		log.Println("Creating security group:", pool.name)
		resp, err := e.CreateSecurityGroup(sgName, fmt.Sprintf("Fiddler env for %s", sgName))
		if err != nil {
			return ec2.SecurityGroup{}, err
		}

		// For now, we're going to open up 4001/7001 to the group
		// and ports 21, 80 to the world
		// This should have a config option
		sg := resp.SecurityGroup

		openPorts := []int{22, 80}
		perms := make([]ec2.IPPerm, 2+len(openPorts))
		perms[0] = ec2.IPPerm{Protocol: "tcp", FromPort: 4001, ToPort: 4001, SourceGroups: []ec2.UserSecurityGroup{ec2.UserSecurityGroup{Id: sg.Id}}}
		perms[1] = ec2.IPPerm{Protocol: "tcp", FromPort: 7001, ToPort: 7001, SourceGroups: []ec2.UserSecurityGroup{ec2.UserSecurityGroup{Id: sg.Id}}}
		for i, port := range openPorts {
			perms[i+2] = ec2.IPPerm{Protocol: "tcp", FromPort: port, ToPort: port, SourceIPs: []string{"0.0.0.0/0"}}
		}

		_, err = e.AuthorizeSecurityGroup(sg, perms)
		if err != nil {
			// Delete unauthorized SG?
			return ec2.SecurityGroup{}, err
		}

		return sg, err
	}
}
func (s *S) SetUpSuite(c *C) {
	testServer.Start()
	auth := aws.Auth{"abc", "123"}
	s.ec2 = ec2.New(auth, aws.Region{EC2Endpoint: testServer.URL})
}
Exemple #24
0
func main() {
	// Config File
	data, err := ioutil.ReadFile(home + "/.serverstyle/config.json")
	if err != nil {
		log.Fatal("Unable to read config file")
	}
	err = json.Unmarshal(data, &config)
	if err != nil {
		log.Fatal("Unable to parse config:", err)
	}

	// ScriptPath
	scriptPath = os.ExpandEnv(config.ScriptPath)

	// AWS Auth
	auth, err := aws.EnvAuth()
	if err != nil {
		log.Fatal("AWS AUTH FAIL!")
	}

	// EC2 Connection
	conn = ec2.New(auth, aws.USEast)

	// S3 Connection/Bucket
	s3conn := s3.New(auth, aws.USEast)
	bucket = s3conn.Bucket(config.BucketName)

	// Instances
	instances = GetInstances()

	// Command Loop
	for {
		line := AskQuestion("(MasterStyle): ")
		parts := strings.Split(line, " ")
		cmd := parts[0]
		args := []string{}
		if len(parts) > 1 {
			args = parts[1:]
		}

		switch cmd {
		case "h", "help":
			help()

		case "exit", "q", "quit":
			fmt.Println("Bye-Bye")
			return

		case "launch":
			Launch(args)

		case "ls", "status":
			Status()

		case "security":
			ListSecurityGroups()

		case "tag":
			Tag()

		case "reboot":
			Reboot(args)
		case "start":
			Start(args)
		case "stop":
			Stop(args)
		case "terminate":
			Terminate(args)

		case "update":
			Update(args)
		case "upgrade":
			Upgrade(args)
		case "install":
			Install(args)
		case "ppa":
			PPAInstall(args)
		case "script":
			Script(args)
		case "test":
			Test(args)

		case "easy_install":
			EasyInstall(args)

		case "s3upload":
			S3Upload()

		case "addresses":
			Addresses()
		case "associate":
			Associate()
		case "disassociate":
			Disassociate()

		case "create_image":
			CreateImage(args)

		default:
			fmt.Println("Command Not Found!")
		}
		continue
	}
}
Exemple #25
0
func (s *S) TestInstanceAgenstConfigHealerHeal(c *gocheck.C) {
	server, err := ec2test.NewServer()
	c.Assert(err, gocheck.IsNil)
	defer server.Quit()
	id := server.NewInstances(1, "small", "ami-123", ec2.InstanceState{Code: 16, Name: "running"}, nil)[0]
	p := JujuProvisioner{}
	m := machine{
		AgentState:    "not-started",
		IpAddress:     "localhost",
		InstanceId:    id,
		InstanceState: "running",
	}
	p.saveBootstrapMachine(m)
	conn, collection := p.bootstrapCollection()
	defer conn.Close()
	defer collection.Remove(m)
	a := app.App{
		Name:  "as_i_rise",
		Units: []app.Unit{{Name: "as_i_rise/0", State: "down", Ip: "server-1081.novalocal"}},
	}
	err = s.conn.Apps().Insert(&a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": "as_i_rise"})
	sshTmpdir, err := commandmocker.Error("ssh", "", 1)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	h := instanceAgentsConfigHealer{}
	auth := aws.Auth{AccessKey: "access", SecretKey: "secret"}
	region := aws.SAEast
	region.EC2Endpoint = server.URL()
	h.e = ec2.New(auth, region)
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"grep",
		"",
		"/etc/init/juju-machine-agent.conf",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"sudo",
		"sed",
		"-i",
		"'s/env JUJU_ZOOKEEPER=.*/env JUJU_ZOOKEEPER=\":2181\"/g'",
		"/etc/init/juju-machine-agent.conf",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"grep",
		"",
		"/etc/init/juju-as_i_rise-0.conf",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"sudo",
		"sed",
		"-i",
		"'s/env JUJU_ZOOKEEPER=.*/env JUJU_ZOOKEEPER=\":2181\"/g'",
		"/etc/init/juju-as_i_rise-0.conf",
	}
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(sshTmpdir), gocheck.DeepEquals, sshOutput)
}
Exemple #26
0
func (s *LocalServerSuite) SetUpSuite(c *C) {
	s.srv.SetUp(c)
	s.ServerTests.ec2 = ec2.New(s.srv.auth, s.srv.region)
	s.clientTests.ec2 = ec2.New(s.srv.auth, s.srv.region)
}