def ctlStatus(self): step = "Checking machinectl status for container {0} (ip {1})".\ format(self.cName, self.ip) msg, ec = exec_command("sudo machinectl status {0} | grep "\ "\"Started Container {1}\"".\ format(self.cName, self.cName)) return msg, ec, step
def checkLowerImageRootMount(self): step = "Checking if loop {0} (image root) is mounted to lower {1}".\ format(self.device, self.lower) cmd = "mount | grep -qE \"{0}.*{1}\"".\ format(self.device, self.lower) msg, ec = exec_command(cmd) return msg, ec, step
def mountImagePartition(self): fdiskOutput = self.getPartition() self.p = parse_fdisk(fdiskOutput) step = "Mounting image root partition to loop " + self.device cmd = "sudo losetup -o {0} {1} {2}".format(self.p.startP, self.device, self.iPath) msg, ec = exec_command(cmd) return msg, ec, step
def mountOverlayFS(self): step = "Trying to mount overlayFS, merged path is " + self.merged self._createDirs() cmd = "sudo mount -t overlay overlay " \ "-o lowerdir={0},upperdir={1},workdir={2} " \ "{3}".format(self.lower, self.upper, self.ofsWork, self.merged) msg, ec = exec_command(cmd) return msg, ec, step
def checkBridgeAddress(self): step = "Checking if bridge has IP {0}/{1} assigned".\ format(self.brIP, self.netPrefix) msg, ec = exec_command("ip -4 -o addr show dev " + self.brName) if "{0}/{1}".format(self.brIP, self.netPrefix) in msg: print(OK(step)) else: printErr(msg) print(FAIL(step))
def checkSSH(self): step = "Checking ssh connection to " + self.ip cmd = ("ssh -o BatchMode=yes -o StrictHostKeyChecking=no " \ "-l {0} -i {1} {2} uptime".\ format(self.user, self.cntCommon.sshPrivateKeyPath, self.ip)) msg, ec = exec_command(cmd) return msg, ec, step
def getPartition(self): msg, ec = exec_command("fdisk -lu " + self.iPath) """ WTF? $ fdisk -lu badabooom fdisk: cannot open badabooom: No such file or directory $ echo $? 0 """ if "No such file or directory" in msg: ec = 1 msg += "Have you launched 'aplspawn.py -o getimage'?" return msg, ec, False
def getPID(self): step = False msg, ec = exec_command("sudo machinectl status {0} | grep "\ "\"Leader:\" ".\ format(self.cName, self.cName)) msgList = msg.split() try: index = msgList.index("Leader:") res = msgList[index + 1] except ValueError: res, ec = False, False step = "Unable to get pid of container. Is container {0} running?".\ format(self.cName) return res, ec, step
def checkLoopDevice(self): step = "Checking if image is mounted correctly to loop device {0} " \ .format(self.device) msg, ec = exec_command("losetup -Jl " + self.device) #try: loop = draftClass(loads(msg)["loopdevices"][0]) imageFullPath = getattr(loop, "back-file") #except: # pass if imageFullPath == self.iPath: msg, ec = True, True else: msg, ec = False, False return msg, ec, step
def rmWorkingDirs(self): step = "Removing content of working directories" wd = self.c.workingDirectory for key in self.c.getKeys(): path = getattr(self.c, key) if key == "workingDirectory": continue if wd in path: if os.path.isdir(path): msg, ec = exec_command("sudo rm -rf {0}/*".format(path)) print(OK(path + " was cleaned up")) else: print(INFO("{} is not under parent working directory," \ " refusing to remove it's cintent").format(path)) return True, "", step
def checkOverlayFS(self): step = "Checking overlayFS mount" msg, ec = exec_command("mount | grep " + self.cName) failed = False for key in self.pConfig.getKeys(): if key in ['localStore', 'workingDirectory']: continue path = getattr(self.pConfig, key) if path not in msg: print(INFO("Not in OFS mount: " + path)) failed = True break if failed: print(FAIL(step)) else: print(OK(step))
def spawn(self): self.varString = self._prepareVars() cmd = "sudo systemd-nspawn -D {0} -n --network-bridge={1} " \ "-M {2} {3}".format(self.merged, self.bridge, self.cName, self.varString) if self.cntCommon.bindHostResolve: cmd += "--bind-ro=/etc/resolv.conf " # mount startup script bindHostFull = "{0}/{1}".\ format(getScriptPath(), self.cntCommon.bindHost) cmd += "--bind-ro={0}:{1} ".format(bindHostFull, self.cntCommon.bindContainer) # don't create own scope unit #cmd += "--keep-unit " cmd += " " + self.cntCommon.startupScript msg, ec = exec_command(cmd, bg=True) return msg, ec, msg
def execute(self, cmd): msg, ec = exec_command(cmd) return msg, ec, False
def iptablesBackup(self): step = "Executing backup of current iptables configuration" msg, ec = exec_command("sudo iptables-save >> iptables.backup") return msg, ec, step
def checkBridgeExists(self): step = "Checking if bridge {} exists".format(self.brName) msg, ec = exec_command("ip link show " + self.brName) return msg, ec, step
def kill(self): step = "Killing container {0} (ip {1})".format(self.cName, self.ip) msg, ec = exec_command("sudo machinectl kill " + self.cName) return msg, ec, step
def releaseDevice(self): step = "Releasing loop device " + self.device cmd = "sudo losetup -d " + self.device msg, ec = exec_command(cmd) return msg, ec, step
def umountImage(self): step = "Umounting image root partition from loop " + self.device cmd = "sudo umount " + self.iPath msg, ec = exec_command(cmd) return msg, ec, step
def terminate(self): step = "Terminating container {0} (ip {1})".format(self.cName, self.ip) msg, ec = exec_command("sudo machinectl terminate " + self.cName) return msg, ec, step
def start(self): step = "Starting container {0} (ip {1})".format(self.cName, self.ip) msg, ec = exec_command("sudo machinectl start " + self.cName) return msg, ec, step
def checkPing(self): step = "Checking ping to " + self.ip msg, ec = exec_command("ping -c1 -W1 " + self.ip) return msg, ec, step
def checkForwarding(self): step = "Checking if traffic forwarding is enabled " \ "/proc/sys/net/ipv4/ip_forward" cmd = "grep -q 1 /proc/sys/net/ipv4/ip_forward" msg, ec = exec_command(cmd) return msg, ec, step
def umount(self): step = "Umount overlayFS " + self.merged msg, ec = exec_command("sudo umount " + self.merged) return msg, ec, step
def umountLower(self): step = "Umounting lower overlayFS layer from loop " + self.device cmd = "sudo umount " + self.lower msg, ec = exec_command(cmd) return msg, ec, step
def mountToLower(self): step = "Mounting {0} as lower overlayFS layer".format(self.device) createDir(self.lower) cmd = "sudo mount -t squashfs {0} {1}".format(self.device, self.lower) msg, ec = exec_command(cmd) return msg, ec, step
def checkBridgeIsUp(self): step = "Checking if bridge {} is up".format(self.brName) cmd = "grep -q up /sys/class/net/{0}/operstate".\ format(self.brName) msg, ec = exec_command(cmd) return msg, ec, step
def checkLeader(self): step = "Checking leader process " + self.cntCommon.startupScript msg, ec = exec_command("sudo machinectl status {0} | grep {1}".\ format(self.cName, self.cntCommon.startupScript)) return msg, ec, step