diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-11-26 11:26:22 +0100 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-11-27 14:45:34 +0100 |
commit | 76fe1b775a2fdad4ee7b8d04f2a71778f693d8e4 (patch) | |
tree | f9dbe020973ad8f242c57231d5861a05c76733bc /ogcp | |
parent | 92ab31650c60163607ea5b00c57a7b223ef49bd4 (diff) |
views: add direct cmd execution view
Reorganize "Run" section of Commands view as follows:
Commands
└── Run
├── Script: run script from folder
├── Cmd: direct command execution
└── Display output: results of last execution
Adapt API REST call to the new interface. Remove strange legacy
;|\n\r terminator. Remove "echo" field and add "inline" field.
Diffstat (limited to 'ogcp')
-rw-r--r-- | ogcp/forms/action_forms.py | 5 | ||||
-rw-r--r-- | ogcp/templates/commands.html | 6 | ||||
-rw-r--r-- | ogcp/views.py | 42 |
3 files changed, 48 insertions, 5 deletions
diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index 49c9f60..24424a1 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -172,6 +172,11 @@ class RunScriptForm(FlaskForm): arguments = StringField(label=_l('Arguments')) submit = SubmitField(label=_l('Submit')) +class RunCmdForm(FlaskForm): + ips = HiddenField() + command = StringField(label=_l('Command')) + submit = SubmitField(label=_l('Submit')) + class ImportClientsForm(FlaskForm): server = HiddenField() room = SelectField(label=_l('Room')) diff --git a/ogcp/templates/commands.html b/ogcp/templates/commands.html index 41a0917..c909ea3 100644 --- a/ogcp/templates/commands.html +++ b/ogcp/templates/commands.html @@ -97,11 +97,13 @@ <div class="dropdown btn"> <button class="btn btn-secondary btn-light dropdown-toggle{% block nav_script %}{% endblock %}" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-expanded="false"> - {{ _('Script') }} + {{ _('Run') }} </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> - <input class="btn btn-light dropdown-item{% block nav_run_script %}{% endblock %}" type="submit" value="{{ _('Run') }}" + <input class="btn btn-light dropdown-item{% block nav_run_script %}{% endblock %}" type="submit" value="{{ _('Script') }}" form="scopesForm" formaction="{{ url_for('action_run_script') }}" formmethod="get"> + <input class="btn btn-light dropdown-item{% block nav_run_cmd %}{% endblock %}" type="submit" value="{{ _('Cmd') }}" + form="scopesForm" formaction="{{ url_for('action_run_cmd') }}" formmethod="get"> <input class="btn btn-light dropdown-item{% block nav_display_output %}{% endblock %}" type="submit" value="{{ _('Display output') }}" form="scopesForm" formaction="{{ url_for('action_script_display_output') }}" formmethod="get"> </div> diff --git a/ogcp/views.py b/ogcp/views.py index d6fc67c..a583dfa 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -15,7 +15,7 @@ from ogcp.forms.action_forms import ( GenericForm, SelectClientForm, ImageUpdateForm, ImportClientsForm, ServerForm, DeleteRepositoryForm, RepoForm, FolderForm, CacheForm, ClientMoveForm, RunScriptForm, ImageConfigForm, ImageFetchForm, - ServerConfigurationForm, SetRepoForm + ServerConfigurationForm, SetRepoForm, RunCmdForm ) from flask_login import ( current_user, LoginManager, @@ -2218,6 +2218,42 @@ def action_client_delete(): else: return redirect(url_for('scopes')) +@app.route('/action/cmd/run', methods=['GET', 'POST']) +@login_required +def action_run_cmd(): + form = RunCmdForm(request.form) + if request.method == 'POST': + ips = form.ips.data.split(' ') + if not validate_elements(ips): + return redirect(url_for('commands')) + + payload = { + 'clients': ips, + 'run': form.command.data, + 'inline': True + } + server = get_server_from_clients(ips) + r = server.post('/shell/run', payload) + + if not r: + return ogserver_down('commands') + if r.status_code != requests.codes.ok: + return ogserver_error('commands') + + flash(_('Command sent successfully'), category='info') + return redirect(url_for('commands')) + else: + ips = parse_elements(request.args.to_dict()) + form.ips.data = " ".join(ips) + if not validate_elements(ips): + return redirect(url_for('commands')) + + scopes, clients = get_scopes(set(ips)) + selected_clients = list(get_selected_clients(scopes['scope']).items()) + return render_template('actions/script_run.html', form=form, + selected_clients=selected_clients, + scopes=scopes) + @app.route('/action/script/run', methods=['GET', 'POST']) @login_required def action_run_script(): @@ -2232,8 +2268,8 @@ def action_run_script(): payload = { 'clients': ips, - 'run': ';|\n\r'.join(cmd_elems), - 'echo': True + 'run': ' '.join(cmd_elems), + 'inline': False } server = get_server_from_clients(ips) r = server.post('/shell/run', payload) |