Skip to content

Instantly share code, notes, and snippets.

@EmmanuelKasper
Created October 13, 2017 11:19
Show Gist options
  • Save EmmanuelKasper/2a99f3f67afc3ac1100affbf4630d9d1 to your computer and use it in GitHub Desktop.
Save EmmanuelKasper/2a99f3f67afc3ac1100affbf4630d9d1 to your computer and use it in GitHub Desktop.
Make wrapper to execute make file in a sandbox VM
#!/usr/bin/perl
use strict;
use warnings;
use English;
use Sys::Virt;
use Net::OpenSSH;
use File::Spec;
use File::Basename;
use Cwd 'realpath';
$OUTPUT_AUTOFLUSH = 1;
my $LIBVIRT_DEFAULT_URI = "qemu:///session";
my $vm_name = "stretch-builder";
my ($makefile, $make_target) = @ARGV;
die "usage: $0 Makefile target\n" if ! $makefile || ! $make_target;
my $make_dir = dirname(File::Spec->rel2abs($makefile));
# TODO
# add builder user
# set up qemu port fortwarding like
# https://snippets.webaware.com.au/howto/running-qemu-with-port-redirection-through-libvirt/
# set up p9 passthrough like
# http://rabexc.org/posts/p9-setup-in-libvirt
#"virsh attach-device --config stretch-builder /dev/stdin << EOF
#<filesystem type='mount' accessmode='mapped'>
# <source dir='/home/manu/Projects/git/fai-cloud-images'/>
# <target dir='vmake-export'/>
#</filesystem>
#EOF"
my $vm_manager = Sys::Virt->new(uri => $LIBVIRT_DEFAULT_URI);
my $vm = $vm_manager->get_domain_by_name($vm_name);
if (! $vm->is_active()) {
$vm->create(); # start the VM
print STDERR "starting builder VM $vm_name\n";
}
my $ssh = Net::OpenSSH->new('root@localhost:2222',
master_opts => [-o => 'UserKnownHostsFile=/dev/null'], # ssh host key will be different on
master_opts => [-o => 'StrictHostKeyChecking=no'] # each build VM
);
$ssh->system("/bin/true");
if ($ssh->error()) {
die "Couldn't establish SSH connection: ". $ssh->error;
} else {
print STDERR "ssh connection established\n";
}
$ssh->system("mkdir --parents /fai-cloud-images && \
mount vmake-export /fai-cloud-images -t 9p -o trans=virtio")
or die "remote command failed: " . $ssh->error;
my $output = $ssh->pipe_out("pmake -C /fai-cloud-images $make_target")
or die "pipe_in failed: " . $ssh->error;
while (my $line = readline $output) {
print $line;
}
close $output;
$vm->shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment