I'm trying to build a setup.py
file to install a python script as a service. So far, I can install the script and manually add a unit in systemd and enable/launch the service, but I'd like to do it automatically. The problem I have is that my bash script which build the unit file requires sudo privileges and I dont want to run pip install
with sudo
.
import os
import subprocess
from setuptools import setup, Extension
import setuptools.command.install
from distutils.command.build import build as build_orig
class install(setuptools.command.install.install):
def run(self):
setuptools.command.install.install.run(self)
current_dir_path = os.path.dirname(os.path.realpath(__file__))
create_service_script_path = os.path.join(current_dir_path, 'bin', 'create_service.sh')
subprocess.check_output([create_service_script_path])
setup(name='dspt',
version=dspt.__version__,
packages=['dspt'],
include_package_data=True,
package_data={"": ["data/template.xml"]},
ext_modules=[ext],
install_requires=['slackclient==1.3.1',
'numpy',
'numba',
'scipy',
'pytz',
'requests',
'pyjwt',
'pysmb',
'boto3'],
cmdclass={
'install': install
},
entry_points={'console_scripts': ['run_dspt = dspt.batch_processing:main']}
)
create_service.sh
#!/bin/bash
SYSTEMD_SCRIPT_DIR=$( cd $(dirname "${BASH_SOURCE:=$0}") && pwd)
cp -f "$SYSTEMD_SCRIPT_DIR/dspt.service" /etc/systemd/system
chown root:root /etc/systemd/system/dspt.service
systemctl daemon-reload
systemctl enable dspt.service
unit file
[Unit]
Description=My super service
After=multi-user.target network.target
[Service]
User=myuser
Group=mygroup
EnvironmentFile=/etc/environment
Type=idle
Restart=always
RestartSec=3
ExecStart=/home/myuser/miniconda3/envs/dspt/bin/run_dspt
[Install]
WantedBy=multi-user.target
Note that for now, the unit file supposes that the package is installed in a conda virtual environment named dspt
.
The bash script create_service.sh
obvioulsy requires sudo privileges and I cant figure how to do it.