-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-service.sh
123 lines (100 loc) · 2.94 KB
/
create-service.sh
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/bash
. ./validatejson.lib
## This script creates a System.d service file based on JSON data.
## This script needs python >= 3.6 and GNU bash (bash version used 5.0.3(1)).
## Created by aang with the help of the open source community, on April 2021.
# Sample config JSON file:
# {
# "service_name": "service-x",
# "description": "description-x",
# "package_path": "/home/user/Project-X/.venv/bin/python",
# "service_path": "main.py arguments",
# "env_variables": { "one_env_variable": "value-x", "other_env_variable": "value-y" }
# }
## execution example of this script
## $ bash create-service.sh conf-file.json
# get the first argument, the JSON file
args=("$@")
DATA_FILE=${args[0]}
# check if argument is passed
if [ -z "$DATA_FILE" ]; then
echo "json file should be passed as first argument"
exit 1
fi
validateJSON DATA_FILE resulted_data # included in the imported lib
if [ $? -ne 0 ]; then
echo "error validating conf json file"
exit 1
fi
delimeter=';'
SplitLineByDelimeter resulted_data delimeter conf_params
echo $?
# initializing the central variables
SERVICE_NAME=${conf_params[0]}
DESCRIPTION=${conf_params[1]}
PKG_PATH=${conf_params[2]}
SERVICE_PATH=${conf_params[3]}
WORKING_DIRECTORY=${conf_params[4]}
ENV_VARIABLES=${conf_params[5]}
echo $SERVICE_NAME
echo $DESCRIPTION
echo $PKG_PATH
echo $SERVICE_PATH
echo $WORKING_DIRECTORY
echo $ENV_VARIABLES
# from this point I need to work with sudo
# create ${service_name}.d folder in /etc/systemd/system/ path
SYSTEMD_PATH=/etc/systemd/system/$SERVICE_NAME
sudo mkdir -p $SYSTEMD_PATH.service.d
# create the file override.conf inside the just created folder
# if it doesn't exists.
OVERRIDE_FILE_PATH=$SYSTEMD_PATH.service.d/override.conf
if [ ! -f $OVERRIDE_FILE_PATH ]
then
echo "creating override.conf file"
sudo cat > $SYSTEMD_PATH.service.d/override.conf << EOF
$ENV_VARIABLES
EOF
else
echo "override.conf file already exists."
fi
# check if service is active
IS_ACTIVE=$(sudo systemctl is-active $SERVICE_NAME)
if [ "$IS_ACTIVE" == "active" ]; then
# restart the service
echo "Service is running"
echo "Restarting service"
sudo systemctl restart $SERVICE_NAME
echo "Service restarted"
else
SERVICE_FILE_PATH=/etc/systemd/system/$SERVICE_NAME.service
echo $SERVICE_FILE_PATH
# create service file if doesn't exists
if [ ! -f $SERVICE_FILE_PATH ];
then
echo "Creating service file"
sudo cat > $SERVICE_FILE_PATH << EOF
[Unit]
Description=$DESCRIPTION
After=network.target
[Service]
PIDFile=/tmp/$SERVICE_NAME-99.pid
WorkingDirectory=$WORKING_DIRECTORY
ExecStart=$PKG_PATH $SERVICE_PATH
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# restart daemon, enable and start service
echo "Reloading daemon and enabling service"
sudo systemctl daemon-reload
sudo systemctl enable $SERVICE_NAME
if [ $? -ne 0 ];
then
exit 1
fi
sudo systemctl start $SERVICE_NAME
echo "Service Started"
fi
fi
exit 0