-
Notifications
You must be signed in to change notification settings - Fork 97
/
provision-cloudbase-init.ps1
121 lines (112 loc) · 4.99 KB
/
provision-cloudbase-init.ps1
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
Set-StrictMode -Version Latest
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
Write-Host
Write-Host "ERROR: $_"
($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1' | Write-Host
($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1' | Write-Host
Write-Host
Write-Host 'Sleeping for 60m to give you time to look around the virtual machine before self-destruction...'
Start-Sleep -Seconds (60*60)
Exit 1
}
# enable TLS 1.2.
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol `
-bor [Net.SecurityProtocolType]::Tls12
$cloudbaseInitHome = 'C:\Program Files\Cloudbase Solutions\Cloudbase-Init'
$cloudbaseInitConfPath = "$cloudbaseInitHome\conf\cloudbase-init.conf"
# see https://github.com/cloudbase/cloudbase-init/releases
# renovate: datasource=github-releases depName=cloudbase/cloudbase-init
$cloudbaseInitVersion = '1.1.6'
$artifactUrl = "https://github.com/cloudbase/cloudbase-init/releases/download/$cloudbaseInitVersion/CloudbaseInitSetup_$($cloudbaseInitVersion -replace '\.','_')_x64.msi"
$artifactPath = "$env:TEMP\$(Split-Path -Leaf $artifactUrl)"
$artifactLogPath = "$artifactPath.log"
$systemVendor = (Get-CimInstance -ClassName Win32_ComputerSystemProduct -Property Vendor).Vendor
if ($systemVendor -eq 'QEMU') {
# qemu-kvm.
$metadataServices = @(
# NoCloudConfigDriveService for use in libvirt.
# see https://cloudbase-init.readthedocs.io/en/latest/services.html#nocloud-configuration-drive
'cloudbaseinit.metadata.services.nocloudservice.NoCloudConfigDriveService'
# ConfigDriveService for use in Proxmox.
# see https://cloudbase-init.readthedocs.io/en/latest/services.html#openstack-configuration-drive
# see https://pve.proxmox.com/wiki/Cloud-Init_Support
'cloudbaseinit.metadata.services.configdrive.ConfigDriveService'
)
} elseif ($systemVendor -eq 'Microsoft Corporation') {
# Hyper-V.
$metadataServices = @(
# NoCloudConfigDriveService for use in Hyper-V.
# see https://cloudbase-init.readthedocs.io/en/latest/services.html#nocloud-configuration-drive
'cloudbaseinit.metadata.services.nocloudservice.NoCloudConfigDriveService'
)
} elseif ($systemVendor -eq 'VMware, Inc.') {
# VMware ESXi.
$metadataServices = @(
# VMwareGuestInfoService for use in VMware ESXi.
# see https://cloudbase-init.readthedocs.io/en/latest/services.html#vmware-guestinfo-service
'cloudbaseinit.metadata.services.vmwareguestinfoservice.VMwareGuestInfoService'
)
} else {
Write-Host "WARNING: cloudbase-init is not supported on your system vendor $systemVendor"
Exit 0
}
# NB we might have to retry the download due to errors:
# The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
while ($true) {
try {
Write-Host 'Downloading the cloudbase-init setup...'
(New-Object System.Net.WebClient).DownloadFile($artifactUrl, $artifactPath)
break
} catch {
Write-Host "Failed to download the cloudbase-init setup. Trying in a bit due to error $_"
Start-Sleep -Seconds 5
}
}
Write-Host 'Installing cloudbase-init...'
# NB this also installs the cloudbase-init service, which will automatically start on the next boot.
# see https://github.com/cloudbase/cloudbase-init-installer
msiexec /i $artifactPath /qn /l*v $artifactLogPath | Out-String -Stream
if ($LASTEXITCODE) {
throw "Failed with Exit Code $LASTEXITCODE. See the logs at $artifactLogPath."
}
Write-Host 'Replacing the configuration...'
# The default configuration is:
# [DEFAULT]
# username=Admin
# groups=Administrators
# inject_user_password=true
# config_drive_raw_hhd=true
# config_drive_cdrom=true
# config_drive_vfat=true
# bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe
# mtools_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\
# verbose=true
# debug=true
# log_dir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\
# log_file=cloudbase-init.log
# default_log_levels=comtypes=INFO,suds=INFO,iso8601=WARN,requests=WARN
# logging_serial_port_settings=
# mtu_use_dhcp_config=true
# ntp_use_dhcp_config=true
# local_scripts_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\
# check_latest_version=true
# see https://cloudbase-init.readthedocs.io/en/latest/tutorial.html#configuration-file
# see https://cloudbase-init.readthedocs.io/en/latest/config.html#config-list
Move-Item $cloudbaseInitConfPath "$cloudbaseInitConfPath.orig"
Set-Content -Encoding ascii $cloudbaseInitConfPath @"
[DEFAULT]
username=Administrator
groups=Administrators
first_logon_behaviour=no
debug=true
log_dir=$cloudbaseInitHome\log\
log_file=cloudbase-init.log
bsdtar_path=$cloudbaseInitHome\bin\bsdtar.exe
mtools_path=$cloudbaseInitHome\bin\
metadata_services=$($metadataServices -join ",`n ")
[config_drive]
locations=cdrom
types=iso
"@