Skip to content

Commit

Permalink
Tag volumes with filesystem
Browse files Browse the repository at this point in the history
to avoid race condition when taking newly created volumes by other
instances
  • Loading branch information
Fabian Stehle committed Jan 21, 2016
1 parent 205c647 commit 7033195
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ func (asgEbs *AsgEbs) findVolume(tagKey string, tagValue string) (*string, error
aws.String(tagValue),
},
},
{
Name: aws.String("tag:filesystem"),
Values: []*string{
aws.String("true"),
},
},
{
Name: aws.String("status"),
Values: []*string{
Expand Down Expand Up @@ -150,6 +156,10 @@ func (asgEbs *AsgEbs) createVolume(createSize int64, createName string, createVo
Key: aws.String("Name"),
Value: aws.String(createName),
},
{
Key: aws.String("filesystem"),
Value: aws.String("false"),
},
}
for k, v := range createTags {
tags = append(tags,
Expand Down Expand Up @@ -234,8 +244,28 @@ func (asgEbs *AsgEbs) attachVolume(volumeId string, attachAs string, deleteOnTer
return nil
}

func (asgEbs *AsgEbs) makeFileSystem(device string) error {
return run("/usr/sbin/mkfs.ext4", device)
func (asgEbs *AsgEbs) makeFileSystem(device string, volumeId string) error {
svc := ec2.New(session.New(asgEbs.AwsConfig))

err := run("/usr/sbin/mkfs.ext4", device)
if err != nil {
return err
}
tags := []*ec2.Tag{
{
Key: aws.String("filesystem"),
Value: aws.String("true"),
},
}
createTagsInput := &ec2.CreateTagsInput{
Resources: []*string{aws.String(volumeId)},
Tags: tags,
}
_, err = svc.CreateTags(createTagsInput)
if err != nil {
return err
}
return nil
}

func (asgEbs *AsgEbs) mountVolume(device string, mountPoint string) error {
Expand Down Expand Up @@ -346,7 +376,7 @@ func main() {

if volumeCreated {
log.WithFields(log.Fields{"device": attachAsDevice}).Info("Creating file system on new volume")
err = asgEbs.makeFileSystem(attachAsDevice)
err = asgEbs.makeFileSystem(attachAsDevice, *volume)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to create file system")
}
Expand Down

0 comments on commit 7033195

Please sign in to comment.