forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmutils.go
193 lines (160 loc) · 6.36 KB
/
vmutils.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
181
182
183
184
185
186
187
188
189
190
191
192
193
// +build go1.7
// Package vmutils provides convenience methods for creating Virtual
// Machine Role configurations.
package vmutils
// Copyright 2017 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"fmt"
vm "github.com/Azure/azure-sdk-for-go/services/classic/management/virtualmachine"
)
const (
errParamNotSpecified = "Parameter %s is not specified."
)
// NewVMConfiguration creates configuration for a new virtual machine Role.
func NewVMConfiguration(name string, roleSize string) vm.Role {
return vm.Role{
RoleName: name,
RoleType: "PersistentVMRole",
RoleSize: roleSize,
ProvisionGuestAgent: true,
}
}
// ConfigureForLinux adds configuration when deploying a generalized Linux
// image. If "password" is left empty, SSH password security will be disabled by
// default. Certificates with SSH public keys should already be uploaded to the
// cloud service where the VM will be deployed and referenced here only by their
// thumbprint.
func ConfigureForLinux(role *vm.Role, hostname, user, password string, sshPubkeyCertificateThumbprint ...string) error {
if role == nil {
return fmt.Errorf(errParamNotSpecified, "role")
}
role.ConfigurationSets = updateOrAddConfig(role.ConfigurationSets, vm.ConfigurationSetTypeLinuxProvisioning,
func(config *vm.ConfigurationSet) {
config.HostName = hostname
config.UserName = user
config.UserPassword = password
if password != "" {
config.DisableSSHPasswordAuthentication = "false"
}
if len(sshPubkeyCertificateThumbprint) != 0 {
config.SSH = &vm.SSH{}
for _, k := range sshPubkeyCertificateThumbprint {
config.SSH.PublicKeys = append(config.SSH.PublicKeys,
vm.PublicKey{
Fingerprint: k,
Path: "/home/" + user + "/.ssh/authorized_keys",
},
)
}
}
},
)
return nil
}
// ConfigureForWindows adds configuration when deploying a generalized
// Windows image. timeZone can be left empty. For a complete list of supported
// time zone entries, you can either refer to the values listed in the registry
// entry "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time
// Zones" or you can use the tzutil command-line tool to list the valid time.
func ConfigureForWindows(role *vm.Role, hostname, user, password string, enableAutomaticUpdates bool, timeZone string) error {
if role == nil {
return fmt.Errorf(errParamNotSpecified, "role")
}
role.ConfigurationSets = updateOrAddConfig(role.ConfigurationSets, vm.ConfigurationSetTypeWindowsProvisioning,
func(config *vm.ConfigurationSet) {
config.ComputerName = hostname
config.AdminUsername = user
config.AdminPassword = password
config.EnableAutomaticUpdates = enableAutomaticUpdates
config.TimeZone = timeZone
},
)
return nil
}
// ConfigureWithCustomDataForLinux configures custom data for Linux-based images.
// The customData contains either cloud-init or shell script to be executed upon start.
//
// The function expects the customData to be base64-encoded.
func ConfigureWithCustomDataForLinux(role *vm.Role, customData string) error {
return configureWithCustomData(role, customData, vm.ConfigurationSetTypeLinuxProvisioning)
}
// ConfigureWithCustomDataForWindows configures custom data for Windows-based images.
// The customData contains either cloud-init or shell script to be executed upon start.
//
// The function expects the customData to be base64-encoded.
func ConfigureWithCustomDataForWindows(role *vm.Role, customData string) error {
return configureWithCustomData(role, customData, vm.ConfigurationSetTypeWindowsProvisioning)
}
func configureWithCustomData(role *vm.Role, customData string, typ vm.ConfigurationSetType) error {
if role == nil {
return fmt.Errorf(errParamNotSpecified, "role")
}
role.ConfigurationSets = updateOrAddConfig(role.ConfigurationSets, typ,
func(config *vm.ConfigurationSet) {
config.CustomData = customData
})
return nil
}
// ConfigureWindowsToJoinDomain adds configuration to join a new Windows vm to a
// domain. "username" must be in UPN form (user@domain.com), "machineOU" can be
// left empty
func ConfigureWindowsToJoinDomain(role *vm.Role, username, password, domainToJoin, machineOU string) error {
if role == nil {
return fmt.Errorf(errParamNotSpecified, "role")
}
winconfig := findConfig(role.ConfigurationSets, vm.ConfigurationSetTypeWindowsProvisioning)
if winconfig != nil {
winconfig.DomainJoin = &vm.DomainJoin{
Credentials: vm.Credentials{Username: username, Password: password},
JoinDomain: domainToJoin,
MachineObjectOU: machineOU,
}
}
return nil
}
func ConfigureWinRMListener(role *vm.Role, protocol vm.WinRMProtocol, certificateThumbprint string) error {
if role == nil {
return fmt.Errorf(errParamNotSpecified, "role")
}
winconfig := findConfig(role.ConfigurationSets, vm.ConfigurationSetTypeWindowsProvisioning)
if winconfig != nil {
listener := vm.WinRMListener{
Protocol: protocol,
CertificateThumbprint: certificateThumbprint,
}
if winconfig.WinRMListeners == nil {
winconfig.WinRMListeners = &[]vm.WinRMListener{}
}
currentListeners := *winconfig.WinRMListeners
// replace existing listener if it's already configured
for i, existingListener := range currentListeners {
if existingListener.Protocol == protocol {
currentListeners[i] = listener
return nil
}
}
// otherwise append to list of listeners
newListeners := append(currentListeners, listener)
winconfig.WinRMListeners = &newListeners
return nil
}
return fmt.Errorf("WindowsProvisioningConfigurationSet not found in 'role'")
}
func ConfigureWinRMOverHTTP(role *vm.Role) error {
return ConfigureWinRMListener(role, vm.WinRMProtocolHTTP, "")
}
func ConfigureWinRMOverHTTPS(role *vm.Role, certificateThumbprint string) error {
return ConfigureWinRMListener(role, vm.WinRMProtocolHTTPS, certificateThumbprint)
}