comparison frontends/src/jp/cmd_adhoc.py @ 2409:d2ff5ff3de77

jp (ad-hoc): new "list" and "run" commands: - list just display commands available at ad-hoc entry point. --verbose allows to display values names, which is useful for automation. - run can either run interactively a command, or execute a workflow to automate them.
author Goffi <goffi@goffi.org>
date Tue, 31 Oct 2017 23:20:04 +0100
parents 3e168cde7a7d
children f9167c053475
comparison
equal deleted inserted replaced
2408:a870daeab15e 2409:d2ff5ff3de77
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 base 20 import base
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from functools import partial
23 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import xmlui_manager
22 25
23 __commands__ = ["AdHoc"] 26 __commands__ = ["AdHoc"]
24 27
25 FLAG_LOOP = 'LOOP' 28 FLAG_LOOP = 'LOOP'
26 MAGIC_BAREJID = '@PROFILE_BAREJID@' 29 MAGIC_BAREJID = '@PROFILE_BAREJID@'
27 30
31
28 class Remote(base.CommandBase): 32 class Remote(base.CommandBase):
29 def __init__(self, host): 33 def __init__(self, host):
30 super(Remote, self).__init__(host, 'remote', use_verbose=True, help=_('Remote control a software')) 34 super(Remote, self).__init__(host, 'remote', use_verbose=True, help=_(u'remote control a software'))
31 35
32 def add_parser_options(self): 36 def add_parser_options(self):
33 self.parser.add_argument("software", type=str, help=_("Software name")) 37 self.parser.add_argument("software", type=str, help=_(u"software name"))
34 self.parser.add_argument("-j", "--jids", type=base.unicode_decoder, nargs='*', default=[], help=_("Jids allowed to use the command")) 38 self.parser.add_argument("-j", "--jids", type=base.unicode_decoder, nargs='*', default=[], help=_(u"jids allowed to use the command"))
35 self.parser.add_argument("-g", "--groups", type=base.unicode_decoder, nargs='*', default=[], help=_("Groups allowed to use the command")) 39 self.parser.add_argument("-g", "--groups", type=base.unicode_decoder, nargs='*', default=[], help=_(u"groups allowed to use the command"))
36 self.parser.add_argument("--forbidden-groups", type=base.unicode_decoder, nargs='*', default=[], help=_("Groups that are *NOT* allowed to use the command")) 40 self.parser.add_argument("--forbidden-groups", type=base.unicode_decoder, nargs='*', default=[], help=_(u"groups that are *NOT* allowed to use the command"))
37 self.parser.add_argument("--forbidden-jids", type=base.unicode_decoder, nargs='*', default=[], help=_("Jids that are *NOT* allowed to use the command")) 41 self.parser.add_argument("--forbidden-jids", type=base.unicode_decoder, nargs='*', default=[], help=_(u"jids that are *NOT* allowed to use the command"))
38 self.parser.add_argument("-l", "--loop", action="store_true", help=_("Loop on the commands")) 42 self.parser.add_argument("-l", "--loop", action="store_true", help=_(u"loop on the commands"))
39 43
40 def start(self): 44 def start(self):
41 name = self.args.software.lower() 45 name = self.args.software.lower()
42 flags = [] 46 flags = []
43 magics = {jid for jid in self.args.jids if jid.count('@')>1} 47 magics = {jid for jid in self.args.jids if jid.count('@')>1}
57 self.disp(_("Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]" % {'path': path, 61 self.disp(_("Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]" % {'path': path,
58 'iface': iface, 62 'iface': iface,
59 'command': command 63 'command': command
60 }),1) 64 }),1)
61 65
66
67 class Run(base.CommandBase):
68 """Run an Ad-Hoc command"""
69
70 def __init__(self, host):
71 super(Run, self).__init__(host, 'run', use_verbose=True, help=_(u'run an Ad-Hoc command'))
72 self.need_loop=True
73
74 def add_parser_options(self):
75 self.parser.add_argument('-n', '--node', type=base.unicode_decoder, default=u'', help=_(u"node of the command (default: list commands)"))
76 self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, default=u'', help=_(u"jid of the service (default: profile's server"))
77 self.parser.add_argument("-S", "--submit", action='append_const', const=xmlui_manager.SUBMIT, dest='workflow', help=_(u"submit form/page"))
78 self.parser.add_argument("-f",
79 "--field",
80 type=base.unicode_decoder,
81 action='append',
82 nargs=2,
83 dest='workflow',
84 metavar=(u"KEY", u"VALUE"),
85 help=_(u"field value"))
86
87 def adHocRunCb(self, xmlui_raw):
88 xmlui = xmlui_manager.create(self.host, xmlui_raw)
89 workflow = self.args.workflow
90 xmlui.show(workflow)
91 if not workflow:
92 if xmlui.type == 'form':
93 xmlui.submitForm()
94 else:
95 self.host.quit()
96
97 def start(self):
98 self.host.bridge.adHocRun(
99 self.args.jid,
100 self.args.node,
101 self.profile,
102 callback=self.adHocRunCb,
103 errback=partial(self.errback,
104 msg=_(u"can't get ad-hoc commands list: {}"),
105 exit_code=C.EXIT_BRIDGE_ERRBACK))
106
107
108 class List(base.CommandBase):
109 """Run an Ad-Hoc command"""
110
111 def __init__(self, host):
112 super(List, self).__init__(host, 'list', use_verbose=True, help=_(u'list Ad-Hoc commands of a service'))
113 self.need_loop=True
114
115 def add_parser_options(self):
116 self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, default=u'', help=_(u"jid of the service (default: profile's server"))
117
118 def adHocListCb(self, xmlui_raw):
119 xmlui = xmlui_manager.create(self.host, xmlui_raw)
120 xmlui.readonly = True
121 xmlui.show()
122 self.host.quit()
123
124 def start(self):
125 self.host.bridge.adHocList(
126 self.args.jid,
127 self.profile,
128 callback=self.adHocListCb,
129 errback=partial(self.errback,
130 msg=_(u"can't get ad-hoc commands list: {}"),
131 exit_code=C.EXIT_BRIDGE_ERRBACK))
132
133
62 class AdHoc(base.CommandBase): 134 class AdHoc(base.CommandBase):
63 subcommands = (Remote,) 135 subcommands = (Run, List, Remote)
64 136
65 def __init__(self, host): 137 def __init__(self, host):
66 super(AdHoc, self).__init__(host, 'ad-hoc', use_profile=False, help=_('Ad-hoc commands')) 138 super(AdHoc, self).__init__(host, 'ad-hoc', use_profile=False, help=_('Ad-hoc commands'))