Notify from an Exec only if the exec was succesfull - puppet

I am currently learning puppet, and I am attempting to have an exec that will notify another exec but only if the first exec is succsfull. However currently I am struggling to figure out how to do this. Right now I have:
exec { 'first command':
command => "some command"
path => '/usr/bin',
notify => Exec["second command"],
}
exec { 'second command':
command => "run another command if first command was succesfull",
path => '/usr/bin',
refreshonly => true,
}
How can this be modified so that the second command is only run if the first command is successful as currently it is run no matter what after the first command?

Related

Node.js - exec shell commands with .bashrc initialized

I'm writing a small utility tool for development to sync files over ssh. Normally I use ssh-agent set up in .bashrc file to connect to my dev server easily. I'd like to use exec in the script, but calling ssh-agent, every time I make a request sounds a bit inoptimal.
Is there a way I could execute the agent code once, and then have it working for all subsequent ssh requests I make? E.g. to spawn a shell process like a terminal emulator, and then use that process to execute a command, rather than invoking a new shell with each command.
The reason I want to do this, is I don't want to store the password in a config file.
You can create one ssh process, and then execute other commands using same process. Here is an example how to use it for bash. I'm creating a new bash shell and execugte the command ls -la and exit you can execute other commands.
const cp = require("child_process")
class MyShell {
constructor(command) {
this._spawned = cp.spawn(command, {
stdio: ["pipe", "pipe", "inherit"],
})
}
execute(command, callback) {
this._spawned.stdin.write(command + "\n")
this._spawned.stdout.on("data", (chunk) => {
if (callback) {
callback(chunk.toString())
}
})
}
}
var myShell = new MyShell("bash")
myShell.execute("ls -la", (result) => {
console.log(result)
})
myShell.execute("exit")

Puppet: how many times an exec block with creates run?

I have the below two exec resources and would like the exec resource run the script to run whenever the file /var/lib/my-file is not present. I'm wondering what would happen if the file never gets created. Would the exec resource check if file exists run forever in a loop until it gets created?
exec { 'run the script':
command => "python my-script.py",
path => '/bin:/usr/bin:/usr/local/bin',
timeout => 900,
subscribe => File["my-settings.yaml"],
refreshonly => true,
}
exec { 'check if file exists':
command => 'true',
path => '/bin:/usr/bin:/usr/local/bin',
creates => '/var/lib/my-file',
notify => Exec['run the script']
}
A resource is only applied once per catalog application, which occurs once per catalog compilation per node. You can verify this for yourself by trying it out.
If the Python script fails to create the file, the resource would simply be applied again during the next catalog application. Otherwise, idempotence prevails and the resource is not applied because the file already exists.
Additionally, you should simplify your resources into:
exec { 'run the script':
command => 'python my-script.py',
path => '/bin:/usr/bin:/usr/local/bin',
timeout => 900,
creates => '/var/lib/my-file',
subscribe => File["my-settings.yaml"],
refreshonly => true,
}
This is functionally the same as what you have in your question and is more efficient and easier to read.

Puppet execute command only if file does not exist

I am trying to use the following code which should perform some action only if a certain file does not exist. If the file exists, it should skip that task. I cannot get it to work.
exec {"is_file_there":
command => "true",
path => ["/usr/bin","/usr/sbin","/bin"],
unless => "test -e /tmp/some_file",
}
somecommand {
..
..
require => Exec['is_file_there'],
}
As an example, I tried using file but even that did not work. This is just an example for purpose of demonstrating the issue.
exec {"is_file_there":
command => "true",
path => ["/usr/bin","/usr/sbin","/bin"],
unless => "test -e /tmp/some_file",
}
file {'/tmp/success'
ensure => present,
require => Exec['is_file_there'],
}
EDIT : I am trying to create a dependency between another resource and exec. The other resource does not allow onlyif. The example about file is just that an example
According to https://puppet.com/docs/puppet/7/types/exec.html#exec-attribute-creates the "creates" option in "exec" will only run "command" if the file doesn't exist:
exec {"is_file_there":
command => "some_action_command",
path => ["/usr/bin","/usr/sbin","/bin"],
creates => "/tmp/some_file",
}
You could try something like this:
exec {"Some title":
command => "Your command",
path => ["/usr/bin","/usr/sbin","/bin"],
unless => "test -e /tmp/some_file",
}
So, your command is going to execute unless the result of test -e /tmp/some_file is success (exit code 0), that is, the file exists.

Run block of bash / shell in node's spawn

I'm writing a command line utility, and I need stdout to write to TTY or use {stdio: 'inherit'} I've been getting by with exec but it's not going to cut it. I need way for a spawn process to execute the following echo commands below. I know that spawn spins up a child process with a given command, and you pass in arguments, but I need it to just take a line-separated string of commands like this. This is what I'm currently feeding to exec. Is this possible?
const spawn = require('child_process').spawn
const child = spawn(`
echo "alpha"
echo "beta"
`)
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`)
});
child.stderr.on('data', (data) => {
console.log(`stderr: ${data}`)
});
child.on('close', (code) => {
console.log(`child process exited with code ${code}`)
});
spawn() does not involve a shell, so in order to have it execute shell commands, you must invoke the shell executable explicitly and pass the shell command(s) as an argument:
const child = spawn('/bin/sh', [ '-c', `
echo "alpha"
echo "beta"
` ])
Note I've used /bin/sh rather than /bin/bash in an effort to make your command run on a wider array of [Unix-like] platforms.
All major POSIX-like shells accept a command string via the -c option.

How to install and run a script in Puppet

I am new to Puppet..I am trying to install a shell script and exceute it using Puppet. The shell script after running creates another conf file and places in a specific location /usr/local/conf/app.conf. How can I write a puppet code to execute this script and then take the output file and scp it to another server (in my case its the webserver). Can someone please help.
Let's assume you have developed a module named webconfig and your puppet config dir is /etc/puppet.
You would need to store your shell script as /etc/puppet/modules/webconfig/files/script.sh
Your puppet code would partially look like this:
file { '/path/to/script.sh':
ensure => present,
source => 'puppet:///modules/webconfig/script.sh',
mode => '0644',
owner => 'root',
group => 'root',
}
->
exec { 'Generate the config':
command => '/path/to/script.sh',
cwd => '/path/to',
user => 'root',
}
->
exec { 'SCP the config':
command => 'scp /usr/local/conf/app.conf user#remote-server:',
cwd => '/path/to',
user => 'root',
}

Resources