Add helper method for 'volumes'

This commit is contained in:
kyg516 2016-09-20 21:13:24 +09:00
parent 595252011e
commit 37538b5112
3 changed files with 84 additions and 62 deletions

View File

@ -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

View File

@ -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 << "<div>"
params.each do |t|
next if volume[t].nil?
html << "<p>"
html << t + " : " + volume[t]
html << "</p>"
end
html << "</div>"
raw(html)
end
end

View File

@ -115,7 +115,7 @@
</div>
<div class="clearfix"></div>
<div class="row">
<%@volumes.each do |t|%>
<% get_volumes.each_with_index do |t, index| %>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="x_panel">
<div class="x_title">
@ -123,7 +123,12 @@
<h2>Infomation <small><%=t["Volume Name"]%></small></h2>
<!-- right title -->
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i <%if t!=@volumes[0]%> class="fa fa-chevron-down" <%else%> class="fa fa-chevron-up" <%end%> ></i></a>
<li><a class="collapse-link">
<% if index == 0 %>
<i class="fa fa-chevron-up"></i>
<% else %>
<i class="fa fa-chevron-down"></i>
<% end %></a>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
@ -149,7 +154,7 @@
<% end %>
</li>
<li>
<%if t['Status'].eql? " Stopped" %>
<%if t['Status'].eql? " Stopped" or t['Status'].eql? " Created" %>
<a><i class="fa fa-circle red"></i></a>
<% else %>
<a><i class="fa fa-circle"></i></a>
@ -158,21 +163,16 @@
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content" <%if t!=@volumes[0]%> style="display: none;" <%end%> >
<% if index == 0 %>
<div class="x_content">
<% else %>
<div class="x_content" style="display: none;">
<% end %>
<!-- left content -->
<div class="col-md-6 col-sm-6 col-xs-12">
<div style="margin: 10px">
<p class="text-muted font-13 m-b-30"><span class="badge bg-blue">Volume Info</span></p>
Type : <%=t["Type"]%> <br>
Volume ID : <%=t["Volume ID"]%> <br>
Status : <%=t["Status"]%> <br>
Number of Bricks : <%=t["Number of Bricks"]%> <br>
Transport-type : <%=t["Transport-type"]%> <br>
Bricks : <%=t["Bricks"]%> <br>
Bricks1 : <%=t["Brick1"]%> <br>
Options Reconfigured : <%=t["Options Reconfigured"]%> <br>
performance.readdir-ahead : <%=t["performance.readdir-ahead"]%> <br>
mount state: <%=t["Mount State"]%> <br>
<%= volume_info t %>
</div>
<% if t["Mount State"] == "mounted" %>
<a class="btn btn-app" href="/volume/unmount/<%=t['Volume Name'].delete(' ')%>"><i class="fa fa-upload"></i> Unmount</a>
@ -292,7 +292,7 @@
alert("Volume name can't contains white spaces");
return;
}
<% @volumes.each do |t| %>
<% get_volumes.each do |t| %>
if(volume_name == "<%=t["Volume Name"].delete(' ')%>"){
alert("Already has a volume which name is same");
return;