def add_net(self, bridge, mac=None): if not mac: mac = utils.get_rnd_mac() net = self.devices.se("interface", type="bridge") net.se("source", bridge=bridge) net.se("model", type="virtio") net.se("mac", address=mac) self.macs.append(mac)
def build(self, image_name=None): if image_name is None: image_name = self.runner_vm_conf.get("image") if image_name is None: # FIXME yield from asyncio.sleep(1) return self.job.set_status("building %s" % image_name) LOG.debug("Building image: %s" % image_name) build_key = (self.h_ssh.hostname, image_name) BUILDING_IMAGES.setdefault(build_key, asyncio.Lock()) with (yield from BUILDING_IMAGES[build_key]): self.dataset, image_source = self._get_source_image(image_name) cmd = "zfs list %s/%s@1" % (self.dataset, image_name) error = yield from self.h_ssh.run(cmd, raise_on_error=False) if not error: LOG.debug("Image %s already built." % image_name) return image_name # delete possibly stale image cmd = "zfs destroy %s/%s" % (self.dataset, image_name) yield from self.h_ssh.run(cmd, raise_on_error=False) # find source image image_conf = self.runner_conf["images"][image_name] parent = image_conf.get("parent") if parent: source = yield from self.build(parent) self.job.set_status("building %s" % image_name) else: source = image_source if "@" not in source: source += "@1" name = utils.get_rnd_name(prefix="rci_build_%s_" % image_name) xml = XML(name=name, memory=image_conf.get("memory", 1024)) target = "/".join([self.dataset, image_name]) try: cmd = "zfs clone %s/%s %s" % (self.dataset, source, target) yield from self.h_ssh.run(cmd) yield from self._add_disks(xml, target) mac = utils.get_rnd_mac() xml.add_net(image_conf.get("build-net", "virbr0"), mac) ssh = yield from self.boot(xml) image_conf = self.runner_conf["images"][image_name] LOG.debug("building image with image_conf %s" % image_conf) for script in image_conf.get("build-scripts", []): yield from self.run_script(ssh, script) yield from self._shutdown(ssh, xml) yield from self.h_ssh.run("zfs snapshot %s@1" % target) return image_name except: LOG.exception("Error while building %s" % image_name) yield from self.h_ssh.run("virsh destroy %s" % xml.name, raise_on_error=False) yield from asyncio.sleep(4) yield from self.h_ssh.run("zfs destroy %s" % target, raise_on_error=False) raise
def _setup_networks(self, xml, cfg): self.macs = [] for net in cfg: mac = net.get("mac", utils.get_rnd_mac()) br = net.get("bridge") if not br: br = self._get_dynamic_bridge(net["dynamic-bridge"]) if asyncio.iscoroutine(br): br = yield from br self.macs.append(mac) xml.add_net(br, mac)