Last active
February 7, 2017 18:33
-
-
Save taroooyan/5c6ba64259b946d0f3e75a4556ada39f 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" | |
"github.com/sacloud/libsacloud/api" | |
"os" | |
"time" | |
) | |
func main() { | |
// settings | |
var ( | |
token = os.Args[1] | |
secret = os.Args[2] | |
zone = os.Args[3] | |
name = "libsacloud demo" | |
description = "libsacloud demo description" | |
tag = "libsacloud-test" | |
cpu = 1 | |
mem = 2 | |
hostName = "libsacloud-test" | |
password = "C8#mf92mp!*s" | |
// sshPublicKey = "ssh-rsa AAAA..." | |
) | |
// authorize | |
client := api.NewClient(token, secret, zone) | |
//search archives | |
fmt.Println("searching archives") | |
archive, _ := client.Archive.FindLatestStableCentOS() | |
// search scripts | |
// fmt.Println("searching scripts") | |
// res, _ := client.Note. | |
// WithNameLike("WordPress"). | |
// WithSharedScope(). | |
// Limit(1). | |
// Find() | |
// script := res.Notes[0] | |
// create a disk | |
fmt.Println("creating a disk") | |
disk := client.Disk.New() | |
disk.Name = name | |
disk.Description = description | |
disk.Tags = []string{tag} | |
disk.SetDiskPlanToSSD() | |
disk.SetSourceArchive(archive.ID) | |
disk, _ = client.Disk.Create(disk) | |
// create a server | |
fmt.Println("creating a server") | |
server := client.Server.New() | |
server.Name = name | |
server.Description = description | |
server.Tags = []string{tag} | |
// set ServerPlan | |
plan, _ := client.Product.Server.GetBySpec(cpu, mem) | |
server.SetServerPlanByID(plan.GetStrID()) | |
server, _ = client.Server.Create(server) | |
// connect to shared segment | |
fmt.Println("connecting the server to shared segment") | |
iface, _ := client.Interface.CreateAndConnectToServer(server.ID) | |
client.Interface.ConnectToSharedSegment(iface.ID) | |
// wait disk copy | |
err := client.Disk.SleepWhileCopying(disk.ID, 120*time.Second) | |
if err != nil { | |
fmt.Println("failed") | |
os.Exit(1) | |
} | |
// config the disk | |
diskConf := client.Disk.NewCondig() | |
diskConf.HostName = &hostName | |
diskConf.Password = &password | |
// diskConf.SSHKey.PublicKey = sshPublicKey | |
// diskConf.AddNote(string(script.ID)) | |
client.Disk.Config(disk.ID, diskConf) | |
// connect to server | |
client.Disk.ConnectToServer(disk.ID, server.ID) | |
// boot | |
fmt.Println("booting the server") | |
client.Server.Boot(server.ID) | |
// stop | |
time.Sleep(3 * time.Second) | |
fmt.Println("stopping the server") | |
client.Server.Stop(server.ID) | |
// wait for server to down | |
err = client.Server.SleepUntilDown(server.ID, 120*time.Second) | |
if err != nil { | |
fmt.Println("failed") | |
os.Exit(1) | |
} | |
// disconnect the disk from the server | |
fmt.Println("disconnecting the disk") | |
client.Disk.DisconnectFromServer(disk.ID) | |
// delete the server | |
fmt.Println("deleting the server") | |
client.Server.Delete(server.ID) | |
// delete the disk | |
fmt.Println("deleting the disk") | |
client.Disk.Delete(disk.ID) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment