This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
crc_virtualbox.ps1
186 lines (166 loc) · 5.06 KB
/
crc_virtualbox.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
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
# Append the path to be able to run vboxmanage.exe commands without absolute path
$env:Path += ";C:\Program Files\Oracle\Virtualbox"
$hostonlyif = (VBoxManage.exe list hostonlyifs)[0].Split(":")[1].Trim()
$hostonlyifIP = (VBoxManage.exe list hostonlyifs)[3].Split(":")[1].Trim()
$user_home = $HOME
function prerequisite()
{
# Backup original Corefile and test1-api file.
cp Corefile Corefile_bak
cp test1-api test1-api_bak
# Copy the disk image in the Virtualbox Folder
mkdir -p "$HOME\VirtualBox VMs\crc"
rm "$user_home\VirtualBox VMs\crc\crc.vmdk"
echo "Copying the vmdk files to ~/VirtualBox\ VMs/ location ..."
cp crc.vmdk "$user_home\VirtualBox VMs\crc"
}
function cluster_create()
{
# Create the hostonly address if not exit
if ( (VBoxManage.exe list hostonlyifs).Length -eq 0 )
{
VBoxManage hostonlyif create
}
# Master configuration
VBoxManage createvm --name crc --ostype Fedora_64 --register
VBoxManage modifyvm crc --cpus 4 --memory 11240 --vram 16
VBoxManage modifyvm crc --nic1 hostonly
VBoxManage modifyvm crc --nictype1 virtio
VBoxManage modifyvm crc --hostonlyadapter1 "$hostonlyif"
VBoxManage modifyvm crc --nic2 nat
VBoxManage modifyvm crc --nictype2 virtio
VBoxManage modifyvm crc --macaddress1 3aceb1219fb2
VBoxManage storagectl crc --name "SATA Controller" --add sata --bootable on --portcount 1
VBoxManage storageattach crc --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "$user_home\VirtualBox VMs\crc\crc.vmdk"
update_corefile
}
function cluster_start()
{
check_cluster
set_dns_config
VBoxManage startvm crc --type headless
sleep 10
if (!(Get-Process coredns -ErrorAction SilentlyContinue))
{
modify_zone_file
start_coredns
}
}
function cluster_stop()
{
check_cluster
VBoxManage controlvm crc poweroff
}
function check_cluster()
{
if ( !((VBoxManage.exe list vms).Contains("crc")) )
{
echo "Cluster is not present"
usage
exit 1
}
}
function cluster_delete()
{
check_cluster
cluster_stop
VBoxManage unregistervm crc --delete
stop_coredns
if ( Test-Path -Path Corefile_bak )
{
rm Corefile
mv Corefile_bak Corefile
}
if ( Test-Path -Path test1-api_bak )
{
rm test1-api
mv test1-api_bak test1-api
}
}
function modify_zone_file()
{
foreach ($entry in $(arp -a))
{
if ($entry.Contains("3a-ce-b1-21-9f-b2"))
{
$crcIp=$entry.Split(" ")[2].Trim()
# Run Coredns after editing tt.testing file.
echo "Master ip address: $crcIp"
Add-Content test1-api "*.apps.tt.testing.`t`tIN`t`tA`t`t$crcIp"
Add-Content test1-api "api.test1.tt.testing.`t`tIN`t`tA`t`t$crcIp"
Add-Content test1-api "test1-989ds-master-0.tt.testing.`t`tIN`t`tA`t`t$crcIp"
Add-Content test1-api "etcd-0.test1.tt.testing.`t`tIN`t`tA`t`t10.0.3.15"
break;
}
}
}
function set_dns_config()
{
$vbInterface = (Get-NetAdapter -Name *VirtualBox*).Name
$hostInterfaces = (Get-NetAdapter -Physical).Name
echo "VBox Hostonly interface: $vbInterface"
echo "Host interfaces: $hostInterfaces"
#set dns server on the host interface (requires elevated shell, run as administrator
foreach ($adapter in $hostInterfaces)
{
Set-DnsClientServerAddress -InterfaceAlias "$adapter" -ServerAddresses "$hostonlyifIP"
}
#set dns server on the VB interface
Set-DnsClientServerAddress -InterfaceAlias "$vbInterface" -ServerAddresses "$hosonlyifIP"
}
function update_corefile()
{
$dnsAddresses = (Get-DnsClientServerAddress).ServerAddresses
foreach($address in $dnsAddresses)
{
$ns = nslookup.exe google.com $address
foreach($line in $ns)
{
if($line.Contains("Addresses"))
{
(Get-Content .\Corefile).Replace("mynameserver", $address) | Set-Content .\Corefile
break;
}
}
}
}
function start_coredns()
{
# start coredns process
echo "Starting Coredns as backgroud process"
echo "Coredns logs are in $env:TEMP\.coredns.log & $env:TEMP\.corednsErr.log file"
Start-Process -FilePath ".\coredns.exe" -RedirectStandardOutput "$env:TEMP\.coredns.log" -RedirectStandardError "$env:TEMP\.corednsErr.log" -NoNewWindow
}
function stop_coredns()
{
Stop-Process -Name coredns
}
function usage()
{
$usage="crc_virtualbox.ps1 [[create | start | stop | delete] | [-h]]
where:
create - Create the cluster resources
start - Start the cluster
stop - Stop the cluster
delete - Delete the cluster
-h - Usage message
"
echo "$usage"
}
function main($arg)
{
if ($arg.Length -ne 1 )
{
usage
exit 1
}
switch($arg) {
create {prerequisite; cluster_create; break;}
start {cluster_start; break;}
stop {cluster_stop; break;}
delete {cluster_delete; break;}
-h {usage; break;}
default {usage; break;}
}
}
main($args)