def process_IN_CREATE(self, event): log.error("GIT(EVENT) %s" % event.pathname) filename = event.pathname time.sleep(1) lines = None with dissomniag.rootContext(): try: with open(filename, 'r') as f: lines = f.readlines() except Exception as e: log.error("GitEventHandler: Cannot read file %s." % filename) return session = dissomniag.Session() if lines != None: for line in lines: try: splitted = line.split("=") appName = splitted[0].strip() branchName = splitted[1].strip() try: app = session.query(dissomniag.model.App).filter( dissomniag.model.App.name == appName).one() vm = session.query(dissomniag.model.VM).filter( dissomniag.model.VM.commonName == branchName).one() liveCd = vm.liveCd rel = session.query( dissomniag.model.AppLiveCdRelation).filter( dissomniag.model.AppLiveCdRelation.liveCd == liveCd).filter( dissomniag.model.AppLiveCdRelation.app == app).one() rel.createRefreshAndResetJob( dissomniag.getIdentity().getAdministrativeUser()) log.info( "App %s auto git update executed on branch %s." % (appName, branchName)) except Exception as e: log.error( "SKIPPED App %s auto git update executed on branch %s. Exception: %s" % (appName, branchName, str(e))) except Exception as e: log.error("process_GIT_EVENT: No valid request.") with dissomniag.rootContext(): try: os.remove(filename) except OSError as e: log.error("Could not delete file %s in git event handler." % filename)
def _checkRunningConfig(self, job = None): inFile = None with dissomniag.rootContext(): try: with open(dissomniag.config.git.pathToConfigFile, 'r') as f: inFile = f.read() except Exception: pass config = self._getNewConfig(job) actualConfig = StringIO.StringIO() config.write(actualConfig) actualConfig = actualConfig.getvalue() inHash = hashlib.sha256() inHash.update(inFile) actHash = hashlib.sha256() actHash.update(actualConfig) if not ((self._getConfigKeySet(config, job) <= self._getHdKeySet(job)) and (self._getConfigKeySet(config, job) <= self._getHdKeySet(job))): log.info("HD Keys and Config Keys differ!") return False grp.getgrnam(str(dissomniag.config.git.gitGroup)).gr_gid if inHash.hexdigest() == actHash.hexdigest(): return True else: return False
def run(self): if not hasattr(self.context, "app") or type( self.context.app) != dissomniag.model.App: self.job.trace("DeleteAppFinally: In Context missing app object.") raise dissomniag.taskManager.UnrevertableFailure( "In Context missing app object.") if self.context.app.AppLiveCdRelations != []: self.multiLog("Cannot delete an app with LiveCd Relations!", log) raise dissomniag.taskManager.TaskFailed( "Cannot delete an app with LiveCd Relations!") self.deleted = True pathToRepository = os.path.join( dissomniag.config.git.pathToGitRepositories, ("%s.git") % self.context.app.name) with dissomniag.rootContext(): try: shutil.rmtree(pathToRepository) except Exception as e: self.multiLog( "Cannot delete local Repository %s %s" % (str(e), pathToRepository), log) session = dissomniag.Session() session.delete(self.context.app) dissomniag.saveCommit(session) return dissomniag.taskManager.TaskReturns.SUCCESS
def revert(self): if not hasattr(self.context, "app") or type(self.context.app) != dissomniag.model.App: self.job.trace("AddAppBranch: In Context missing app object. REVERT") raise dissomniag.taskManager.UnrevertableFailure("In Context missing app object. REVERT") if not hasattr(self.context, "liveCd") or type(self.context.liveCd) != dissomniag.model.LiveCd: self.job.trace("AddAppBranch: In Context missing liveCd object. REVERT") raise dissomniag.taskManager.UnrevertableFailure("In Context missing liveCd object. REVERT") session = dissomniag.Session() self.multiLog("IN REVERT Delete App Branch.") try: appLiveCdRel = session.query(dissomniag.model.AppLiveCdRelation).filter(dissomniag.model.AppLiveCdRelation.app == self.context.app).filter(dissomniag.model.AppLiveCdRelation.liveCd == self.context.liveCd).one() except NoResultFound: self.job.trace("AddAppBranch: No Relation object found. REVERT") raise dissomniag.taskManager.UnrevertableFailure("No Relation object found. REVERT") except MultipleResultsFound: appLiveCdRel = appLiveCdRel[0] pathToRepository = os.path.join(dissomniag.config.git.pathToGitRepositories, ("%s.git") % appLiveCdRel.app.name) branchName = self.context.liveCd.vm.commonName with dissomniag.rootContext(): try: repo = git.Repo(pathToRepository) if branchName in repo.heads: #repo.delete_head(branchName) repo.git.branch("-D", branchName) except Exception as e: self.multiLog("Cannot delete branch in Revert %s" % str(e), log) raise dissomniag.taskManager.TaskFailed("Cannot delete branch in Revert %s" % str(e)) return dissomniag.taskManager.TaskReturns.SUCCESS
def update(self, job): if not isinstance(job, dissomniag.taskManager.Job): raise dissomniag.utils.MissingJobObject() if not self.isAdminUsable: return False with dissomniag.rootContext(): self.multiLog("Entering git.update", job) #1. git pull self._pull(job) #2. getNewConfig config = self._getNewConfig(job) with open(dissomniag.config.git.pathToConfigFile, 'w') as f: config.write(f) index = self.adminRepo.index index.add(["gitosis.conf"]) #3a) getUsedKeyList #3b) getHdKeyList #3c) addNewKeys self._addKeys(config, job) #3d) calc hdKeyList - usedKeyList #3e) delete not used keys (5.) on Hd self._deleteNotLongerUsedKeys(config, job = None) #5. commit repo self._commit(job) #6. push repo self._push(job)
def _disableStrictServerKeyChecking(self, hostOrIp="localhost"): with dissomniag.rootContext(): sshFileName = "/etc/ssh/ssh_config" if os.path.isfile(sshFileName): lines = None pattern = ("^Host %s$" % hostOrIp) prog = re.compile(pattern) with open(sshFileName, 'r') as f: lines = f.readlines() foundHosts = [] for line in lines: if prog.match(line): return True if not os.path.isfile(sshFileName): try: with open(sshFileName, mode='w') as f: f.write("") os.chmod(sshFileName, 0o644) except OSError: pass with open(sshFileName, 'a') as f: f.write("Host %s\n" % hostOrIp) f.write("\tStrictHostKeyChecking no\n\n")
def update(self, job): if not isinstance(job, dissomniag.taskManager.Job): raise dissomniag.utils.MissingJobObject() if not self.isAdminUsable: return False with dissomniag.rootContext(): self.multiLog("Entering git.update", job) #1. git pull self._pull(job) #2. getNewConfig config = self._getNewConfig(job) with open(dissomniag.config.git.pathToConfigFile, 'w') as f: config.write(f) index = self.adminRepo.index index.add(["gitosis.conf"]) #3a) getUsedKeyList #3b) getHdKeyList #3c) addNewKeys self._addKeys(config, job) #3d) calc hdKeyList - usedKeyList #3e) delete not used keys (5.) on Hd self._deleteNotLongerUsedKeys(config, job=None) #5. commit repo self._commit(job) #6. push repo self._push(job)
def _checkRunningConfig(self, job=None): inFile = None with dissomniag.rootContext(): try: with open(dissomniag.config.git.pathToConfigFile, 'r') as f: inFile = f.read() except Exception: pass config = self._getNewConfig(job) actualConfig = StringIO.StringIO() config.write(actualConfig) actualConfig = actualConfig.getvalue() inHash = hashlib.sha256() inHash.update(inFile) actHash = hashlib.sha256() actHash.update(actualConfig) if not ( (self._getConfigKeySet(config, job) <= self._getHdKeySet(job)) and (self._getConfigKeySet(config, job) <= self._getHdKeySet(job))): log.info("HD Keys and Config Keys differ!") return False grp.getgrnam(str(dissomniag.config.git.gitGroup)).gr_gid if inHash.hexdigest() == actHash.hexdigest(): return True else: return False
def _addKeys(self, config, job = None): if job != None: self.multiLog("Entering git._addKeys", job) addSet = self._getKeysToAdd(config, job) keys = set() session = dissomniag.Session() index = self.adminRepo.index #Get User Keys try: mkeys = session.query(dissomniag.auth.PublicKey).all() for key in mkeys: if key.getUserHostString() in addSet: keys.add(key) except NoResultFound: pass #GetNodeKeys try: mkeys = session.query(dissomniag.model.SSHNodeKey).all() for key in mkeys: if key.getUserHostString() in addSet: keys.add(key) except NoResultFound: pass with dissomniag.rootContext(): for key in keys: localFileName = "keydir/%s" % key.getPublicFileString() pathToFile = os.path.join(dissomniag.config.git.pathToKeyFolder, key.getPublicFileString()) try: with open(pathToFile, 'w') as f: f.write(key.publicKey) except Exception: pass index.add([localFileName]) return
def makeInitialCommit(self, app, job=None): skeletonFolder, skeletonRepo = self._getSkeletonFolder(job) if skeletonFolder == None: self.multiLog("Could not create tmp skeleton folder", job) raise dissomniag.taskManager.TaskFailed( "Could not create tmp skeleton folder") with dissomniag.rootContext(): try: gitosisRepo = str( "%s@%s:%s.git" % (dissomniag.config.git.gitUser, dissomniag.config.git.gitosisHost, app.name)) skeletonRepo.create_remote("origin", gitosisRepo) skeletonRepo.git.push("origin", "master:refs/heads/master") self.addUpdateScript(app, job) except Exception as e: self.multiLog( "Cannot push to origin repo. %s, Exception: %s" % (gitosisRepo, str(e)), job) raise dissomniag.taskManager.TaskFailed( "Cannot push to origin repo. %s" % gitosisRepo) finally: try: del (skeletonRepo) shutil.rmtree(skeletonFolder) except Exception as e: pass return True
def revert(self): self.patternFolder = os.path.join(dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) self.chrootFolder = os.path.join(self.patternFolder, "chroot") with dissomniag.rootContext(): self.cleanUp() return dissomniag.taskManager.TaskReturns.SUCCESS
def _setUpGitFileChangeListener(self): if os.access(dissomniag.config.git.scriptSyncFolder, os.F_OK): with dissomniag.rootContext(): try: shutil.rmtree(dissomniag.config.git.scriptSyncFolder) except Exception as e: pass self.multiLog("No %s Folder. Try to create it." % dissomniag.config.git.scriptSyncFolder) with dissomniag.rootContext(): try: os.makedirs(dissomniag.config.git.scriptSyncFolder) uid, gid = self._getGitUserGroup() os.chown(dissomniag.config.git.scriptSyncFolder, uid, gid) except OSError, e: self.multiLog("Could not create script Sync folder. %s" % e) self.isAdminUsable = False return
def cleanUpPrepare(self, job): self.patternFolder = os.path.join(dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) if not dissomniag.chDir(self.patternFolder): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) raise dissomniag.taskManager.TaskFailed() with dissomniag.rootContext(): #Clean to binary cmd = "lb clean --binary" self.multiLog("running %s" % cmd, job, log) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB clean --binary error", job, log) #raise dissomniag.taskManager.TaskFailed() self.stageDir = os.path.join(self.patternFolder, ".build") self.binLocalInc = os.path.join(self.patternFolder, "config/includes.binary/") try: shutil.rmtree(self.binLocalInc) except (OSError, IOError) as e: pass try: os.makedirs(self.binLocalInc) except (OSError, IOError) as e: self.multiLog("Cannot recreate %s" % self.binLocalInc, job, log) isolinuxCfgDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "includes.binary/isolinux") try: os.makedirs(os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot create config/includes.binary/isolinux/") listings = os.listdir(isolinuxCfgDir) for infile in listings: try: shutil.copy2(os.path.join(isolinuxCfgDir, infile), os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot copy includes.binary") # #if self.stageDir.endswith("/"): # cmd = "rm %sbinary_disk %sbinary_checksums %sbinary_includes" % (self.stageDir, self.stageDir, self.stageDir) #else: # cmd = "rm %s/binary_disk %s/binary_checksums %s/binary_includes" % (self.stageDir, self.stageDir, self.stageDir) #self.multiLog("exec %s" % cmd, job, log) #ret, output = dissomniag.utils.StandardCmd(cmd, log).run() #if ret != 0: # self.multiLog("Could not exec %s correctly" % cmd, job, log) return True
def _commit(self, job = None, commitMessage = None): with dissomniag.rootContext(): if job != None: self.multiLog("Entering git._commit", job) if commitMessage == None: commitMessage = str(time.strftime("%d.%m.%Y, %H:%M:%S")) index = self.adminRepo.index if job != None: self.multiLog("Commit gitosis-admin Repo %s" % commitMessage, job) ret = index.commit(commitMessage) self.adminRepo.commit() return True
def run(self): if not hasattr(self.context, "app") or type( self.context.app) != dissomniag.model.App: self.job.trace( "DeleteAppLiveCdRelation: In Context missing app object.") raise dissomniag.taskManager.UnrevertableFailure( "In Context missing app object.") if not hasattr(self.context, "liveCd") or type( self.context.liveCd) != dissomniag.model.LiveCd: self.job.trace( "DeleteAppLiveCdRelation: In Context missing liveCd object.") raise dissomniag.taskManager.UnrevertableFailure( "In Context missing liveCd object.") session = dissomniag.Session() try: appLiveCdRel = session.query( dissomniag.model.AppLiveCdRelation).filter( dissomniag.model.AppLiveCdRelation.app == self.context.app ).filter(dissomniag.model.AppLiveCdRelation.liveCd == self.context.liveCd).one() except NoResultFound: self.job.trace( "DeleteAppLiveCdRelation: No Relation object found.") raise dissomniag.taskManager.UnrevertableFailure( "No Relation object found.") except MultipleResultsFound: appLiveCdRel = appLiveCdRel[0] pathToRepository = os.path.join( dissomniag.config.git.pathToGitRepositories, ("%s.git") % appLiveCdRel.app.name) branchName = self.context.liveCd.vm.commonName with dissomniag.rootContext(): try: repo = git.Repo(pathToRepository) if branchName in repo.heads: #repo.delete_head(branchName) repo.git.branch("-D", branchName) except Exception as e: self.multiLog("Cannot delete branch in Revert %s" % str(e), log) raise dissomniag.taskManager.TaskFailed( "Cannot delete branch in Revert %s" % str(e)) self.deleted = True session = dissomniag.Session() session.delete(appLiveCdRel) dissomniag.saveCommit(session) return dissomniag.taskManager.TaskReturns.SUCCESS
def _commit(self, job=None, commitMessage=None): with dissomniag.rootContext(): if job != None: self.multiLog("Entering git._commit", job) if commitMessage == None: commitMessage = str(time.strftime("%d.%m.%Y, %H:%M:%S")) index = self.adminRepo.index if job != None: self.multiLog("Commit gitosis-admin Repo %s" % commitMessage, job) ret = index.commit(commitMessage) self.adminRepo.commit() return True
def process_IN_CREATE(self, event): log.error("GIT(EVENT) %s" % event.pathname) filename = event.pathname time.sleep(1) lines = None with dissomniag.rootContext(): try: with open(filename, 'r') as f: lines = f.readlines() except Exception as e: log.error("GitEventHandler: Cannot read file %s." % filename) return session = dissomniag.Session() if lines != None: for line in lines: try: splitted = line.split("=") appName = splitted[0].strip() branchName = splitted[1].strip() try: app = session.query(dissomniag.model.App).filter(dissomniag.model.App.name == appName).one() vm = session.query(dissomniag.model.VM).filter(dissomniag.model.VM.commonName == branchName).one() liveCd = vm.liveCd rel = session.query(dissomniag.model.AppLiveCdRelation).filter(dissomniag.model.AppLiveCdRelation.liveCd == liveCd).filter(dissomniag.model.AppLiveCdRelation.app == app).one() rel.createRefreshAndResetJob(dissomniag.getIdentity().getAdministrativeUser()) log.info("App %s auto git update executed on branch %s." % (appName, branchName)) except Exception as e: log.error("SKIPPED App %s auto git update executed on branch %s. Exception: %s" % (appName, branchName, str(e))) except Exception as e: log.error("process_GIT_EVENT: No valid request.") with dissomniag.rootContext(): try: os.remove(filename) except OSError as e: log.error("Could not delete file %s in git event handler." % filename)
def reset(self, job): # 1. Delete Local Image self.multiLog("Delete local LiveCd image.", job, log) self.multiLog("Rmtree %s" % self.vm.getLocalUtilityFolder(job.getUser()), job, log) with dissomniag.rootContext(): try: shutil.rmtree(self.vm.getLocalUtilityFolder(job.getUser())) except (IOError, OSError) as e: self.multiLog("Cannot delete local LiveCd image. LocalFolder: %s " % self.vm.getLocalUtilityFolder(job.getUser()), job, log) self.vm.changeState(dissomniag.model.NodeState.NOT_CREATED) else: self.vm.changeState(dissomniag.model.NodeState.NOT_CREATED) return True
def _push(self, job=None): with dissomniag.rootContext(): if job != None: self.multiLog("Entering git._push", job) origin = self.adminRepo.remotes.origin dissomniag.getIdentity().refreshSSHEnvironment() try: origin.push() except Exception: dissomniag.getIdentity().refreshSSHEnvironment() if job != None: self.multiLog("Entering git._push AGAIN", job) origin.push() return True
def addUpdateScript(self, app, job = None): log.info("Trying to add Upadte Hook.") targetFile = os.path.join(dissomniag.config.git.pathToGitRepositories, "%s.git/hooks/update" % app.name) with dissomniag.rootContext(): try: with open(targetFile, 'w') as f: f.write(self.getScriptPattern(str(app.name))) except OSError as e: log.error("Could not create update Script.") return False with dissomniag.rootContext(): try: uid, gid = self._getGitUserGroup() os.chown(targetFile, uid, gid) os.chmod(targetFile, 0o755) except OSError as e: try: shutil.rmtree(targetFile) log.error("Cannot change User for update hook.") except Exception as e: log.error("Cannot delete update hook.") return True
def _push(self, job = None): with dissomniag.rootContext(): if job != None: self.multiLog("Entering git._push", job) origin = self.adminRepo.remotes.origin dissomniag.getIdentity().refreshSSHEnvironment() try: origin.push() except Exception: dissomniag.getIdentity().refreshSSHEnvironment() if job != None: self.multiLog("Entering git._push AGAIN", job) origin.push() return True
def addUpdateScript(self, app, job=None): log.info("Trying to add Upadte Hook.") targetFile = os.path.join(dissomniag.config.git.pathToGitRepositories, "%s.git/hooks/update" % app.name) with dissomniag.rootContext(): try: with open(targetFile, 'w') as f: f.write(self.getScriptPattern(str(app.name))) except OSError as e: log.error("Could not create update Script.") return False with dissomniag.rootContext(): try: uid, gid = self._getGitUserGroup() os.chown(targetFile, uid, gid) os.chmod(targetFile, 0o755) except OSError as e: try: shutil.rmtree(targetFile) log.error("Cannot change User for update hook.") except Exception as e: log.error("Cannot delete update hook.") return True
def _pull(self, job=None): with dissomniag.rootContext(): if job != None: self.multiLog("Entering git._pull", job) if self.adminRepo.is_dirty(): self._commit(job, "Dirty repo commit") origin = self.adminRepo.remotes.origin dissomniag.getIdentity().refreshSSHEnvironment() try: origin.pull() except Exception: dissomniag.getIdentity().refreshSSHEnvironment() if job != None: self.multiLog("Entering git._pull AGAIN", job) origin.pull() return True
def _pull(self, job = None): with dissomniag.rootContext(): if job != None: self.multiLog("Entering git._pull", job) if self.adminRepo.is_dirty(): self._commit(job, "Dirty repo commit") origin = self.adminRepo.remotes.origin dissomniag.getIdentity().refreshSSHEnvironment() try: origin.pull() except Exception: dissomniag.getIdentity().refreshSSHEnvironment() if job != None: self.multiLog("Entering git._pull AGAIN", job) origin.pull() return True
def run(self): self.infoObj = dissomniag.model.LiveCdEnvironment() #1. Check if Utility Folder exists if not os.access(dissomniag.config.dissomniag.utilityFolder, os.F_OK): with dissomniag.rootContext(): try: os.makedirs(dissomniag.config.dissomniag.utilityFolder) os.chown(dissomniag.config.dissomniag.utilityFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) os.makedirs(dissomniag.config.dissomniag.serverFolder) except OSError, e: self.infoObj.errorInfo.append("Could not create utility folder. %s" % e) self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure()
def cleanUp(self): self.patternFolder = os.path.join(dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) if not dissomniag.chDir(self.patternFolder): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) raise dissomniag.taskManager.TaskFailed() with dissomniag.rootContext(): #Clean to binary cmd = "lb clean --binary" self.multiLog("running %s" % cmd) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB clean --binary error") #raise dissomniag.taskManager.TaskFailed() self.stageDir = os.path.join(self.patternFolder, ".build") self.binLocalInc = os.path.join(self.patternFolder, "config/includes.binary/") try: shutil.rmtree(self.binLocalInc) except (OSError, IOError) as e: pass try: os.makedirs(self.binLocalInc) except (OSError, IOError) as e: self.multiLog("Cannot recreate %s" % self.binLocalInc) isolinuxCfgDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "includes.binary/isolinux") try: os.makedirs(os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot create config/includes.binary/isolinux/") listings = os.listdir(isolinuxCfgDir) for infile in listings: try: shutil.copy2(os.path.join(isolinuxCfgDir, infile), os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot copy includes.binary")
def makeInitialCommit(self, app, job = None): skeletonFolder, skeletonRepo = self._getSkeletonFolder(job) if skeletonFolder == None: self.multiLog("Could not create tmp skeleton folder", job) raise dissomniag.taskManager.TaskFailed("Could not create tmp skeleton folder") with dissomniag.rootContext(): try: gitosisRepo = str("%s@%s:%s.git" % (dissomniag.config.git.gitUser, dissomniag.config.git.gitosisHost, app.name)) skeletonRepo.create_remote("origin", gitosisRepo) skeletonRepo.git.push("origin", "master:refs/heads/master") self.addUpdateScript(app, job) except Exception as e: self.multiLog("Cannot push to origin repo. %s, Exception: %s" % (gitosisRepo, str(e)), job) raise dissomniag.taskManager.TaskFailed("Cannot push to origin repo. %s" % gitosisRepo) finally: try: del(skeletonRepo) shutil.rmtree(skeletonFolder) except Exception as e: pass return True
def run(self): if not hasattr(self.context, "app") or type(self.context.app) != dissomniag.model.App: self.job.trace("DeleteAppFinally: In Context missing app object.") raise dissomniag.taskManager.UnrevertableFailure("In Context missing app object.") if self.context.app.AppLiveCdRelations != []: self.multiLog("Cannot delete an app with LiveCd Relations!", log) raise dissomniag.taskManager.TaskFailed("Cannot delete an app with LiveCd Relations!") self.deleted = True pathToRepository = os.path.join(dissomniag.config.git.pathToGitRepositories, ("%s.git") % self.context.app.name) with dissomniag.rootContext(): try: shutil.rmtree(pathToRepository) except Exception as e: self.multiLog("Cannot delete local Repository %s %s" % (str(e), pathToRepository), log) session = dissomniag.Session() session.delete(self.context.app) dissomniag.saveCommit(session) return dissomniag.taskManager.TaskReturns.SUCCESS
def _addKeys(self, config, job=None): if job != None: self.multiLog("Entering git._addKeys", job) addSet = self._getKeysToAdd(config, job) keys = set() session = dissomniag.Session() index = self.adminRepo.index #Get User Keys try: mkeys = session.query(dissomniag.auth.PublicKey).all() for key in mkeys: if key.getUserHostString() in addSet: keys.add(key) except NoResultFound: pass #GetNodeKeys try: mkeys = session.query(dissomniag.model.SSHNodeKey).all() for key in mkeys: if key.getUserHostString() in addSet: keys.add(key) except NoResultFound: pass with dissomniag.rootContext(): for key in keys: localFileName = "keydir/%s" % key.getPublicFileString() pathToFile = os.path.join( dissomniag.config.git.pathToKeyFolder, key.getPublicFileString()) try: with open(pathToFile, 'w') as f: f.write(key.publicKey) except Exception: pass index.add([localFileName]) return
def prepare(self, job): self.infoObj = dissomniag.model.LiveCdEnvironment() if not self.infoObj.prepared: self.multiLog( "LiveCD Environment not prepared. No LiveCD creation possible!", job, log) self.vm.changeState(dissomniag.model.NodeState.PREPARE_ERROR) raise dissomniag.taskManager.TaskFailed( "LiveCD Environment not prepared. No LiveCD creation possible!" ) self.patternFolder = os.path.join( dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) with dissomniag.rootContext(): try: if not dissomniag.chDir(self.patternFolder): self.multiLog("Cannot chdir to %s" % self.patternFolder, job, log) self.vm.changeState( dissomniag.model.NodeState.PREPARE_ERROR) raise dissomniag.taskManager.TaskFailed( "Could not change to pattern folder.") self.lockFile = os.path.join( self.patternFolder, dissomniag.config.dissomniag.patternLockFile) self.myLock = lockfile.FileLock(self.lockFile, threaded=True) with self.myLock: cmd = "lb config" self.multiLog("Run lb config", job, log) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB config error") raise dissomniag.taskManager.TaskFailed() # 1. Copy infoXML self.liveInfoString, self.versioningHash = self.liveCd.getInfoXMLwithVersionongHash( job.getUser()) lifeInfoFile = os.path.join( self.patternFolder, "config/includes.binary/liveInfo.xml") self.multiLog(str(lifeInfoFile), job, log) with open(lifeInfoFile, 'w') as f: f.write(self.liveInfoString) ### #ToDo: At other thinks to binary include like Predefined Apps ### #2. Make initial Build # Try 10 times (Solves some repository connection timeout problems) #cmd = "lb build" #self.multiLog("Make initial Build", job, log) cmd = "lb binary" self.multiLog("lb binary", job, log) success = False for i in range(1, 11): if job._getStatePrivate( ) == dissomniag.taskManager.jobs.JobStates.CANCELLED: self.multiLog( "Job cancelled. Initial LivdCD build failed.", job, log) self.vm.changeState( dissomniag.model.NodeState.PREPARE_ERROR) raise dissomniag.taskManager.TaskFailed( "Job cancelled. Initial LivdCD build failed.") ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog( "Initial LiveCD build failed. Retry %d" % i, job, log) continue else: success = True break if not success: self.multiLog("Initial LiveCD build failed finally.", job, log) self.vm.changeState( dissomniag.model.NodeState.PREPARE_ERROR) raise dissomniag.taskManager.TaskFailed( "Initial LiveCD build failed finally.") #3. Copy iso to final Folder # Check if folder for image exists if not os.access( self.vm.getLocalUtilityFolder(job.getUser()), os.F_OK): os.makedirs( self.vm.getLocalUtilityFolder(job.getUser())) shutil.copy2("./binary.iso", self.vm.getLocalPathToCdImage(job.getUser())) with open( os.path.join( self.vm.getLocalUtilityFolder(job.getUser()), "configHash"), 'w') as f: f.write(self.versioningHash) self.liveCd.imageCreated = True finally: if not dissomniag.resetDir(): self.multiLog("Cannot chdir to %s" % self.patternFolder, job, log) self.cleanUpPrepare(job) self.multiLog("LiveCd Image created.", job, log) self.vm.changeState(dissomniag.model.NodeState.PREPARED) return True
def cleanUpPrepare(self, job): self.patternFolder = os.path.join( dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) if not dissomniag.chDir(self.patternFolder): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) raise dissomniag.taskManager.TaskFailed() with dissomniag.rootContext(): #Clean to binary cmd = "lb clean --binary" self.multiLog("running %s" % cmd, job, log) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB clean --binary error", job, log) #raise dissomniag.taskManager.TaskFailed() self.stageDir = os.path.join(self.patternFolder, ".build") self.binLocalInc = os.path.join(self.patternFolder, "config/includes.binary/") try: shutil.rmtree(self.binLocalInc) except (OSError, IOError) as e: pass try: os.makedirs(self.binLocalInc) except (OSError, IOError) as e: self.multiLog("Cannot recreate %s" % self.binLocalInc, job, log) isolinuxCfgDir = os.path.join( dissomniag.config.dissomniag.staticLiveFolder, "includes.binary/isolinux") try: os.makedirs( os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot create config/includes.binary/isolinux/") listings = os.listdir(isolinuxCfgDir) for infile in listings: try: shutil.copy2( os.path.join(isolinuxCfgDir, infile), os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot copy includes.binary") # #if self.stageDir.endswith("/"): # cmd = "rm %sbinary_disk %sbinary_checksums %sbinary_includes" % (self.stageDir, self.stageDir, self.stageDir) #else: # cmd = "rm %s/binary_disk %s/binary_checksums %s/binary_includes" % (self.stageDir, self.stageDir, self.stageDir) #self.multiLog("exec %s" % cmd, job, log) #ret, output = dissomniag.utils.StandardCmd(cmd, log).run() #if ret != 0: # self.multiLog("Could not exec %s correctly" % cmd, job, log) return True
def run(self): if (not hasattr(self.context, "LiveCd")): self.multiLog("No LiveCd Object in Context", log) raise dissomniag.taskManager.TaskFailed("No VM Object in Context") self.infoObj = dissomniag.model.LiveCdEnvironment() if not self.infoObj.prepared: self.multiLog("LiveCD Environment not prepared. No LiveCD creation possible!", log) raise dissomniag.taskManager.TaskFailed("LiveCD Environment not prepared. No LiveCD creation possible!") self.patternFolder = os.path.join(dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) with dissomniag.rootContext(): try: if not dissomniag.chDir(self.patternFolder): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) raise dissomniag.taskManager.TaskFailed() self.lockFile = os.path.join(self.patternFolder, dissomniag.config.dissomniag.patternLockFile) self.mylock = lockfile.FileLock(self.lockFile, threaded = True) with self.myLock: # 1. Copy infoXML self.versioningHash, self.liveInfoString = self.context.VM.getInfoXMLwithVersionongHash(self.job.getUser()) with open("./config/includes.binary/liveInfo.xml") as f: f.write(self.liveInfoString) ### #ToDo: At other thinks to binary include like Predefined Apps ### #2. Make initial Build # Try 10 times (Solves some repository connection timeout problems) #cmd = "lb build" #self.multiLog("Make initial build", log) cmd = "lb binary" self.multiLog("lb binary", log) success = False for i in range(1, 11): if self.job._getStatePrivate() == dissomniag.taskManager.jobs.JobStates.CANCELLED: self.multiLog("Job cancelled. Initial LivdCD build failed.") raise dissomniag.taskManager.TaskFailed("Job cancelled. Initial LivdCD build failed.") ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("Initial LiveCD build failed. Retry %d" % i, log) continue else: success = True break if not success: self.multiLog("Initial LiveCD build failed finally.", log) raise dissomniag.taskManager.TaskFailed("Initial LiveCD build failed finally.") #3. Copy iso to final Folder # Check if folder for image exists if not os.access(self.context.LiveCd.vm.getUtilityFolder(), os.F_OK): os.makedirs(self.context.LiveCd.vm.getUtilityFolder()) shutil.copy2("./binary.iso", self.context.LiveCd.vm.getLocalPathToCdImage(self.job.getUser())) with open(os.path.join(self.context.LiveCd.vm.getLocalUtilityFolder(), "configHash"), 'w') as f: f.write(self.versioningHash) self.context.LiveCd.imageCreated = True finally: if not dissomniag.resetDir(): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) self.cleanUp() return dissomniag.taskManager.TaskReturns.SUCCESS
def _checkAdmin(self, job=None): #1. Check if Utility Folder exists if job != None: job.trace("Start _checkAdmin in GitEnvironment") ### FIX: Delete every time before creation. Not clean but it works. So git repo doesn't get unusable #try: # if os.access(dissomniag.config.git.pathToLocalUtilFolder, os.F_OK): # dissomniag.getRoot() # shutil.rmtree(dissomniag.config.git.dissomniag.config.git.scriptSyncFolderpathToLocalUtilFolder) #except Exception as e: # if job != None: # self.multiLog("INITIAL DELETE ERROR %s" % str(e), job) #finally: # dissomniag.resetPermissions() try: if not os.access(dissomniag.config.git.scriptSyncFolder, os.F_OK): if job != None: self.multiLog( "No %s Folder. Try to create it." % dissomniag.config.git.scriptSyncFolder, job) with dissomniag.rootContext(): try: os.makedirs(dissomniag.config.git.scriptSyncFolder) uid, gid = self._getGitUserGroup() os.chown(dissomniag.config.git.scriptSyncFolder, uid, gid) except OSError, e: if job != None: self.multiLog( "Could not create script Sync folder. %s" % e, job) self.isAdminUsable = False return if not os.access(dissomniag.config.git.pathToLocalUtilFolder, os.F_OK): if job != None: self.multiLog( "No %s Folder. Try to create it." % dissomniag.config.git.pathToLocalUtilFolder, job) with dissomniag.rootContext(): try: os.makedirs( dissomniag.config.git.pathToLocalUtilFolder) os.chown(dissomniag.config.git.pathToLocalUtilFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) except OSError, e: if job != None: self.multiLog( "Could not create utility folder. %s" % e, job) self.isAdminUsable = False return if self._makeInitialCheckout(job): self.isAdminUsable = True try: self.adminRepo = git.Repo( dissomniag.config.git.pathToLocalUtilFolder) self.update(job) except Exception as e: if job != None: self.multiLog(str(e), job) self.isAdminUsable = False return False else: self.isAdminUsable = False
def run(self): self.infoObj = dissomniag.model.LiveCdEnvironment() if self.infoObj.prepared: return self.returnSuccess() #If the Environment is not prepared, create it. #1. Check if Pattern folder exists. # True: Delete, and recreate it # False: just create it self.patternFolder = os.path.join(dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.liveCdPatternDirectory) with dissomniag.rootContext(): self.checkIfPatternFolderExists() try: #3. Change current working Directory if not dissomniag.chDir(self.patternFolder): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) raise dissomniag.taskManager.TaskFailed() self.deleteOldDebianLiveFolders(self.patternFolder) #2. Create File Lock for Pattern environment self.lockFile = os.path.join(self.patternFolder, dissomniag.config.dissomniag.patternLockFile) self.mylock = lockfile.FileLock(self.lockFile, threaded = True) with self.mylock: #3a. Make auto Directory and link config files self.autoFolder = os.path.join(self.patternFolder, "auto/") try: os.makedirs(self.autoFolder) except OSError: self.multiLog("Cannot create AutoFolder") raise dissomniag.taskManager.TaskFailed() staticAutoFolder = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "auto") infiles = os.listdir(staticAutoFolder) for myfile in infiles: try: #print os.path.join(staticAutoFolder, myfile) #print os.path.join(self.autoFolder, myfile) os.symlink(os.path.join(staticAutoFolder, myfile), os.path.join(self.autoFolder, myfile)) except OSError: self.multiLog("Cannot Symlink %s" % myfile , log) if myfile == "config": raise dissomniag.taskManager.TaskFailed() #3b. Create initial stucture cmd = "lb config" self.multiLog("running %s" % cmd) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB Config error") raise dissomniag.taskManager.TaskFailed() #3c. Copy dissomniag packagelist packageListFile = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "packagesLists/dissomniag.list") chrootLocalPackagesListFolder = os.path.join(self.patternFolder, "config/package-lists") try: os.symlink(os.path.abspath(packageListFile), os.path.join(chrootLocalPackagesListFolder, "dissomniag.list")) except OSError: self.multiLog("Cannot Symlink dissomniag.list") raise dissomniag.taskManager.TaskFailed() #3d. Copy all chroot_local files chrootLocalFilesDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "chroot_local-includes") listings = os.listdir(chrootLocalFilesDir) for infile in listings: try: shutil.copytree(os.path.join(chrootLocalFilesDir, infile), os.path.join(self.patternFolder, "config/includes.chroot/" + infile), symlinks = True) except OSError: src = os.path.join(chrootLocalFilesDir, infile) dst = os.path.join(self.patternFolder, "config/includes.chroot/" + infile) self.multiLog("Cannot copy an includes.chroot, src= %s , dst= %s" % (src,dst), log) #3e. Copy all chroot_local hooks hooksFilesDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "hooks") listings = os.listdir(hooksFilesDir) for infile in listings: try: shutil.copy2(os.path.join(hooksFilesDir, infile), os.path.join(self.patternFolder, "config/hooks/")) except OSError: self.multiLog("Cannot copy an chroot_local-hook") tasksFilesDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "task-lists/") listings = os.listdir(tasksFilesDir) for infile in listings: try: shutil.copy2(os.path.join(tasksFilesDir, infile), os.path.join(self.patternFolder, "config/task-lists/")) except OSError: self.multiLog("Cannot copy an chroot_local-hook") #4. Install Live Daemon self.installLiveDaemon(self.patternFolder) #5. Copy needed files. (like OMNeT binaries) self.installOmnet(self.patternFolder) #5b Copy syslinux Debian package packagesChrootStaticDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "packages.chroot") listings = os.listdir(packagesChrootStaticDir) for infile in listings: try: shutil.copy2(os.path.join(packagesChrootStaticDir, infile), os.path.join(self.patternFolder, "config/packages.chroot/")) except OSError: self.multiLog("Cannot copy packages.chroot") #6c Copy isolinux cfg isolinuxCfgDir = os.path.join(dissomniag.config.dissomniag.staticLiveFolder, "includes.binary/isolinux") try: os.makedirs(os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot create config/includes.binary/isolinux/") listings = os.listdir(isolinuxCfgDir) for infile in listings: try: shutil.copy2(os.path.join(isolinuxCfgDir, infile), os.path.join(self.patternFolder, "config/includes.binary/isolinux/")) except OSError: self.multiLog("Cannot copy includes.binary") #6. Init debian live environment cmd = "lb config" self.multiLog("running %s" % cmd) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB Config error") raise dissomniag.taskManager.TaskFailed() #7. Make bootstrap # Try 10 times (Solves some repository connection timeout problems) cmd = "lb bootstrap" self.multiLog("Run lb bootstrap", log) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB bootstrap error") raise dissomniag.taskManager.TaskFailed() #8. Make chrooot # Try 10 times (Solves some repository connection timeout problems) cmd = "lb chroot" self.multiLog("Run lb chroot", log) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB chroot error") raise dissomniag.taskManager.TaskFailed() #9. Make binary # Try 10 times (Solves some repository connection timeout problems) cmd = "lb binary" self.multiLog("Run lb binary", log) ret, output = dissomniag.utils.StandardCmd(cmd, log).run() if ret != 0: self.multiLog("LB binary error") raise dissomniag.taskManager.TaskFailed() #8. #cmd = "lb build" #self.multiLog("Run lb build", log) #ret, output = dissomniag.utils.StandardCmd(cmd, log).run() #success = False #for i in range(1, 11): # if self.job._getStatePrivate() == dissomniag.taskManager.jobs.JobStates.CANCELLED: # self.multiLog("Job cancelled. Initial LivdCD build failed. (lb build") # raise dissomniag.taskManager.TaskFailed("Job cancelled. Initial LivdCD build failed.") # # ret, output = dissomniag.utils.StandardCmd(cmd, log).run() # if ret != 0: # self.multiLog("Initial LiveCD build failed. Retry %d" % i, log) # continue # else: # success = True # break #if not success: # self.multiLog("Initial LiveCD build failed finally.", log) # raise dissomniag.taskManager.TaskFailed("Initial LiveCD build failed finally.") #8. #7. Mark environment as Prepared preparedFile = os.path.join(self.patternFolder, "CHECKED") myFile = open(preparedFile, 'w') myFile.write("CHECKED") myFile.close() self.infoObj.usable = True self.infoObj.prepared = True self.returnSuccess() finally: if not dissomniag.resetDir(): self.multiLog("Cannot chdir to %s" % self.patternFolder, log) self.cleanUp()
class LiveCdEnvironmentChecks(dissomniag.taskManager.AtomicTask): def run(self): self.infoObj = dissomniag.model.LiveCdEnvironment() #1. Check if Utility Folder exists if not os.access(dissomniag.config.dissomniag.utilityFolder, os.F_OK): with dissomniag.rootContext(): try: os.makedirs(dissomniag.config.dissomniag.utilityFolder) os.chown(dissomniag.config.dissomniag.utilityFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) os.makedirs(dissomniag.config.dissomniag.serverFolder) except OSError, e: self.infoObj.errorInfo.append("Could not create utility folder. %s" % e) self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure() #2 Check if Utility Folder is writable if not os.access(dissomniag.config.dissomniag.utilityFolder, os.W_OK): try: with dissomniag.rootContext(): os.chown(dissomniag.config.dissomniag.utilityFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) except OSError as e: self.infoObj.errorInfo.append("Utility Folder is not writable.") self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure() if not os.access(dissomniag.config.dissomniag.serverFolder, os.W_OK): try: with dissomniag.rootContext(): os.makedirs(dissomniag.config.dissomniag.serverFolder) os.chown(dissomniag.config.dissomniag.serverFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) except OSError as e: self.infoObj.errorInfo.append("Server Folder is not writable.") self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure() if not os.access(dissomniag.config.dissomniag.vmsFolder, os.W_OK): try: with dissomniag.rootContext(): os.makedirs(dissomniag.config.dissomniag.vmsFolder) os.chown(dissomniag.config.dissomniag.vmsFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) except OSError as e: self.infoObj.errorInfo.append("LiveImages Folder is not writable.") self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure() #3. Check if server has root permissions if not os.getuid() == 0: self.infoObj.errorInfo.append("The server needs root privileges to create live cd's.") self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure() #4. Check if all utilities to create live cd's are installed. Try to install them. with dissomniag.rootContext(): cache = apt.Cache() execInstall = False try: liveBuild = cache["live-build"] if not liveBuild.is_installed: liveBuild.markInstall() execInstall = True except KeyError: self.infoObj.errorInfo.append("A apt package is not available!") self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure() if execInstall: if cache.commit(apt.progress.TextFetchProgress(), apt.progress.InstallProgress()): self.infoObj.usable = True return dissomniag.taskManager.TaskReturns.SUCCESS else: self.infoObj.error.append("Installation Error!") self.infoObj.usable = False self.job.trace(self.infoObj.getErrorInfo()) raise dissomniag.taskManager.UnrevertableFailure()
def _checkAdmin(self, job = None): #1. Check if Utility Folder exists if job != None: job.trace("Start _checkAdmin in GitEnvironment") ### FIX: Delete every time before creation. Not clean but it works. So git repo doesn't get unusable #try: # if os.access(dissomniag.config.git.pathToLocalUtilFolder, os.F_OK): # dissomniag.getRoot() # shutil.rmtree(dissomniag.config.git.dissomniag.config.git.scriptSyncFolderpathToLocalUtilFolder) #except Exception as e: # if job != None: # self.multiLog("INITIAL DELETE ERROR %s" % str(e), job) #finally: # dissomniag.resetPermissions() try: if not os.access(dissomniag.config.git.scriptSyncFolder, os.F_OK): if job != None: self.multiLog("No %s Folder. Try to create it." % dissomniag.config.git.scriptSyncFolder, job) with dissomniag.rootContext(): try: os.makedirs(dissomniag.config.git.scriptSyncFolder) uid, gid = self._getGitUserGroup() os.chown(dissomniag.config.git.scriptSyncFolder, uid, gid) except OSError, e: if job != None: self.multiLog("Could not create script Sync folder. %s" % e, job) self.isAdminUsable = False return if not os.access(dissomniag.config.git.pathToLocalUtilFolder, os.F_OK): if job != None: self.multiLog("No %s Folder. Try to create it." % dissomniag.config.git.pathToLocalUtilFolder, job) with dissomniag.rootContext(): try: os.makedirs(dissomniag.config.git.pathToLocalUtilFolder) os.chown(dissomniag.config.git.pathToLocalUtilFolder, dissomniag.config.dissomniag.userId, dissomniag.config.dissomniag.groupId) except OSError, e: if job != None: self.multiLog("Could not create utility folder. %s" % e, job) self.isAdminUsable = False return if self._makeInitialCheckout(job): self.isAdminUsable = True try: self.adminRepo = git.Repo(dissomniag.config.git.pathToLocalUtilFolder) self.update(job) except Exception as e: if job != None: self.multiLog(str(e), job) self.isAdminUsable = False return False else: self.isAdminUsable = False