Skip to content

Commit

Permalink
Move disk-wizard service actions from 'disk-wizard' to 'disk-service'…
Browse files Browse the repository at this point in the history
… controller and change the routes accordingly
  • Loading branch information
tmkasun committed Aug 27, 2014
1 parent 1d857df commit 54e0c44
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
43 changes: 43 additions & 0 deletions app/controllers/disk_service_controller.rb
Expand Up @@ -12,4 +12,47 @@ def get_all_devices
def check_label
render text: "check_label"
end

# Generate fpaste URL which contains required information to debug an error.
# Directly calling system tools via open3 library not using CommandExecutor library(Still able to generate debug URL even CommandExecutor library fails).
def debug_info
require "open3"
script_location = "/var/hda/apps/520ut3lo6w/elevated/"
Open3.popen3("sudo", "./debug_info.sh", :chdir => script_location) { |stdin, stdout, stderr, wait_thr|
exit_status = wait_thr.value.exitstatus
if not (exit_status.equal? 0)
error = stderr.read
render json: {error: error}
return false
end
result = stdout.read
valid_url = /https?:\/\/[\S]+/
url = urls = nil
result.each_line do |line|
urls = line.split.grep(valid_url) if line.match valid_url
end
url = urls.group_by(&:size).max.last[0] if urls
render json: {url: url}
}
end

# Return JSON encoded progress message to view layer.
# This action is used by progress.html.erb to notify user about currently executing operation.
# From view layer, use pooling method(send AJAX request to here in every 1 second) to fetch the information from backend.
# TODO: Push data to front-end via a websocket instead of pooling(wast of bandwidth)
# Use current progress(percentage in Integer) to get the associated progress_message. TODO: change percentage to number of operations(i.e instead of 45% complete, show 4/10 operations completed)
def operations_progress
message = Device.progress_message(Device.progress)
render json: {percentage: Device.progress, message: message}
end

# Render progress page when click on apply button in 'confirmation' step
# Implicitly send HTTP POST request to process_disk action to start processing the operations
# Expected key:values in @params:
# :debug => Integer value(1) if debug mode has selected in fourth step(confirmation), else nil
def progress
debug_mode = params[:debug]
self.user_selections = {debug: debug_mode}
end

end
47 changes: 3 additions & 44 deletions app/controllers/disk_wizard_controller.rb
Expand Up @@ -70,7 +70,7 @@ def process_disk
DebugLogger.info "|#{self.class.name}|>|#{__method__}|:Selected disk/partition = #{path}"
disk = Device.find path

CommandsExecutor.debug_mode = !!(self.user_selections['debug'])
CommandExecutor.debug_mode = !!(self.user_selections['debug'])

jobs_queue = JobQueue.new(user_selections.length)
jobs_queue.enqueue({job_name: :pre_checks_job, job_para: {path: path}})
Expand Down Expand Up @@ -102,18 +102,9 @@ def process_disk
end
end

# Render progress page when click on apply button in 'confirmation' step
# Implicitly send HTTP POST request to process_disk action to start processing the operations
# Expected key:values in @params:
# :debug => Integer value(1) if debug mode has selected in fourth step(confirmation), else nil
def progress
debug_mode = params[:debug]
self.user_selections = {debug: debug_mode}
end

# process_disk action redirects here if all operations are completed successfully
def done
@operations = CommandsExecutor.operations_log
@operations = CommandExecutor.operations_log
flash[:success] = 'All disks operations have been completed successfully!'
@user_selections = self.user_selections
end
Expand Down Expand Up @@ -143,15 +134,6 @@ def user_selections=(hash)
DebugLogger.info "|#{self.class.name}|>|#{__method__}|:Updated session[:user_selections] #{session[:user_selections]}"
end

# Return JSON encoded progress message to view layer.
# This action is used by progress.html.erb to notify user about currently executing operation.
# From view layer, use pooling method(send AJAX request to here in every 1 second) to fetch the information from backend.
# TODO: Push data to front-end via a websocket instead of pooling(wast of bandwidth)
# Use current progress(percentage in Integer) to get the associated progress_message. TODO: change percentage to number of operations(i.e instead of 45% complete, show 4/10 operations completed)
def operations_progress
message = Device.progress_message(Device.progress)
render json: {percentage: Device.progress, message: message}
end

# Show errors/exceptions to the user if an error occurred while processing the operations.
# Show the exceptions raised from the operating system level(stderr) via open3.
Expand All @@ -162,30 +144,7 @@ def error
# Clear the debug_mode flag.
# TODO: This is used as a before_filter with exceptions,this might cause redundant calls to this method(reset the flag which has been already cleared)
def clear_mode
CommandsExecutor.debug_mode = nil
end

# Generate fpaste URL which contains required information to debug an error.
# Directly calling system tools via open3 library not using CommandsExecutor library(Still able to generate debug URL even CommandsExecutor library fails).
def debug_info
require "open3"
script_location = "/var/hda/apps/520ut3lo6w/elevated/"
Open3.popen3("sudo", "./debug_info.sh", :chdir => script_location) { |stdin, stdout, stderr, wait_thr|
exit_status = wait_thr.value.exitstatus
if not (exit_status.equal? 0)
error = stderr.read
render json: {error: error}
return false
end
result = stdout.read
valid_url = /https?:\/\/[\S]+/
url = urls = nil
result.each_line do |line|
urls = line.split.grep(valid_url) if line.match valid_url
end
url = urls.group_by(&:size).max.last[0] if urls
render json: {url: url}
}
CommandExecutor.debug_mode = nil
end

end
24 changes: 14 additions & 10 deletions config/routes.rb
Expand Up @@ -10,16 +10,20 @@
root :to => 'disk_wizards#select_device'
get "get_all_devices" => 'disk_service#get_all_devices'
get "check_label" => 'disk_service#check_label'
match 'select' => 'disk_wizards#select_device',via: [:get,:post]
match 'file_system' => 'disk_wizards#select_fs',via: [:get,:post]
match 'manage' => 'disk_wizards#manage_disk',via: [:get,:post]
match 'confirmation' => 'disk_wizards#confirmation',via: [:get,:post]
get 'complete' => 'disk_wizards#done'
get 'get_progress' => 'disk_wizards#operations_progress'
get 'error' => 'disk_wizards#error'
get 'debug_info' => 'disk_wizards#debug_info'
post 'process' => 'disk_wizards#progress'
post 'ajax_process' => 'disk_wizards#process_disk'
get 'debug_info' => 'disk_service#debug_info'
post 'process' => 'disk_service#progress'
get 'get_progress' => 'disk_service#operations_progress'

match 'select' => 'disk_wizard#select_device',via: [:get,:post]
match 'file_system' => 'disk_wizard#select_fs',via: [:get,:post]
match 'manage' => 'disk_wizard#manage_disk',via: [:get,:post]
match 'confirmation' => 'disk_wizard#confirmation',via: [:get,:post]
get 'complete' => 'disk_wizard#done'
get 'get_progress' => 'disk_wizard#operations_progress'
get 'error' => 'disk_wizard#error'
get 'debug_info' => 'disk_wizard#debug_info'
post 'process' => 'disk_wizard#progress'
post 'ajax_process' => 'disk_wizard#process_disk'


resources :disks
Expand Down

0 comments on commit 54e0c44

Please sign in to comment.