Push to Git using Rugged - ruby-on-rails

I worked in a script to push to git using a script, so I choose Rugged to do that, my probleme when I try to push to repo, it gives me error, Can you help me ?
require 'rugged'
git_email = 'ettaheri.nizar#gmail.com'
git_name = 'Nizar'
repo_name = '/Users/euphor/Desktop/test/testignore1'
repo = Rugged::Repository.new('/Users/euphor/Desktop/test/testignore1')
puts "1"
index = repo.index
puts "3"
oid = repo.write("This is a blob.", :blob)
index.add(:path => "testignore1", :oid => oid, :mode => 0100644)
puts "4"
options = {}
options[:tree] = index.write_tree(repo)
puts "5"
options[:author] = { :email => git_email, :name => git_name, :time => Time.now }
options[:committer] = { :email => git_email, :name => 'Test Author', :time => Time.now }
puts "6"
options[:message] ||= "Making a commit via Rugged!"
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
options[:update_ref] = 'HEAD'
puts "7"
Rugged::Commit.create(repo, options)
puts "8"
**repo.push 'origin'** # this is my error
puts "Done"
the message of my error is :
/Library/Ruby/Gems/2.0.0/gems/rugged-0.23.0/lib/rugged/repository.rb:224:in
push': Unsupported URL protocol (Rugged::NetworkError) from
/Library/Ruby/Gems/2.0.0/gems/rugged-0.23.0/lib/rugged/repository.rb:224:in
push' from vips.rb:43:in `'

Looks like you have libgit2 (backend for Rugged) not installed properly. Most convenient packaging solution for Mac OS if Homebrew, and you need it to install additional libraries.
Install Homebrew
Follow any on-screen instructions after executing this line:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install openssh
brew install openssh
Reinstall rugged gem
gem uninstall rugged
gem install rugged
Now your snippet should run fine.

Related

Brakeman not skipping Gemfile.lock with --skip-files param

I'm adding Brakeman to a Rails product but I'm running into an issue. I want it to ignore my Gemfile and Gemfile.lock but when I run it with a command like
brakeman --skip-files Gemfile.lock,Gemfile
it's still touching the files. We use other systems to monitor our gems, but is it not possible to ignore the gem files completely? I can use a brakeman.ignore file of course but would prefer not to. Thanks for any assistance.
I believe this is the check to which you are referring:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/scanner.rb#L39-L40
Brakeman.notify "Processing gems..."
process_gems
The process_gems function is defined here:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/scanner.rb#L131-L152
#Process Gemfile
def process_gems
gem_files = {}
if #app_tree.exists? "Gemfile"
gem_files[:gemfile] = { :src => parse_ruby(#app_tree.read("Gemfile")), :file => "Gemfile" }
elsif #app_tree.exists? "gems.rb"
gem_files[:gemfile] = { :src => parse_ruby(#app_tree.read("gems.rb")), :file => "gems.rb" }
end
if #app_tree.exists? "Gemfile.lock"
gem_files[:gemlock] = { :src => #app_tree.read("Gemfile.lock"), :file => "Gemfile.lock" }
elsif #app_tree.exists? "gems.locked"
gem_files[:gemlock] = { :src => #app_tree.read("gems.locked"), :file => "gems.locked" }
end
if gem_files[:gemfile] or gem_files[:gemlock]
#processor.process_gems gem_files
end
rescue => e
Brakeman.notify "[Notice] Error while processing Gemfile."
tracker.error e.exception(e.message + "\nWhile processing Gemfile"), e.backtrace
end
The AppTree::exists? function is defined here:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/app_tree.rb#L82-L84
def exists?(path)
File.exist?(File.join(#root, path))
end
The GemProcessor::process_gems function is defined here:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/processors/gem_processor.rb#L11
...lots of code...
I don't see any code that would skip this functionality if a certain switch is provided to brakeman. It also looks like the AppTree::exists? function does not take into account if a file was provided to the --skip-files option.
Unfortunately, I believe the current answer is that you can not ignore the gem files completely.
You could create a PR to do what you want and see if the Brakeman team includes it in the next build:
https://brakemanscanner.org/docs/contributing/
Let us know if you discover a way to solve your problem.

Vagrant SSH command responded with a non-zero exit status

I'm trying to install docker on Ubuntu 18.04-VM (via vagrant) using the setup below. Is there any way I can make docker installation succeed on vagrant ubuntu 18.04 VM using the Vagrantfile? Note: I need to know how to apply the suggested solution into the Vagrantfile.
Vagrantfile:
servers=[
{
:hostname => "manager",
:ip => "192.168.2.1",
:box => "ubuntu/bionic64",
:ram => 2048,
:cpu => 4
},
{
:hostname => "worker-1",
:ip => "192.168.2.2",
:box => "ubuntu/bionic64",
:ram => 2048,
:cpu => 4
},
{
:hostname => "worker-2",
:ip => "192.168.2.3",
:box => "ubuntu/bionic64",
:ram => 2048,
:cpu => 4
}
]
Vagrant.configure(2) do |config|
servers.each do |machine|
config.vm.define machine[:hostname] do |node|
node.vm.box = machine[:box]
node.vm.hostname = machine[:hostname]
node.vm.network "private_network", ip: machine[:ip]
if machine[:hostname] == "manager"
node.vm.provision "docker",
images: ["ubuntu/bionic64"]
else
node.vm.provision "docker"
end
node.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
end
end
end
end
Dockerfile:
FROM ubuntu:18.04
RUN apt-get install -y python python-pip --no-install-recommends
RUN apt-get install vim -y
RUN apt update -y
ADD app /home/app/
WORKDIR /home/app
EXPOSE 8080
Exception/Error Output Message:
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
curl -sSL https://get.docker.com/ | sh
Stdout from the command:
Executing docker install script, commit: 02d7c3c
Stderr from the command:
Either your platform is not easily detectable or is not supported by this
installer script.
Please visit the following URL for more detailed installation instructions:
https://docs.docker.com/engine/installation/
I finally figured it out spawning virtual servers with Ubuntu 18, using Vagrant. The link has all the simple instructions: Spawn virtual servers on the fly

Remote Execute Command using SSH from Rails Application

I have a Rails Application from which I will call one Ruby Definition in the controller, like this.
ssh root#host "uptime" >> /tmp/output
When I doing this, only the /tmp/output is created but not the content.
When I running the same from simple Ruby script its working fine.
my controller definition
def chefclient1
`ssh root#host "uptime" >> /tmp/output`
#time = Time.now
end
my view
= link_to('Start uptime', host_chefclient1_path)
You can use net-ssh gem to access remote host via ssh fo a ruby app. Set your environment up to:
HOSTNAME = 'host'
USER = 'root'
PASS = 'password'
Add to your_helper.rb: something like:
def chefclient1
result = nil
Net::SSH.start( ENV[ 'HOSTNAME' ], ENV[ 'USER' ], :password => ENV[ 'PASS' ] ) do| ssh |
result = ssh.exec! 'uptime'
puts result
end
File.open( '/tmp/output', 'w' ) {| f | f.puts result }
end
And use the helper method as you with from a view.

run the shell script from rails application after login to the remote system

I want to run a shell script from my Rails application. The script is started on the remote server via the net-ssh Gem. Here is my code:
Net::SSH.start('localhost', 'user', :password => '1234', :port => port) do |session|
session.exec!("#{Rails.root}/script/myscript")
end
I have checked that the script is present in my local application. Script is taking about 1 hour for completion. I have two questions:
Is this the right way for doing this?
How can I run the script in the background?
The sample doc says that the simple, and quite proper way to run the Net::SSH session is the following:
HOST = '192.168.1.113'
USER = 'username'
PASS = 'password'
Net::SSH.start( HOST, USER, :password => PASS ) do| ssh |
result = ssh.exec! 'ls'
puts result
end
I recomment to pass at least password argument via shell environment to don't store it in the script plainly. Also you could use micro-optparse gem to pass other argument via command line. So it could be as follows:
require 'micro-optparse'
options = Parser.new do |p|
p.banner = "This is a fancy script, for usage see below"
p.version = "fancy script 0.0 alpha"
p.option :host, "set host", :default => 'localhost'
p.option :host, "set user", :default => ''
end.parse!
Net::SSH.start( options[ :host ], options[ :user ], :password => ENV[ 'PASSWORD' ] ) do| ssh |
result = ssh.exec! 'ls'
puts result
end
And run from command line:
$ bundle exec ./1.rb --host=111.ru --user=user
{:host=>"111.ru", :user=>"user"}
Of course the support for :port argument can be added in the same manner.
Use nohup or screen utitilies to run a script as a service in linux:
result = ssh.exec! "nohup #{Rails.root}/script/myscript"
Why shouldn't it be the right way?
To run a shell script in the background, append & to the command
For example:
session.exec!("#{Rails.root}/script/myscript&")

Nokogiri parsing different on server versus localhost

I'm getting some weird differences when running Nokogiri locally versus running it on my server. On my local machine the entire document seems to parse and be available but on the server I seem to get the doctype tab and some random comment tags.
To start off, to make sure it wasn't a problem with open-uri I checked it - the results are not exact but do contain the correct markup.
Local:
ruby-1.8.7-p352 :005 > s = open('http://www.pennstateind.com/store/PK2WAY.html')
=> #<File:/var/folders/G8/G8bsAGBk1o82Eyks3ZmFtq-+3Y6/-Tmp-/open-uri20120626-5891-10y2ncr-0>
ruby-1.8.7-p352 :006 > s.length
=> 88408
Server:
rb(main):008:0> s = open('http://www.pennstateind.com/store/PK2WAY.html')
=> #<File:/tmp/open-uri20120626-22167-1td2l72-0>
irb(main):009:0> s.length
=> 98184
When I run this on my local machine I get this:
ruby-1.8.7-p352 :003 > d = Nokogiri::HTML(open('http://www.pennstateind.com/store/PK2WAY.html'))
=> [ OUTPUT OMITTED FOR BREVITY - CAN SUPPLY ON REQUEST ]
ruby-1.8.7-p352 :004 > d.to_s.length
=> 85212
But when I run this on the server I get this:
rb(main):006:0> d = Nokogiri::HTML(open('http://www.pennstateind.com/store/PK2WAY.html'))
=> #<Nokogiri::HTML::Document:0x36620e14b580 name="document" children= [#<Nokogiri::XML::DTD:0x36620e14b1c0 name="html">, #<Nokogiri::XML::Comment:0x36620e14b170 " Open Graph Tags ">, #<Nokogiri::XML::Comment:0x36620e14a98c " Customer_Session_Verified: 0 ">]>
irb(main):007:0> d.to_s.length
=> 172
The only apparent gem difference is for the JS compiler - all other gems are the exact version between local and server:
Local => libv8 (3.3.10.4 x86-darwin-10)
Server => libv8 (3.3.10.4 x86_64-linux)
Any ideas how to figure out what is going on and/or fix this?
Update - to isolate where the problem actually was I pulled a file from the server and from localhost then ran them on each. The results below show that the problem definitely lies in Nokogiri - what the problem is I am still perplexed by...
Running locally:
# FILE ORIGINALLY PULLED FROM SERVER
ruby-1.8.7-p352 :015 > server_file = File.open("/Users/jmcdonald/Desktop/files/SERVER.txt", "r")
=> #<File:/Users/jmcdonald/Desktop/files/SERVER.txt>
ruby-1.8.7-p352 :016 > server_file.read.length
=> 93071
ruby-1.8.7-p352 :022 > Nokogiri::HTML(server_file).to_s.length
=> 98793
# FILE ORIGINALLY PULLED FROM LOCALHOST
=> #<File:/Users/jmcdonald/Desktop/files/LOCAL.txt>
ruby-1.8.7-p352 :018 > local_file.read.length
=> 89622
ruby-1.8.7-p352 :026 > Nokogiri::HTML(local_file).to_html.length
=> 94632
Running on server:
# FILE ORIGINALLY PULLED FROM SERVER
irb(main):001:0> sf = File.open('/home/charlest/public_html/files/nokogiri_issue/SERVER.txt', 'r')
=> #<File:/home/charlest/public_html/files/nokogiri_issue/SERVER.txt>
irb(main):002:0> sf.read.length
=> 93071
irb(main):004:0> Nokogiri::HTML(sf).to_s.length
=> 896 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< WRONG
# FILE ORIGINALLY PULLED FROM LOCALHOST
irb(main):008:0> lf = File.open('/home/charlest/public_html/files/nokogiri_issue/LOCAL.txt', 'r')
=> #<File:/home/charlest/public_html/files/nokogiri_issue/LOCAL.txt>
irb(main):009:0> lf.read.length
=> 89622
irb(main):011:0> Nokogiri::HTML(lf).to_s.length
=> 896 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< WRONG
It looks like your server and local environment are using different versions of libxml2. Older versions are known to have strange parsing bugs, so updating your server to the latest version you possibly can (or at least to the same version you're using for development) should fix you up.
There was also a bug with a shipped version of Nokogiri (I believe it affected 1.5.1) which affected the parsing in a some limited situations. I would suggest making sure your gems are updated. (gem update)
Try using File#read rather than File#open or make sure you're running lf.rewind before you try to parse w/ Nokogiri. The behavior you're seeing is most likely the result of your lf file handle being at the end of the file, which means Nokogiri is parsing an empty document.
> remote = File.open('./PK2WAY.html')
# => #<File:./PK2WAY.html>
> remote.read.length
# => 92978
> remote.read.length
# => 0
> Nokogiri::HTML(remote).to_s.length
# => 108
> remote.rewind
# => 0
> Nokogiri::HTML(remote).to_s.length
# => 93847

Resources