Created
October 6, 2012 21:08
-
-
Save benjaminws/3846137 to your computer and use it in GitHub Desktop.
playing with aws/goroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Attempt at provisioning aws instances via goroutines/channels | |
// Start playing with porting microarmy | |
package main | |
import ( | |
"launchpad.net/goamz/aws" | |
"launchpad.net/goamz/ec2" | |
) | |
// Probably wasn't necessary to extract this, but hey | |
func setupInstanceTypes() ec2.RunInstances { | |
options := ec2.RunInstances{ | |
ImageId: "ami-ccf405a5", | |
InstanceType: "t1.micro", | |
} | |
return options | |
} | |
// Fire up the instances, drop the response on a channel | |
func runInstances(options *ec2.RunInstances, e **ec2.EC2, resp_out chan ec2.RunInstancesResp) { | |
resp, err := e.RunInstances(options) | |
if err != nil { | |
panic(err) | |
} | |
resp_out <- *resp | |
// Close out the channel so our consumers know we're done | |
close(resp_out) | |
} | |
// Listen for a message off the channel, notify us when we're done | |
func notifyComplete(resp_in chan ec2.RunInstancesResp, done chan<- bool) { | |
// Holy shit I don't remember how to dereference pointers | |
resp := <- *(&resp_in) | |
for _, instance := range resp.Instances { | |
println("Now running", instance.InstanceId) | |
} | |
println("Make sure you terminate instances to stop the cash flow.") | |
done <- true | |
} | |
// I don't know how computers work | |
func main() { | |
auth, err := aws.EnvAuth() | |
if err != nil { | |
panic(err) | |
} | |
e := ec2.New(auth, aws.USEast) | |
// Setup our channels | |
responseChan := make(chan ec2.RunInstancesResp) | |
done := make(chan bool) | |
options := setupInstanceTypes() | |
// Should be able to fire off as many of these as you want, will scale | |
go runInstances(&options, &e, responseChan) | |
go notifyComplete(responseChan, done) | |
<-done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Show me how to go better.