JStree : create jstree from smbclient directory structure - ruby-on-rails

I am using sambal gem for smbclient.
I want to create jstree from recursive directory listing from smbclient.
require "sambal"
client = Sambal::Client.new(domain:domain-name, host: host, share: 'all' ,user: 'abc', password: 'pswd')
client.cd("abc")
client.ask("recurse")
all_files_hash = client.ask("ls")
I have modified parse_files method of smbclient to have path in file listing hash.
Sample hash of direcory listing that I want to display in JStree:
all_files_hash = {"."=>{:type=>:directory}, ".."=>{:type=>:directory},
"log"=>{:type=>:directory, :path=>"\\abc"},
"softwares"=>{:type=>:directory, :path=>"\\abc"},
"test.txt"=>{:type=>:file, :path=>""},
"test_data"=>{:type=>:directory, :path=>"\\abc"},
".keep"=>{:type=>:file, :path=>"\\abc\\log"},
"alert.log"=>{:type=>:file, :path=>"\\abc\\log"},
"development.log"=>{:type=>:file, :path=>"\\abc\\log"},
"test.log"=>{:type=>:file, :path=>"\\abc\\test_data"},
"robomongo-0.8.4-x86_64.deb"=>{:type=>:file, :path=>"\\abc\\softwares"},
"sublime-text_build-3065_amd64.deb"=>{:type=>:file, :path=>"\\abc\\softwares"},
"ubuntu"=>{:type=>:directory, :path=>"\\abc\\softwares"},
"ubuntu.css"=>{:type=>:file, :path=>"\\abc\\softwares\\ubuntu"}}
How can I create jstree from recursive file listing that I have got in all_files_hash?
Expected Output :
jstree_hash = [{"text" => "abc","path" => "\\abc","children" => [{"text" => "log","path"=>"\\abc\\log","children" => [{"text" => "development.log","path"=>"\\abc\\log\\developemnt.log"},{"text" => "alert.log","path"=>"\\abc\\log\\alert.log"}]}]}}]
I followed below links but no luck :
nested hash and
iterate over deep hash
Thanks in advance..

Related

Extra object in Google Cloud Storage when using Fog

I am following the fog.io/storage example for creating a directory and then uploading a file. Everything works great when I push my file into Google Cloud Storage except there is always a "binary/octet-stream" file named exactly as the deepest file I create.
My code is very similar to the AWS example in that I create a directory and from that new directory, I create a file. The directory structure is created properly and the file is uploaded properly but there is always an extra, 0-byte file. My code looks like:
job_number = 100
connection = Fog::Storage.new({
:provider => 'Google',
:google_storage_access_key_id => YOUR_GCE_ACCESS_KEY_ID,
:google_storage_secret_access_key => YOUR_GCE_SECRET_ACCESS_KEY
})
directory = connection.directories.create(
:key => "test-project/uploads/#{job_number}",
:public => false
)
file = directory.files.create(
:key => 'file.pdf',
:content_type => 'application/pdf',
:body => File.open("/path/to/my/file.pdf"),
:public => false
)
The directory structure is perfect (gs://test-project/uploads/100 folder exists) and the file.pdf file exists in that directory as well (gs://test-project/uploads/100/file.pdf).
The problem is that after the:
directory = connection.directories.create(
:key => "test-project/uploads/#{job_number}",
:public => false
)
command runs, there is a file at gs://test-project/uploads/100 as well as a directory gs://test-project/uploads/100/. When I walk through the code, the connection.directories.create(...) command is definitely creating the extra file but I cannot figure out why.
I have also tried to add a trailing slash to the key value for the connection.directories.create(...) command but that actually creates a different directory structure problem that is worse than this (this isn't bad, just annoying).
Has anyone seen this or know how to correctly have the directory structure created through Fog?
Instead of creating the directory right up to the file, just create/get the base directory/bucket and then save the file with the rest of the directory structure. So it would look like this:
job_number = 100
connection = Fog::Storage.new({
:provider => 'Google',
:google_storage_access_key_id => YOUR_GCE_ACCESS_KEY_ID,
:google_storage_secret_access_key => YOUR_GCE_SECRET_ACCESS_KEY
})
directory = connection.directories.create(
:key => "test-project",
:public => false
)
file = directory.files.create(
:key => 'uploads/#{job_number}/file.pdf',
:content_type => 'application/pdf',
:body => File.open("/path/to/my/file.pdf"),
:public => false
)

How to get host under a host group, By Ruby script

I am able to get the host group id by
puts zbx.hostgroups.get(:name => "Dev" )
give this o/p
{"groupid"=>"13", "name"=>"Dev", "internal"=>"0", "flags"=>"0"}
But I want to get all the nodes under this host group. Although I tried in other way like
get all host under this host group but I didnt find groupid attribute in host please refer below o/p
puts zbx.hosts.get(:host => "ip-10-10-111-11.ec2.internal")
{"maintenances"=>[], "hostid"=>"10251", "proxy_hostid"=>"10109",
"host"=>"ip-10-10-111-11.ec2.internal", "status"=>"0",
"disable_until"=>"0", "error"=>"", "available"=>"1",
"errors_from"=>"0", "lastaccess"=>"0", "ipmi_authtype"=>"0",
"ipmi_privilege"=>"2", "ipmi_username"=>"", "ipmi_password"=>"",
"ipmi_disable_until"=>"0", "ipmi_available"=>"0",
"snmp_disable_until"=>"0", "snmp_available"=>"0",
"maintenanceid"=>"0", "maintenance_status"=>"0",
"maintenance_type"=>"0", "maintenance_from"=>"0",
"ipmi_errors_from"=>"0", "snmp_errors_from"=>"0", "ipmi_error"=>"",
"snmp_error"=>"", "jmx_disable_until"=>"0", "jmx_available"=>"0",
"jmx_errors_from"=>"0", "jmx_error"=>"",
"name"=>"ip-10-10-111-11.ec2.internal", "flags"=>"0", "templateid"=>"0"}
I didnot find any relation between hosts & hostgroups.
I got it working in the below manner
host_grps = zbx.query(
:method => "host.get",
:params => {
"output" => "extend",
"groupids" => [14]
}
)
puts host_grps
Its gives host groups in list of hashmap.
A simple JSON request for method host.get with the following parameters gives me information for all hosts that belong to the specified groups:
{ "output" : "extend", "groupids": [ "4", "12" ] }
Please consult host.get API documentation for more information.

How to implement badgeville in ruby on rails

Can anyone tell me how to implement badgeville in ruby on rails?
EDIT:
how can I apply this on ruby on rails?
https://github.com/badgeville/badgeville-ruby
I'm not sure how this particular gem work, but normally it should work with following steps
create a file called 'badgeville.rb' inside app/config/initializers
add the following code to badgeville.rb
BadgevilleBerlin::Config.conf(
:host_name => "http://example.com",
:api_key => MY_API_KEY)
restart
then you should be able to use the following commands inside your
controllers/ models
Ex:
new_site = BadgevilleBerlin::Site.new(
:name => "My Website",
:url => "mydomain.com",
:network_id => MY_NETWORK_ID )
success = new_site.save

Rails / Paperclip attaching via command line

I have a bunch of jpeg files in a folder on my server, and I'm attempting to attach them to their corresponding Property instances through a rake task.
property.rb has the following code:
has_attached_file :temp_photo,
:styles => PropertyImage::STYLES,
:url => "/assets/:class/:attachment/:id_partition/:style_:basename.:extension",
:path => "#{Rails.root}/public/assets/:class/:attachment/:id_partition/:style_:basename.:extension"
I use paperclip on other models, and there are no issues whatsoever, but I get a problem when I attempt the following:
p = Property.find(id)
file = File.open(temp_file_path)
p.temp_photo = file
p.save
# => false
file.close
p.errors
# => "/tmp/stream20110524-1126-1cunv0y-0.jpg is not recognized by the 'identify' command."
The file definitely exists, and I've tried changing the permissions. Restarting the server doesn't help. The problem seems to be with using the command line, as the normal form / HTTP approach works fine. This is only a temporary set-up, so I'm looking for a working way to import a batch of files into my rails app paperclip model.
Any suggestions?
path = 'target_file_path'
attach_name = 'temp_photo'
p = Property.find(id)
attach = Paperclip::Attachment.new(attach_name, p, p.class.attachment_definitions[attach_name.to_suym])
file = File.open(path)
attach.assign file
attach.save
file.close

ArgumentError on application requests

I've written a basic Rails 3 application that shows a form and an upload form on specific URLs. It was all working fine yesterday, but now I'm running into several problems that require fixing. I'll try to describe each problem as best as I can. The reason i'm combining them, is because I feel they're all related and preventing me from finishing my task.
1. Cannot run the application in development mode
For some unknown reason, I cannot get the application to run in development mode. Currently i've overwritten the production.rb file from the environment with the settings from the development environment to get actuall stacktraces.
I've added the RailsEnv production setting to my VirtualHost setting in apache2, but it seems to make no difference. Nor does settings ENV variable to production.
2. ArgumentError on all calls
Whatever call I seem to make, results in this error message. The logfile tells me the following:
Started GET "/" for 192.168.33.82 at
Thu Apr 07 00:54:48 -0700 2011
ArgumentError (wrong number of
arguments (1 for 0)):
Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.0ms) Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(4.1ms) Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb
within rescues/layout (8.4ms)
This means nothing to me really. I have no clue what's going wrong. I currently have only one controller which looks like this:
class SearchEngineController < ApplicationController
def upload
end
def search
#rows = nil
end
# This function will receive the query string from the search form and perform a search on the
# F.I.S.E index to find any matching results
def query
index = Ferret::Index::Index.new :path => "/public/F.I.S.E", :default_field => 'content'
#rows = Array.New
index.search_each "content|title:#{params[:query]}" do |id,score, title|
#rows << {:id => id, :score => score, :title => title}
end
render :search
end
# This function will receive the file uploaded by the user and process it into the
# F.I.S.E for searching on keywords and synonims
def process
index = Ferret::Index::Index.new :path => "public/F.I.S.E", :default_field => 'content'
file = File.open params[:file], "r"
xml = REXML::Document.new file
filename = params[:file]
title = xml.root.elements['//body/title/text()']
content = xml.root.elements['normalize-space(//body)']
index << { :filename => filename, :title => title, :content => content}
file.close
FileUtils.rm file
end
end
The routing of my application has the following setup: Again this is all pretty basic and probably can be done better.
Roularta::Application.routes.draw do
# define all the url paths we support
match '/upload' => 'search_engine#upload', :via => :get
match '/process' => 'search_engine#process', :via => :post
# redirect the root of the application to the search page
root :to => 'search_engine#search'
# redirect all incoming requests to the query view of the search engine
match '/:controller(/:action(/:id))' => 'search_engine#search'
end
If anyone can spot what's wrong and why this application is failing, please let me know. If needed I can edit this awnser and include additional files that might be required to solve this problem.
EDIT
i've managed to get further by renaming one of the functions on the controller. I renamed search into create and now I'm getting back HAML errors. Perhaps I used a keyword...?
woot, finally found the solutions....
Seems I used keywords to define my actions, and Rails didn't like this. This solved issue 2.
Issue 1 got solved by adding Rails.env= 'development' to the environment.rb file

Resources