Created
April 30, 2017 18:26
-
-
Save mbonell/528ebf1fa01f6765313981c6e3ebcbd3 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"time" | |
"github.com/gophercloud/gophercloud" | |
"github.com/gophercloud/gophercloud/openstack" | |
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers" | |
) | |
func main() { | |
// Get OpenStack auth values from environment | |
authOpts, err := openstack.AuthOptionsFromEnv() | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
// Autenticate into OpenStack | |
provider, err := openstack.AuthenticatedClient(authOpts) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
// Create the service client for interact with swift | |
var regionName = os.Getenv("OS_REGION_NAME") | |
computeClient, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ | |
Region: regionName, | |
}) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
// Script to execute after the instance creation | |
userData := fmt.Sprintf(`#!/usr/bin/env bash | |
wget https://gist.githubusercontent.com/MBonell/eb7c8ff166077f742a1157d89293b339/raw/611b5eb7e95ab773a4675e1b6dcdd851941d903c/init.sh | |
bash init.sh`) | |
// Create a worker instance | |
log.Println("Creating web server and deploying website...") | |
server, err := servers.Create(computeClient, servers.CreateOpts{ | |
Name: "webserver-" + time.Now().Format("2006-01-02-15:04:05"), | |
FlavorRef: "92416632-fece-42f0-a6a4-70b2d416fa1d", | |
ImageRef: "bcc83dea-fa28-46f4-bafe-5b95990afa84", | |
SecurityGroups: []string{"web"}, | |
UserData: []byte(userData), | |
}).Extract() | |
if err != nil { | |
log.Println("[Error] Unable to create web server: " + err.Error()) | |
return | |
} | |
log.Println("[Success] Web server created!, ID: " + server.ID) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment