capistrano run_locally wrong number of arguments - capistrano3

after :finishing, :send_rollbar_notice do
on roles(:web) do
access_token = 'special_key_goes_here'
environment = fetch(:environment)
local_username = `whoami`.strip
revision = `git log -n 1 --pretty=format:"%H"`
curl_command = %{curl https://api.rollbar.com/api/1/deploy/ -F 'access_token=#{access_token}' -F 'environment=#{environment}' -F 'revision=#{revision}' -F 'local_username=#{local_username}'}
puts curl_command
output = run_locally "#{curl_command}"
puts output
end
end
Can someone help me figure out why I'm getting the follow error after I run the cap deploy command?
ArgumentError: wrong number of arguments (1 for 0)
Any help is much appreciated!

Got it! :)
Replace on roles(:web) do with run_locally do

Related

shell mock --define from array: ERROR: Bad option for '--define' ("dist). Use --define 'macro expr'

I am currently writing a script which should make it more easy for me to build some RPMs using mock.
The plan is to make it possible to add values for the mock (and therefor rpmbuild) --define parameter.
The error I get if I add such a define value is
ERROR: Bad option for '--define' ("dist). Use --define 'macro expr'
When I execute the script with as simple as ./test.sh --define "dist .el7" the "debug" output is as follows:
/usr/bin/mock --init -r epel-7-x86_64 --define "dist .el7"
If I copy this and execute it in the shell directly it is actually working. Does anybody have an idea why this is the case?
My script can be cut down to the following:
#!/bin/sh
set -e
set -u
set -o pipefail
C_MOCK="/usr/bin/mock"
MOCK_DEFINES=()
_add_mock_define() {
#_check_parameters_count_strict 1 ${#}
local MOCK_DEFINE="${1}"
MOCK_DEFINES+=("${MOCK_DEFINE}")
}
_print_mock_defines_parameter() {
if [ ${#MOCK_DEFINES[#]} -eq 0 ]; then
return 0
fi
printf -- "--define \"%s\" " "${MOCK_DEFINES[#]}"
}
_mock_init() {
local MOCK_DEFINES_STRING="$(_print_mock_defines_parameter)"
local MOCK_PARAMS="--init"
MOCK_PARAMS="${MOCK_PARAMS} -r epel-7-x86_64"
[ ! "${#MOCK_DEFINES_STRING}" -eq 0 ] && MOCK_PARAMS="${MOCK_PARAMS} ${MOCK_DEFINES_STRING}"
echo "${C_MOCK} ${MOCK_PARAMS}"
${C_MOCK} ${MOCK_PARAMS}
local RC=${?}
if [ ${RC} -ne 0 ]; then
_exit_error "Error while mock initializing ..." ${RC}
fi
}
while (( ${#} )); do
case "${1}" in
-s|--define)
shift 1
_add_mock_define "${1}"
;;
esac
shift 1
done
_mock_init
exit 0
After asking this question a coworker I was pointed to this question on unix stackexchange: Unix Stackexchange question
The way this problem was solved can be broken down to following lines:
DEFINES=()
DEFINES+=(--define "dist .el7")
DEFINES+=(--define "foo bar")
/usr/bin/mock --init -r epel-7-x86_64 "${DEFINES[#]}"
Just in case somebody else stumbles upon this kind of issue.

shell script : if array value was greater than a number then run a command

i have a a files containing usernames and users sent count mail per line . for example (dont know how many line have ) :
info.txt >
500 example1
40 example2
20 example3
....
..
.
if the number was greater than X , i want to run commands containing the user name and act on user .
getArray() {
users=() # Create array
while IFS= read -r line # Read a line
do
users+=("$line") # Append line to the array
done < "$1"
}
getArray "/root/.myscripts/spam1/info.txt"
# i know this part is incorrect and need help here :
if [ "${users[1$]}" -gt "50" ]
then
echo "${users[2$] has sent ${users[1$]} emails"
fi
please Help
Thanks
Not knowing how many lines of input you have is no reason to use an array. Indeed, it is generally more useful if you assume your input is infinite (an input stream), so reading into an array is impossible. Just read each line and take action if necessary:
#!/bin/sh
while read -r count user; do
if test "$count" -gt 50; then
echo "$user has sent $count emails"
fi
done < /root/.myscripts/spam1/info.txt

Is it possible for sshkit capture to not error when the command executed returns nothing

What I'm trying to achieve is a capistrano3 task that does a log file grep on all servers - this would save a lot of time as we have a lot of servers so doing it manually or even scripted but sequentially takes ages.
I have a rough at the edges task that actually works except when one of the servers returns nothing for the grep. In this case the whole command falls over.
Hence wondering if there is a way to set capture to accept empty returns.
namespace :admin do
task :log_grep, :command, :file do |t,args|
command = args[:command] || 'ask for a command'
file = args[:file] || 'log_grep_results'
outs = {}
on roles(:app), in: :parallel do
outs[host.hostname] = capture(:zgrep, "#{command}")
end
File.open(file, 'w') do |fh|
outs.each do |host,out|
fh.write(out)
end
end
end
end
Should anyone else come to this question, here's solution - raise_on_non_zero_exit: false
i wanted:
resp = capture %([ -f /var/run/xxx/xxx.pid ] && echo "ok")
error:
SSHKit::Command::Failed: [ -f /var/run/xxx/xxx.pid ] && echo "ok" exit status: 1
[ -f /var/run/xxx/xxx.pid ] && echo "ok" stdout: Nothing written
[ -f /var/run/xxx/xxx.pid ] && echo "ok" stderr: Nothing written
solution:
resp = capture %([ -f /var/run/xxx/xxx.pid ] && echo "ok"), raise_on_non_zero_exit: false
# resp => ""
So the work around I did was to start adding what I'm calling Capistrano utility scripts in the repo. Then capistrano runs these scripts. All the scripts are is a wrapper around a grep and some logic to output something if the return is empty.
Capistrano code:
namespace :utils do
task :log_grep, :str, :file, :save_to do |t,args|
command_args = "#{args[:str]} #{args[:file]}"
outs = {}
on roles(:app), in: :parallel do
outs[host.hostname] = capture(:ruby, "#{fetch(:deploy_to)}/current/bin/log_grep.rb #{args[:str]} #{args[:file]}")
end
file = args[:save_to]
file ||= 'log_grep_output'
File.open(file, 'w') do |fh|
outs.each do |host,out|
s = "#{host} -- #{out}\n"
fh.write(s)
end
end
end
end
Ruby script log_grep.rb:
a = `zgrep #{ARGV[0]} #{ARGV[1]}`
if a.empty?
puts 'Nothing Found'
else
puts a
end

Monkeyrunner throwing "ShellCommandUnrespo​nsiveException" - any work around?

I am facing some issues in device.shell('ping -c 2 192.168.1.1') inside a monkeyrunner script.
Its throwing
ShellCommandUnrespo‌​nsiveException
[main] [com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.ShellCommandUnrespo‌​nsiveException
while (count<1000) :
device.shell('dmesg -c')
print '****swithing OFF wifi in loop NO-',count
device.touch(400,155,MonkeyDevice.DOWN_AND_UP)
time.sleep(10)
print '****switching ON wifi in loop NO-',count
device.touch(400,155,MonkeyDevice.DOWN_AND_UP)
time.sleep(25)
fd=open('pingstats.txt','a+b')
fd.write('***Loop-%i \n************\n%s\n****************\n' % (int(count),ping))
ping = device.shell('ping -c 2 192.168.1.1')
status=re.search('unreachable',ping)
if status:
dmesg=device.shell('dmesg')
fd.write(logcat)
fd.close()
count = count + 1
Please see above script. How can I fix this?
your ping waits to long
add a -t
start with -t 1
Just add -t . An example below works perfectly !
device.shell('pm enable packageName -t 15')

Error while issuing ping command inside monkeyrunner script

i am facing some issues in device.shell('ping -c 2 192.168.1.1') inside a monkeyrunner script.
Its throwing below error:-
120202 20:12:17.192:S [main] [com.android.chimpchat.adb.AdbChimpDevice] Error executing command: ping -c 2 192.168.1.1
while (count<1000) :
device.shell('dmesg -c')
print '****swithing OFF wifi in loop NO-',count
device.touch(400,155,MonkeyDevice.DOWN_AND_UP)
time.sleep(10)
print '****switching ON wifi in loop NO-',count
device.touch(400,155,MonkeyDevice.DOWN_AND_UP)
time.sleep(25)
fd=open('pingstats.txt','a+b')
fd.write('***Loop-%i \n************\n%s\n****************\n' % (int(count),ping))
ping = device.shell('ping -c 2 192.168.1.1')
status=re.search('unreachable',ping)
if status:
dmesg=device.shell('dmesg')
fd.write(logcat)
fd.close()
count = count + 1
please see above script...
Someone please help....

Resources