summaryrefslogtreecommitdiffstats
path: root/funcweb/funcweb/controllers.py
blob: 51dd20da5153ea8d047b18153cadbfb7dc9bb82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import logging
log = logging.getLogger(__name__)

from turbogears import controllers, expose, flash, identity, redirect
from func.overlord.client import Overlord, Minions
from turbogears import mochikit
from funcweb.widget_automation import WidgetListFactory,RemoteFormAutomation,RemoteFormFactory

class Root(controllers.RootController):

    @expose(template="funcweb.templates.minions")
    #@identity.require(identity.not_anonymous())
    def minions(self, glob='*'):
        """ Return a list of our minions that match a given glob """
        return dict(minions=Minions(glob).get_all_hosts())

    index = minions # start with our minion view, for now

    @expose(template="funcweb.templates.minion")
    #@identity.require(identity.not_anonymous())
    def minion(self, name="*", module=None, method=None):
        """ Display module or method details for a specific minion.

        If only the minion name is given, it will display a list of modules
        for that minion.  If a module is supplied, it will display a list of
        methods.  If a method is supplied, it will display a method execution
        form.
        """
        fc = Overlord(name)
        if not module: # list all modules
            #just list those who have get_method_args
            modules = fc.system.list_modules()
            display_modules = []
            #FIXME slow really i know !
            for module in modules.itervalues():
                for mod in module:
                    if getattr(fc,mod).get_method_args()[name]:
                        display_modules.append(mod)
            modules = {}
            modules[name]=display_modules
            
            return dict(modules=modules)
        else: # a module is specified
            if method: # minion.module.method specified; bring up execution form
                return dict(minion=name, module=module, method=method,
                            tg_template="funcweb.templates.method")
            else: # return a list of methods for specified module
                modules = getattr(fc, module).list_methods()
                return dict(modules=modules, module=module,
                            tg_template="funcweb.templates.module")


    @expose(template="funcweb.templates.run")
    #@identity.require(identity.not_anonymous())
    def run(self, minion="*", module=None, method=None, arguments=''):
        fc = Overlord(minion)
        results = getattr(getattr(fc, module), method)(*arguments.split())
        cmd = "%s.%s.%s(%s)" % (minion, module, method, arguments)
        return dict(cmd=cmd, results=results)


    @expose(template="funcweb.templates.method_args")
    #@identity.require(identity.not_anonymous())
    def method_display(self,minion=None,module=None,method=None):
   
        fc = Overlord(minion)
        method_args = getattr(fc,module).get_method_args()
        
        if not method_args.values():
            print "Not registered method here"
            return dict(minion_form = None)

        the_one = method_args[minion][method]['args']
        if the_one:
            wlist_object = WidgetListFactory(the_one)
            wlist_object = wlist_object.get_widgetlist_object()
            #minion_form =RemoteFormFactory( wlist_object.get_widgetlist_object()).get_remote_form()
            minion_form = RemoteFormAutomation(wlist_object)

            del wlist_object
            del the_one
            #print minion_form.fields

            #print minion_form
            return dict(minion_form=minion_form)
        else:
            return dict(minion_form = None)


    @expose(template="funcweb.templates.login")
    def login(self, forward_url=None, previous_url=None, *args, **kw):
        from cherrypy import request, response
        if not identity.current.anonymous \
            and identity.was_login_attempted() \
            and not identity.get_identity_errors():
            raise redirect(forward_url)

        forward_url=None
        previous_url= request.path

        if identity.was_login_attempted():
            msg=_("The credentials you supplied were not correct or "
                   "did not grant access to this resource.")
        elif identity.get_identity_errors():
            msg=_("You must provide your credentials before accessing "
                   "this resource.")
        else:
            msg=_("Please log in.")
            forward_url= request.headers.get("Referer", "/")

        response.status=403
        return dict(message=msg, previous_url=previous_url, logging_in=True,
                    original_parameters=request.params,
                    forward_url=forward_url)
        
    @expose(allow_json=True)
    def post_form(self,**kw):
        """
        Data processing part
        """
        return "I got that data from the remote minion form :<br/>%r"%kw


    @expose()
    def logout(self):
        identity.current.logout()
        raise redirect("/")