forked from emerose/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-flint-vm
executable file
·225 lines (191 loc) · 4.39 KB
/
build-flint-vm
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
set -e
usage()
{
cat << EOF
usage: $0 [-q] <VMName>
This script builds the flint vm
OPTIONS:
-h Show this message
-p Do not package the VM.
-i Install only, assume the VM is already copied and running
<VMName> Should be of the form Flint_1.2.3
EOF
}
while getopts "hqpi" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
p)
NOPACKAGE=1
;;
i)
INSTALL_ONLY=1
;;
?)
usage
exit
;;
esac
done
shift $((OPTIND-1))
VMNAME=${1:?"Must provide a VM name, such as \"FlintVM-X.X\""}
if [[ ! -n $VMWUSER ]]; then
log "Please set the VMWUSER env var to the vmware user"
exit 1
fi
if [[ ! -n $VMWPASS ]]; then
log "Please set the VMWPASS env var to the vmware password"
exit 1
fi
echo "Building VM named $VMNAME"
if [[ -n $NOPACKAGE ]]; then
echo "VM will NOT be packaged."
fi
SSH="ssh -o StrictHostKeyChecking=no"
SCP="scp -o StrictHostKeyChecking=no"
VMDIR=/var/lib/vmware/vms
SEEDVM=Ubuntu_8.04_JeOS
VMIP=10.75.1.146
VMRUN="vmrun -T server -h https://tick.chi.matasano.com:8333/sdk -u $VMWUSER -p $VMWPASS"
WDIR=`(cd $WDIR && pwd)`
function log
{
echo "$@"
}
TDIR=$VMDIR/$VMNAME
SDIR=$VMDIR/$SEEDVM
function start_vm
{
VMX="[standard] $1/$1.vmx"
log "Starting $VMX"
if $VMRUN listRegisteredVM | grep -q "$1/$1.vmx"; then
log "Already registered."
else
$VMRUN register "$VMX"
fi
if $VMRUN list | grep -q "$1/$1.vmx"; then
log "Already running."
else
$VMRUN start "$VMX"
fi
}
function stop_vm
{
VMX="[standard] $1/$1.vmx"
log "Stopping $VMX"
if $VMRUN list | grep -q "$1/$1.vmx"; then
$VMRUN stop "$VMX"
else
log "Not running."
fi
}
function unregister_vm
{
VMX=$VMDIR/$1/$1.vmx
log "Unregistering $VMX"
if $VMRUN listRegisteredVM | grep -q "$1/$1.vmx"; then
$VMRUN unregister "$VMX"
else
log "Not registered."
fi
}
function wait_for_host
{
log "Waiting for host $1"
while ! netcat -z $1 22; do
log "Not ready yet"
sleep 1
done
}
function copy_vm
{
echo "Copying $SDIR to $TDIR"
cp -r $SDIR $TDIR
rm $TDIR/readme.txt
cp ../README $TDIR/README
( cd $TDIR
cat - > $VMNAME.vmx <<EOF
#!/usr/local/bin/vmware
.encoding = "UTF-8"
config.version = "8"
virtualHW.version = "4"
scsi0.present = "TRUE"
scsi0.virtualDev = "lsilogic"
memsize = "512"
scsi0:0.present = "TRUE"
scsi0:0.fileName = "root.vmdk"
scsi0:0.writeThrough = "TRUE"
ide1:0.present = "FALSE"
ide1:0.deviceType = "cdrom-image"
ide1:0.startConnected = "FALSE"
floppy0.startConnected = "FALSE"
floppy0.fileName = "/dev/fd0"
Ethernet0.present = "TRUE"
displayName = "$VMNAME"
guestOS = "Ubuntu"
priority.grabbed = "normal"
priority.ungrabbed = "normal"
powerType.powerOff = "soft"
powerType.powerOn = "hard"
powerType.suspend = "hard"
powerType.reset = "soft"
ethernet0.addressType = "generated"
scsi0:1.present = "TRUE"
scsi0:1.fileName = "swap.vmdk"
scsi0:0.redo = ""
scsi0:1.redo = ""
tools.syncTime = "TRUE"
machine.id = "$VMNAME"
EOF
chmod +x $VMNAME.vmx
)
}
function wait_for_host
{
log "Waiting for host $1"
while ! netcat -z $1 22; do
log "Not ready yet"
sleep 1
done
}
function install_flint
{
#$SCP -i vmbuilder prepvm.sh root@$VMIP:prepvm.sh
#$SSH -i vmbuilder -t -t root@$VMIP "./prepvm.sh"
$SCP -i vmbuilder interfaces root@$VMIP:/etc/network/interfaces
$SCP -i vmbuilder flint.profile root@$VMIP:/home/flint/.profile
$SSH -i vmbuilder root@$VMIP "chown flint /home/flint/.profile"
$SSH -i vmbuilder root@$VMIP "su -l flint -c \"git clone http://runplaybook.com/git/flint.git; cd flint; git checkout --track -b releases origin/releases\""
$SSH -i vmbuilder root@$VMIP "su -l flint -c \"cd flint; rake install\""
}
function clean_flint_vm
{
# remove and re-install $SSHd, so we get uniue host keys
$SCP -i vmbuilder scrubvm.sh root@$VMIP:scrubvm.sh
$SCP -i vmbuilder initial_vm_config root@$VMIP:/usr/local/bin/initial_vm_config
$SCP -i vmbuilder rc.local root@$VMIP:/etc/rc.local
$SSH -i vmbuilder root@$VMIP "/bin/bash -x ./scrubvm.sh && rm ./scrubvm.sh && history -c"
}
if [[ ! -n $INSTALL_ONLY ]]; then
stop_vm $SEEDVM
sleep 10
stop_vm $VMNAME || true
sleep 10
unregister_vm $VMNAME
sleep 10
rm -rf $TDIR
copy_vm
fi
start_vm $VMNAME
wait_for_host $VMIP
install_flint
clean_flint_vm
stop_vm $VMNAME
if [[ ! -n $NOPACKAGE ]]; then
sleep 60
./package-vm $VMNAME
fi