mirror of https://github.com/ansible/ansible.git
Handle disabled service units (#69349)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>pull/69805/head
parent
2abaf320d7
commit
723a904f4e
@ -0,0 +1,11 @@
|
||||
[Unit]
|
||||
Description=Ansible Test Service
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/ansible_test_service "Test\nthat newlines in scripts\nwork"
|
||||
ExecReload=/bin/true
|
||||
Type=forking
|
||||
PIDFile=/var/run/ansible_test_service.pid
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# this is mostly based off of the code found here:
|
||||
# http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
import resource
|
||||
import signal
|
||||
import time
|
||||
|
||||
UMASK = 0
|
||||
WORKDIR = "/"
|
||||
MAXFD = 1024
|
||||
|
||||
if (hasattr(os, "devnull")):
|
||||
REDIRECT_TO = os.devnull
|
||||
else:
|
||||
REDIRECT_TO = "/dev/null"
|
||||
|
||||
|
||||
def createDaemon():
|
||||
try:
|
||||
pid = os.fork()
|
||||
except OSError as e:
|
||||
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
||||
|
||||
if (pid == 0):
|
||||
os.setsid()
|
||||
|
||||
try:
|
||||
pid = os.fork()
|
||||
except OSError as e:
|
||||
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
||||
|
||||
if (pid == 0):
|
||||
os.chdir(WORKDIR)
|
||||
os.umask(UMASK)
|
||||
else:
|
||||
f = open('/var/run/ansible_test_service.pid', 'w')
|
||||
f.write("%d\n" % pid)
|
||||
f.close()
|
||||
os._exit(0)
|
||||
else:
|
||||
os._exit(0)
|
||||
|
||||
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
|
||||
if (maxfd == resource.RLIM_INFINITY):
|
||||
maxfd = MAXFD
|
||||
|
||||
for fd in range(0, maxfd):
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError: # ERROR, fd wasn't open to begin with (ignored)
|
||||
pass
|
||||
|
||||
os.open(REDIRECT_TO, os.O_RDWR)
|
||||
os.dup2(0, 1)
|
||||
os.dup2(0, 2)
|
||||
|
||||
return (0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
||||
|
||||
retCode = createDaemon()
|
||||
|
||||
while True:
|
||||
time.sleep(1000)
|
@ -0,0 +1,32 @@
|
||||
- name: remove the systemd unit file
|
||||
file:
|
||||
path: /usr/lib/systemd/system/ansible_test.service
|
||||
state: absent
|
||||
register: remove_systemd_result
|
||||
|
||||
- name: assert that the systemd unit file was removed
|
||||
assert:
|
||||
that:
|
||||
- "remove_systemd_result.path == '/usr/lib/systemd/system/ansible_test.service'"
|
||||
- "remove_systemd_result.state == 'absent'"
|
||||
|
||||
- name: remove python systemd test script file
|
||||
file:
|
||||
path: /usr/sbin/ansible_test_service
|
||||
state: absent
|
||||
register: remove_systemd_binary_result
|
||||
|
||||
- name: assert that python systemd test script file was removed
|
||||
assert:
|
||||
that:
|
||||
- "remove_systemd_binary_result.path == '/usr/sbin/ansible_test_service'"
|
||||
- "remove_systemd_binary_result.state == 'absent'"
|
||||
|
||||
- name: make sure systemd is reloaded
|
||||
shell: systemctl daemon-reload
|
||||
register: restart_systemd_result
|
||||
|
||||
- name: assert that systemd was reloaded
|
||||
assert:
|
||||
that:
|
||||
- "restart_systemd_result.rc == 0"
|
@ -0,0 +1,26 @@
|
||||
- name: install the test daemon script
|
||||
copy:
|
||||
src: ansible_test_service.py
|
||||
dest: /usr/sbin/ansible_test_service
|
||||
mode: '755'
|
||||
|
||||
- name: rewrite shebang in the test daemon script
|
||||
lineinfile:
|
||||
path: /usr/sbin/ansible_test_service
|
||||
line: "#!{{ ansible_python_interpreter | realpath }}"
|
||||
insertbefore: BOF
|
||||
firstmatch: yes
|
||||
|
||||
- name: install the systemd unit file
|
||||
copy:
|
||||
src: ansible.systemd
|
||||
dest: /etc/systemd/system/ansible_test.service
|
||||
mode: '0644'
|
||||
register: install_systemd_result
|
||||
|
||||
- name: assert that the systemd unit file was installed
|
||||
assert:
|
||||
that:
|
||||
- "install_systemd_result.dest == '/etc/systemd/system/ansible_test.service'"
|
||||
- "install_systemd_result.state == 'file'"
|
||||
- "install_systemd_result.mode == '0644'"
|
@ -0,0 +1,36 @@
|
||||
- name: start the ansible test service
|
||||
service:
|
||||
name: ansible_test
|
||||
enabled: yes
|
||||
state: started
|
||||
register: enable_result
|
||||
|
||||
- name: assert that the service was enabled and changes reported
|
||||
assert:
|
||||
that:
|
||||
- "enable_result.enabled == true"
|
||||
- "enable_result is changed"
|
||||
|
||||
- name: disable the ansible test service
|
||||
service:
|
||||
name: ansible_test
|
||||
state: stopped
|
||||
enabled: no
|
||||
register: start_result
|
||||
|
||||
- name: assert that the service was stopped
|
||||
assert:
|
||||
that:
|
||||
- "start_result.state == 'stopped'"
|
||||
- "start_result is changed"
|
||||
|
||||
- name: Populate service facts
|
||||
service_facts:
|
||||
|
||||
- name: get ansible_test service's state
|
||||
debug:
|
||||
var: services['ansible_test.service'].state
|
||||
|
||||
- name: ansible_test service's running state should be \"inactive\"
|
||||
assert:
|
||||
that: "services['ansible_test.service'].state == 'inactive'"
|
Loading…
Reference in New Issue