Scripting foreman-rake console commands

Problem:
We needed to iterate over a series of stuck tasks and destroy them

Expected outcome:
We wanted to script it to avoid tedious console sessions.

Foreman and Proxy versions:
foreman 1.24.2 katello 3.14.1

Foreman and Proxy plugin versions:

Distribution and version:
CentOS 7.7

This is the relevant line we devised. Hammer and awk were used to build the array which is iterated to run this command. While this worked perfectly, I am always looking for more elegant solutions.

eval "$(echo -e "foreman-rake console <<< 'ForemanTasks::Task.find(\"$id\").destroy'")"

Hello, just feed the standard input with your script:

echo "puts 'Hello world'" | foreman-rake console

That will not work with a shell variable within single quotes. That, of course, was our first try.

Hi,

since the single quotes are masked by the double quotes, it should indeed work (and does for me):

export TEST="Hello World"
echo "puts '$TEST'" | foreman-rake console

Rubocop not loaded.
Loading production environment (Rails 5.2.1)
Switch to inspect mode.
puts 'Hello World'
Hello World
nil

Works even with nested double quotes when masked by backslashes like in your initial post:

echo "puts '\"$TEST\"'" | foreman-rake console
Rubocop not loaded.
Loading production environment (Rails 5.2.1)
Switch to inspect mode.
puts '"Hello World"'
"Hello World"
nil

Regards

1 Like

Yes, I have done this multiple times and it works from the day one. You had a typo or something…

Another option to keep in mind is that the tasks plugin actually has a built in cleanup rake task. the manual, though a bit outdated, has details on the various options. I believe that this hasn’t changed in a while, but @tasks team can probably answer that. If the list of tasks you are deleting is something you can define using this task’s configuration, you can easily script it without having to deal with manually generating the list etc.

1 Like