diff --git a/.gitignore b/.gitignore
index 4bbb9ed..75af97e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,5 @@
/test/dummy/tmp/development_secret.txt
.byebug_history
+
+.DS_Store
diff --git a/Gemfile.lock b/Gemfile.lock
index 57d8452..f396eb4 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -3,6 +3,7 @@ PATH
specs:
rake-ui (0.1.0)
actionpack
+ activestorage
activesupport
railties
rake
diff --git a/README.md b/README.md
index 45f4c5c..a00a1c1 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,20 @@ RakeUi.configuration do |config|
end
```
+### Enabling ActiveStorage
+In order to turn on/off active_storage for RakeUI gem, add configuration in initializer as follows
+
+```rb
+RakeUi.configuration do |config|
+ config.active_storage = false
+end
+```
+if you want to specify active storage service,
+update initializer with
+```
+storage_service = :amazon
+```
+
We recommend adding guards in your route to ensure that the proper authentication is in place to ensure that users are authenticated so that if this were ever to be rendered in production, you would be covered. The best way for that is [router constraints](https://guides.rubyonrails.org/routing.html#specifying-constraints)
## Testing
diff --git a/app/controllers/rake_ui/application_controller.rb b/app/controllers/rake_ui/application_controller.rb
index 150a06e..f77b435 100644
--- a/app/controllers/rake_ui/application_controller.rb
+++ b/app/controllers/rake_ui/application_controller.rb
@@ -3,13 +3,51 @@
module RakeUi
class ApplicationController < ActionController::Base
before_action :black_hole_production
+ before_action :auth_validate
+ before_action :policy_validate
+
+ # include Pundit::Authorization
+
+ # before_action :authorize_pundit
+ # before_action :authorize!
+
+ STAGING_OK = (Rails.env.staging? && RakeUi.configuration.allow_staging)
+ PROD_OK = RakeUi.configuration.allow_production
private
+ def authorize_pundit
+ r = 3
+ @current_user = authenticate_admin_user!
+ authorize(@current_user)
+ # binding.pry
+ # authorize :rake_tasks, :show?
+ end
+
def black_hole_production
- return if Rails.env.test? || Rails.env.development? || RakeUi.configuration.allow_production
+ return if Rails.env.test? || Rails.env.development? || STAGING_OK || PROD_OK
raise ActionController::RoutingError, "Not Found"
end
+
+ def auth_validate
+ return true unless RakeUi.configuration.auth_engine
+
+ if defined?(RakeUi.configuration.auth_engine)
+ cb = RakeUi.configuration.auth_callback
+ return false unless cb && (cb.class == Proc)
+ RakeUi.configuration.auth_callback.call(self)
+ end
+ end
+
+ def policy_validate
+ return true unless RakeUi.configuration.policy_engine
+
+ if defined?(RakeUi.configuration.policy_engine)
+ cb = RakeUi.configuration.policy_callback
+ return false unless cb && (cb.class == Proc)
+ RakeUi.configuration.policy_callback.call(self)
+ end
+ end
end
end
diff --git a/app/controllers/rake_ui/rake_task_logs_controller.rb b/app/controllers/rake_ui/rake_task_logs_controller.rb
index 8cc1868..72f5992 100644
--- a/app/controllers/rake_ui/rake_task_logs_controller.rb
+++ b/app/controllers/rake_ui/rake_task_logs_controller.rb
@@ -12,8 +12,7 @@ class RakeTaskLogsController < ApplicationController
:log_file_full_path].freeze
def index
- @rake_task_logs = RakeUi::RakeTaskLog.all.sort_by(&:id)
-
+ @rake_task_logs = klass.all
respond_to do |format|
format.html
format.json do
@@ -25,8 +24,7 @@ def index
end
def show
- @rake_task_log = RakeUi::RakeTaskLog.find_by_id(params[:id])
-
+ @rake_task_log = klass.find_by_id(params[:id])
@rake_task_log_content = @rake_task_log.file_contents.gsub("\n", "
")
@rake_task_log_content_url = rake_task_log_path(@rake_task_log.id, format: :json)
@is_rake_task_log_finished = @rake_task_log.finished?
@@ -55,5 +53,10 @@ def rake_task_log_as_json(task)
def rake_task_logs_as_json(tasks = [])
tasks.map { |task| rake_task_log_as_json(task) }
end
+
+ def klass
+ RakeUi.configuration.active_storage ? ::RakeTaskLog : RakeUi::RakeTaskLog
+ end
+
end
end
diff --git a/app/helpers/rake_ui/rake_task_log_helper.rb b/app/helpers/rake_ui/rake_task_log_helper.rb
new file mode 100644
index 0000000..5389e89
--- /dev/null
+++ b/app/helpers/rake_ui/rake_task_log_helper.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module RakeUi
+ module RakeTaskLogHelper
+ def attributers_to_show
+ RakeUi.configuration.active_storage ? ::RakeTaskLog::ATTRIBUTES_TO_SHOW : []
+ end
+ end
+end
diff --git a/app/models/concerns/rake_task_logs.rb b/app/models/concerns/rake_task_logs.rb
new file mode 100644
index 0000000..9967137
--- /dev/null
+++ b/app/models/concerns/rake_task_logs.rb
@@ -0,0 +1,39 @@
+module RakeTaskLogs
+ extend ActiveSupport::Concern
+ ID_DATE_FORMAT = "%Y-%m-%d-%H-%M-%S%z"
+ REPOSITORY_DIR = Rails.root.join("tmp", "rake_ui")
+ FILE_DELIMITER = "____"
+ TASK_HEADER_OUTPUT_DELIMITER = "-------------------------------"
+ FILE_ITEM_SEPARATOR = ": "
+ FINISHED_STRING = "+++++ COMMAND FINISHED +++++"
+ INPROGRESS = "Task is in Progress...."
+ NOT_AVAILABLE = "Log File Not Avaialable..."
+
+
+ class_methods do
+ def create_tmp_file_dir
+ FileUtils.mkdir_p(REPOSITORY_DIR.to_s)
+ end
+
+ def generate_task_attributes(raker_id:)
+ date = Time.now.strftime(ID_DATE_FORMAT)
+ id = "#{date}#{FILE_DELIMITER}#{raker_id}"
+ log_file_name = "#{id}.txt"
+ log_file_full_path = REPOSITORY_DIR.join(log_file_name).to_s
+ {
+ date:,
+ id:,
+ log_file_name:,
+ log_file_full_path:
+ }
+ end
+ end
+
+ def rake_command_with_logging
+ "#{rake_command} 2>&1 >> #{log_file_full_path}"
+ end
+
+ def command_to_mark_log_finished
+ "echo #{FINISHED_STRING} >> #{log_file_full_path}"
+ end
+end
diff --git a/app/models/rake_task_log.rb b/app/models/rake_task_log.rb
new file mode 100644
index 0000000..9436fff
--- /dev/null
+++ b/app/models/rake_task_log.rb
@@ -0,0 +1,55 @@
+class RakeTaskLog < ApplicationRecord
+ include RakeTaskLogs
+
+ ATTRIBUTES_TO_SHOW = %w[id name date args environment rake_command rake_definition_file log_file_name log_file_full_path]
+ STORAGE_SERVICE = RakeUi.configuration.storage_service.presence || Rails.application.config.active_storage.service
+ has_one_attached :log_file, service: STORAGE_SERVICE
+ enum status: { in_progress: 0, finished: 1 }
+
+ def self.build_new_for_command(name:, rake_definition_file:, rake_command:, raker_id:, args: nil, environment: nil)
+ create_tmp_file_dir
+ generate_file_content(log_file_full_path: generate_task_attributes(raker_id:)[:log_file_full_path])
+ create_rake_task_log(name:, args:, environment:, rake_command:, rake_definition_file:, raker_id:)
+ end
+
+ def self.generate_file_content(log_file_full_path:)
+ File.open(log_file_full_path, "w+") do |f|
+ f.puts TASK_HEADER_OUTPUT_DELIMITER.to_s
+ f.puts " INVOKED RAKE TASK OUTPUT BELOW"
+ f.puts TASK_HEADER_OUTPUT_DELIMITER.to_s
+ end
+ end
+
+ def self.create_rake_task_log(name:, args:, environment:, rake_command:, rake_definition_file:, raker_id:)
+ attributes = generate_task_attributes(raker_id:)
+
+ ::RakeTaskLog.create(name:,
+ args:,
+ environment:,
+ rake_command:,
+ rake_definition_file:,
+ log_file_name: attributes[:log_file_name],
+ log_file_full_path: attributes[:log_file_full_path],
+ raker_id:)
+ end
+
+ def attach_file_with_rake_task_log
+ if File.exist?(log_file_full_path)
+ log_file.attach(io: File.open(log_file_full_path), filename: 'log.txt')
+ end
+ self.status = :finished
+ self.save!
+ File.delete(log_file_full_path)
+ end
+
+ def file_contents
+ return INPROGRESS unless finished?
+
+ log_file.download if log_file.attached?
+ end
+
+ def date
+ created_at
+ end
+
+end
diff --git a/app/models/rake_ui/rake_task.rb b/app/models/rake_ui/rake_task.rb
index 7bab735..7531d45 100644
--- a/app/models/rake_ui/rake_task.rb
+++ b/app/models/rake_ui/rake_task.rb
@@ -82,8 +82,7 @@ def internal_task?
def call(args: nil, environment: nil)
rake_command = build_rake_command(args: args, environment: environment)
-
- rake_task_log = RakeUi::RakeTaskLog.build_new_for_command(
+ rake_task_log = klass.build_new_for_command(
name: name,
args: args,
environment: environment,
@@ -98,6 +97,7 @@ def call(args: nil, environment: nil)
system(rake_task_log.rake_command_with_logging)
system(rake_task_log.command_to_mark_log_finished)
+ rake_task_log.attach_file_with_rake_task_log if RakeUi.configuration.active_storage
end
rake_task_log
@@ -122,5 +122,9 @@ def build_rake_command(args: nil, environment: nil)
command
end
+
+ def klass
+ @klass ||= RakeUi.configuration.active_storage ? ::RakeTaskLog : RakeUi::RakeTaskLog
+ end
end
end
diff --git a/app/models/rake_ui/rake_task_log.rb b/app/models/rake_ui/rake_task_log.rb
index 861f14b..560792a 100644
--- a/app/models/rake_ui/rake_task_log.rb
+++ b/app/models/rake_ui/rake_task_log.rb
@@ -2,17 +2,7 @@
module RakeUi
class RakeTaskLog < OpenStruct
- # year-month-day-hour(24hour time)-minute-second-utc
- ID_DATE_FORMAT = "%Y-%m-%d-%H-%M-%S%z"
- REPOSITORY_DIR = Rails.root.join("tmp", "rake_ui")
- FILE_DELIMITER = "____"
- FINISHED_STRING = "+++++ COMMAND FINISHED +++++"
- TASK_HEADER_OUTPUT_DELIMITER = "-------------------------------"
- FILE_ITEM_SEPARATOR = ": "
-
- def self.create_tmp_file_dir
- FileUtils.mkdir_p(REPOSITORY_DIR.to_s)
- end
+ include RakeTaskLogs
def self.truncate
FileUtils.rm_rf(Dir.glob(REPOSITORY_DIR.to_s + "/*"))
@@ -116,18 +106,10 @@ def log_file_full_path
super || parsed_file_contents[:log_file_full_path]
end
- def rake_command_with_logging
- "#{rake_command} 2>&1 >> #{log_file_full_path}"
- end
-
def file_contents
@file_contents ||= File.read(log_file_full_path)
end
- def command_to_mark_log_finished
- "echo #{FINISHED_STRING} >> #{log_file_full_path}"
- end
-
def finished?
file_contents.include? FINISHED_STRING
end
diff --git a/app/views/rake_ui/rake_task_logs/_log_attributes.html.erb b/app/views/rake_ui/rake_task_logs/_log_attributes.html.erb
new file mode 100644
index 0000000..99206fd
--- /dev/null
+++ b/app/views/rake_ui/rake_task_logs/_log_attributes.html.erb
@@ -0,0 +1,4 @@
+