From 8535cd9a9c0b81177bccef1ec68099945745d159 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Thu, 9 Dec 2021 16:44:05 +0100 Subject: [PATCH] Add option to sync single execution environment --- .../execution_environments_controller.rb | 16 +++++++++++++++- app/policies/execution_environment_policy.rb | 2 +- app/views/execution_environments/show.html.slim | 10 ++++++++-- config/locales/de.yml | 4 ++++ config/locales/en.yml | 4 ++++ config/routes.rb | 1 + 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/controllers/execution_environments_controller.rb b/app/controllers/execution_environments_controller.rb index 67410942..bc05e631 100644 --- a/app/controllers/execution_environments_controller.rb +++ b/app/controllers/execution_environments_controller.rb @@ -4,7 +4,7 @@ class ExecutionEnvironmentsController < ApplicationController include CommonBehavior before_action :set_docker_images, only: %i[create edit new update] - before_action :set_execution_environment, only: MEMBER_ACTIONS + %i[execute_command shell statistics] + before_action :set_execution_environment, only: MEMBER_ACTIONS + %i[execute_command shell statistics sync_to_runner_management] before_action :set_testing_framework_adapters, only: %i[create edit new update] def authorize! @@ -166,6 +166,20 @@ class ExecutionEnvironmentsController < ApplicationController update_and_respond(object: @execution_environment, params: execution_environment_params) end + def sync_to_runner_management + return unless Runner.management_active? + + begin + Runner.strategy_class.sync_environment(@execution_environment) + rescue Runner::Error => e + Rails.logger.debug { "Runner error while synchronizing execution environment with id #{@execution_environment.id}: #{e.message}" } + Sentry.capture_exception(e) + redirect_to @execution_environment, alert: t('execution_environments.index.synchronize.failure', error: e.message) + else + redirect_to @execution_environment, notice: t('execution_environments.index.synchronize.success') + end + end + def sync_all_to_runner_management authorize ExecutionEnvironment diff --git a/app/policies/execution_environment_policy.rb b/app/policies/execution_environment_policy.rb index a83b1f29..2e4be0e0 100644 --- a/app/policies/execution_environment_policy.rb +++ b/app/policies/execution_environment_policy.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ExecutionEnvironmentPolicy < AdminOnlyPolicy - %i[execute_command? shell? statistics? show?].each do |action| + %i[execute_command? shell? statistics? show? sync_to_runner_management?].each do |action| define_method(action) { admin? || author? } end diff --git a/app/views/execution_environments/show.html.slim b/app/views/execution_environments/show.html.slim index e1b15ec6..fbc58f0e 100644 --- a/app/views/execution_environments/show.html.slim +++ b/app/views/execution_environments/show.html.slim @@ -1,6 +1,12 @@ -h1 - = @execution_environment +h1.d-inline-block = @execution_environment +.btn-group.float-right = render('shared/edit_button', object: @execution_environment) + button.btn.btn-secondary.float-right.dropdown-toggle data-toggle='dropdown' type='button' + ul.dropdown-menu.dropdown-menu-right role='menu' + li = link_to(t('execution_environments.index.synchronize.button'), sync_to_runner_management_execution_environment_path(@execution_environment), method: :post, class: 'dropdown-item text-dark') if policy(@execution_environment).sync_to_runner_management? + li = link_to(t('execution_environments.index.shell'), shell_execution_environment_path(@execution_environment), class: 'dropdown-item text-dark') if policy(@execution_environment).shell? + li = link_to(t('shared.statistics'), statistics_execution_environment_path(@execution_environment), 'data-turbolinks' => "false", class: 'dropdown-item text-dark') if policy(@execution_environment).statistics? + li = link_to(t('shared.destroy'), @execution_environment, data: {confirm: t('shared.confirm_destroy')}, method: :delete, class: 'dropdown-item text-dark') if policy(@execution_environment).destroy? = row(label: 'execution_environment.name', value: @execution_environment.name) = row(label: 'execution_environment.user', value: link_to_if(policy(@execution_environment.author).show?, @execution_environment.author, @execution_environment.author)) diff --git a/config/locales/de.yml b/config/locales/de.yml index 4d8a602d..73a0648d 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -303,6 +303,10 @@ de: not_synced_to_runner_management: Die Ausführungsumgebung wurde erstellt, aber aufgrund eines Fehlers nicht zum Runnermanagement synchronisiert. index: shell: Shell + synchronize: + button: Synchronisieren + success: Die Ausführungsumgebung wurde erfolgreich synchronisiert. + failure: "Beim Synchronisieren der Ausführungsumgebung ist folgender Fehler aufgetreten: %{error}" synchronize_all: button: Alle synchronisieren success: Alle Ausführungsumgebungen wurden erfolgreich synchronisiert. diff --git a/config/locales/en.yml b/config/locales/en.yml index 4f93be50..f54599a3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -303,6 +303,10 @@ en: not_synced_to_runner_management: The execution environment was created but not synced to the runner management due to an error. index: shell: Shell + synchronize: + button: Synchronize + success: The execution environemnt was synchronized successfully. + failure: "The execution environment could not be synchronised due to the following error: %{error}" synchronize_all: button: Synchronize all success: All execution environemnts were synchronized successfully. diff --git a/config/routes.rb b/config/routes.rb index 4d2c77ff..c0042292 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,6 +66,7 @@ Rails.application.routes.draw do get :shell post 'shell', as: :execute_command, action: :execute_command get :statistics + post :sync_to_runner_management end post :sync_all_to_runner_management, on: :collection