-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_packer.py
58 lines (51 loc) · 2.19 KB
/
run_packer.py
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
"""
Script for building a VirtualBox image with Packer
"""
import argparse
import os
import subprocess
AUTOUNATTEND_TEMPLATE = './answerfiles/windows10/Autounattend.xml.template'
AUTOUNATTEND_XML = './answerfiles/windows10/Autounattend.xml'
def run_packer(username, password, skip_window_updates):
"""Runs the packer command"""
write_autounattend_file(username, password)
os.environ['WINRM_USERNAME'] = username
os.environ['WINRM_PASSWORD'] = password
os.environ['SKIP_WINDOWS_UPDATES'] = str(skip_window_updates)
os.environ['PACKER_LOG'] = '1'
os.environ['PACKER_LOG_PATH'] = 'packer.log'
subprocess.check_call(['packer', 'build', '-var-file=variables.json', 'windows_10.json'])
def write_autounattend_file(username, password):
"""Write the autounattend file with username and password"""
template = open(AUTOUNATTEND_TEMPLATE).read()
autounattend_content = template.replace('${username}', username)
autounattend_content = autounattend_content.replace('${password}', password)
autounattend_file = open(AUTOUNATTEND_XML, 'w')
autounattend_file.write(autounattend_content)
autounattend_file.close()
def main():
"""Main function"""
parser = argparse.ArgumentParser(
description=('Script for building a VirtualBox image with Packer'))
parser.set_defaults(skip_window_updates=False)
parser.add_argument('-u', '--username',
dest='username',
action='store',
required=True,
help='The name of the user in the VirtualBox image')
parser.add_argument('-p', '--password',
dest='password',
action='store',
required=True,
help='The users password')
parser.add_argument('-n', '--no-updates',
dest='skip_window_updates',
action='store_true',
required=False,
help='Skip installing Windows updates')
args = parser.parse_args()
run_packer(args.username,
args.password,
args.skip_window_updates)
if __name__ == '__main__':
main()