Created
December 18, 2017 12:29
-
-
Save dominikholler/73dfbd9179ad89a002c669a936cd97e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# see https://github.com/oVirt/ovirt-node-ng/blob/master/scripts/node-setup/setup-node-appliance.sh | |
import os | |
import requests | |
import subprocess | |
import random | |
import sys | |
ROOT_PW='123456' | |
BUILD_URL = ('http://jenkins.ovirt.org/job/' | |
'ovirt-node-ng_master_build-artifacts-el7-x86_64/' | |
'lastSuccessfulBuild/artifact/exported-artifacts/') | |
CHECKSUMS_URL = BUILD_URL + 'CHECKSUMS.sha256' | |
ISO_NAME_XPATH = '//a[1]/@href' | |
CHUNK_SIZE = 1024*1024 | |
BOOT_ISO_NAME = 'boot.iso' | |
BOOT_URL = ('http://mirror.centos.org/centos/7/os/x86_64/images/' + | |
BOOT_ISO_NAME) | |
#BOOT_LOCATION = 'http://mirror.centos.org/centos-7/7/os/x86_64/' | |
# using an iso as location requires root, | |
# http boot location seems not to work here | |
BOOT_LOCATION = BOOT_ISO_NAME | |
VM_MEM = 4096 | |
VM_CPU = 2 | |
KICKSTART=""" | |
lang en_US.UTF-8 | |
keyboard us | |
timezone --utc Etc/UTC | |
auth --enableshadow --passalgo=sha512 | |
rootpw {rootpw} | |
selinux --enforcing | |
network --bootproto=dhcp | |
firstboot --reconfig | |
clearpart --all --initlabel --disklabel=gpt | |
bootloader --timeout=0 | |
reqpart --add-boot | |
part pv.01 --size=42000 --grow | |
volgroup HostVG pv.01 | |
logvol swap --vgname=HostVG --name=swap --fstype=swap --recommended | |
logvol none --vgname=HostVG --name=HostPool --thinpool --size=40000 --grow | |
logvol / --vgname=HostVG --name=root --thin --poolname=HostPool --fsoptions="defaults,discard" --size=6000 | |
logvol /var --vgname=HostVG --name=var --thin --poolname=HostPool --fsoptions="defaults,discard" --size=15000 | |
install | |
liveimg --url="{img}" | |
poweroff | |
%post | |
set -x | |
imgbase --debug layout --init | |
%end | |
""" | |
def write_ks_file(file_path, squashfs_img_name): | |
with open(file_path,'w') as f: | |
f.write(KICKSTART.format(img=get_squashfs_img_url(squashfs_img_name), | |
rootpw=ROOT_PW)) | |
def get_squashfs_img_url(squashfs_img_name): | |
return BUILD_URL + squashfs_img_name | |
def get_last_successful_squashfs_img_name(): | |
response = requests.get(CHECKSUMS_URL) | |
assert response.ok | |
return filter(lambda l: 'squashfs.img' in l, | |
response.content.splitlines())[0].split(' ')[-1] | |
def download_boot_iso(): | |
if not os.path.isfile(BOOT_ISO_NAME): | |
download_file(BOOT_URL) | |
def download_file(url): | |
local_filename = url.split('/')[-1] | |
r = requests.get(url, stream=True) | |
with open(local_filename, 'wb') as f: | |
count = 0 | |
for chunk in r.iter_content(chunk_size=CHUNK_SIZE): | |
if chunk: | |
f.write(chunk) | |
count += 1 | |
print count, '/', int(r.headers['content-length'])/CHUNK_SIZE | |
return local_filename | |
download_boot_iso() | |
squashfs_img_name = get_last_successful_squashfs_img_name() | |
vm_name = squashfs_img_name[0:-13] | |
ks_file = vm_name + '.ks' | |
write_ks_file(ks_file, squashfs_img_name) | |
cmd = ['virt-install', | |
'--name {}'.format(vm_name), | |
'--boot menu=off', | |
'--memory {}'.format(VM_MEM), | |
'--vcpus {}'.format(VM_CPU), | |
'--cpu host', | |
'--location {}'.format(BOOT_LOCATION), | |
'--extra-args "inst.ks=file:///{} console=ttyS0"'.format(ks_file), | |
'--initrd-inject {}'.format(ks_file), | |
'--graphics none', | |
'--disk cache=unsafe,size=60,path={}.qcow2'.format(vm_name), | |
'--os-type=linux', | |
'--os-variant=rhel7', | |
'--transient', | |
] | |
p = subprocess.Popen(' '.join(cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
for line in iter(p.stdout.readline, ''): | |
sys.stdout.write(line) | |
sys.exit(p.wait()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment