Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
| 8 | |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 9 | var provider, username, password string |
| 10 | |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 11 | var region, serverName, imageRef, flavorRef *string |
| 12 | var adminPass = flag.String("a", "", "Administrator password (auto-assigned if none)") |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 13 | var quiet = flag.Bool("quiet", false, "Quiet mode for acceptance tests. $? non-zero if error.") |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 14 | |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 15 | func configure() { |
| 16 | provider, username, password = getCredentials() |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 17 | region = flag.String("r", "DFW", "Rackspace region in which to create the server") |
| 18 | serverName = flag.String("n", randomString(16), "Server name (what you see in the control panel)") |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 19 | imageRef = flag.String("i", "", "ID of image to deploy onto the server") |
| 20 | flavorRef = flag.String("f", "", "Flavor of server to deploy image upon") |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 21 | |
| 22 | flag.Parse() |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 23 | } |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 24 | |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 25 | func aSuitableImage(api gophercloud.CloudServersProvider) string { |
| 26 | images, err := api.ListImages() |
| 27 | if err != nil { |
| 28 | panic(err) |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 29 | } |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 30 | |
| 31 | // TODO(sfalvo): |
| 32 | // Works for Rackspace, might not work for your provider! |
| 33 | // Need to figure out why ListImages() provides 0 values for |
| 34 | // Ram and Disk fields. |
| 35 | // |
| 36 | // Until then, just return Ubuntu 12.04 LTS. |
| 37 | for i := 0; i < len(images); i++ { |
| 38 | if images[i].Id == "6a668bb8-fb5d-407a-9a89-6f957bced767" { |
| 39 | return images[i].Id |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 42 | panic("Image 6a668bb8-fb5d-407a-9a89-6f957bced767 (Ubuntu 12.04 LTS) not found.") |
| 43 | } |
| 44 | |
| 45 | func aSuitableFlavor(api gophercloud.CloudServersProvider) string { |
| 46 | flavors, err := api.ListFlavors() |
| 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | |
| 51 | // TODO(sfalvo): |
| 52 | // Works for Rackspace, might not work for your provider! |
| 53 | // Need to figure out why ListFlavors() provides 0 values for |
| 54 | // Ram and Disk fields. |
| 55 | // |
| 56 | // Until then, just return Ubuntu 12.04 LTS. |
| 57 | for i := 0; i < len(flavors); i++ { |
| 58 | if flavors[i].Id == "2" { |
| 59 | return flavors[i].Id |
| 60 | } |
| 61 | } |
| 62 | panic("Flavor 2 (512MB 1-core 20GB machine) not found.") |
| 63 | } |
| 64 | |
| 65 | func main() { |
| 66 | configure() |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 67 | |
| 68 | auth, err := gophercloud.Authenticate( |
| 69 | provider, |
| 70 | gophercloud.AuthOptions{ |
| 71 | Username: username, |
| 72 | Password: password, |
| 73 | }, |
| 74 | ) |
| 75 | if err != nil { |
| 76 | panic(err) |
| 77 | } |
| 78 | |
| 79 | servers, err := gophercloud.ServersApi(auth, gophercloud.ApiCriteria{ |
| 80 | Name: "cloudServersOpenStack", |
| 81 | Region: *region, |
| 82 | VersionId: "2", |
| 83 | UrlChoice: gophercloud.PublicURL, |
| 84 | }) |
| 85 | if err != nil { |
| 86 | panic(err) |
| 87 | } |
| 88 | |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 89 | if *imageRef == "" { |
| 90 | *imageRef = aSuitableImage(servers) |
| 91 | } |
| 92 | |
| 93 | if *flavorRef == "" { |
| 94 | *flavorRef = aSuitableFlavor(servers) |
| 95 | } |
| 96 | |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 97 | _, err = servers.CreateServer(gophercloud.NewServer{ |
| 98 | Name: *serverName, |
| 99 | ImageRef: *imageRef, |
| 100 | FlavorRef: *flavorRef, |
| 101 | AdminPass: *adminPass, |
| 102 | }) |
| 103 | if err != nil { |
| 104 | panic(err) |
| 105 | } |
| 106 | |
| 107 | allServers, err := servers.ListServers() |
| 108 | if err != nil { |
| 109 | panic(err) |
| 110 | } |
| 111 | |
Samuel A. Falvo II | 4b0ba75 | 2013-07-11 17:42:26 -0700 | [diff] [blame^] | 112 | if !*quiet { |
| 113 | fmt.Printf("ID,Name,Status,Progress\n") |
| 114 | for _, i := range allServers { |
| 115 | fmt.Printf("%s,\"%s\",%s,%d\n", i.Id, i.Name, i.Status, i.Progress) |
| 116 | } |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 117 | } |
| 118 | } |