Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Components sample

  • Loading branch information...
commit 463c19036eaf96c7c6c5d41749370cbe875fb502 1 parent fcee9b5
Eugene Pankov Eugeny authored
3  ajenti/api/__init__.py
View
@@ -1,3 +1,4 @@
from api import *
from helpers import *
-from urlhandler import *
+from urlhandler import *
+from components import *
1  ajenti/api/api.py
View
@@ -25,6 +25,7 @@ def match_event(self, event):
def event(self, event, *params, **kwparams):
pass
+
class IXSLTFunctionProvider(Interface):
def get_funcs(self):
pass
64 ajenti/api/components.py
View
@@ -0,0 +1,64 @@
+from ajenti.com import *
+from ajenti.utils import *
+
+
+class IComponent (Interface):
+ def run(self):
+ pass
+
+class Component (Plugin, BackgroundWorker):
+ implements(IComponent)
+
+ name = 'unknown'
+
+ def __init__(self):
+ BackgroundWorker.__init__(self)
+
+ def start(self):
+ self.on_starting()
+ BackgroundWorker.start(self)
+
+ def stop(self):
+ self.on_stopping()
+ self.kill()
+ self.on_stopped()
+
+ def run(self):
+ pass
+
+ def on_starting(self):
+ pass
+
+ def on_stopping(self):
+ pass
+
+ def on_stopped(self):
+ pass
+
+
+class ComponentManager (Plugin):
+ instance = None
+
+ @staticmethod
+ def create(app):
+ ComponentManager.instance = ComponentManager(app)
+
+ @staticmethod
+ def get():
+ return ComponentManager.instance
+
+ def __init__(self):
+ self.components = self.app.grab_plugins(IComponent)
+ for c in self.components:
+ c.start()
+ c.proxy = ClassProxy(c)
+
+ def stop(self):
+ for c in self.components:
+ c.stop()
+
+ def find(self, name):
+ for c in self.components:
+ if c.name == name:
+ return c.proxy
+
1  ajenti/core/application.py
View
@@ -85,6 +85,7 @@ def __init__(self, config=None):
functions
)
+
self.log.debug('Initialized')
10 ajenti/standalone.py
View
@@ -6,8 +6,9 @@
from twisted.internet import reactor, ssl
from twisted.web.wsgi import WSGIResource
+from ajenti.api import ComponentManager
from ajenti.config import Config
-from ajenti.core import AppDispatcher
+from ajenti.core import Application, AppDispatcher
from ajenti.plugmgr import load_plugins
from ajenti import version
import ajenti.utils
@@ -47,8 +48,11 @@ def run_server(log_level=logging.INFO, config_file=''):
# Load external plugins
load_plugins(config.get('ajenti', 'plugins'), log)
- # Start server
+ # Start components
+ ComponentManager.create(Application(config))
+
+ # Start server
host = config.get('ajenti','bind_host')
port = config.getint('ajenti','bind_port')
log.info('Listening on %s:%d'%(host, port))
@@ -77,6 +81,8 @@ def run_server(log_level=logging.INFO, config_file=''):
reactor.run()
+ ComponentManager.get().stop()
+
if hasattr(reactor, 'restart_marker'):
log.info('Restarting by request')
1  ajenti/utils/__init__.py
View
@@ -2,3 +2,4 @@
from error import *
from PrioList import PrioList
from misc import *
+from interlocked import *
34 ajenti/utils/interlocked.py
View
@@ -0,0 +1,34 @@
+import threading
+
+
+class ClassProxy (object):
+ inner = None
+ locks = None
+
+ def __init__(self, inner):
+ self.inner = inner
+ self.locks = {}
+
+ def __getattr__(self, attr):
+ if not attr in self.locks:
+ self.locks[attr] = threading.Lock()
+
+ return MethodProxy(getattr(self.inner, attr), self.locks[attr])
+
+
+class MethodProxy (object):
+ def __init__(self, method, lock):
+ self.lock = lock
+ self.method = method
+
+ def __call__(self, *args, **kwargs):
+ self.lock.acquire()
+
+ res = None
+
+ try:
+ res = self.method(*args, **kwargs)
+ finally:
+ self.lock.release()
+
+ return res
1  ajenti/utils/misc.py
View
@@ -68,6 +68,7 @@ def kill(self):
class BackgroundWorker:
def __init__(self, *args):
self.thread = KThread(target=self.__run, args=args)
+ self.thread.daemon = True
self.alive = False
self.output = ''
self._aborted = False
4 plugins/network/nctp_bsd.py
View
@@ -36,7 +36,6 @@ def get_info(self, iface):
def get_tx(self, iface):
s = shell('netstat -bI %s | grep -v Link | grep -v pkts'%iface.name)
- print s
try:
s = s.split()[10]
except:
@@ -45,7 +44,6 @@ def get_tx(self, iface):
def get_rx(self, iface):
s = shell('netstat -bI %s | grep -v Link | grep -v pkts'%iface.name)
- print s
try:
s = s.split()[7]
except:
@@ -84,4 +82,4 @@ def up(self, iface):
def down(self, iface):
shell('ifconfig %s down' % iface.name)
time.sleep(1)
-
+
5 plugins/network/nctp_linux.py
View
@@ -36,7 +36,6 @@ def get_info(self, iface):
def get_tx(self, iface):
s = shell('ifconfig %s | grep \'TX bytes\''%iface.name)
- print s
try:
s = s.split()[5].split(':')[1]
except:
@@ -45,7 +44,6 @@ def get_tx(self, iface):
def get_rx(self, iface):
s = shell('ifconfig %s | grep \'RX bytes\''%iface.name)
- print s
try:
s = s.split()[1].split(':')[1]
except:
@@ -79,7 +77,6 @@ def detect_dev_class(self, iface):
def detect_iface_bits(self, iface):
r = ['linux-basic']
cls = self.detect_dev_class(iface)
- print iface.type, iface.addressing
if iface.type == 'inet' and iface.addressing == 'static':
r.append('linux-ipv4')
if iface.type == 'inet6' and iface.addressing == 'static':
@@ -105,4 +102,4 @@ def up(self, iface):
def down(self, iface):
shell('ifconfig %s down' % iface.name)
time.sleep(1)
-
+
2  plugins/pkgman/__init__.py
View
@@ -1,4 +1,4 @@
-MODULES = ['api', 'main', 'pm_apt', 'pm_pacman', 'pm_ports', 'pm_yum']
+MODULES = ['api', 'main', 'component', 'pm_apt', 'pm_pacman', 'pm_ports', 'pm_yum']
DEPS = [
(['debian'],
33 plugins/pkgman/component.py
View
@@ -0,0 +1,33 @@
+from ajenti.api import *
+from ajenti import apis
+
+import time
+
+
+class PackageManagerComponent (Component):
+ name = 'pkgman'
+
+ def on_starting(self):
+ self.status = apis.pkgman.Status()
+ self.last_refresh = 0
+
+ self.mgr = self.app.get_backend(apis.pkgman.IPackageManager)
+
+ def get_status(self):
+ if time.time() - self.last_refresh >= 5 * 60:
+ self.last_refresh = time.time()
+ self.mgr.refresh(self.status)
+ return self.status
+
+ def refresh(self):
+ self.last_refresh = 0
+ return self.proxy.get_status()
+
+ def run(self):
+ while True:
+ self.get_status()
+ time.sleep(5*60 + 1)
+
+ def __getattr__(self, attr):
+ return getattr(self.mgr, attr)
+
15 plugins/pkgman/main.py
View
@@ -13,19 +13,23 @@ class PackageManagerPlugin(CategoryPlugin):
folder = 'system'
def on_init(self):
- self.mgr = self.app.get_backend(apis.pkgman.IPackageManager)
+ self.mgr = ComponentManager.get().find('pkgman')
+
if self._in_progress and not self.mgr.is_busy():
self._need_refresh = True
self.mgr.mark_cancel_all(self._status)
self._in_progress = False
+
if self._need_refresh:
+ self.mgr.refresh()
self._need_refresh = False
- self.mgr.refresh(self._status)
+
+ self._status = self.mgr.get_status()
def on_session_start(self):
- self._status = apis.pkgman.Status()
+ self._status = None
self._current = 'upgrades'
- self._need_refresh = True
+ self._need_refresh = False
self._confirm_apply = False
self._in_progress = False
self._search = {}
@@ -107,8 +111,6 @@ def get_ui(self):
)
tbl_pkgs.append(r)
- ui_misc = UI.Button(text='Select all', id='upgradeall')
-
if self._current == 'broken':
for p in sorted(self._status.full.keys()):
p = self._status.full[p]
@@ -129,7 +131,6 @@ def get_ui(self):
)
)
tbl_pkgs.append(r)
- ui_misc = None
if self._current == 'search':
for p in self._search:
1  plugins/pkgman/pm_apt.py
View
@@ -90,7 +90,6 @@ def get_expected_result(self, st):
cmd = 'apt-get -qq -s install '
for x in st.pending:
cmd += x + ('+ ' if st.pending[x] == 'install' else '- ')
- print cmd
r = self._parse_apt(utils.shell(cmd).splitlines())
for x in r:
if r[x].state == 'installed':
Please sign in to comment.
Something went wrong with that request. Please try again.