-
Notifications
You must be signed in to change notification settings - Fork 0
/
solus.go
136 lines (116 loc) · 3.98 KB
/
solus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"context"
"fmt"
"github.com/solusio/solus-go-sdk"
"net/url"
"time"
)
func createVirtualServers(settings ImportSettings, importPlanFilePath string, recreate bool) error {
plan, err := loadImportPlan(importPlanFilePath)
if err != nil {
return err
}
plan.Settings = settings
if err := plan.Validate(); err != nil {
return fmt.Errorf("failed to validate import plan: %v", err)
}
baseURL, err := url.Parse(plan.Settings.APIURL)
if err != nil {
return fmt.Errorf("parse api url %q: %w", plan.Settings.APIURL, err)
}
client, err := solus.NewClient(baseURL, solus.APITokenAuthenticator{Token: plan.Settings.APIToken})
if err != nil {
return fmt.Errorf("create client: %w", err)
}
for i, vsPlan := range plan.VirtualServers {
if vsPlan.VirtualServerID != 0 && !recreate {
continue
}
crID := plan.Settings.Defaults.ComputeResourceID
if vsPlan.ComputeResourceID != 0 {
crID = vsPlan.ComputeResourceID
}
for i, d := range vsPlan.AdditionalDisks {
if d.DiskOfferID == 0 {
vsPlan.AdditionalDisks[i].DiskOfferID = plan.Settings.Defaults.AdditionalDiskOfferID
}
}
data := solus.VirtualServerCreateRequest{
Name: vsPlan.Hostname,
SSHKeys: append(plan.Settings.Defaults.SSHKeys, vsPlan.SSHKeys...),
ProjectID: plan.Settings.Defaults.ProjectID,
LocationID: plan.Settings.Defaults.LocationID,
ComputeResourceID: crID,
OSImageVersionID: plan.Settings.Defaults.GuestOSToOSImageVersionID[vsPlan.GuestOS],
CustomPlan: &vsPlan.CustomPlan,
AdditionalIPCount: vsPlan.AdditionalIPv4,
PrimaryIP: vsPlan.PrimaryIP,
AdditionalDisks: diskToAdditionalDiskCreateRequest(vsPlan.AdditionalDisks),
MacAddress: vsPlan.MacAddress,
Firmware: vsPlan.Firmware,
}
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
vs, err := client.VirtualServers.Create(ctx, data)
if err != nil {
cancel()
return fmt.Errorf("create virtual server %q: %w", vsPlan.Hostname, err)
}
cancel()
fmt.Printf("Virtual server %q created as ID %d\n", vsPlan.Hostname, vs.ID)
ctx, cancel = context.WithTimeout(context.Background(), 35*time.Second)
disks, err := client.VirtualServers.Disks(ctx, vs.ID)
if err != nil {
cancel()
return fmt.Errorf("create virtual server %q: %w", vsPlan.Hostname, err)
}
cancel()
plan.VirtualServers[i].VirtualServerUUID = vs.UUID
plan.VirtualServers[i].VirtualServerID = vs.ID
for _, dest := range disks {
if dest.IsPrimary {
plan.VirtualServers[i].PrimaryDiskDestinationPath = dest.FullPath
continue
}
for y, source := range plan.VirtualServers[i].AdditionalDisks {
if dest.Name == source.Name {
plan.VirtualServers[i].AdditionalDisks[y].DestinationPath = dest.FullPath
}
}
}
if err := saveImportPlan(importPlanFilePath, plan); err != nil {
return fmt.Errorf("save import plan: %w", err)
}
}
return nil
}
func diskToAdditionalDiskCreateRequest(disks []Disk) []solus.AdditionalDiskCreateRequest {
var createRequest []solus.AdditionalDiskCreateRequest
for _, disk := range disks {
createRequest = append(createRequest, solus.AdditionalDiskCreateRequest{
OfferID: disk.DiskOfferID,
Size: disk.Size,
Name: disk.Name,
})
}
return createRequest
}
func fillSolusPlanDefaults(plan solus.Plan) solus.Plan {
plan.BackupSettings.IncrementalBackupsLimit = 3
plan.Params.VCPUUnits = 1000
plan.Params.VCPULimit = 100
plan.Params.IOPriority = 4
plan.ResetLimitPolicy = "never"
plan.NetworkTotalTrafficType = "separate"
plan.Netfilter.Value = "full"
plan.Limits.DiskIOPS.Unit = "iops"
plan.Limits.DiskBandwidth.Unit = "Bps"
plan.Limits.BackupsNumber.Unit = "units"
plan.Limits.NetworkIncomingBandwidth.Unit = "Mbps"
plan.Limits.NetworkOutgoingBandwidth.Unit = "Mbps"
plan.Limits.NetworkIncomingTraffic.Unit = "GiB"
plan.Limits.NetworkOutgoingTraffic.Unit = "GiB"
plan.Limits.NetworkTotalTraffic.Unit = "GiB"
plan.Limits.NetworkReduceBandwidth.Unit = "Kbps"
return plan
}