README.md
1libsacloud
2===
3
4[![GoDoc](https://godoc.org/github.com/sacloud/libsacloud?status.svg)](https://godoc.org/github.com/sacloud/libsacloud)
5[![Build Status](https://travis-ci.org/sacloud/libsacloud.svg?branch=master)](https://travis-ci.org/sacloud/libsacloud)
6[![Go Report Card](https://goreportcard.com/badge/github.com/sacloud/libsacloud)](https://goreportcard.com/report/github.com/sacloud/libsacloud)
7
8This project provides various Go packages to perform operations
9on [`SAKURA CLOUD APIs`](http://developer.sakura.ad.jp/cloud/api/1.1/).
10
11See list of implemented API clients under this.
12
13 - [High-level API(builder package)](https://godoc.org/github.com/sacloud/libsacloud/builder)
14 - [Low-level API(api package)](https://godoc.org/github.com/sacloud/libsacloud/api)
15 - [Model defines(sacloud package)](https://godoc.org/github.com/sacloud/libsacloud/sacloud)
16
17# Installation
18
19 go get -d github.com/sacloud/libsacloud
20
21# Sample (High-level API)
22
23This sample will create a server by using a High-level API.
24
25High-level API's document is [here](https://godoc.org/github.com/sacloud/libsacloud/builder).
26
27## Create a server
28
29```go
30package main
31
32import (
33 "fmt"
34 "github.com/sacloud/libsacloud/api"
35 "github.com/sacloud/libsacloud/builder"
36 "github.com/sacloud/libsacloud/sacloud/ostype"
37)
38
39var (
40 token = "PUT-YOUR-TOKEN" // API token
41 secret = "PUT-YOUR-SECRET" // API secret
42 zone = "tk1a" // target zone [tk1a or is1b]
43 serverName = "example-server" // server name
44 password = "PUT-YOUR-PASSWORD" // password
45 core = 2 // cpu core
46 memory = 4 // memory size(GB)
47 diskSize = 100 // disk size(GB)
48
49 // public key
50 sshKey = "ssh-rsa AAAA..."
51
52 // startup script
53 script = `#!/bin/bash
54yum -y update || exit 1
55exit 0`
56)
57
58func main() {
59
60 // create SakuraCloud API client
61 client := api.NewClient(token, secret, zone)
62
63 // Create server using CentOS public archive
64 builder := builder.ServerPublicArchiveUnix(client, ostype.CentOS, serverName, password)
65 builder.AddPublicNWConnectedNIC() // connect shared segment
66 builder.SetCore(core) // set cpu core
67 builder.SetMemory(memory) // set memory size
68 builder.SetDiskSize(diskSize) // set disk size
69 builder.AddSSHKey(sshKey) // regist ssh public key
70 builder.SetDisablePWAuth(true) // disable password auth
71 builder.AddNote(script) // regist startup script
72 result, err := builder.Build()
73
74 if err != nil {
75 panic(err)
76 }
77 fmt.Printf("%#v", result.Server)
78}
79```
80
81
82# Sample (Low-level API)
83
84This sample is a translation of the examples of [saklient](http://sakura-internet.github.io/saklient.doc/) to golang.
85
86Original(saklient) sample codes is [here](http://sakura-internet.github.io/saklient.doc/).
87
88Low-level API's document is [here](https://godoc.org/github.com/sacloud/libsacloud/api).
89
90## Create a server
91
92```go
93
94package main
95
96import (
97 "fmt"
98 "github.com/sacloud/libsacloud/api"
99 "os"
100 "time"
101)
102
103func main() {
104
105 // settings
106 var (
107 token = os.Args[1]
108 secret = os.Args[2]
109 zone = os.Args[3]
110 name = "libsacloud demo"
111 description = "libsacloud demo description"
112 tag = "libsacloud-test"
113 cpu = 1
114 mem = 2
115 hostName = "libsacloud-test"
116 password = "C8#mf92mp!*s"
117 sshPublicKey = "ssh-rsa AAAA..."
118 )
119
120 // authorize
121 client := api.NewClient(token, secret, zone)
122
123 //search archives
124 fmt.Println("searching archives")
125 archive, _ := client.Archive.FindLatestStableCentOS()
126
127 // search scripts
128 fmt.Println("searching scripts")
129 res, _ := client.Note.
130 WithNameLike("WordPress").
131 WithSharedScope().
132 Limit(1).
133 Find()
134 script := res.Notes[0]
135
136 // create a disk
137 fmt.Println("creating a disk")
138 disk := client.Disk.New()
139 disk.Name = name
140 disk.Description = description
141 disk.Tags = []string{tag}
142 disk.SetDiskPlanToSSD()
143 disk.SetSourceArchive(archive.ID)
144
145 disk, _ = client.Disk.Create(disk)
146
147 // create a server
148 fmt.Println("creating a server")
149 server := client.Server.New()
150 server.Name = name
151 server.Description = description
152 server.Tags = []string{tag}
153
154 // set ServerPlan
155 plan, _ := client.Product.Server.GetBySpec(cpu, mem)
156 server.SetServerPlanByID(plan.GetStrID())
157
158 server, _ = client.Server.Create(server)
159
160 // connect to shared segment
161
162 fmt.Println("connecting the server to shared segment")
163 iface, _ := client.Interface.CreateAndConnectToServer(server.ID)
164 client.Interface.ConnectToSharedSegment(iface.ID)
165
166 // wait disk copy
167 err := client.Disk.SleepWhileCopying(disk.ID, 120*time.Second)
168 if err != nil {
169 fmt.Println("failed")
170 os.Exit(1)
171 }
172
173 // config the disk
174 diskConf := client.Disk.NewCondig()
175 diskConf.SetHostName(hostName)
176 diskConf.SetPassword(password)
177 diskConf.AddSSHKeyByString(sshPublicKey)
178 diskConf.AddNote(script.GetStrID())
179 client.Disk.Config(disk.ID, diskConf)
180
181 // connect to server
182 client.Disk.ConnectToServer(disk.ID, server.ID)
183
184 // boot
185 fmt.Println("booting the server")
186 client.Server.Boot(server.ID)
187
188 // stop
189 time.Sleep(3 * time.Second)
190 fmt.Println("stopping the server")
191 client.Server.Stop(server.ID)
192
193 // wait for server to down
194 err = client.Server.SleepUntilDown(server.ID, 120*time.Second)
195 if err != nil {
196 fmt.Println("failed")
197 os.Exit(1)
198 }
199
200 // disconnect the disk from the server
201 fmt.Println("disconnecting the disk")
202 client.Disk.DisconnectFromServer(disk.ID)
203
204 // delete the server
205 fmt.Println("deleting the server")
206 client.Server.Delete(server.ID)
207
208 // delete the disk
209 fmt.Println("deleting the disk")
210 client.Disk.Delete(disk.ID)
211}
212
213```
214
215## Download a disk image
216
217**Pre requirements**
218 * install ftps libs. please run `go get github.com/webguerilla/ftps`
219 * create a disk named "GitLab"
220
221```go
222
223package main
224
225import (
226 "fmt"
227 "github.com/webguerilla/ftps"
228 API "github.com/sacloud/libsacloud/api"
229 "os"
230 "time"
231)
232
233func main() {
234
235 // settings
236 var (
237 token = os.Args[1]
238 secret = os.Args[2]
239 zone = os.Args[3]
240 srcName = "GitLab"
241 )
242
243 // authorize
244 api := API.NewClient(token, secret, zone)
245
246 // search the source disk
247 res, _ := api.Disk.
248 WithNameLike(srcName).
249 Limit(1).
250 Find()
251 if res.Count == 0 {
252 panic("Disk `GitLab` not found")
253 }
254
255 disk := res.Disks[0]
256
257 // copy the disk to a new archive
258 fmt.Println("copying the disk to a new archive")
259
260 archive := api.Archive.New()
261 archive.Name = fmt.Sprintf("Copy:%s", disk.Name)
262 archive.SetSourceDisk(disk.ID)
263 archive, _ = api.Archive.Create(archive)
264 api.Archive.SleepWhileCopying(archive.ID, 180*time.Second)
265
266 // get FTP information
267 ftp, _ := api.Archive.OpenFTP(archive.ID, false)
268 fmt.Println("FTP information:")
269 fmt.Println(" user: " + ftp.User)
270 fmt.Println(" pass: " + ftp.Password)
271 fmt.Println(" host: " + ftp.HostName)
272
273 // download the archive via FTPS
274 ftpsClient := &ftps.FTPS{}
275 ftpsClient.TLSConfig.InsecureSkipVerify = true
276 ftpsClient.Connect(ftp.HostName, 21)
277 ftpsClient.Login(ftp.User, ftp.Password)
278 err := ftpsClient.RetrieveFile("archive.img", "archive.img")
279 if err != nil {
280 panic(err)
281 }
282 ftpsClient.Quit()
283
284 // delete the archive after download
285 fmt.Println("deleting the archive")
286 api.Archive.CloseFTP(archive.ID)
287 api.Archive.Delete(archive.ID)
288
289}
290
291```
292
293# License
294
295 `libsacloud` Copyright (C) 2016-2020 The Libsacloud Authors.
296
297 This project is published under [Apache 2.0 License](LICENSE).
298
299