forked from bandit145/vmware-template-builds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
85 lines (75 loc) · 2.56 KB
/
build.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
param(
[String[]]$PackerConfig,
[String]$ConfigFile = "config.json",
[String]$Builds = "builds/"
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version 2
try{
Import-Module -Name Vmware.VimAutomation.Core
}
catch {
Write-Error "You are missing the PowerCLI Module"
}
$sensitive_env_keys = @("vcenter_username","vcenter_password","redhat_sub_pass")
function Get-Pass($msg){
$temp_pass = Read-Host -Prompt $msg -AsSecureString
return [system.Runtime.interopServices.marshal]::PtrToStringAuto([system.runtime.Interopservices.Marshal]::SecurestringToBstr($temp_pass))
}
function Remove-PackerTemplate($config){
$current_config = Get-Content -Path $config | ConvertFrom-JSON
if ($current_config.variables.vm_name -in (Get-Template).name){
Remove-Template -Template $current_config.variables.vm_name -DeletePermanently -Confirm:$false
}
}
$config = Get-Content -Path $ConfigFile | ConvertFrom-JSON
$env:vcenter_username = Read-Host -Prompt "Vcenter User"
$env:vcenter_password = Get-Pass -msg "Password"
$env:login_password = Get-Pass -msg "Host login password"
try{
$global:DefaultViServer | Out-Null
}
catch {
Connect-VIServer -Server $config.vcenter_server -User $env:vcenter_username -Password $env:vcenter_password | Out-Null
}
foreach($key in ($config | Get-Member -MemberType NoteProperty).Name){
if (Test-Path -Path ENV:$key){
Remove-Item -Path ENV:$key
}
New-Item -Path ENV:$key -Value $config.$key
}
$esx_hosts = Get-VMHost
$env:esx_host = $esx_hosts[(Get-Random -Minimum 0 -Maximum $esx_hosts.Count)].Name
if ($PackerConfig){
#if redhat build ask for sub managet password
if ($PackerConfig -match "rhel"){
$env:redhat_sub_pass = Get-Pass -msg "RHEL subpass"
}
foreach($packer in $PackerConfig){
Remove-PackerTemplate -config "$Builds/$packer"
packer build -force "$Builds/$packer"
if ($LASTEXITCODE -ne 0){
Write-Error "Packer build failed"
}
}
}
else{
#add jobs at some point
if ((Get-ChildItem -Path $Builds).name -match "rhel"){
$env:redhat_sub_pass = Get-Pass -msg "RHEL subpass"
}
foreach($packer in (Get-ChildItem -Path $Builds)){
$packer_config.builders[0].guest_os_type
Remove-PackerTemplate -config "$Builds/$packer"
packer build -force "$Builds/$packer"
if ($LASTEXITCODE -ne 0){
Write-Error "Packer build failed"
}
}
}
#clear sensitive info
foreach($cred in $sensitive_env_keys){
if(Test-Path -Path ENV:$cred){
Remove-Item -Path ENV:$cred
}
}