-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-editor.ps1
145 lines (123 loc) · 5.87 KB
/
config-editor.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
. $PSScriptRoot\config.ps1
# Global settings
$SnapShotDefault = Read-Host "Do you want to make a snapshot of the VM before the configuration? (y/n)"
if ($SnapShotDefault -like "y") { $SnapShotDefault = $true} else { $SnapShotDefault = $false }
$Memory = Read-Host "Enter the default memory for the VM in MB, (default: 2048)"
if ($Memory -eq "") { $Memory = 2048 } else { $Memory = [int]$Memory }
[int]$MaxCPUCores = nproc
Write-Host "The Maximum number of CPU cores: $MaxCPUCores" -ForegroundColor Yellow | Out-Host
[int]$CPU = Read-Host "Enter the default number of CPUs for the VM, (default: 2)"
if ($CPU -eq "") { $CPU = 2 } else {
while ($CPU -gt $MaxCPUCores) {
Write-Host "The number of CPUs ($CPU) is greater than the maximum number of CPU cores ($MaxCPUCores)" -ForegroundColor Red
[int]$CPU = Read-Host "Enter the default number of CPUs for the VM, (default: 2)"
if ($CPU -eq "") { $CPU = 2 }
}
}
$DiskSize = Read-Host "Enter the default disk size for the VM in GB, (default: 32)"
if ($DiskSize -eq "") { $DiskSize = "32G" } else { $DiskSize = "$([int]$DiskSize)G" }
$HardwareDefault = Read-Host "Do you want to use this hardware settings as default? (y/n)"
if ($HardwareDefault -like "y") { $HardwareDefault = $true} else { $HardwareDefault = $false }
# CloudInit settings
$CloudInitUserName = Read-Host "Enter the default username for the cloud-init configuration"
write-host "Enter the path for default public key for the cloud-init configuration"
$CloudInitPublicKey = Read-Host "Enter the path for the public key (blank for ~/.ssh/id_rsa.pub)"
if ($CloudInitPublicKey -eq "") { $CloudInitPublicKey = "~/.ssh/id_rsa.pub" } else {
if (!(Test-Path -Path $CloudInitPublicKey)) {
Write-Host "The file $CloudInitPublicKey does not exist" -ForegroundColor Red
if (Test-Path "~/.ssh/id_rsa.pub") {
write-host "Using the default public key (~/.ssh/id_rsa.pub)"
$CloudInitPublicKey = "~/.ssh/id_rsa.pub"
} else {
write-host "Could not find default public key, Using an empty public key"
$CloudInitPublicKey = ""
}
}
}
# VM Network settings
$AvailableBridge | Out-Host
$vmbr = Read-Host "Enter the default bridge for the VM network, (default: vmbr1)"
if ($vmbr -eq "") { $vmbr = "vmbr1" }
$IPSettings = Read-Host "Would you like to use DHCP or Static IP? (dhcp/static)"
if ($IPSettings -like "static") {
$IP4Address = "static"
$IP6Address = "static"
} else {
$IP4Address = "DHCP"
$IP6Address = "DHCP"
}
$CloudInitDefault = Read-Host "Do you want to use this as default cloud-init settings? (y/n)"
if ($CloudInitDefault -like "y") { $CloudInitDefault = $true} else { $CloudInitDefault = $false }
################### SSH Config settings
$SSHConfigFilePath = "~/.ssh/config"
$SSHConfigFile = Get-Content -Path $SSHConfigFilePath
$AvailableBridges = Get-Content -Path /etc/network/interfaces | Where-Object {$_ -match "iface " -or $_ -match "address "}
$AddHost = Read-Host "Do you want to add a new host/nework to SSH config file? (y/n)"
if ($AddHost -eq "y") {
$AvailableBridges | out-host
Write-Host "eg. 10.10.10.12 for a single host or 10.10.10.* for a network" -ForegroundColor Yellow
$HostAddress = Read-Host "Enter the IP address of the host/network"
$User = Read-Host "Enter the username (blank for $CloudInitUserName)"
if ($User -eq "") { $User = $CloudInitUserName } else { $User = $User }
$Port = Read-Host "Enter the port (blank for 22)"
if ($Port -eq "") { $Port = 22 } else { $Port = $Port }
$IdentityFile = Read-Host "Enter the path to the private key (blank for ~/.ssh/id_rsa)"
if ($IdentityFile -eq "") { $IdentityFile = "~/.ssh/id_rsa" } else { $IdentityFile = $IdentityFile }
$NewHostSettings = @"
Host $HostAddress
User $User
Port $Port
IdentityFile $IdentityFile
"@
$HostFound = 0
$SSHConfigFile | ForEach-Object {
if ($_ -match "Host $HostAddress") {
$HostFound = $HostFound + 1
write-host "$_ already exists in SSH config file" -ForegroundColor Yellow
}
}
if ($HostFound -ne 0) {
Write-Host "Host $HostAddress already exists in SSH config file" -ForegroundColor Yellow
} else {
Write-Host "Adding $HostAddress to SSH config file" -ForegroundColor Green
$NewHostSettings | Out-File -FilePath $SSHConfigFilePath -Append
Write-Host "Host $HostAddress added to SSH config file" -ForegroundColor Green
Write-Host "SSH config file content:" -ForegroundColor Yellow
Get-Content -Path $SSHConfigFilePath | Out-Host
}
} else { Write-Host "No new host/network added to SSH config file" -ForegroundColor Yellow }
$ConfigFile = "$ConfigFolder/config.json"
$ConfigSettings = [PSCustomObject]@{
# Global settings
Global = [PSCustomObject]@{
HostName = hostname
AppFolder = $PSScriptRoot
ConfigFolder = $ConfigFolder
}
# VM settings
VM = [PSCustomObject]@{
Hardware = [PSCustomObject]@{
# Hardware settings
UseAsDefault = $HardwareDefault
Memory = $Memory
Cores = $CPU
DiskSize = $DiskSize
# Default if the user want to make a snapshot of the VM before the configuration
SnapShot = $SnapShotDefault
}
# CloudInit settings
CloudInit = [PSCustomObject]@{
UseAsDefault = $CloudInitDefault
UserName = $CloudInitUserName
PublicKey = $CloudInitPublicKey
Bridge = $VMBR
IPV4 = $IP4Address
IPV6 = $IP6Address
}
}
}
Write-Host "Saving the file $ConfigFile..." -ForegroundColor Green
$ConfigSettings | ConvertTo-Json -Depth 5 | Out-File -FilePath $ConfigFile -Encoding UTF8 -Force
start-sleep -Seconds 2
$Config = Get-Content -Path $ConfigFile | ConvertFrom-Json -Depth 5
Write-Host "The file $ConfigFile saved successfully." -ForegroundColor Green