This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
/
vm.go
180 lines (154 loc) · 4.83 KB
/
vm.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package azurecompute
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"io/ioutil"
"log"
"neuron/cloud/azure/access"
"os"
)
var (
token, _, subscription = auth.GetServicePrincipalToken()
fakepubkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjCX8wh0lnk2KUvoCulBER4TQ+4+repQF5vvQeCVc5eWHNQKIPuSxy4fGcEbar15U4wjEJYDsXUGhW0JIh4peIKFf+dXUtZlMQEo7QvPGGORjVm8Zf+je/cVqGQJOvUP4s1/J8EQ+/n6gidtByBL+4lN/vDp/lgPSZzRgb08zVuW40z6jFrxfwalru10FHzzPmkCEtW54YkdJ2yEnLzk+xZDJXmG7JE4c2yRl+Y35HCzHfeRsUqcF1ErV2KYHcRWqwzD9oDZ5V2uTC4ERHkF102Ve7LOSyYK3cvJ8QSWMoOCOPA/UpdrkJRq9e2eVdpIqvnbu2vp6xazU080ZNu/BB"
ctx = context.Background()
)
func getVMClient() compute.VirtualMachinesClient {
vmClient := compute.NewVirtualMachinesClient(subscription)
vmClient.Authorizer = autorest.NewBearerAuthorizer(token)
return vmClient
}
type VMIn struct {
ResourceGroup string
VmName string `json:"vmname,omitempty"`
NicID string `json:"nicid,omitempty"`
UserName string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Flavour string `json:"os,omitempty"`
SSHPublicKeyPath string `json:"sshkeypath,omitempty"`
Location string `json:"location,omitempty"`
}
// CreateVM creates a new virtual machine with the specified name using the specified NIC.
// Username, password, and sshPublicKeyPath determine logon credentials.
func (v VMIn) CreateVM() (vm compute.VirtualMachine, err error) {
// see the network samples for how to create and get a NIC resource
osconfig := Image(v.Flavour)
var sshKeyData string
if _, err = os.Stat(v.SSHPublicKeyPath); err == nil {
sshBytes, err := ioutil.ReadFile(v.SSHPublicKeyPath)
if err != nil {
log.Fatalf("failed to read SSH key data: %v", err)
}
sshKeyData = string(sshBytes)
} else {
sshKeyData = fakepubkey
}
vmClient := getVMClient()
future, err := vmClient.CreateOrUpdate(
ctx,
v.ResourceGroup,
v.VmName,
compute.VirtualMachine{
Location: to.StringPtr(v.Location),
VirtualMachineProperties: &compute.VirtualMachineProperties{
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.BasicA0,
},
StorageProfile: &compute.StorageProfile{
ImageReference: &compute.ImageReference{
Publisher: to.StringPtr(osconfig.Publisher),
Offer: to.StringPtr(osconfig.Offer),
Sku: to.StringPtr(osconfig.Sku),
Version: to.StringPtr(osconfig.Version),
},
},
OsProfile: &compute.OSProfile{
ComputerName: to.StringPtr(v.VmName),
AdminUsername: to.StringPtr(v.UserName),
AdminPassword: to.StringPtr(v.Password),
LinuxConfiguration: &compute.LinuxConfiguration{
SSH: &compute.SSHConfiguration{
PublicKeys: &[]compute.SSHPublicKey{
{
Path: to.StringPtr(
fmt.Sprintf("/home/%s/.ssh/authorized_keys",
v.UserName)),
KeyData: to.StringPtr(sshKeyData),
},
},
},
},
},
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{
{
ID: to.StringPtr(v.NicID),
NetworkInterfaceReferenceProperties: &compute.NetworkInterfaceReferenceProperties{
Primary: to.BoolPtr(true),
},
},
},
},
},
},
)
if err != nil {
return vm, fmt.Errorf("cannot create vm: %v", err)
}
err = future.WaitForCompletion(ctx, vmClient.Client)
if err != nil {
return vm, fmt.Errorf("cannot get the vm create or update future response: %v", err)
}
return future.Result(vmClient)
}
func (v VMIn) DeleteVM() (ar autorest.Response, err error) {
vmClient := getVMClient()
future, err := vmClient.Delete(
ctx,
v.ResourceGroup,
v.VmName,
)
if err != nil {
return ar, fmt.Errorf("cannot delete VM: %v", err)
}
err = future.WaitForCompletion(ctx, vmClient.Client)
if err != nil {
return ar, fmt.Errorf("cannot get the VM delete future response: %v", err)
}
return future.Result(vmClient)
}
func (v VMIn) GetVM() (vm compute.VirtualMachine, err error) {
vmClient := getVMClient()
future, err := vmClient.Get(
ctx,
v.ResourceGroup,
v.VmName,
"")
if err != nil {
return vm, fmt.Errorf("cannot get virtual VM: %v", err)
}
return future, err
}
func (v VMIn) ListVM() (vm []compute.VirtualMachine, err error) {
vmClient := getVMClient()
future, err := vmClient.List(
ctx,
v.ResourceGroup,
)
if err != nil {
return vm, fmt.Errorf("cannot list the VMs in a resourcegroup: %v", err)
}
return future.Values(), err
}
func ListAllVM() (vm []compute.VirtualMachine, err error) {
vmClient := getVMClient()
future, err := vmClient.ListAll(
ctx,
)
if err != nil {
return vm, fmt.Errorf("cannot list the VMs: %v", err)
}
return future.Values(), err
}