When trying to run rspec spec/controllers/api_controller_spec.rb:406 --color command on linux machine. We already have redis server running at port 6379 but as below error Unable to load rspec spec_helper configuration & getting error:
Code:
REDIS_PID = "/var/run/redis.pid"
REDIS_CACHE_PATH = "tmp/cache/"
Dir.mkdir "#{Rails.root}/tmp" unless Dir.exists? "#{Rails.root}/tmp"
Dir.mkdir "#{Rails.root}/tmp/pids" unless Dir.exists? "#{Rails.root}/tmp/pids"
Dir.mkdir "#{Rails.root}/tmp/cache" unless Dir.exists? "#{Rails.root}/tmp/cache"
config.before(:suite) do
redis_options = {
"daemonize" => 'yes',
"pidfile" => 7528,
"port" => 6379,
"timeout" => 300,
"save 900" => 1,
"save 300" => 1,
"save 60" => 10000,
"dbfilename" => "dump.rdb",
"dir" => REDIS_CACHE_PATH,
"loglevel" => "debug",
"logfile" => "stdout",
"databases" => 16
}.map { |k, v| "#{k} #{v}" }.join("\n")
`echo '#{redis_options}' | redis-server -`
end
config.after(:suite) do
%x{
cat #{REDIS_PID} | xargs kill -QUIT
rm -f #{REDIS_CACHE_PATH}dump.rdb
}
end
Error:
------------
------------
------------
Finished searching in 0.09762763977050781 seconds.
Request#<ActionController::TestRequest:0x00000004139f58>
^[[?1;2c^[[?1;2c^[[?1;2c^[[?1;2c^[[?1;2c^[[?1;2cFcat: /var/run/redis.pid: No such file or directory
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]
------------
------------
------------
Restart the redis server redis-server stop/start. The redis.pid file generated and rspec is able to access the redis configuration during background processing.
Related
I have used the official puppet jenkins module. I have added jenkins jobs like this.
jenkins::job { 'test-build-job':
config => template("${templates}/test-build-job.xml.erb"),
}
when I run this command in puppet agent
puppet agent -tv
I get error like this:
Error: Failed to apply catalog: Validation of Exec[reload-jenkins] failed:
'java -jar /usr/lib/jenkins/jenkins-cli.jar -s http://localhost:8080
reload-configuration'
is not qualified and no path was specified. Please qualify the command or specify a path. (file: /etc/puppetlabs/code/environments/production/modules/jenkins/manifests/cli.pp, line: 42)
I'm getting error in cli.pp, and my cli.pp looks like
class jenkins::cli {
if $caller_module_name != $module_name {
fail("Use of private class ${name} by ${caller_module_name}")
}
$jar = "${jenkins::libdir}/jenkins-cli.jar"
$extract_jar = "jar -xf ${jenkins::libdir}/jenkins.war WEB-INF/jenkins-cli.jar"
$move_jar = "mv WEB-INF/jenkins-cli.jar ${jar}"
$remove_dir = 'rm -rf WEB-INF'
exec { 'jenkins-cli' :
command => "${extract_jar} && ${move_jar} && ${remove_dir}",
path => ['/bin', '/usr/bin'],
cwd => '/tmp',
creates => $jar,
require => Service['jenkins'],
}
file { $jar:
ensure => file,
require => Exec['jenkins-cli'],
}
$port = jenkins_port()
# The jenkins cli command with required parameter(s)
$cmd = "java -jar ${jar} -s http://localhost:${port}"
# Reload all Jenkins config from disk (only when notified)
exec { 'reload-jenkins':
command => "${cmd} reload-configuration",
tries => 10,
try_sleep => 2,
refreshonly => true,
require => File[$jar],
}
# Do a safe restart of Jenkins (only when notified)
exec { 'safe-restart-jenkins':
command => "${cmd} safe-restart && /bin/sleep 10",
tries => 10,
try_sleep => 2,
refreshonly => true,
require => File[$jar],
}
}
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
I am using net/ssh gem in Ruby.
By the following code I can enter into the server from my local machine. But I want to execute the commands on the server by entering as a ROOT.
Normally, I enter into the server as a ROOT by the command
sudo su -
Following is my code.
require 'rubygems'
require 'net/ssh'
list_of_servers = "servers.csv"
username="XYZ"
IO.readlines(list_of_servers).each do |line|
line.chomp!
server = line.split(',')[0]
password = line.split(',')[1]
puts "---- " + server
Net::SSH.start(server, username, :password => password,:verbose => Logger::DEBUG) do |ssh|
result=ssh.exec!("sudo su -")
puts result
end
end
Output I get after entering into server.
D, [2015-11-21T21:48:26.005576 #56654] DEBUG -- io[3fce29cc9548]: received packet nr 15 type 97 len 12
I, [2015-11-21T21:48:26.005628 #56654] INFO -- net.ssh.connection.session[3fce29ce9834]: channel_close: 0
D, [2015-11-21T21:48:26.005796 #56654] DEBUG -- io[3fce29cc9548]: queueing packet nr 11 type 97 len 28
sudo: no tty present and no askpass program specified
Note:
I can successfully log into the server from my local machine but I cannot run the ROOT command (sudo su -).
You have to start tty for that
scenario = []
scenario << ""
scenario << "su"
#session = Net::SSH.start(ip , user,
:password => pass,
:auth_methods => ['password'],
:timeout => 10
)
#session.open_channel do |channel|
channel.request_pty do |ch, success|
ch.send_channel_request("shell") do |shell, success|
shell.on_data do |c, data|
#output << data
end
scenario.each do |s|
cmd = "#{s}\r\n"
shell.send_data(cmd)
end
end
end
end
According to man sudo, if you specify -S before the command, sudo takes the password from stdin.
I tried this both on a terminal and from ruby with success:
(insert your password, and the command you wish to run in the generic example below.)
echo "password" | sudo -S "command"
works for me.
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.
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&")