-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.py
executable file
·146 lines (108 loc) · 3.82 KB
/
deploy.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# deploy_it, alpha release
# Copyright 2019, Aswin Babu Karuvally
# import serious stuff
import os
import json
import subprocess
import sys
import logging
import shutil
from distutils.dir_util import copy_tree
import pdb # debug
def setup_service(config):
unit_file = config["systemd_service"]["unit_file"]
if not os.path.isfile(unit_file):
logging.critical(unit_file + " does not exist, exiting")
sys.exit(1)
shutil.copy(unit_file, "/etc/systemd/system")
# enable and start service
execute_command("systemctl enable " + os.path.basename(unit_file))
execute_command("systemctl start " + os.path.basename(unit_file))
logging.info(os.path.basename(unit_file) + " is enabled and running")
def setup_logging():
format_string = "[%(asctime)s] %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(
filename = os.path.join("build.log"),
level = logging.DEBUG,
format = format_string,
datefmt = date_format
)
# print logs to stderr
logging.getLogger().addHandler(logging.StreamHandler())
# execute command
def execute_command(command):
return_code = subprocess.call(command, shell=True)
if return_code != 0:
logging.critical(command + " cannot be executed, exiting")
sys.exit(1)
# create virtualenv for the app
def create_virtualenv(config):
# read config
install_path = config["basics"]["install_path"]
requirements_file = config["basics"]["requirements_file"]
requirements_file_path = os.path.join(install_path, requirements_file)
# create virtualenv
execute_command("python3 -m venv " + install_path)
# copy files
copy_tree("src", os.path.join(install_path, "src"))
# upgrade pip
execute_command(install_path + "/bin/pip3 install --upgrade pip")
# install requirements
execute_command(
install_path + "/bin/pip3 install -r " + requirements_file_path
)
# install virtualenv
def install_virtualenv():
# get the distribution name
distro = os.popen("lsb_release -i")
distro = distro.read().rstrip()
distro = distro.split()[2]
# read distro specific info
install_cmd_file = open("install_venv.json")
install_cmd_string = install_cmd_file.read()
install_cmds = json.loads(install_cmd_string)
if distro in install_cmds:
command_list = install_cmds[distro]
else:
logging.critical("virtualenv cannot be installed, exiting")
sys.exit(1)
# install virtualenv
for command in command_list:
execute_command(command)
# the main function
def main():
# check if user is root
if os.getuid() != 0:
print("please run the deployer as root")
sys.exit(1)
setup_logging()
# read the configution
if not os.path.isfile("config.json"):
logging.critical("config.json does not exist, exiting...")
sys.exit(1)
with open("config.json") as config_file:
config = config_file.read()
config = json.loads(config)
# install virtualenv module
install_virtualenv()
logging.info("installed virtualenv module")
# create virtualenv
create_virtualenv(config)
logging.info("created virtualenv for app")
# run post install script
if config["post_install_script"]["enable"]:
execute_command(config["post_install_script"]["script_file"])
# setup service
if config["systemd_service"]["enable"]:
setup_service(config)
# add symlink
if config["symlink"]["enable"]:
target_path = os.path.join(config["basics"]["install_path"])
link_path = config["symlink"]["link_path"]
execute_command("ln -s " + target_path + " " + link_path)
logging.info("creating symlink to " + link_path)
logging.info("installation is complete.")
if __name__ == "__main__":
main()