Skip to content

Commit 3abec36

Browse files
authored
Merge pull request puppetlabs#23 from tphoney/no_agent_here
(feature) task for linux services (no agent)
2 parents 3178aba + 1e40557 commit 3abec36

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
## Description
1313

14-
This module provides the service task. This task allows you to manage and inspect the state of services, including starting, stopping, enabling, and disabling services.
14+
This module provides services tasks, There are two kinds of tasks. The default task: that uses the puppet agent on the target node to manage and inspect the state of services. The linux task: that manipulates services on a linux derivative without a puppet agent installed on the target node.
15+
1516

1617
## Requirements
1718
This module is compatible with Puppet Enterprise and Puppet Bolt.
@@ -24,6 +25,8 @@ This module is compatible with Puppet Enterprise and Puppet Bolt.
2425

2526
To run a service task, use the task command, specifying the action and the name of the service.
2627

28+
### Default task
29+
2730
* With PE on the command line, run `puppet task run service action=<ACTION> name=<SERVICE_NAME>`.
2831
* With Bolt on the command line, run `bolt task run service action=<ACTION> name=<SERVICE_NAME>`.
2932

@@ -32,6 +35,16 @@ For example, to check the status of the Apache httpd service, run:
3235
* With PE, run `puppet task run service action=status name=httpd --nodes neptune`
3336
* With Bolt, run `bolt task run service action=status name=httpd --nodes neptune --modulepath ~/modules`
3437

38+
### Linux task
39+
40+
* With PE on the command line, run `puppet task run service::linux action=<ACTION> name=<SERVICE_NAME>`.
41+
* With Bolt on the command line, run `bolt task run service::linux action=<ACTION> name=<SERVICE_NAME>`.
42+
43+
For example, to check the status of the Apache httpd service, run:
44+
45+
* With PE, run `puppet task run service::linux action=status name=httpd --nodes neptune`
46+
* With Bolt, run `bolt task run service::linux action=status name=httpd --nodes neptune --modulepath ~/modules`
47+
3548
You can also run tasks in the PE console. See PE task documentation for complete information.
3649

3750
## Reference
@@ -45,4 +58,3 @@ For a complete list of services that are supported see the Puppet [services](htt
4558
To display help for the service task, run `puppet task show service`
4659

4760
To show help for the task CLI, run `puppet task run --help` or `bolt task run --help`
48-

spec/acceptance/linux_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# run a test task
2+
require 'spec_helper_acceptance'
3+
# bolt regexes
4+
# expect_multiple_regexes(result: result, regexes: [%r{"status":"(stopped|in_sync)"}, %r{Ran on 1 node}])
5+
# expect_multiple_regexes(result: result, regexes: [%r{"status":"stopped"}, %r{"enabled":"false"}, %r{Ran on 1 node}])
6+
7+
describe 'linux service task', unless: fact_on(default, 'osfamily') == 'windows' do
8+
package_to_use = ''
9+
before(:all) do
10+
if fact_on(default, 'osfamily') == 'RedHat' && fact_on(default, 'operatingsystemrelease') < '6'
11+
run_task(task_name: 'service::linux', params: 'action=stop name=syslog')
12+
end
13+
package_to_use = 'rsyslog'
14+
apply_manifest("package { \"#{package_to_use}\": ensure => present, }")
15+
end
16+
describe 'stop action' do
17+
it "stop #{package_to_use}" do
18+
result = run_task(task_name: 'service::linux', params: "action=stop name=#{package_to_use}")
19+
expect_multiple_regexes(result: result, regexes: [%r{status.*(stop)}, %r{#{task_summary_line}}])
20+
end
21+
end
22+
describe 'start action' do
23+
it "start #{package_to_use}" do
24+
result = run_task(task_name: 'service::linux', params: "action=start name=#{package_to_use}")
25+
expect_multiple_regexes(result: result, regexes: [%r{status.*(start)}, %r{#{task_summary_line}}])
26+
end
27+
end
28+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
HOSTS:
2+
ubuntu-14.04:
3+
roles:
4+
- agent
5+
- default
6+
platform: ubuntu-14.04-amd64
7+
box: puppetlabs/ubuntu-14.04-64-nocm
8+
hypervisor: vagrant
9+
CONFIG:
10+
type: foss

tasks/linux.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"description": "Manage the state of services (without a puppet agent)",
3+
"input_method": "environment",
4+
"parameters": {
5+
"action": {
6+
"description": "The operation (start, stop) to perform on the service",
7+
"type": "Enum[start, stop]"
8+
},
9+
"name": {
10+
"description": "The name of the service to operate on.",
11+
"type": "String[1]"
12+
}
13+
}
14+
}

tasks/linux.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
inux.sh: line 19: local: can only be used in a function
2+
#!/bin/bash
3+
4+
action="$PT_action"
5+
name="$PT_name"
6+
service_managers[0]="systemctl"
7+
service_managers[1]="service"
8+
service_managers[2]="initctl"
9+
10+
# example cli /opt/puppetlabs/puppet/bin/bolt task run service::linux action=stop name=ntp --nodes localhost --modulepath /etc/puppetlabs/code/modules --password puppet --user root
11+
12+
check_command_exists() {
13+
(which "$1") > /dev/null 2>&1
14+
command_exists=$?
15+
return $command_exists
16+
}
17+
18+
for service_manager in "${service_managers[@]}"
19+
do
20+
check_command_exists "$service_manager"
21+
command_exists=$?
22+
if [ $command_exists -eq 0 ]; then
23+
command_line="$service_manager $action $name"
24+
if [ $service_manager == "service" ]; then
25+
command_line="$service_manager $name $action"
26+
fi
27+
output=$($command_line 2>&1)
28+
status_from_command=$?
29+
# set up our status and exit code
30+
if [ $status_from_command -eq 0 ]; then
31+
echo "{ \"status\": \"$name $action\" }"
32+
exit 0
33+
else
34+
# initd is special, starting an already started service is an error
35+
if [[ $service_manager == "service" && "$output" == *"Job is already running"* ]]; then
36+
echo "{ \"status\": \"$name $action\" }"
37+
exit 0
38+
fi
39+
echo "{ \"status\": \"unable to run command '$command_line'\" }"
40+
exit $status_from_command
41+
fi
42+
fi
43+
done
44+
45+
echo "{ \"status\": \"No service managers found\" }"
46+
exit 255

0 commit comments

Comments
 (0)