-
Notifications
You must be signed in to change notification settings - Fork 0
/
installservice.py
executable file
·38 lines (29 loc) · 1.34 KB
/
installservice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
import configparser
import os
import pwd
import grp
import sys
import subprocess
base_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(base_dir)
config = configparser.RawConfigParser()
config_file = 'conf/config.ini'
systemd_file = '/etc/systemd/system/oe-agent.service'
config.read(config_file)
pid= config.get('SelfConfig', 'pid_file')
run_user = config.get('SelfConfig', 'run_user')
sparser = configparser.ConfigParser()
sparser.optionxform= str
sparser['Unit'] = {'Description': 'OddEye Agent Service', 'After': 'syslog.target'}
sparser['Install'] = {'WantedBy': 'multi-user.target'}
groups = [g.gr_name for g in grp.getgrall() if run_user in g.gr_mem]
gid = pwd.getpwnam(run_user).pw_gid
group = grp.getgrgid(gid).gr_name
sparser['Service'] = {'Type': 'simple', 'User': run_user, 'Group': group, 'WorkingDirectory': base_dir + '/',
'ExecStart': sys.executable + ' ' + base_dir + '/oddeye.py systemd', 'PIDFile': pid, 'Restart': 'on-failure'}
with open(systemd_file, 'w') as servicefile:
sparser.write(servicefile)
subprocess.Popen('systemctl daemon-reload', stdout=subprocess.PIPE, shell=True).communicate()
subprocess.Popen('systemctl enable oe-agent.service', stdout=subprocess.PIPE, shell=True).communicate()
subprocess.Popen('systemctl start oe-agent', stdout=subprocess.PIPE, shell=True).communicate()