def executePost(self, db=True): """Method for executing a POST requests that initialize a new SOS service .. note:: This method creates: 1. service folder, 2. service configuration file 3. C{sensorML} folder 4. C{virtual procedures} folder 5. a new schema with the same name of your service 6. istSOS tables and relations in the new schema The POST must be in Json format with mandatory service key, if databease keys are not provided the server default connection are used: >>> { "service" : "service_name", "user" : "pinco" "password" : "pallino" "dbname" : "sos_db" "host" : "10.7.5.3" "port" : "5432" } """ # check to be at service level without serviceID provided in url if not self.urlservicename is None and db is True: raise Exception("POST action with url service name not supported") # check that service name was provided if not "service" in self.json: raise Exception("PUT action require the new service" "name sent within request body") # validate schemaname against injection utils.preventInjection(self.json["service"]) # validate db connection jsoncount = 0 dbkeys = ["user", "password", "dbname", "host", "port"] for key in dbkeys: if key in self.json: jsoncount += 1 defaultcfgpath = os.path.join(self.waEnviron["services_path"], "default.cfg") defaultconfig = configManager.waServiceConfig(defaultcfgpath) if jsoncount == 5: servicedb = databaseManager.PgDB(self.json["user"], self.json["password"], self.json["dbname"], self.json["host"], self.json["port"]) elif jsoncount == 0: defaultconnection = defaultconfig.get("connection") servicedb = databaseManager.PgDB(defaultconnection['user'], defaultconnection['password'], defaultconnection['dbname'], defaultconnection['host'], defaultconnection['port']) else: raise Exception("db parameters [service, user, password, dbname, " "host, port] must be all or none provided") # verify that the schema does not exist sql = "SELECT count(*) from pg_namespace WHERE nspname = %s" par = (self.json["service"], ) res = servicedb.select(sql, par) if len(res) == 1: pass else: raise Exception("a schema '%s' already exist" % self.json["service"]) # check if service folder does not exist: in case create it newservicepath = os.path.join(self.waEnviron["services_path"], self.json["service"]) try: os.makedirs(newservicepath) except OSError as exc: if exc.errno == errno.EEXIST: raise Exception("Service %s already exist" % (self.json["service"])) else: raise exc # create configuration file configfile = "%s.cfg" % os.path.join(self.waEnviron["services_path"], self.json["service"], self.json["service"]) open(configfile, 'w').close() # create sensorML folder smldir = os.path.join(self.waEnviron["services_path"], self.json["service"], "sml") os.makedirs(smldir) # create virtual procedure path virtualdir = os.path.join(self.waEnviron["services_path"], self.json["service"], "virtual") os.makedirs(virtualdir) if db is True: try: #create schema sql = "CREATE SCHEMA %s" % self.json["service"] servicedb.executeInTransaction(sql) #set db path sql = "SET search_path = %s, public, pg_catalog" % ( self.json["service"]) servicedb.executeInTransaction(sql, par) #create tableas and relations from walib import sqlschema defaultepsg = defaultconfig.get("geo")['istsosepsg'] import sys if not "epsg" in self.json: sql = sqlschema.createsqlschema.replace( "$SRID", defaultepsg).replace("$schema", self.json["service"]) else: sql = sqlschema.createsqlschema.replace( "$SRID", self.json['epsg']) #set correct default EPSG of the new service newconfig = configManager.waServiceConfig( defaultcfgpath, configfile) allowed = newconfig.get('geo')['allowedEPSG'].split(",") if self.json['epsg'] in allowed: newallowedepsg = ",".join( [x for x in allowed if x != self.json['epsg']]) newconfig.put('geo', 'allowedEPSG', newallowedepsg) newconfig.save() newconfig.put('geo', 'istsosepsg', self.json['epsg']) newconfig.save() servicedb.executeInTransaction(sql) servicedb.commitTransaction() except: servicedb.rollbackTransaction() raise # Setting proxy configuration from walib.istsos.services.configsections import serviceurl surl = configManager.waServiceConfig(defaultcfgpath, configfile) url = '' if self.waEnviron['server_port'] == '80': url = 'http://' else: url = 'https://' url = "%s%s%s/%s" % (url, self.waEnviron['server_name'], self.waEnviron['script_name'], self.json["service"]) surl.put("serviceurl", "url", url) surl.save() self.setMessage("New service <%s> correctly created" % str(self.json["service"]))
def executePost(self,db=True): """ Method for executing a POST requests that initialize a new SOS service .. note:: This method creates: 1. service folder, 2. service configuration file 3. C{sensorML} folder 4. C{virtual procedures} folder 5. a new schema with the same name of your service 6. istSOS tables and relations in the new schema The POST must be in Json format with mandatory service key, if databease keys are not provided the server default connection are used: >>> { "service" : "service_name", "user" : "pinco" "password" : "pallino" "dbname" : "sos_db" "host" : "10.7.5.3" "port" : "5432" } """ #check to be at service level without serviceID provided in url if not self.urlservicename == None and db==True: raise Exception("POST action with url service name not supported") #check that service name was provided if not "service" in self.json: raise Exception("PUT action require the new service name sent within request body") #validate schemaname against injection utils.preventInjection(self.json["service"]) #validate db connection #------------------------------------- jsoncount = 0 dbkeys = ["user","password","dbname","host","port"] for key in dbkeys: if key in self.json : jsoncount += 1 defaultcfgpath = os.path.join(self.waEnviron["services_path"],"default.cfg") defaultconfig = configManager.waServiceConfig(defaultcfgpath) if jsoncount == 5: servicedb = databaseManager.PgDB( self.json["user"], self.json["password"], self.json["dbname"], self.json["host"], self.json["port"]) elif jsoncount == 0: defaultconnection = defaultconfig.get("connection") servicedb = databaseManager.PgDB( defaultconnection['user'], defaultconnection['password'], defaultconnection['dbname'], defaultconnection['host'], defaultconnection['port']) else: raise Exception("db parameters [service,user,password,dbname,host,port] must be all or none provided") #verify that the schema does not exist #------------------------------------- sql = "SELECT count(*) from pg_namespace WHERE nspname = %s" par = (self.json["service"],) res = servicedb.select(sql,par) if len(res)==1: pass else: raise Exception("a schema '%s' already exist" % self.json["service"]) #check if service folder does not exist: in case create it #------------------------------------------------------------------- newservicepath = os.path.join( self.waEnviron["services_path"],self.json["service"] ) try: os.makedirs(newservicepath) except OSError as exc: if exc.errno == errno.EEXIST: raise Exception("Service %s already exist" %(self.json["service"]) ) else: raise exc #create configuration file #------------------------------------------------------------------- configfile = os.path.join(self.waEnviron["services_path"],self.json["service"],self.json["service"])+".cfg" open(configfile, 'w').close() #--set EPSGdefault #create sensorML folder #------------------------------------------------------------------- smldir = os.path.join(self.waEnviron["services_path"],self.json["service"],"sml") os.makedirs(smldir) #create virtual procedure path #------------------------------------------------------------------- virtualdir = os.path.join(self.waEnviron["services_path"],self.json["service"],"virtual") os.makedirs(virtualdir) if db==True: try: #create schema #------------------------------------------------------------------- sql = "CREATE SCHEMA %s" % self.json["service"] servicedb.executeInTransaction(sql) #set db path #------------------------------------------------------------------- sql = "SET search_path = %s, public, pg_catalog" % self.json["service"] servicedb.executeInTransaction(sql,par) #create tableas and relations #------------------------------------------------------------------- from walib import sqlschema defaultepsg = defaultconfig.get("geo")['istsosepsg'] import sys if not "epsg" in self.json: sql = sqlschema.createsqlschema.replace("$SRID",defaultepsg).replace("$schema",self.json["service"]) else: sql = sqlschema.createsqlschema.replace("$SRID",self.json['epsg']) #set correct default EPSG of the new service #------------------------------------------------------------------- newconfig = configManager.waServiceConfig(defaultcfgpath,configfile) allowed = newconfig.get('geo')['allowedEPSG'].split(",") if self.json['epsg'] in allowed: newallowedepsg = ",".join([x for x in allowed if x != self.json['epsg']]) newconfig.put('geo','allowedEPSG',newallowedepsg) newconfig.save() newconfig.put('geo','istsosepsg',self.json['epsg']) newconfig.save() servicedb.executeInTransaction(sql) servicedb.commitTransaction() except: servicedb.rollbackTransaction() raise # Setting proxy configuration from walib.istsos.services.configsections import serviceurl surl = configManager.waServiceConfig(defaultcfgpath, configfile) url = '' if self.waEnviron['server_port'] == '80': url = 'http://' else: url = 'https://' url = "%s%s%s/%s" % (url, self.waEnviron['server_name'], self.waEnviron['script_name'], self.json["service"]) surl.put("serviceurl", "url", url) surl.save() self.setMessage( "New service <%s> correctly created" % str(self.json["service"]) )
def executeDelete(self): """ Method for executing a DELETE requests that erase a SOS service .. note:: This method delete: 1. service folder, 2. service configuration file 3. C{sensorML} folder 4. C{virtual procedures} folder 5. a new schema with the same name of your service 6. istSOS tables and relations in the new schema The POST must be in Json format with mandatory service key >>> { "service" : "service_name" } """ #check schemaname input if self.urlservicename is None: raise Exception("DELETE action without url service" "name not supported") #validate schemaname against injection utils.preventInjection(self.urlservicename) #get database connection and initialize it defaultcfgpath = os.path.join(self.waEnviron["services_path"], "default.cfg") servicepath = os.path.join(self.waEnviron["services_path"], self.urlservicename) servicecfgpath = "%s.cfg" % os.path.join( self.waEnviron["services_path"], self.urlservicename, self.urlservicename) if not os.path.isdir(servicepath): raise Exception("service [%s] does not exists." % self.urlservicename) if not os.path.isdir( os.path.join(self.waEnviron["services_path"], self.urlservicename, "virtual")): raise Exception( "service [%s] misconfigured, missing <virtual> folder." % (self.urlservicename)) if not os.path.isdir( os.path.join(self.waEnviron["services_path"], self.urlservicename, "sml")): raise Exception( "service [%s] misconfigured, missing <sml> folder." % (self.urlservicename)) if not os.path.isfile(servicecfgpath): raise Exception( "service [%s] misconfigured, missing config file." % (self.urlservicename)) config = configManager.waServiceConfig(defaultcfgpath, servicecfgpath) connection = config.get("connection") servicedb = databaseManager.PgDB(connection['user'], connection['password'], connection['dbname'], connection['host'], connection['port']) #verify that the schema exist sql = "SELECT count(*) from pg_namespace WHERE nspname = %s" par = (self.urlservicename, ) res = servicedb.select(sql, par) if len(res) == 1: pass else: raise Exception("the db schema <<%s>> doesn't exist" % self.urlservicename) try: #drop schema sql = "DROP SCHEMA %s CASCADE" % self.urlservicename servicedb.executeInTransaction(sql) #remove service folder and subfolder contents shutil.rmtree(servicepath) servicedb.commitTransaction() except: servicedb.rollbackTransaction() raise self.setMessage("Service <%s> correctly deleted" % self.urlservicename)
def executeDelete(self): """ Method for executing a DELETE requests that erase a SOS service .. note:: This method delete: 1. service folder, 2. service configuration file 3. C{sensorML} folder 4. C{virtual procedures} folder 5. a new schema with the same name of your service 6. istSOS tables and relations in the new schema The POST must be in Json format with mandatory service key >>> { "service" : "service_name" } """ #check schemaname input if self.urlservicename == None: raise Exception("DELETE action without url service name not supported") #validate schemaname against injection utils.preventInjection(self.urlservicename) #get database connection and initialize it defaultcfgpath = os.path.join(self.waEnviron["services_path"],"default.cfg") servicepath = os.path.join(self.waEnviron["services_path"],self.urlservicename) servicecfgpath = os.path.join(self.waEnviron["services_path"],self.urlservicename,self.urlservicename)+".cfg" if not os.path.isdir(servicepath): raise Exception("service [%s] does not exists." % self.urlservicename) if not os.path.isdir(os.path.join(self.waEnviron["services_path"],self.urlservicename,"virtual")): raise Exception("service [%s] misconfigured, missing <virtual> folder." % self.urlservicename) if not os.path.isdir(os.path.join(self.waEnviron["services_path"],self.urlservicename,"sml")): raise Exception("service [%s] misconfigured, missing <sml> folder." % self.urlservicename) if not os.path.isfile(servicecfgpath): raise Exception("service [%s] misconfigured, missing config file." % self.urlservicename) config = configManager.waServiceConfig(defaultcfgpath,servicecfgpath) connection = config.get("connection") servicedb = databaseManager.PgDB(connection['user'], connection['password'], connection['dbname'], connection['host'], connection['port'] ) #verify that the schema exist #------------------------------------- sql = "SELECT count(*) from pg_namespace WHERE nspname = %s" par = (self.urlservicename,) res = servicedb.select(sql,par) if len(res)==1: pass else: raise Exception("the db schema <<%s>> doesn't exist" % self.urlservicename) try: #drop schema #------------------------------------------------------------------- sql = "DROP SCHEMA %s CASCADE" % self.urlservicename servicedb.executeInTransaction(sql) #remove service folder and subfolder contents #------------------------------------------------------------------- shutil.rmtree(servicepath) servicedb.commitTransaction() except: servicedb.rollbackTransaction() raise self.setMessage("Service <%s> correctly deleted" % self.urlservicename)