From 5976423e0a726cb636a67bd77d61c8f36b23e7ce Mon Sep 17 00:00:00 2001 From: Ciro Scognamiglio Date: Tue, 10 Mar 2015 19:39:10 +0100 Subject: [PATCH] loads dynamically components --- forge/urls.py | 7 +++ influxdb/__init__.py | 0 influxdb/client.py | 119 ++++++++++++++++++++++++++++++++++++++++++ influxdb/urls.py | 6 +++ myslice/components.py | 16 ++++++ myslice/settings.py | 19 ++++--- myslice/urls.py | 70 +++++++++++-------------- 7 files changed, 190 insertions(+), 47 deletions(-) create mode 100644 forge/urls.py create mode 100644 influxdb/__init__.py create mode 100644 influxdb/client.py create mode 100644 influxdb/urls.py create mode 100644 myslice/components.py diff --git a/forge/urls.py b/forge/urls.py new file mode 100644 index 00000000..9ede7e36 --- /dev/null +++ b/forge/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import patterns, url, include + +import forge.views + +urlpatterns = patterns('', + (r'^studentslabs/(?P[^/]+)/?$', forge.views.CreateCourseViev.as_view()) +) \ No newline at end of file diff --git a/influxdb/__init__.py b/influxdb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/influxdb/client.py b/influxdb/client.py new file mode 100644 index 00000000..50daa303 --- /dev/null +++ b/influxdb/client.py @@ -0,0 +1,119 @@ +from django.http import HttpResponse +from rest import error +import os,json +import ConfigParser +import string, random + +from portal.models import MeasurementsDB + +from manifold.core.query import Query, AnalyzedQuery +from manifoldapi.manifoldapi import execute_query + +from influxdb import InfluxDBClient + +def createDatabase(request, slicename): + result = {} + + Config = config(request) + + server = Config.get('influxdb', 'server') + #port = Config.get('influxdb', 'port', 8086) + port = 8086 + user = Config.get('influxdb', 'user') + password = Config.get('influxdb', 'password') + + dbname = slicename + dbuser = request.user.username + dbpassword = generatePassword() + + query = Query().get('user').filter_by('user_email', '==', dbuser).select('slices') + slices = execute_query(request, query) + + if not slicename in slices: + result['status'] = 'fail' + result['message'] = 'no permissions' + return HttpResponse(json.dumps(result), content_type="application/json") + + client = InfluxDBClient(server, port, user, password, dbname) + + try : + client.create_database(dbname) + except Exception as e: + print e + + # Add database user + try : + client.add_database_user(dbuser, dbpassword) + except Exception as e: + print e + + # Make user a database admin + client.set_database_admin(dbuser) + + + # Insert an entry in the Influxdb table + i = MeasurementsDB( + backend = 'influxdb', + server = server, + port = port, + dbname = dbname, + dbuser = dbuser, + dbpassword = dbpassword + ) + i.save() + + + result['status'] = 'ok' + + return HttpResponse(json.dumps(result), content_type="application/json") + +def infoDatabase(request, slicename): + Config = config(request) + + res = MeasurementsDB.objects.get(dbname=slicename, dbuser=request.user.username) + result = { + 'server' : res.server, + 'port' : res.port, + 'dbname' : res.dbname, + 'dbuser' : res.dbuser, + 'dbpassword' : res.dbpassword + } + return HttpResponse(json.dumps(result), content_type="application/json") + +def config(request): + Config = ConfigParser.ConfigParser() + Config.read(os.getcwd() + "/myslice/measurements.ini") + + if not request.user.is_authenticated : + return HttpResponse(json.dumps({'error' : 'not authenticated'}), content_type="application/json") + + #if Config.has_section('influxdb') : + if not Config.has_option('influxdb', 'server') : + return HttpResponse(json.dumps({'error' : 'server not specified'}), content_type="application/json") + + if not Config.has_option('influxdb', 'user') : + return HttpResponse(json.dumps({'error' : 'user not specified'}), content_type="application/json") + + if not Config.has_option('influxdb', 'password') : + return HttpResponse(json.dumps({'error' : 'server not specified'}), content_type="application/json") + + return Config + +def generatePassword(): + password_len = 16 + password = [] + + for group in (string.ascii_letters, string.punctuation, string.digits): + password += random.sample(group, 3) + + password += random.sample( + string.ascii_letters + string.punctuation + string.digits, + password_len - len(password)) + + random.shuffle(password) + password = ''.join(password) + + + return password + + diff --git a/influxdb/urls.py b/influxdb/urls.py new file mode 100644 index 00000000..bf47489c --- /dev/null +++ b/influxdb/urls.py @@ -0,0 +1,6 @@ +from django.conf.urls import patterns, url, include + +urlpatterns = patterns('', + (r'^influxdb/create/(?P[^/]+)/?$', 'influxdb.client.createDatabase'), + (r'^influxdb/info/(?P[^/]+)/?$', 'influxdb.client.infoDatabase'), +) \ No newline at end of file diff --git a/myslice/components.py b/myslice/components.py new file mode 100644 index 00000000..44d66241 --- /dev/null +++ b/myslice/components.py @@ -0,0 +1,16 @@ +from django.conf.urls import include, url +from myslice.configengine import ConfigEngine + +def urls(): + config = ConfigEngine() + u = [] + for component in config.myslice.components.split(','): + try: + __import__(component) + u.append( url(r'^%s/' % component, include('%s.urls' % component)) ) + except Exception, e: + print "-> Cannot load component (%s): %s" % (component, e) + else: + print "-> Loaded component %s" % component + + return u diff --git a/myslice/settings.py b/myslice/settings.py index 10b39533..a9018f2d 100644 --- a/myslice/settings.py +++ b/myslice/settings.py @@ -4,8 +4,8 @@ from __future__ import print_function import os.path -import djcelery -djcelery.setup_loader() +# import djcelery +# djcelery.setup_loader() ### detect if we're in a build environment try: @@ -44,7 +44,12 @@ try: theme = configEngine.myslice.theme except: pass - + +## +# components module +## +import components + # find out HTTPROOT, which is different from ROOT # when deployed from a package # this code is run by collectstatic too, so we cannot @@ -253,17 +258,17 @@ INSTALLED_APPS = [ # Uncomment the next line to enable the admin: 'django.contrib.admin', # FORGE Plugin app - 'djcelery', - 'forge', +# 'djcelery', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'portal', - # SLA - 'sla', ] # this app won't load in a build environment if not building: INSTALLED_APPS.append ('rest') +for component in components.list() : + INSTALLED_APPS.append(component) + BROKER_URL = "amqp://myslice:myslice@localhost:5672/myslice" for aux in auxiliaries: diff --git a/myslice/urls.py b/myslice/urls.py index ca150a40..ad151789 100644 --- a/myslice/urls.py +++ b/myslice/urls.py @@ -2,6 +2,11 @@ from django.conf.urls import patterns, include, url from django.conf import settings from django.contrib import admin +## +# components module +## +import components + # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() @@ -12,24 +17,20 @@ add_to_builtins('insert_above.templatetags.insert_tags') from settings import auxiliaries, INSTALLED_APPS +import portal.about +import portal.institution +import portal.registrationview +import portal.accountview +import portal.contactview +import portal.termsview +import portal.supportview + import portal.platformsview import portal.dashboardview import portal.homeview import portal.newsview import portal.loginwidget -from portal.about import AboutView -from portal.registrationview import RegistrationView -from portal.accountview import AccountView, account_process -from portal.institution import InstitutionView - -from portal.supportview import SupportView -from portal.contactview import ContactView - -from portal.termsview import TermsView - -home_view=portal.homeview.HomeView.as_view() -dashboard_view=portal.dashboardview.DashboardView.as_view() platforms_view=portal.platformsview.PlatformsView.as_view() #import portal.testbedlist @@ -45,19 +46,6 @@ import portal.slicetabmeasurements import portal.managementtababout import portal.managementtabrequests -import forge.views - -#### high level choices -# main entry point (set to the / URL) -# beware that if this view is broken you end up in an endless cycle... -# maybe platforms_view would be best on the longer run -the_default_view=home_view -# where to be redirected after login -the_after_login_view=dashboard_view -# where to redirect when login is required -# might need another one ? -the_login_view=home_view -admin.autodiscover() urls = [ '', # Examples: @@ -66,17 +54,17 @@ urls = [ # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: - url(r'^admin/', include(admin.site.urls)), + url(r'^admin/', include(admin.site.urls)), # # default / view - (r'^/?$', the_default_view), + (r'^/?$', portal.homeview.HomeView.as_view()), # # login / logout - (r'^login-ok/?$', the_after_login_view, {'state': 'Welcome to MySlice'} ), + (r'^login-ok/?$', portal.dashboardview.DashboardView.as_view(), {'state': 'Welcome to MySlice'} ), # # seems to be what login_required uses to redirect ... - (r'^accounts/login/$', the_login_view), - (r'^login/?$', the_login_view), + (r'^accounts/login/$', portal.homeview.HomeView.as_view()), + (r'^login/?$', portal.homeview.HomeView.as_view()), (r'^logout/?$', 'auth.views.logout_user'), # # the manifold proxy @@ -114,20 +102,20 @@ urls = [ (r'^testbeds/(?P[^/]+)/?$', portal.slicetabtestbeds.SliceTabTestbeds.as_view()), (r'^measurements/(?P[^/]+)/?$', portal.slicetabmeasurements.SliceTabMeasurements.as_view()), (r'^experiment/(?P[^/]+)/?$', portal.slicetabexperiment.ExperimentView.as_view()), - (r'^studentslabs/(?P[^/]+)/?$', forge.views.CreateCourseViev.as_view()), - url(r'^about/?$', AboutView.as_view(), name='about'), - url(r'^institution/?$', InstitutionView.as_view(), name='institution'), + url(r'^about/?$', portal.about.AboutView.as_view(), name='about'), + + url(r'^institution/?$', portal.institution.InstitutionView.as_view(), name='institution'), (r'^management/requests/?$', portal.managementtabrequests.ManagementRequestsView.as_view()), (r'^management/about/?$', portal.managementtababout.ManagementAboutView.as_view()), # - url(r'^register/?$', RegistrationView.as_view(), name='registration'), - url(r'^account/?$', AccountView.as_view(), name='account'), - url(r'^account/account_process/?$', account_process), - url(r'^contact/?$', ContactView.as_view(), name='contact'), - url(r'^terms/?$', TermsView.as_view(), name='terms'), - url(r'^support/?$', SupportView.as_view(), name='support'), + url(r'^register/?$', portal.registrationview.RegistrationView.as_view(), name='registration'), + url(r'^account/?$', portal.accountview.AccountView.as_view(), name='account'), + url(r'^account/account_process/?$', portal.accountview.account_process), + url(r'^contact/?$', portal.contactview.ContactView.as_view(), name='contact'), + url(r'^terms/?$', portal.termsview.TermsView.as_view(), name='terms'), + url(r'^support/?$', portal.supportview.SupportView.as_view(), name='support'), # url(r'^portal/', include('portal.urls')), @@ -135,11 +123,13 @@ urls = [ # url(r'^sla/', include('sla.urls')), ] +urls.extend( components.urls() ) + #this one would not match the convention # url(r'^debug/', include('debug_platform.urls')), # but it was commented out anyways for aux in auxiliaries: if aux in INSTALLED_APPS: - urls.append ( url ( r'^%s/'%aux, include ('%s.urls'%aux ))) + urls.append ( url ( r'^%s/'%aux, include ('%s.urls' % aux ))) urlpatterns = patterns(*urls) -- 2.43.0