diff --git a/jobs/README.md b/jobs/README.md new file mode 100644 index 00000000..c761b7d5 --- /dev/null +++ b/jobs/README.md @@ -0,0 +1,13 @@ +# Job Templates + +This directory contains job templates for the +[foreman_remote_execution](https://github.com/theforeman/foreman_remote_execution/) +plugin. + +## Contributing + +If you think a job is useful to include here, feel free to open an PR. +foreman_remote_execution 0.3.2 supports directly exporting your job template +into an appropriate format, with metadata in the first ERB comment. + +Thanks! diff --git a/jobs/ssh/package_action.erb b/jobs/ssh/package_action.erb new file mode 100644 index 00000000..cf601822 --- /dev/null +++ b/jobs/ssh/package_action.erb @@ -0,0 +1,65 @@ +<%# +kind: job_template +name: Package Action - SSH Default +job_category: Packages +description_format: "%{action} package(s) %{package}" +provider_type: SSH +template_inputs: +- name: pre_script + description: A script to run prior to the package action + input_type: user + required: false + advanced: true +- name: action + description: 'The package action: install, update, or remove' + input_type: user + required: true + options: "install\nupdate\nremove\ngroup install\ngroup remove" +- name: package + description: The name of the package, if any + input_type: user + required: false +- name: post_script + description: A script to run after the package action + input_type: user + required: false + advanced: true +%> + +<% + supported_families = ['Redhat', 'Debian'] + render_error(N_('Unsupported or no operating system found for this host.')) unless @host.operatingsystem && supported_families.include?(@host.operatingsystem.family) +%> + +# Helper function that exits with a particular message and code. +# +# Usage: +# exit_with_message "Could not do a thing" 2 +# +function exit_with_message() { + echo "${1}, exiting..." + exit $2 +} + +<% unless input("pre_script").blank? -%> + # Pre Script + <%= input("pre_script") %> + RETVAL=$? + [ $RETVAL -eq 0 ] || exit_with_message "Pre script failed" $RETVAL +<% end -%> + +# Action +<% if @host.operatingsystem.family == 'Redhat' -%> + yum -y <%= input("action") %> <%= input("package") %> +<% elsif @host.operatingsystem.family == 'Debian' -%> + apt-get -y <%= input("action") %> <%= input("package") %> +<% end -%> +RETVAL=$? +[ $RETVAL -eq 0 ] || exit_with_message "Package action failed" $RETVAL + +<% unless input("post_script").blank? -%> + # Post Script + <%= input("post_script") %> + RETVAL=$? + [ $RETVAL -eq 0 ] || exit_with_message "Post script failed" $RETVAL +<% end -%> diff --git a/jobs/ssh/power_action.erb b/jobs/ssh/power_action.erb new file mode 100644 index 00000000..36fd857b --- /dev/null +++ b/jobs/ssh/power_action.erb @@ -0,0 +1,22 @@ +<%# +kind: job_template +name: Power Action - SSH Default +job_category: Power +description_format: '%{action} host' +provider_type: SSH +template_inputs: +- name: action + description: Action to perform on the service + input_type: user + options: "restart\nshutdown" + required: true +%> + +echo <%= input('action') %> host && sleep 3 +<%= case input('action') + when 'restart' + 'reboot' + else + 'shutdown -h now' + end %> + diff --git a/jobs/ssh/puppet_run_once.erb b/jobs/ssh/puppet_run_once.erb new file mode 100644 index 00000000..ed905fba --- /dev/null +++ b/jobs/ssh/puppet_run_once.erb @@ -0,0 +1,13 @@ +<%# +kind: job_template +name: Puppet Run Once - SSH Default +job_category: Puppet +description_format: 'Run Puppet once with "%{puppet_options}"' +provider_type: SSH +template_inputs: +- name: puppet_options + description: Additional options to pass to puppet + input_type: user + required: false +%> +puppet agent --onetime --no-usecacheonfailure <%= input("puppet_options") %> diff --git a/jobs/ssh/run_command.erb b/jobs/ssh/run_command.erb new file mode 100644 index 00000000..ba6b536c --- /dev/null +++ b/jobs/ssh/run_command.erb @@ -0,0 +1,13 @@ +<%# +kind: job_template +name: Run Command - SSH Default +job_category: Commands +description_format: "Run %{command}" +provider_type: SSH +template_inputs: +- name: command + description: Command to run on the host + input_type: user + required: true +%> +<%= input("command") %> diff --git a/jobs/ssh/service_action.erb b/jobs/ssh/service_action.erb new file mode 100644 index 00000000..71abeec5 --- /dev/null +++ b/jobs/ssh/service_action.erb @@ -0,0 +1,22 @@ +<%# +kind: job_template +name: Service Action - SSH Default +job_category: Services +description_format: '%{action} service %{service}' +provider_type: SSH +template_inputs: +- name: action + description: Action to perform on the service + input_type: user + options: "restart\nstart\nstop\nstatus" + required: true +- name: service + description: Name of the service + input_type: user + required: true +%> +<% if @host.operatingsystem.family == "Redhat" && @host.operatingsystem.major.to_i > 6 %> +systemctl <%= input("action") %> <%= input("service") %> +<% else %> +service <%= input("service") %> <%= input("action") %> +<% end -%>