def clean_dnsname(self): # featuredCount = Country.objects.filter(featured=True).count() # if featuredCount >= 5 and self.cleaned_data['featured'] is True: # raise forms.ValidationError("5 Countries can be featured at most!") # return self.cleaned_data['featured']a saturnserver = self.cleaned_data['dnsname'] try: p = PollServer(saturnserver) p.InstallScripts() except: logger.error( "Error with Saturn server specified on the form, will try to disable server " + saturnserver) try: obj = StorageHost.objects.get(dnsname=saturnserver) obj.enabled = False obj.save() raise forms.ValidationError( "Error with Saturn Server, therefore disabled " + saturnserver) except: logger.error( "Could not install scripts on the new server, new server not in DB, check its DNS entry" ) raise forms.ValidationError( "Error with Saturn Server, check its DNS entry perhaps? " + saturnserver) return self.cleaned_data['dnsname']
def setUp(self): """ Setup the test saturn host (where the storage is provisioned) Assuming a VG exists on that host, run VGscan This means that the SSH key should be on that host already """ print "Here is where we can set some state" #Dummy super user for interfaces test self.config = ConfigReader('saturn.ini') self.saturnringip = self.config.get('tests', 'saturnringip') self.saturnringport = self.config.get('tests', 'saturnringport') self.iscsiserver = self.config.get('tests', 'saturniscsiserver') my_admin = User.objects.create_superuser('myuser', '*****@*****.**', 'password') testhost = StorageHost(dnsname=self.iscsiserver, ipaddress=self.iscsiserver, storageip1=self.iscsiserver) testhost.save() outStr = check_output([ "curl", "-X", "GET", "http://" + self.saturnringip + ":" + self.saturnringport + "/api/vgscan/", "-d", "saturnserver=" + self.iscsiserver ]) for eachElement in literal_eval(outStr): vguuid = eachElement['vguuid'] vg = VG(vghost=testhost, vguuid=vguuid, vgpesize=1.0, vgtotalpe=10.0, vgsize=1.0) vg.save() self.host = literal_eval(outStr)[0]['vghost'] self.psrvr = PollServer(self.host)
def DeleteTargetObject(obj): p = PollServer(obj.targethost) lv = LV.objects.get(target=obj) if p.DeleteTarget(obj.iqntar, lv.vg.vguuid) == 1: newth = TargetHistory(owner=obj.owner, iqntar=obj.iqntar, iqnini=obj.iqnini, created_at=obj.created_at, sizeinGB=obj.sizeinGB, rkb=obj.rkb, wkb=obj.wkb) newth.save() tarVG = lv.vg tarVG.maxavlGB = tarVG.maxavlGB + obj.sizeinGB tarVG.save() obj.delete() connection.close( ) #close DB connection to prevent RQ connection reset error in PG database logs return 0 else: connection.close( ) #close DB connection to prevent RQ connection reset error in PG database logs return 1
def get(self, request): logger.info("VG scan request received: %s " % (request.DATA, )) saturnserver = request.DATA[u'saturnserver'] if (StorageHost.objects.filter( Q(dnsname__contains=saturnserver) | Q(ipaddress__contains=saturnserver))): p = PollServer(saturnserver) savedvguuidStr = p.GetVG() if type(savedvguuidStr) is not str: logger.warn('GetVG returned error integer: ' + str(savedvguuidStr)) return Response('Error scanning VG, contact admin:') listvguuid = savedvguuidStr.split(',') readVG = VG.objects.filter(vguuid__in=listvguuid).values( 'vguuid', 'vghost') return Response(readVG) logger.info("RETURNING THIS " + str(readVG)) #return savedvguuidStr else: logger.warn("Unknown saturn server " + str(request.DATA)) return Response("Unregistered or uknown Saturn server " + str(request.DATA), status=status.HTTP_400_BAD_REQUEST)
def UpdateOneState(host): try: p = PollServer(host) vguuidList = p.GetVG() logger.info("getvg returns " + str(vguuidList)) if type(vguuidList) is str: for vguuid in vguuidList.split(','): try: vg = VG.objects.get(vguuid=vguuid) p.UpdateLVs(vg) except: logger.error("Cannot work with VG %s on %s" % (vguuid, host)) logger.error(format_exc()) p.GetTargetsState() p.GetInterfaces() except: logger.error("UpdateOneState failed for %s " % (str(host), )) finally: connection.close()
def ExecMakeTarget(storemedia, targetvguuid, targetHost, clientiqn, serviceName, storageSize, aagroup, clumpgroup, subnet, owner): chosenVG = VG.objects.get(vguuid=targetvguuid) clientiqnHash = hashlib.sha1(clientiqn).hexdigest()[:8] iqnTarget = "".join( ["iqn.2014.01.", targetHost, ":", serviceName, ":", clientiqnHash]) try: targets = Target.objects.filter( iqntar__contains="".join([serviceName, ":", clientiqnHash])) if len(targets) == 0: raise ObjectDoesNotExist for t in targets: iqnComponents = t.iqntar.split(':') if ((serviceName == iqnComponents[1]) and (clientiqnHash == iqnComponents[2])): logger.info( 'Target already exists for (serviceName=%s,clientiqn=%s) tuple' % (serviceName, clientiqn)) try: existingTargetstoremedia = LV.objects.get( target=t).vg.storemedia except: logger.error( "Target %s exists in DB but LV does not, inconsistent" % (t.iqntar)) return ( -1, "Target %s exists in DB but LV does not, inconsistent" % (t.iqntar)) if (existingTargetstoremedia == storemedia): return (1, t.iqntar) else: errorStr = "Target %s on DIFFERENT storemedia %s already exists." % ( t.iqntar, existingTargetstoremedia) logger.info(errorStr) return (-1, errorStr) else: raise ObjectDoesNotExist except ObjectDoesNotExist: # try: # if subnet != 'public': # IPRange.objects.get(iprange=subnet) # except: # logger.debug('Subnet %s not found on host %s while trying to create target %s, creation aborted, contact admin' %(subnet, targetHost, iqnTarget )) # return (-1,"Invalid subnet specified") (quotaFlag, quotaReason) = CheckUserQuotas(float(storageSize), owner) if quotaFlag == -1: logger.debug(quotaReason) return (-1, quotaReason) else: logger.info(quotaReason) logger.info( "Creating new target for request {%s %s %s}, this is the generated iSCSItarget: %s" % (clientiqn, serviceName, str(storageSize), iqnTarget)) targethost = StorageHost.objects.get(dnsname=targetHost) p = PollServer(targetHost) storeip1 = targethost.storageip1 storeip2 = targethost.storageip2 if subnet != 'public': try: storeip1 = Interface.objects.get( owner=owner, storagehost=targethost, iprange__iprange=unicode(subnet)).ip storeip2 = storeip1 except: logger.error( 'Chosen host %s is missing IP addresses in requested subnet' % (targethost, )) return ( -1, 'Error in host network configuration or ownership for the required subnet, contact storage admin' ) if p.CreateTarget(iqnTarget, clientiqn, str(storageSize), storeip1, storeip2, targetvguuid) == 1: logger.info("SUCCESSFUL TARGET RUN") BASE_DIR = os.path.dirname(os.path.dirname(__file__)) config = ConfigParser.RawConfigParser() config.read(os.path.join(BASE_DIR, 'saturn.ini')) (devDic, tarDic) = ParseSCSTConf( os.path.join(BASE_DIR, config.get('saturnring', 'iscsiconfigdir'), targetHost + '.scst.conf')) logger.info("DevDic = " + str(devDic)) logger.info("TarDic = " + str(tarDic)) if iqnTarget in tarDic: newTarget = Target(owner=owner, targethost=targethost, iqnini=clientiqn, iqntar=iqnTarget, sizeinGB=float(storageSize), storageip1=storeip1, storageip2=storeip2) newTarget.save() lvDict = p.GetLVs(targetvguuid) lvName = 'lvol-' + hashlib.md5(iqnTarget + '\n').hexdigest()[0:8] logger.info("Looking for %s in lvDict %s" % (lvName, str(lvDict))) if lvName in lvDict: newLV = LV( target=newTarget, vg=chosenVG, lvname=lvName, lvsize=storageSize, #lvthinmapped=lvDict[lvName]['Mapped size'], lvuuid=lvDict[lvName]['LV UUID']) newLV.save() chosenVG.CurrentAllocGB = max( 0, chosenVG.CurrentAllocGB) + float(storageSize) chosenVG.maxavlGB = max( 0, chosenVG.maxavlGB - float(storageSize)) chosenVG.save() else: logger.error( 'Error - could not use ParseSCSTConf while working with target creation of %s, check if git and %s are in sync' % (iqnTarget, targethost + '.scst.conf')) return (-1, "CreateTarget returned error 2, contact admin") tar = Target.objects.get(iqntar=iqnTarget) aa = AAGroup(name=aagroup, target=tar) aa.save() aa.hosts.add(targethost) aa.save() newTarget.aagroup = aa cg = ClumpGroup(name=clumpgroup, target=tar) cg.save() cg.hosts.add(targethost) cg.save() newTarget.clumpgroup = cg newTarget.save() connection.close( ) #close DB connection to prevent RQ connection reset error in PG database logs return (0, iqnTarget) else: connection.close( ) #close DB connection to prevent RQ connection reset error in PG database logs logger.error('CreateTarget did not work') return (-1, "CreateTarget returned error 1, contact admin")
class GSMTestCase(TestCase): """ Test cases for GlobalStatemanager Test GetLVs Test GetVGs """ def setUp(self): """ Setup the test saturn host (where the storage is provisioned) Assuming a VG exists on that host, run VGscan This means that the SSH key should be on that host already """ print "Here is where we can set some state" #Dummy super user for interfaces test self.config = ConfigReader('saturn.ini') self.saturnringip = self.config.get('tests', 'saturnringip') self.saturnringport = self.config.get('tests', 'saturnringport') self.iscsiserver = self.config.get('tests', 'saturniscsiserver') my_admin = User.objects.create_superuser('myuser', '*****@*****.**', 'password') testhost = StorageHost(dnsname=self.iscsiserver, ipaddress=self.iscsiserver, storageip1=self.iscsiserver) testhost.save() outStr = check_output([ "curl", "-X", "GET", "http://" + self.saturnringip + ":" + self.saturnringport + "/api/vgscan/", "-d", "saturnserver=" + self.iscsiserver ]) for eachElement in literal_eval(outStr): vguuid = eachElement['vguuid'] vg = VG(vghost=testhost, vguuid=vguuid, vgpesize=1.0, vgtotalpe=10.0, vgsize=1.0) vg.save() self.host = literal_eval(outStr)[0]['vghost'] self.psrvr = PollServer(self.host) def test_GetLVs(self): """ Test if LVs are being read off the test server """ allvgs = VG.objects.all() for eachvg in allvgs: lvs = self.psrvr.GetLVs(eachvg.vguuid) pprint(lvs) self.assertNotEqual(len(lvs), 0) def test_InstallScripts(self): """ Test if SCP installation of scripts works Tests via comparing directory listing of the remote install directory before and after InstallScripts """ installLoc = join(self.config.get('saturnnode', 'install_location'), 'saturn-bashscripts') BASE_DIR = dirname(dirname(__file__)) localLoc = join(BASE_DIR, self.config.get('saturnring', 'bashscripts')) fH = open(join(localLoc, 'test_InstallScript.txt'), 'w') fH.write('Test started') fH.close() dirlistcmd = 'ls -al ' + installLoc b4installdirout = self.psrvr.Exec(dirlistcmd) print "BEFORE" pprint(b4installdirout) self.psrvr.InstallScripts() afterinstalldirout = self.psrvr.Exec(dirlistcmd) print "AFTER" pprint(afterinstalldirout) self.assertNotEqual(str(b4installdirout), str(afterinstalldirout)) remove(join(localLoc, 'test_InstallScript.txt')) rmfilecmd = 'rm ' + join(installLoc, 'test_InstallScript.txt') self.psrvr.Exec(rmfilecmd) def test_Exec(self): """ Test if SSH/Exec works """ rtnStr = self.psrvr.Exec("uptime") self.assertIn("load average", str(rtnStr)) def test_GetInterfaces(self): """ Test if the interface scanning works """ oldEntries = Interface.objects.all() oldEntries.delete() self.psrvr.GetInterfaces() interfaces = Interface.objects.all() for eachInterface in interfaces: print eachInterface.ip self.assertNotEqual(len(interfaces), 0)