-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (130 loc) · 4.63 KB
/
main.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
137
138
139
140
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"weathertimemachineinfra/methods"
"github.com/vultr/govultr/v2"
"golang.org/x/oauth2"
)
func main() {
apiKey := os.Getenv("VULTR_APIKEY")
config := &oauth2.Config{}
ctx := context.Background()
ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: apiKey})
vultrClient := govultr.NewClient(oauth2.NewClient(ctx, ts))
// Optional changes
_ = vultrClient.SetBaseURL("https://api.vultr.com")
vultrClient.SetUserAgent("mycool-app")
vultrClient.SetRateLimit(500)
i, err := methods.ReadYaml("infra.yml")
if err != nil {
log.Fatal(err)
}
if i.Method == "1" {
//ssh creation
checkSSHID, checkSSH, err := methods.ListSSHkey(vultrClient)
if err != nil {
log.Fatal(err)
}
if checkSSH != i.SSH.Name {
fmt.Printf("SSH key doesn't exist, creating one named: %s", i.SSH.Name)
ssh, err := methods.CreateSSH(vultrClient, i)
if err != nil {
log.Fatal(err)
}
fmt.Printf("SSH key was created at %s, ID: %s, Name: %s.\n", ssh.DateCreated, ssh.ID, ssh.Name)
} else {
fmt.Printf("There is already a key named: %s. Please choose another name for new SSH key. \n", i.SSH.Name)
fmt.Print("Continue to Private Network.....\n")
}
//network creation
checkNetworkID, checkNetwork, checkNetworkSubnetMask, err := methods.ListPrivateNetwork(vultrClient)
if err != nil {
log.Fatal(err)
}
if checkNetwork != os.Getenv("WTM_SUBNET") {
network, err := methods.CreatePrivateNetwork(vultrClient, i)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Private network was created at %s, ID: %s, Network Description: %s. Range: %s/%d. \n", network.DateCreated, network.NetworkID, network.Description, network.V4Subnet, network.V4SubnetMask)
} else {
fmt.Printf("There is already a Private Network with the same subnet: %s/%d. Please choose another subnet for new Private network. \n", checkNetwork, checkNetworkSubnetMask)
fmt.Print("Continue to Instance.....\n")
}
//instance creation
_, checkInstanceTag, err := methods.ListInstance(vultrClient)
if err != nil {
log.Fatal(err)
}
time.Sleep(10 * time.Second)
if len(checkNetworkID) == 0 || len(checkSSHID) == 0 {
fmt.Println("Insntance can't be created without SSH and/or Private network")
} else if checkInstanceTag == i.Instance.Tag {
fmt.Println("There is already instance with same tag. Please choose another tag for the new instance")
} else {
fmt.Println("Creating Instance...")
instance, err := methods.CreateInstance(vultrClient, i, checkNetworkID, checkSSHID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Instance was created at %s , Hostname: %s , OS: %s", instance.DateCreated, instance.Label, instance.Os)
}
}
if i.Method == "2" {
sshID, sSHName, err := methods.ListSSHkey(vultrClient)
if err != nil {
log.Fatal(err)
}
if len(sSHName) == 0 {
fmt.Print("There is no SSH key to delete. \n")
fmt.Print("Continue to delete private network.... \n")
} else if sSHName != i.SSH.Name {
log.Fatalf("SSH mismatch, existing SSH key: %s, proposed SSH key for deletion: %s.\n", sSHName, i.SSH.Name)
} else {
err = methods.DeleteSSH(vultrClient, sshID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("SSH key deleted , ID: %s,Name: %s\n", sshID, sSHName)
}
networkID, networkSubnet, networkSubnetMask, err := methods.ListPrivateNetwork(vultrClient)
if err != nil {
log.Fatal(err)
}
instanceID, instanceTag, err := methods.ListInstance(vultrClient)
if err != nil {
log.Fatal(err)
}
subnetMask := fmt.Sprintf("%d", networkSubnetMask)
if len(networkSubnet) == 0 {
fmt.Print("There is no Private Network to delete.\n")
} else if networkSubnet != os.Getenv("WTM_SUBNET") && subnetMask != os.Getenv("WTM_SUBNET_MASK") {
log.Fatalf("PrivateNetwork mismatch, existing PrivateNetwork: %s,proposed PrivateNetwork for deletion: %s.\n", networkSubnet, os.Getenv("WTM_SUBNET"))
} else {
err = methods.DetachNetwork(vultrClient, instanceID, networkID)
if err != nil {
log.Fatal(err)
}
err = methods.DeletePrivateNetwork(vultrClient, networkID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Private Network deleted, ID: %s, Subnet: %s\n", networkID, networkSubnet)
}
if len(instanceTag) == 0 {
log.Fatalf("There is no Instance with the define tag: %s", instanceTag)
} else if instanceTag != i.Instance.Tag {
log.Fatalf("Instance mismatch, existing Instance: %s,proposed Instance for deletion :%s.\n", i.Instance.Tag, instanceTag)
} else {
err = methods.DeleteInstance(vultrClient, instanceID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Instance deleted, ID: %s ,Tag: %s\n", instanceID, instanceTag)
}
}
}