From 37538b5112eef4ac4f8df33da5b99ff391ccc55b Mon Sep 17 00:00:00 2001 From: kyg516 Date: Tue, 20 Sep 2016 21:13:24 +0900 Subject: [PATCH] Add helper method for 'volumes' --- app/controllers/volume_controller.rb | 47 ------------------- app/helpers/volume_helper.rb | 69 ++++++++++++++++++++++++++++ app/views/volume/index.html.erb | 30 ++++++------ 3 files changed, 84 insertions(+), 62 deletions(-) diff --git a/app/controllers/volume_controller.rb b/app/controllers/volume_controller.rb index 8b83545..de6cf9e 100644 --- a/app/controllers/volume_controller.rb +++ b/app/controllers/volume_controller.rb @@ -1,61 +1,14 @@ class VolumeController < ApplicationController def index - @volumes = Array.new file_directory("/mnt") get_conf - info = get_volume_info.split("\n") - if info.blank? - flash[:danger] = "Check Server" - else - parse_info(info) - end - end - - def parse_info(info) - volume = Hash.new - df = get_df - info << "\n" - info.each do |t| - next if t.equal? info.first - if t.include? ":" - temp = t.split(":") - volume[temp[0]] = temp[1] - else - String state = (df.include? volume['Volume Name'].delete(' ')) ? "mounted" : "unmounted" - volume['Mount State'] = state - if state.eql? "mounted" - s = df.split("\n") - s.each do |t| - if t.include? volume['Volume Name'].delete(' ') - mnt_point = t.split(" ")[5] - volume['Mount Point'] = mnt_point - end - end - end - @volumes << volume - volume = Hash.new - end - end end def get_df return `df -P` end - def get_volume_info - command = String.new - command << "sshpass -p" + @config["host_password"].to_s - command << " ssh " - if !@config["host_port"].nil? - command << "-p " + @config["host_port"].to_s + " " - end - command << @config["host_user"].to_s + "@" + @config["host_ip"].to_s - command << " gluster volume info" - puts command - return `#{command}` - end - def file_upload df = get_df mnt_dir = String.new diff --git a/app/helpers/volume_helper.rb b/app/helpers/volume_helper.rb index 27d5fd2..d692e1c 100644 --- a/app/helpers/volume_helper.rb +++ b/app/helpers/volume_helper.rb @@ -1,2 +1,71 @@ module VolumeHelper + + def get_conf + config = Hash.new + output = `cat configure.conf`.split("\n") + output.each do |t| + if t.include? "project_path=" + config["project_path"] = t.split("project_path=")[1] + elsif t.include? "server_name=" + config["server_name"] = t.split("server_name=")[1] + elsif t.include? "host_user=" + config["host_user"] = t.split("host_user=")[1] + elsif t.include? "host_ip=" + config["host_ip"] = t.split("host_ip=")[1] + elsif t.include? "host_port=" and !t.split("host_port=")[1].nil? + config["host_port"] = t.split("host_port=")[1] + " " + elsif t.include? "host_password=" + config["host_password"] = t.split("host_password=")[1] + end + end + return config + end + + def get_volumes + volumes = Array.new + volume = Hash.new + command = "df -P" + df = `#{command}` + conf = get_conf + command = "sshpass -p#{conf['host_password']} ssh #{conf['host_user']}@#{conf['host_ip']} gluster volume info" + info = `#{command}`.split("\n") + info << "\n" + info.each do |t| + next if t.equal? info.first + if t.include? ":" + temp = t.split(":") + volume[temp[0]] = temp[1] + else + String state = (df.include? volume['Volume Name'].delete(' ')) ? "mounted" : "unmounted" + volume['Mount State'] = state + if state.eql? "mounted" + s = df.split("\n") + s.each do |t| + if t.include? volume['Volume Name'].delete(' ') + mnt_point = t.split(" ")[5] + volume['Mount Point'] = mnt_point + end + end + end + volumes << volume + volume = Hash.new + end + end + return volumes + end + + def volume_info(volume) + params = ['Type', 'Volume ID', 'Status', 'Number of Bricks', 'Transport-type', + 'Bricks', 'Options Reconfigured', 'Mount State', 'Mount Point'] + html = '' + html << "
" + params.each do |t| + next if volume[t].nil? + html << "

" + html << t + " : " + volume[t] + html << "

" + end + html << "
" + raw(html) + end end diff --git a/app/views/volume/index.html.erb b/app/views/volume/index.html.erb index b0201bf..3c86b96 100644 --- a/app/views/volume/index.html.erb +++ b/app/views/volume/index.html.erb @@ -115,7 +115,7 @@
- <%@volumes.each do |t|%> + <% get_volumes.each_with_index do |t, index| %>
@@ -123,7 +123,12 @@

Infomation <%=t["Volume Name"]%>

-
style="display: none;" <%end%> > + <% if index == 0 %> +
+ <% else %> +