Browse Source

create and run systemd and upstart service file

sys-systemd-and-upstart
Vladimir Smagin 5 months ago
parent
commit
ce4ff31a01

+ 4
- 0
.gitignore View File

@@ -0,0 +1,4 @@
1
+*.retry
2
+*.pyc
3
+__PYCACHE__
4
+

+ 0
- 1
nginx+php-fpm7.1/deploy.retry View File

@@ -1 +0,0 @@
1
-10.0.2.46

+ 17
- 0
systemd-and-upstart/inventory.ini View File

@@ -0,0 +1,17 @@
1
+[upstart:children]
2
+upstart-desktops
3
+upstart-servers
4
+
5
+[upstart-desktops]
6
+desk1204 ansible_host=10.9.0.50 ansible_user=vlad ansible_password=123 ansible_sudo_password=123
7
+desk1404 ansible_host=10.9.0.51 ansible_user=vlad ansible_password=123 ansible_sudo_password=123
8
+
9
+[upstart-servers]
10
+serv1204 ansible_host=10.9.0.48 ansible_user=vlad ansible_password=123 ansible_sudo_password=123
11
+serv1404 ansible_host=10.9.0.49 ansible_user=vlad ansible_password=123 ansible_sudo_password=123
12
+
13
+[systemd:children]
14
+systemd-desktops
15
+
16
+[systemd-desktops]
17
+desk1804 ansible_host=10.9.0.155 ansible_user=vlad ansible_password=123 ansible_sudo_password=123

+ 10
- 0
systemd-and-upstart/roles/preinstall/tasks/main.yml View File

@@ -0,0 +1,10 @@
1
+---
2
+- name: Minimum packages for Debian-like
3
+  apt: name="{{ item }}" state=present update_cache=yes
4
+  become: yes
5
+  become_method: sudo
6
+  with_items:
7
+    - python
8
+    - mc
9
+    - htop
10
+  when: preinstall is defined

+ 13
- 0
systemd-and-upstart/roles/set_service/files/app.py View File

@@ -0,0 +1,13 @@
1
+import time
2
+import multiprocessing
3
+import child_process
4
+
5
+
6
+print("Hello world")
7
+
8
+processes = [None] * 4
9
+for i in range(4):
10
+    processes[i] = multiprocessing.Process(target=child_process.run, args=(i,))
11
+    processes[i].start()
12
+for i in range(4):
13
+    processes[i].join()

+ 12
- 0
systemd-and-upstart/roles/set_service/files/child_process.py View File

@@ -0,0 +1,12 @@
1
+import time
2
+import multiprocessing
3
+
4
+def test(info):
5
+    while 1:
6
+	print 'TEST', info[0], info[1]
7
+
8
+def run(proc_id):
9
+    pool = multiprocessing.Pool(processes=4)
10
+    pool.map(test, [(proc_id, i) for i in range(4)])
11
+    pool.close()
12
+    pool.join()

+ 30
- 0
systemd-and-upstart/roles/set_service/tasks/main.yml View File

@@ -0,0 +1,30 @@
1
+---
2
+- name: Install app.py
3
+  copy:
4
+    src: files/app.py
5
+    dest: /opt/app.py
6
+    owner: root
7
+    group: root
8
+    mode: 0644
9
+
10
+- name: Install child_process.py
11
+  copy:
12
+    src: files/child_process.py
13
+    dest: /opt/child_process.py
14
+    owner: root
15
+    group: root
16
+    mode: 0644
17
+
18
+- name: Create service file for upstart Ubuntu versions
19
+  template: src=upstart-service.template.j2 dest=/etc/init/{{service_name}}.conf backup=no mode=0644
20
+  when: ansible_distribution == 'Ubuntu' and (ansible_distribution_version == "14.04" or ansible_distribution_version == "12.04")
21
+
22
+- name: Create service file for systemd Ubuntu versions
23
+  template: src=systemd-service.template.j2 dest=/etc/systemd/system/{{service_name}}.service backup=no mode=0644
24
+  when: ansible_distribution == 'Ubuntu' and (ansible_distribution_version == "16.04" or ansible_distribution_version == "18.04")
25
+
26
+- name: Enable service {{ service_name }}
27
+  service: name={{ service_name }} enabled=yes
28
+
29
+- name: Start service {{ service_name }}
30
+  service: name={{ service_name }} state=restarted

+ 15
- 0
systemd-and-upstart/roles/set_service/templates/systemd-service.template.j2 View File

@@ -0,0 +1,15 @@
1
+[Unit]
2
+Description={{service_description}}
3
+Requires=network.target
4
+After=syslog.target network.target
5
+TimeoutStopSpec=60
6
+[Service]
7
+Type=simple
8
+ExecStart={{interpretator}} {{installation_path}}/app.py
9
+ExecReload=/bin/kill -HUP $MAINPID
10
+WorkingDirectory={{installation_path}}
11
+User=vlad
12
+Restart=always
13
+RestartSec=30
14
+[Install]
15
+WantedBy=multi-user.target

+ 25
- 0
systemd-and-upstart/roles/set_service/templates/upstart-service.template.j2 View File

@@ -0,0 +1,25 @@
1
+description "{{ service_description }}"
2
+
3
+start on runlevel [2345]
4
+stop on runlevel [!2345]
5
+
6
+expect fork
7
+kill timeout 60 # when upstart issued a stop, send SIGTERM, wait 60 sec before sending SIGKILL
8
+
9
+respawn
10
+respawn limit 5 30 #try 5 times within 60 seconds, or giveup
11
+
12
+script
13
+    echo $$ > {{installation_path}}/app.pid
14
+    exec {{ interpretator }} {{ installation_path}}/app.py
15
+end script
16
+
17
+pre-start script
18
+    touch {{installation_path}}/app.log
19
+    echo "\n[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> {{installation_path}}/app.log
20
+end script
21
+
22
+pre-stop script
23
+    rm -f {{installation_path}}/app.pid
24
+    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> {{installation_path}}/app.log
25
+end script

+ 4
- 0
systemd-and-upstart/roles/set_service/vars/main.yml View File

@@ -0,0 +1,4 @@
1
+service_name: app
2
+service_description: my app.py
3
+installation_path: /opt
4
+interpretator: /usr/bin/python

+ 4
- 0
systemd-and-upstart/roles/test/tasks/main.yml View File

@@ -0,0 +1,4 @@
1
+---
2
+- name: test
3
+  debug:
4
+    msg: "OS Version: {{ansible_distribution }} {{ansible_distribution_release}} {{ansible_distribution_version}}"

+ 6
- 0
systemd-and-upstart/services.yml View File

@@ -0,0 +1,6 @@
1
+- hosts: all
2
+  roles:
3
+    - test
4
+    - preinstall
5
+    - { role: set_service, become: yes }
6
+

Loading…
Cancel
Save