comparison libervia/server/server.py @ 1146:76d75423ef53

server: tasks manager first draft: A new task manager will check /tasks directory of website to scripts to execute before launching the site. This allows to generate docs, scripts, or do anything else useful. Generated files are put in in sat local dir, in cache, and are accessible from the website using the new "build_dir" variable.
author Goffi <goffi@goffi.org>
date Fri, 25 Jan 2019 08:58:41 +0100
parents 2af117bfe6cc
children 02afab1b15c5
comparison
equal deleted inserted replaced
1145:29eb15062416 1146:76d75423ef53
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import re
21 import glob
22 import os.path
23 import sys
24 import tempfile
25 import shutil
26 import uuid
27 import urlparse
28 import urllib
29 import time
20 from twisted.application import service 30 from twisted.application import service
21 from twisted.internet import reactor, defer 31 from twisted.internet import reactor, defer
22 from twisted.web import server 32 from twisted.web import server
23 from twisted.web import static 33 from twisted.web import static
24 from twisted.web import resource as web_resource 34 from twisted.web import resource as web_resource
40 const_TIMEOUT as BRIDGE_TIMEOUT, 50 const_TIMEOUT as BRIDGE_TIMEOUT,
41 ) 51 )
42 from sat.core.i18n import _, D_ 52 from sat.core.i18n import _, D_
43 from sat.core import exceptions 53 from sat.core import exceptions
44 from sat.tools import utils 54 from sat.tools import utils
55 from sat.tools import config
45 from sat.tools.common import regex 56 from sat.tools.common import regex
46 from sat.tools.common import template 57 from sat.tools.common import template
47 from sat.tools.common import uri as common_uri 58 from sat.tools.common import uri as common_uri
48
49 import re
50 import glob
51 import os.path
52 import sys
53 import tempfile
54 import shutil
55 import uuid
56 import urlparse
57 import urllib
58 import time
59 from httplib import HTTPS_PORT 59 from httplib import HTTPS_PORT
60 import libervia 60 import libervia
61 from libervia.server import websockets 61 from libervia.server import websockets
62 from libervia.server.pages import LiberviaPage 62 from libervia.server.pages import LiberviaPage
63 from libervia.server.utils import quote, ProgressHandler 63 from libervia.server.utils import quote, ProgressHandler
64 from libervia.server.tasks import TasksManager
64 from functools import partial 65 from functools import partial
65 66
66 try: 67 try:
67 import OpenSSL 68 import OpenSSL
68 from twisted.internet import ssl 69 from twisted.internet import ssl
1704 1705
1705 def __init__(self, options): 1706 def __init__(self, options):
1706 self.options = options 1707 self.options = options
1707 self.initialised = defer.Deferred() 1708 self.initialised = defer.Deferred()
1708 self.waiting_profiles = WaitingRequests() # FIXME: should be removed 1709 self.waiting_profiles = WaitingRequests() # FIXME: should be removed
1710 self._main_conf = None
1709 1711
1710 if self.options["base_url_ext"]: 1712 if self.options["base_url_ext"]:
1711 self.base_url_ext = self.options.pop("base_url_ext") 1713 self.base_url_ext = self.options.pop("base_url_ext")
1712 if self.base_url_ext[-1] != "/": 1714 if self.base_url_ext[-1] != "/":
1713 self.base_url_ext += "/" 1715 self.base_url_ext += "/"
1751 roots = list(set(self.vhost_root.hosts.values())) 1753 roots = list(set(self.vhost_root.hosts.values()))
1752 default = self.vhost_root.default 1754 default = self.vhost_root.default
1753 if default is not None and default not in roots: 1755 if default is not None and default not in roots:
1754 roots.insert(0, default) 1756 roots.insert(0, default)
1755 return roots 1757 return roots
1758
1759 @property
1760 def main_conf(self):
1761 """SafeConfigParser instance opened on configuration file (sat.conf)"""
1762 if self._main_conf is None:
1763 self._main_conf = config.parseMainConf()
1764 return self._main_conf
1756 1765
1757 def _namespacesGetCb(self, ns_map): 1766 def _namespacesGetCb(self, ns_map):
1758 self.ns_map = ns_map 1767 self.ns_map = ns_map
1759 1768
1760 def _namespacesGetEb(self, failure_): 1769 def _namespacesGetEb(self, failure_):
1792 for key in to_delete: 1801 for key in to_delete:
1793 del conf[key] 1802 del conf[key]
1794 if default_dict: 1803 if default_dict:
1795 conf[u''] = default_dict 1804 conf[u''] = default_dict
1796 1805
1806 @defer.inlineCallbacks
1797 def backendReady(self, __): 1807 def backendReady(self, __):
1798 self.media_dir = self.bridge.getConfig("", "media_dir") 1808 self.media_dir = self.bridge.getConfig("", "media_dir")
1799 self.local_dir = self.bridge.getConfig("", "local_dir") 1809 self.local_dir = self.bridge.getConfig("", "local_dir")
1800 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR) 1810 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR)
1801 self.renderer = template.Renderer(self, self._front_url_filter) 1811 self.renderer = template.Renderer(self, self._front_url_filter)
1811 default_site_path = os.path.dirname(libervia.__file__) 1821 default_site_path = os.path.dirname(libervia.__file__)
1812 # self.sat_root is official Libervia site 1822 # self.sat_root is official Libervia site
1813 self.sat_root = default_root = LiberviaRootResource( 1823 self.sat_root = default_root = LiberviaRootResource(
1814 host=self, host_name=u'', site_name=u'', site_path=default_site_path, 1824 host=self, host_name=u'', site_name=u'', site_path=default_site_path,
1815 path=self.html_dir) 1825 path=self.html_dir)
1826 tasks_manager = TasksManager(self, self.sat_root)
1827 yield tasks_manager.runTasks()
1816 LiberviaPage.importPages(self, self.sat_root) 1828 LiberviaPage.importPages(self, self.sat_root)
1817 # FIXME: handle _setMenu in a more generic way, taking care of external sites 1829 # FIXME: handle _setMenu in a more generic way, taking care of external sites
1818 self.sat_root._setMenu(self.options["menu_json"]) 1830 self.sat_root._setMenu(self.options["menu_json"])
1819 self.vhost_root.default = default_root 1831 self.vhost_root.default = default_root
1820 existing_vhosts = {u'': default_root} 1832 existing_vhosts = {u'': default_root}
1842 host=self, 1854 host=self,
1843 host_name=host_name, 1855 host_name=host_name,
1844 site_name=site_name, 1856 site_name=site_name,
1845 site_path=site_path, 1857 site_path=site_path,
1846 path=root_path) 1858 path=root_path)
1859 tasks_manager = TasksManager(self, res)
1860 yield tasks_manager.runTasks()
1861 res.putChild(C.BUILD_DIR, static.File(self.getBuildPath(site_name)))
1847 self.vhost_root.addHost(host_name.encode('utf-8'), res) 1862 self.vhost_root.addHost(host_name.encode('utf-8'), res)
1848 LiberviaPage.importPages(self, res) 1863 LiberviaPage.importPages(self, res)
1849 # FIXME: default pages are accessible if not overriden by external website 1864 # FIXME: default pages are accessible if not overriden by external website
1850 # while necessary for login or re-using existing pages 1865 # while necessary for login or re-using existing pages
1851 # we may want to disable access to the page by direct URL 1866 # we may want to disable access to the page by direct URL
2359 wrapped_res = web_resource.EncodingResourceWrapper( 2374 wrapped_res = web_resource.EncodingResourceWrapper(
2360 resource, [server.GzipEncoderFactory()]) 2375 resource, [server.GzipEncoderFactory()])
2361 for root in self.roots: 2376 for root in self.roots:
2362 root.putChild(path, wrapped_res) 2377 root.putChild(path, wrapped_res)
2363 2378
2379 def getBuildPath(self, site_name):
2380 """Generate build path for a given site name
2381
2382 @param site_name(unicode): name of the site
2383 @return (unicode): path to the build directory
2384 """
2385 build_path_elts = [
2386 config.getConfig(self.main_conf, "", "local_dir"),
2387 C.CACHE_DIR,
2388 C.LIBERVIA_CACHE,
2389 regex.pathEscape(site_name)]
2390 build_path = u"/".join(build_path_elts)
2391 return os.path.abspath(os.path.expanduser(build_path))
2392
2364 def getExtBaseURLData(self, request): 2393 def getExtBaseURLData(self, request):
2365 """Retrieve external base URL Data 2394 """Retrieve external base URL Data
2366 2395
2367 this method tried to retrieve the base URL found by external user 2396 this method tried to retrieve the base URL found by external user
2368 It does by checking in this order: 2397 It does by checking in this order: