Echo messages from a script run by Puppet - bash

I am writing a bash script that will be run by puppet by an Exec resource (I know. It is not ideal). I would like to know if is possible create debug messages in the script. Normally in Bash I use echo and in a manifest I use notify. But echo seems not work when the script is executed as an Exec resource in Puppet.

Check the documentation here for the logoutput attribute: https://docs.puppet.com/puppet/latest/reference/types/exec.html#exec-attribute-logoutput
You can always see the output from your echo in the script during Puppet agent execution with logoutput set to true.
For example:
exec { '/bin/sh script.sh': logoutput => true }

Related

Is it possible to run commandLine in gradle with bash -c?

I have a task in a build.gradle file, in which I'd like to run this command:
(export ENV_VAR=/my/path ; /path/to/build.sh)
I tried running this in gradle:
task myTask {
doLast {
exec {
commandLine ['bash', '-c', '"(export ENV_VAR=/my/path ; /path/to/build.sh)"']
}
}
}
Unfortunately, I have an error that says
Successfully started process 'command 'bash''
bash: (export ENV_VAR=/my/path ; /path/to/build.sh): No such file or directory
Now I'm sure the file exists and the specified paths are correct. Running this command manually in the terminal works.
Is there something in gradle that makes a bash -c like this not work? I can't think of another way to make an export like this otherwise.
Try without the extra quotes:
commandLine ['bash', '-c', '(export ENV_VAR=/my/path ; /path/to/build.sh)']
When you run that in the command line, your shell needs the quotes to pass to the command (which happens to be bash) as a single argument, but gradle is already doing that with that syntax, so bash is receiving literally one argument "(export ENV_VAR=/my/path ; /path/to/build.sh)" and since it does not recognize this as internal syntax, tries to run a command with this name.

Jenkins build passes when the shell script has execution errors in it

I have a shell script that executes multiple sql files that updates to the database. I am calling the shell script from jenkins- build- execute shell. The jenkins console shows success at all times irrespective of the errors from the sql files. I want Jenkins to fail the build, if there is an error or any of the sql file failed executing and send the console output to the developer, if fails.
I tried echo $? in the shell script but it shows 0.
#!/bin/bash
walk_dir () {
shopt -s nullglob dotglob
for pathname in "$1"/*; do
if [ -d "$pathname" ]; then
walk_dir "$pathname"
else
case "$pathname" in
*.sql|*.SQL)
printf '%s\n Executing SQL File:' "$pathname"
sudo -u postgres psql <DBName> -f $pathname
rm $pathname
esac
fi
done
}
DOWNLOADING_DIR=/home/jenkins/DB/
walk_dir "$DOWNLOADING_DIR"
Jenkins Console results
ALTER TABLE
ERROR: cannot change return type of existing
DETAIL: Row type defined by OUT parameters is different.
CREATE FUNCTION
ALTER FUNCTION
CREATE FUNCTION
ALTER FUNCTION
Finished: SUCCESS
Expected Results: Failed from Jenkins (if any of the sql files failed executing from shell script) but it is showing as passed in Jenkins
Thanks for all the inputs. I was able to fix this issue. I installed 'log parser plugin' in Jenkins which will parse the keywords like /Error/ in the console output and make the build to fail.
This S/O answer will probably address your scenario: Automatic exit from bash shell script on error
This duplicate answer also provides useful guidance:Stop on first error [duplicate]
Essentially use set -e or #!/bin/bash -e.
If you don't trap every potential error, then the next step in the script will execute and the return code will be that of the last command in the script.
Direct link to www.davidpashley.com - Writing Robust Bash Shell Scripts
** This also assumes any external commands (eg: psql) also properly trap and return status codes.
Below code may help you. This is how i sorted the issue with mine.
Instead of sudo -u postgres psql <DBName> -f $pathname
Use below code:
OUTPUT=$(psql -U postgres -d <DBName> -c "\i $pathname;")
echo $OUTPUT | grep ERROR
if [[ $? -eq 0 ]]
then
echo "Error while running sql file $pathname"
exit 2
else
echo "$pathname - SQL file Executed, successfully"
fi
Jenkins will not provide you the SQL files error code. Jenkins just checks if your shell script is executed or not and based on that it will return a status code which generally is zero as it is executing the shell script successfully.

Jenkins fails with Execute shell script

I have my bash script in ${JENKINS_HOME}/scripts/convertSubt.sh
My job has build step Execute shell:
However after I run job it fails:
The error message (i.e. the 0: part) suggests, that there is an error while executing the script.
You could run the script with
sh -x convertSubt.sh
For the safe side, you could also do a
ls -l convertSubt.sh
file convertSubt.sh
before you run it.
make sure that the script exist with ls
no need to sh , just ./convertSubs.sh ( make sure you have run permissions)

Chef run sh script

I have a problem trying to run shell script via Chef (with docker-provisioning).
This is how I try to execute my script:
bash 'shell_try' do
user "root"
run = "#{some_path_to_script}/my_script.sh some_params"
code " #{run} > stdout.txt 2> stderr.txt"
end
(note that this script should run another scripts, processes and write logs)
Here's no errors in the output, but when I log into machine and run ps aux process isn't running.
I guess something wrong with permissions (or env variables), because when I try the same command manually - it works.
A bash resource just runs the provided script text directly, if you wanted to run a long-running process generally you would set up an Upstart or systemd service and use the service resource to start it.
Finally find a solution (thanks to #coderanger) -
Install supervisor:
Download supervisor cookbook
Add:
include_recipe 'supervisor::default'
Add my service to supervisor:
supervisor_service "name" do
action :enable
#action :start
command '/path/script.sh start'
end
Run supervisor service
All done!
Please see the Chef documentation for your resource: https://docs.chef.io/resource_bash.html. The bash resource does not support a run attribute. Text of the code attribute is run as a bash script. The default action is to run the script unless told otherwise by the resource.
bash 'shell_try' do
user "root"
code " #{run} > stdout.txt 2> stderr.txt"
action :run
end
The code attribute is written to a temporary file where it is then run using the attributes specified in the resource.
The line run = "#{some_path_to_script}/my_script.sh some_params" at this point does nothing.

Calling Puppet from bash script

I'm trying to call puppet from a bash script and whilst it works, it causes my script to end prematurely.
#!/bin/bash
...
function runPuppetLocally()
{
echo "...running Puppet locally"
exec puppet agent --test
echo "Puppet complete"
}
runPuppetLocally
I presume Puppet is issuing an exit or something similar which causes my script to end. Is there a means by which I can call it without it terminating my script?
Why do you use exec? Read help exec:
Replace the shell with the given command.
Your script is replaced with the puppet. If you do not want it to replace your shell, call it normally, i.e.
puppet agent --test

Resources