Blame view

install/install.py 13 KB
2e0c8f94   Unknown   New install scrip...
1
2
3
4
5
6
from sys import *
import platform
import os
import subprocess
import shutil
import fileinput
ca58ff86   Jeremy   Update
7

1e9aab20   Jeremy   Added install.py
8

2e0c8f94   Unknown   New install scrip...
9
class Colors:
a075147a   aklotz   Windows 32 LXML
10
11
12
13
14
15
16
17
    if (platform.system() == "Windows"):
         ERROR = ''
         END = ''
         LOG_BLUE = ''
    else:
         ERROR = '\033[91m'
         END = '\033[0m'
         LOG_BLUE = '\033[94m'
2e0c8f94   Unknown   New install scrip...
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

def replacePatternInFile(pattern, replace, file_path):
    try:

        with fileinput.FileInput(file_path, inplace=True, backup='.bak') as file:
            for line in file:
                print(line.replace(pattern, replace), end='')
    except:
        stderr.write(Colors.ERROR + "ERROR !: replacement in file failed !" + Colors.END + "\r\n")
        return 1
    return 0



def install_dependency_ubuntu(command, mode):
    #
    ##install dependency then check the return code
    #

    old = command
    if (mode == 'i'):
        command = 'apt-get install ' + command
    elif (mode == 'u'):
        command = 'apt-get update'
    elif (mode == 'a'):
        command = 'add-apt-repository ' + command
    process = subprocess.Popen(command, shell=True)
    process.wait()
    if process.returncode != 0:
        stderr.write(Colors.ERROR + "ERROR !: installation of " + old + " failed !" + Colors.END + "\r\n")


def install_required_ubuntu():
2e0c8f94   Unknown   New install scrip...
51
    install_dependency_ubuntu("update", 'u')
2e0c8f94   Unknown   New install scrip...
52
53
54
55
56
57
    install_dependency_ubuntu("python-lxml", 'i')
    install_dependency_ubuntu("libxml2-dev", 'i')
    install_dependency_ubuntu("libxslt-dev", 'i')
    install_dependency_ubuntu("zlib1g-dev", 'i')
    install_dependency_ubuntu("update", 'u')
    install_dependency_ubuntu("rabbitmq-server", 'i')
29ecf1c6   Quentin Durand   fix unittest
58
    #install_dependency_ubuntu("libmysqlclient-dev", 'i')
2e0c8f94   Unknown   New install scrip...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79


def install_dependency_centos(command, mode):
    #
    ##install dependency then check the return code
    #
    old = command
    if (mode == 'i'):
        command = 'yum -y install ' + command
    elif (mode == 'u'):
        command = 'yum update ' + command
    process = subprocess.Popen(command, shell=True)
    process.wait()
    if process.returncode != 0:
        stderr.write(Colors.ERROR + "ERROR !: installation of " + old + " failed !" + Colors.END + "\r\n")


def install_required_centos():
    install_dependency_centos("yum", 'u')
    install_dependency_centos("kernel", 'u')
    install_dependency_centos("", 'u')
2e0c8f94   Unknown   New install scrip...
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
    install_dependency_centos("libxml2", 'i')
    install_dependency_centos("libxslt libxslt-2", 'i')
    install_dependency_centos("libxslt-devel libxml2-devel", 'i')
    install_dependency_centos("rabbitmq-server", 'i')
    install_dependency_centos("mariadb-server", 'i')
    install_dependency_centos("mariadb", 'i')
    install_dependency_centos("mariadb-devel", 'i')

    process = subprocess.Popen("systemctl start mariadb.service", shell=True)
    process.wait()
    if process.returncode != 0:
        stderr.write(Colors.ERROR + "ERROR !" + Colors.END + "\r\n")

    process = subprocess.Popen("systemctl enable mariadb.service", shell=True)
    process.wait()
    if process.returncode != 0:
        stderr.write(Colors.ERROR + "ERROR !" + Colors.END + "\r\n")

    process = subprocess.Popen("mysql_secure_installation", shell=True)
    process.wait()
    if process.returncode != 0:
        stderr.write(Colors.ERROR + "ERROR !" + Colors.END + "\r\n")


def install_required():
    # checking if user is sudo then install the needed dependencies
    # find the linux distribution and call the related function
    distribution = platform.dist()
    if not 'SUDO_UID' in os.environ.keys():
a1dbc688   Unknown   normalize paths
109
        stderr.write("Super user rights are needed to install prerequisites\r\n")
2e0c8f94   Unknown   New install scrip...
110
111
112
113
114
115
116
117
118
119
120
        exit(1)
    if distribution[0] == "Ubuntu" or distribution[0] == "Debian":
        install_required_ubuntu()
    elif distribution[0] == "centos":
        install_required_centos()
    else:
        print("Requirements are made for Ubuntu, Debian and CentOS only")
        exit(1)


def install_python_modules(venv):
7452c5bb   Etienne Pallier   bugfix install sc...
121
122
    REINSTALL = True
    #REINSTALL = False
2e0c8f94   Unknown   New install scrip...
123
124
125
126
127
128
129
130
131
    if 'SUDO_UID' in os.environ.keys():
        answer = input(
            "You are about to install your virtualenv only for root, this is discouraged, are you sure ? (Y/N) If you are not sure relaunch the script without super user privileges\n")
        while (answer != 'Y' and answer != 'y' and answer != 'n' and answer != 'N'):
            answer = input(
                "You are about to install your virtualenv only for root, this is discouraged, are you sure ? (Y/N) \n")
        if (answer not in ['y', 'Y']):
            exit(1)

7452c5bb   Etienne Pallier   bugfix install sc...
132
    if (os.path.basename(os.getcwd()) != "private"):
2e0c8f94   Unknown   New install scrip...
133
134
135
136
137
138
139
140
141
        if not(os.path.isdir("../private")):
            print(Colors.LOG_BLUE + "-----------------------------Creating \'private\' directory-----------------------------" + Colors.END)
            os.mkdir("../private")
        print(Colors.LOG_BLUE + "-----------------------------cd private-----------------------------" + Colors.END)
        os.chdir("../private")
    #
    ## deleting if already exist then creating the venv
    #

7452c5bb   Etienne Pallier   bugfix install sc...
142
    while REINSTALL and True:
ca58ff86   Jeremy   Update
143
        try:
2e0c8f94   Unknown   New install scrip...
144
145
146
147
148
149
150
151
152
153
154
155
            if (os.path.isdir(venv)):
                print(Colors.LOG_BLUE + "-----------------------------Deleting existing venv-----------------------------" + Colors.END)
                shutil.rmtree(venv)
            break
        except:
            continue


    #
    ## update of tools and installation of dependencies in REQUIREMENTS.txt or REQUIREMENTS_WINDOWS.txt
    #

f7b38c2d   Etienne Pallier   refactorised inst...
156
157
158
    end_of_line = '\n\n'
    python = 'python3'
    pip = '/bin/pip'
451aee74   Unknown   Updating packages...
159
    
f7b38c2d   Etienne Pallier   refactorised inst...
160
161
162
    REQUIREMENTS = 'REQUIREMENTS.txt'
    sql_user = "pyros"
    sql_pswd = "DjangoPyros"
13333260   Etienne Pallier   bugfixed install ...
163
    if platform.dist()[0] == "centos": print("centos platform")
7452c5bb   Etienne Pallier   bugfix install sc...
164
165

    # NEWER MYSQL:
2ce20c50   Quentin Durand   applying workshop...
166
    req = "CREATE DATABASE IF NOT EXISTS pyros;  CREATE DATABASE IF NOT EXISTS pyros_test; CREATE USER IF NOT EXISTS pyros; GRANT USAGE ON *.* TO 'pyros'; DROP USER 'pyros'; GRANT ALL ON pyros.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros'; GRANT ALL ON test_pyros.* TO 'pyros'@'localhost'; GRANT ALL PRIVILEGES ON test_pyros_test.* TO 'pyros'@'localhost'; GRANT ALL ON pyros_test.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros'"
13333260   Etienne Pallier   bugfixed install ...
167
168
    # OLDER MYSQL: Try this instead for OLDER mysql (works on CentOS 6.4 and Centos 7.5 with mysql 5.5):
    #req = "drop database pyros; CREATE DATABASE pyros; drop database pyros_test ; CREATE DATABASE pyros_test; drop user 'pyros'@'localhost' ; CREATE USER pyros; GRANT USAGE ON *.* TO 'pyros'; DROP USER 'pyros'; GRANT ALL ON pyros.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros'; GRANT ALL ON test_pyros.* TO 'pyros'@'localhost'; GRANT ALL PRIVILEGES ON test_pyros_test.* TO 'pyros'@'localhost'; GRANT ALL ON pyros_test.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros'"
7452c5bb   Etienne Pallier   bugfix install sc...
169
170
    #req = "drop database pyros ; CREATE DATABASE pyros; drop database pyros_test ; CREATE DATABASE pyros_test; DROP USER 'pyros'@'localhost' ; GRANT USAGE ON *.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros' WITH GRANT OPTION; DROP USER 'pyros'@'localhost'; GRANT ALL ON pyros.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros'; GRANT ALL PRIVILEGES ON pyros.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros' WITH GRANT OPTION;  GRANT ALL PRIVILEGES ON pyros_test.* TO pyros@localhost IDENTIFIED BY 'DjangoPyros' WITH GRANT OPTION;"
    # (EP) ok for CENTOS 7 I suppose (but not for CentOS 6):
13333260   Etienne Pallier   bugfixed install ...
171
172
    #req_centos = "CREATE DATABASE IF NOT EXISTS pyros; CREATE DATABASE IF NOT EXISTS pyros_test; GRANT USAGE ON *.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros' WITH GRANT OPTION; DROP USER 'pyros'@'localhost'; GRANT ALL ON pyros.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros'; GRANT ALL PRIVILEGES ON pyros.* TO 'pyros'@'localhost' IDENTIFIED BY 'DjangoPyros' WITH GRANT OPTION;  GRANT ALL PRIVILEGES ON pyros_test.* TO pyros@localhost IDENTIFIED BY 'DjangoPyros' WITH GRANT OPTION;"
    req_centos = req
5f832754   Unknown   fixing req_centos
173
174
    if platform.dist()[0] == "centos":
        req = req_centos
f7b38c2d   Etienne Pallier   refactorised inst...
175
176
177
178
179
180
181
182
    sql_request="\"" + req + "\"" + " | mysql -u root -p"
    if (platform.system() == "Windows"):
        end_of_line = "\r\n\r\n"
        python = 'py'
        pip = '\Scripts\pip'
        REQUIREMENTS = 'REQUIREMENTS_WINDOWS.txt'
        sql_request=req + " |\"C:\Program Files\MySQL\MySQL Server 5.7\\bin\mysql\" -u root -p"
    
7452c5bb   Etienne Pallier   bugfix install sc...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    if REINSTALL:
    	print(Colors.LOG_BLUE + "-----------------------------Creating venv " + venv + "-----------------------------"+end_of_line + Colors.END)
    	os.system(python+" -m venv " + venv)
    	print(Colors.LOG_BLUE + "-----------------------------Installing python packages via pip-----------------------------" + Colors.END)
    	if (platform.system() == "Windows"):    
            os.system(venv + '\Scripts\python -m pip install --upgrade pip')
    	else: # Linux
            os.system(venv + '/bin/python -m pip install --upgrade pip')
    	os.system(venv + pip+' install --upgrade wheel')
    	os.system(venv + pip+' install --upgrade setuptools')
    	if (platform.system() == "Windows"):
            if (platform.machine() == "x86"):
                os.system(venv + '\Scripts\pip install ../install\windows\lxml-4.1.1-cp36-cp36m-win32.whl')
            else:
                os.system(venv + '\Scripts\pip install ../install\windows\lxml-4.1.1-cp36-cp36m-win_amd64.whl')
    	os.system(venv + pip+' install -r ../install' + os.sep + REQUIREMENTS)
    	print(Colors.LOG_BLUE + "-----------------------------cd ../install-----------------------------" + Colors.END)
    	os.chdir("../install")
2e0c8f94   Unknown   New install scrip...
201
202
203
204
    
    #
    ## creating database and creating and granting user pyros
    #
f7b38c2d   Etienne Pallier   refactorised inst...
205
206
207
208
209
210
211
212
213
214
215
216
217
    print(Colors.LOG_BLUE + end_of_line+"-----------------------------Launching mysql to create database and create and grant user pyros-----------------------------" + Colors.END)
    print(Colors.LOG_BLUE +"-----------------------------Please enter your MYSQL root password-----------------------------" + Colors.END)
    process = subprocess.Popen("echo " + sql_request, shell=True)
    process.wait()
    if (process.returncode != 0):
        stderr.write(Colors.ERROR + "ERROR !: db configuration failed !" + Colors.END + "\r\n")
        return -1
    print(Colors.LOG_BLUE + end_of_line+"-----------------------------Database created and user pyros successfully created and granted-----------------------------" + Colors.END)

    #
    ## replacing pattern in settings.py to use mysql
    #
    print(Colors.LOG_BLUE + "-----------------------------setting MYSQL = True in settings-----------------------------" + Colors.END)
a1dbc688   Unknown   normalize paths
218
    replacePatternInFile("MYSQL = False", "MYSQL = True", os.path.normpath("../src/pyros/settings.py"))
f7b38c2d   Etienne Pallier   refactorised inst...
219
220
221
222
223
224
225
226
227
    if (platform.system() == "Windows"):
        #
        ## moving voeventparse in site-packages directory
        #
        try:
            site_packages = "..\private\\venv_py3_pyros\Lib\site-packages\\"
            if (not os.path.isdir(site_packages + "voevent_parse-0.9.5.dist-info") and
            not os.path.isdir(site_packages + "voeventparse")):
                print(Colors.LOG_BLUE + "\r\n\r\n-----------------------------Copying the voevent library in Lib/site-packages-----------------------------" + Colors.END)
947b3dcf   Quentin Durand   replacing shutil....
228
229
230
231
232
233
234
                process = subprocess.Popen("xcopy /i /y windows\\voeventparse " + site_packages + "voeventparse")
                process.wait()
                if (process.returncode != 0):raise Exception
                process = subprocess.Popen("xcopy /i /y windows\\voevent_parse-0.9.5.dist-info " + site_packages + "voevent_parse-0.9.5.dist-info")
                process.wait()
                if (process.returncode != 0): raise Exception

f7b38c2d   Etienne Pallier   refactorised inst...
235
236
237
                print(Colors.LOG_BLUE + "\r\n-----------------------------library successfully copied-----------------------------" + Colors.END)
        except Exception as e:
            print(Colors.ERROR + "ERROR while Copying the voevent library in Lib/site-packages" + Colors.END, file=stderr)
975e2c98   Quentin Durand   Auto stash before...
238
            return False
2e0c8f94   Unknown   New install scrip...
239

244507e6   Unknown   adding migrations...
240
241
242

    print(Colors.LOG_BLUE + "\r\n-----------------------------cd ..-----------------------------" + Colors.END)
    os.chdir("..")
ca58ff86   Jeremy   Update
243

2e0c8f94   Unknown   New install scrip...
244
    #
244507e6   Unknown   adding migrations...
245
    ##  Executing migrations
2e0c8f94   Unknown   New install scrip...
246
    #
ca58ff86   Jeremy   Update
247

56b06950   Unknown   Install script fi...
248
    print(Colors.LOG_BLUE + "\r\n\r\n-----------------------------Migrate : executing pyros.py init_database-----------------------------" + Colors.END)
244507e6   Unknown   adding migrations...
249
    try:
56b06950   Unknown   Install script fi...
250
        process = subprocess.Popen(python + " pyros.py init_database"  , shell=True)
244507e6   Unknown   adding migrations...
251
252
        process.wait()
    except Exception as e:
3185a08a   Unknown   updating init_dat...
253
        print(Colors.ERROR + "Error while initialising database :" + Colors.END)
244507e6   Unknown   adding migrations...
254
        return -1
56b06950   Unknown   Install script fi...
255
256


244507e6   Unknown   adding migrations...
257
258
259
    print(Colors.LOG_BLUE + "\r\n\r\n-----------------------------Install successfull !-----------------------------" + Colors.END)
    return 0

2e0c8f94   Unknown   New install scrip...
260
261
262
def _help():
    print(
        "Welcome in the installation script of the pyros venv.\t\nPlease launch it from the install directory of pyros.\n\tIf you're on Ubuntu Debian or CentOS:\n\tlaunch it with sudo and <--prerequisites> or <-p> to install the prerequisites.\n\t-->sudo ./test_install.py -p\n\n\tFor the python packages launch it from the install directory of pyros without sudo and without parameter\n\t-->./test_install.py")
ca58ff86   Jeremy   Update
263
264


2e0c8f94   Unknown   New install scrip...
265
266
267
268
269
270
271
272
273
274
if __name__ == '__main__':
    if (len(argv) > 1):
        if argv[1] == "--prerequisites" or argv[1] == "-p":
            install_required()
        else:
            _help()
    elif len(argv) == 1:
        install_python_modules("venv_py3_pyros")
    else:
        _help()