Пример #1
0
func Init() (storagedriver.Driver, error) {
	instanceDocument, err := getInstanceIdendityDocument()
	if err != nil {
		return nil, fmt.Errorf("%s: %s", storagedriver.ErrDriverInstanceDiscovery, err)
	}

	auth := aws.Auth{AccessKey: os.Getenv("AWS_ACCESS_KEY"), SecretKey: os.Getenv("AWS_SECRET_KEY")}
	region := os.Getenv("AWS_REGION")
	if region == "" {
		region = instanceDocument.Region
	}
	ec2Instance := ec2.New(
		auth,
		aws.Regions[region],
	)

	// table := InitDD(auth, aws.Regions[instanceDocument.Region])

	driver := &Driver{
		EC2Instance:      ec2Instance,
		InstanceDocument: instanceDocument,
		// DDTable:          table,
	}

	if os.Getenv("REXRAY_DEBUG") == "true" {
		log.Println("Storage Driver Initialized: " + providerName)
	}

	return driver, nil
}
Пример #2
0
func (d *driver) Init(r *core.RexRay) error {
	d.r = r

	var err error
	d.instanceDocument, err = getInstanceIdentityDocument()
	if err != nil {
		return errors.WithFields(ef(), "error getting instance id doc")
	}

	auth := aws.Auth{
		AccessKey: d.r.Config.AwsAccessKey,
		SecretKey: d.r.Config.AwsSecretKey,
	}
	region := d.r.Config.AwsRegion
	if region == "" {
		region = d.instanceDocument.Region
	}
	d.ec2Instance = ec2.New(
		auth,
		aws.Regions[region],
	)

	log.WithField("provider", providerName).Info("storage driver initialized")

	return nil
}
Пример #3
0
func Init(conf *config.Config) (storage.Driver, error) {

	instanceDocument, err := getInstanceIdendityDocument()
	if err != nil {
		return nil, errors.WithFields(ef(), "error getting instance id doc")
	}

	auth := aws.Auth{AccessKey: conf.AwsAccessKey, SecretKey: conf.AwsSecretKey}
	region := conf.AwsRegion
	if region == "" {
		region = instanceDocument.Region
	}
	ec2Instance := ec2.New(
		auth,
		aws.Regions[region],
	)

	// table := InitDD(auth, aws.Regions[instanceDocument.Region])

	driver := &Driver{
		EC2Instance:      ec2Instance,
		InstanceDocument: instanceDocument,
		Config:           conf,
		// DDTable:          table,
	}

	log.WithField("provider", ProviderName).Debug(
		"storage driver initialized")

	return driver, nil
}
Пример #4
0
func (s *AmazonClientSuite) SetUpSuite(c *gocheck.C) {
	if !*amazon {
		c.Skip("AmazonClientSuite tests not enabled")
	}
	s.srv.SetUp(c)
	s.elb = elb.New(s.srv.auth, aws.USEast)
	s.ec2 = ec2.New(s.srv.auth, aws.USEast)
}
Пример #5
0
func (pc *NodeController) getNodeAddEc2Action(c *gin.Context) {

	a, err := models.AccessMapper.FetchOne("ec2")
	if err != nil {
		panic(err)
	}

	if a == nil {

		c.HTML(http.StatusOK, "node_add_ec2.html", map[string]interface{}{})
		return
	}

	auth, err := aws.GetAuth(a.AccessKey, a.PrivateKey, "", time.Now().Add(time.Hour))
	if err != nil {
		panic(err)
	}

	var vpcs []ec2.VPC
	var securityGroups []ec2.SecurityGroupInfo
	region := c.Query("availability_zone")
	vpc := c.Query("vpc")
	securityGroup := c.Query("security_group")
	if region != "" {

		awsec2 := ec2.New(auth, aws.Regions[region])
		res, _ := awsec2.DescribeVpcs(nil, nil)

		if res != nil {
			vpcs = res.VPCs
		}

		if vpc != "" {
			if groups, _ := awsec2.SecurityGroups(nil, nil); groups != nil {
				for _, g := range groups.Groups {
					if g.VpcId == vpc {
						securityGroups = append(securityGroups, g)
					}
				}
			}
		}

	}

	log.Println("vpcs:", vpcs)

	c.HTML(http.StatusOK, "node_add_ec2.html", map[string]interface{}{
		"AccessKey":      a.AccessKey,
		"AWSRegions":     aws.Regions,
		"VPCs":           vpcs,
		"SecurityGroups": securityGroups,
		"query": map[string]interface{}{
			"availability_zone": region,
			"vpc":               vpc,
			"security_group":    securityGroup,
		},
	})
}
Пример #6
0
// NewEC2Client returns a new EC2 client
func (m *AwsMgr) NewEC2Client(accId, region string) (*ec2.EC2, error) {
	// Get Auth
	auth, err := m.GetAuth(accId)
	if err != nil {
		return nil, err
	}
	ec2 := ec2.New(*auth, aws.Regions[region])
	return ec2, nil
}
func main() {
	initFlags()

	filter := ec2.NewFilter()
	for _, t := range tags {
		filter.Add(t.FilterName, t.FilterValue)
	}

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

	for {
		resp, err := e.DescribeInstances(nil, filter)
		if err != nil {
			log.Fatal(err)
		}
		instances := flattenReservations(resp.Reservations)

		tagKeys := tags.Keys()
		if len(tagKeys) == 0 {
			tagKeys = allTagKeys(instances)
		}

		targetGroups := groupByTags(instances, tagKeys)
		b := marshalTargetGroups(targetGroups)
		if dest == "-" {
			_, err = os.Stdout.Write(b)
		} else {
			err = atomicWriteFile(dest, b, ".new")
		}
		if err != nil {
			log.Fatal(err)
		}

		if sleep == 0 {
			break
		} else {
			time.Sleep(sleep)
		}
	}
}
Пример #8
0
func (pc *NodeController) postNodeAddEc2Action(c *gin.Context) {

	var form models.EC2NodeCreateForm
	if err := c.Bind(&form); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	if err := form.Validate(); err != nil {
		c.Redirect(http.StatusFound, c.Request.Referer())
		return
	}

	a, err := models.AccessMapper.FetchOne("ec2")
	if err != nil {
		panic(err)
	}

	if a == nil {
		c.Redirect(http.StatusFound, c.Request.Referer())
		return
	}

	basicAuth := env.Get("BASIC_AUTH")
	if auth := env.Get("BASIC_AUTH"); auth != "" {
		basicAuth = "-u " + auth + " "
	}

	auth, err := aws.GetAuth(a.AccessKey, a.PrivateKey, "", time.Now().Add(time.Hour))
	if err != nil {
		panic(err)
	}
	awsec2 := ec2.New(auth, aws.Regions[form.AvailabilityZone])
	// Create public key
	// Waiting for merge pull request https://github.com/goamz/goamz/pull/111
	// {
	// key, err := ssh.GetPublicKey()
	// 	if err != nil {
	// 		panic(err)
	// 	}
	// 	if _, err := awsec2.ImportKeyPair(&ImportKeyPairOptions{
	// 		KeyName:           "karhu",
	// 		PublicKeyMaterial: string(key),
	// 	}); err != nil {
	// 		panic(err)
	// 	}
	// }

	if _, err := awsec2.RunInstances(&ec2.RunInstancesOptions{
		ImageId:        "ami-e31a6594",
		MinCount:       1,
		MaxCount:       0,
		KeyName:        "karhu",
		InstanceType:   form.InstanceType,
		SecurityGroups: []ec2.SecurityGroup{{Id: form.SecurityGroup}},
		// KernelId               :  string
		// RamdiskId              :  string
		UserData: []byte(fmt.Sprintf(`#!/bin/bash
sudo apt-get update && \
sudo apt-get install -y curl && \
curl %s"%s/api/nodes/register.sh?monit=1&ssh_port=22" | sudo -i -u admin bash`, basicAuth, env.Get("PUBLIC_HOST"))),
		AvailabilityZone: "eu-west-1c", // Waiting for https://github.com/goamz/goamz/pull/112
		// PlacementGroupName     :  string
		Tenancy:    "default",
		Monitoring: form.Monitoring == "on",
		SubnetId:   "subnet-425a4f27", // Waiting for https://github.com/goamz/goamz/pull/112
		// DisableAPITermination  :  bool
		// ShutdownBehavior       :  string
		// PrivateIPAddress       :  string
		// IamInstanceProfile      : IamInstanceProfile
		// BlockDevices            : []BlockDeviceMapping
		// EbsOptimized            : bool
		// AssociatePublicIpAddress :bool
	}); err != nil {
		panic(err)
	}

	c.Redirect(http.StatusFound, "/nodes")
}
Пример #9
0
func (d *driver) CopySnapshot(runAsync bool,
	volumeID, snapshotID, snapshotName, destinationSnapshotName,
	destinationRegion string) (*core.Snapshot, error) {

	if volumeID == "" && snapshotID == "" && snapshotName == "" {
		return nil, errors.New("Missing volumeID, snapshotID, or snapshotName")
	}

	snapshots, err := d.getSnapshot(volumeID, snapshotID, snapshotName)
	if err != nil {
		return nil, err
	}

	if len(snapshots) > 1 {
		return nil, errors.ErrMultipleVolumesReturned
	} else if len(snapshots) == 0 {
		return nil, errors.ErrNoVolumesReturned
	}

	snapshotID = snapshots[0].Id

	options := &ec2.CopySnapshot{
		SourceRegion:      d.ec2Instance.Region.Name,
		DestinationRegion: destinationRegion,
		SourceSnapshotId:  snapshotID,
		Description: fmt.Sprintf("[Copied %s from %s]",
			snapshotID, d.ec2Instance.Region.Name),
	}
	resp := &ec2.CopySnapshotResp{}

	auth := aws.Auth{
		AccessKey: d.r.Config.AwsAccessKey,
		SecretKey: d.r.Config.AwsSecretKey}
	destec2Instance := ec2.New(
		auth,
		aws.Regions[destinationRegion],
	)

	origec2Instance := d.ec2Instance
	d.ec2Instance = destec2Instance
	defer func() { d.ec2Instance = origec2Instance }()

	resp, err = d.ec2Instance.CopySnapshot(options)
	if err != nil {
		return nil, err
	}

	if destinationSnapshotName != "" {
		_, err := d.ec2Instance.CreateTags(
			[]string{resp.SnapshotId},
			[]ec2.Tag{{"Name", destinationSnapshotName}})

		if err != nil {
			return nil, err
		}
	}

	if !runAsync {
		log.Println("Waiting for snapshot copy to complete")
		err = d.waitSnapshotComplete(resp.SnapshotId)
		if err != nil {
			return nil, err
		}
	}

	snapshot, err := d.GetSnapshot("", resp.SnapshotId, "")
	if err != nil {
		return nil, err
	}

	return snapshot[0], nil
}
Пример #10
0
//helper function for getting an EC2 handle at US east
func getUSEast(creds aws.Auth) *ec2.EC2 {
	return ec2.New(creds, aws.USEast)
}
Пример #11
0
// Periodically populate the host-inventory:
func Updater(config *types.Config) {

	log.Infof("[hostInventoryUpdater] Started")

	updateFrequency := 5

	// Run forever:
	for {

		// Sleep until the next run:
		log.Debugf("[hostInventoryUpdater] Sleeping for %vs ...", updateFrequency)
		time.Sleep(time.Duration(updateFrequency) * time.Second)

		// Authenticate with AWS:
		awsAuth, err := aws.GetAuth("", "", "", time.Now())
		if err != nil {
			log.Errorf("[hostInventoryUpdater] Unable to authenticate to AWS! (%s)", err)
			continue
		} else {
			log.Debugf("[hostInventoryUpdater] Authenticated to AWS")
		}

		// Make a new EC2 connection:
		log.Debugf("[hostInventoryUpdater] Connecting to EC2 ...")
		ec2Connection := ec2.New(awsAuth, aws.Regions[config.AWSRegion])

		// Prepare a filter:
		filter := ec2.NewFilter()
		filter.Add("instance-state-name", "running")

		// Make a "DescribeInstances" call (lists ALL instances in your account):
		describeInstancesResponse, err := ec2Connection.DescribeInstances([]string{}, filter)
		if err != nil {
			log.Errorf("[hostInventoryUpdater] Failed to make describe-instances call: %v", err)
		} else {
			log.Debugf("[hostInventoryUpdater] Found %v instances running in your account", len(describeInstancesResponse.Reservations))

			// Lock the host-list (so we don't change it while another goroutine is using it):
			log.Tracef("[hostInventoryUpdater] Trying to lock config.HostInventoryMutex ...")
			config.HostInventoryMutex.Lock()
			log.Tracef("[hostInventoryUpdater] Locked config.HostInventoryMutex")

			// Clear out the existing host-inventory:
			config.HostInventory = types.HostInventory{
				Environments: make(map[string]types.Environment),
			}

			// Re-populate it from the describe instances response:
			for _, reservation := range describeInstancesResponse.Reservations {

				// Search for our role and environment tags:
				var role, environment string
				for _, tag := range reservation.Instances[0].Tags {
					if tag.Key == config.RoleTag {
						role = tag.Value
					}
					if tag.Key == config.EnvironmentTag {
						environment = tag.Value
					}
				}

				// Make sure we have environment and role tags:
				if environment == "" || role == "" {
					log.Debugf("[hostInventoryUpdater] Instance (%v) must have both 'environment' and 'role' metadata in order for DNS records to be creted!", reservation.Instances[0].InstanceId)

					// Continue with the next instance:
					continue
				} else {
					log.Infof("[hostInventoryUpdater] Building records for instance (%v) in zone (%v) ...", reservation.Instances[0].InstanceId, reservation.Instances[0].AvailabilityZone)
				}

				// Add a new environment to the inventory (unless we already have it):
				if _, ok := config.HostInventory.Environments[environment]; !ok {
					config.HostInventory.Environments[environment] = types.Environment{
						DNSRecords: make(map[string][]string),
					}
				}

				// Either create or add to the role-per-zone record:
				internalZoneRecord := fmt.Sprintf("%v.%v.i.%v.%v", role, reservation.Instances[0].AvailabilityZone, environment, config.DNSDomainName)
				if _, ok := config.HostInventory.Environments[environment].DNSRecords[internalZoneRecord]; !ok {
					config.HostInventory.Environments[environment].DNSRecords[internalZoneRecord] = []string{reservation.Instances[0].PrivateIPAddress}
				} else {
					config.HostInventory.Environments[environment].DNSRecords[internalZoneRecord] = append(config.HostInventory.Environments[environment].DNSRecords[internalZoneRecord], reservation.Instances[0].PrivateIPAddress)
				}

				// Either create or add to the role-per-region record:
				internalRegionRecord := fmt.Sprintf("%v.%v.i.%v.%v", role, config.AWSRegion, environment, config.DNSDomainName)
				if _, ok := config.HostInventory.Environments[environment].DNSRecords[internalRegionRecord]; !ok {
					config.HostInventory.Environments[environment].DNSRecords[internalRegionRecord] = []string{reservation.Instances[0].PrivateIPAddress}
				} else {
					config.HostInventory.Environments[environment].DNSRecords[internalRegionRecord] = append(config.HostInventory.Environments[environment].DNSRecords[internalRegionRecord], reservation.Instances[0].PrivateIPAddress)
				}

				// Either create or add to the external record:
				if reservation.Instances[0].IPAddress != "" {
					externalRecord := fmt.Sprintf("%v.%v.e.%v.%v", role, config.AWSRegion, environment, config.DNSDomainName)
					if _, ok := config.HostInventory.Environments[environment].DNSRecords[externalRecord]; !ok {
						config.HostInventory.Environments[environment].DNSRecords[externalRecord] = []string{reservation.Instances[0].IPAddress}
					} else {
						config.HostInventory.Environments[environment].DNSRecords[externalRecord] = append(config.HostInventory.Environments[environment].DNSRecords[externalRecord], reservation.Instances[0].IPAddress)
					}
				}

			}

		}

		// Unlock the host-inventory:
		log.Tracef("[hostInventoryUpdater] Unlocking config.HostInventoryMutex ...")
		config.HostInventoryMutex.Unlock()

		// Now set the sleep time to the correct value:
		updateFrequency = config.HostUpdateFrequency

	}

}