Blame view

install/install.py 18.3 KB
5c5b079e   Etienne Pallier   Requirements harm...
1
2
#!/usr/bin/env python3

2e0c8f94   Unknown   New install scrip...
3
import platform
c1f53cef   Alain Klotz   Update install/in...
4
import os, sys
2e0c8f94   Unknown   New install scrip...
5
6
7
import subprocess
import shutil
import fileinput
ca58ff86   Jeremy   Update
8

1e9aab20   Jeremy   Added install.py
9

faa9b662   Etienne Pallier   bugfix windows
10
# By default, install the virtual environment AND the database
b6c0aca8   Etienne Pallier   petit oubli
11
INSTALL_VENV = True
faa9b662   Etienne Pallier   bugfix windows
12
INSTALL_DB = True
9d37b424   Etienne Pallier   refactorised and ...
13

3ea57509   Etienne Pallier   install.py cleanup
14
15
16
17
18
19
20
21
VENV = "venv_py3_pyros"

SQL_DATABASE = "pyros"
SQL_DATABASE_TEST = "pyros_test"
SQL_USER = "pyros"
SQL_PSWD = "DjangoPyros"
MYSQL_EXE_PATH = ""

b604fd0a   Etienne Pallier   1 unique requirem...
22
23
REQUIREMENTS = 'REQUIREMENTS.txt'
#REQUIREMENTS = 'REQUIREMENTS_36.txt'
70fcc6ab   Etienne Pallier   install script re...
24
#REQUIREMENTS = 'REQUIREMENTS_37.txt'
3ea57509   Etienne Pallier   install.py cleanup
25
26
END_OF_LINE = '\n\n'
VENV_BIN = '/bin/'
6cf96eb4   Etienne Pallier   bugfix install sc...
27
WINDOWS = False
9d37b424   Etienne Pallier   refactorised and ...
28
29
30
31
# --------------------------------------------
# --- Modified values for Windows
# --------------------------------------------
if (platform.system() == "Windows"):
3ea57509   Etienne Pallier   install.py cleanup
32
    WINDOWS = True 
082ccda3   Etienne Pallier   pyros.py : enrich...
33
34
    REQUIREMENTS = 'REQUIREMENTS_WINDOWS.txt'
    #REQUIREMENTS = 'REQUIREMENTS_WINDOWS_36.txt'
3ea57509   Etienne Pallier   install.py cleanup
35
36
37
38
    END_OF_LINE = "\r\n\r\n"
    VENV_BIN = '\\Scripts\\'
    #MYSQL_EXE_PATH = "C:/Program Files (x86)/MySQL/MySQL Server 5.0/bin/"
    #question = "Enter the path of the MySQL server if it is not the following name (" + MYSQL_EXE_PATH + "): "
9d37b424   Etienne Pallier   refactorised and ...
39
40
    #res = input(question)
    #if res!="":
3ea57509   Etienne Pallier   install.py cleanup
41
42
43
    #   MYSQL_EXE_PATH = res
VENV_PIP = VENV + VENV_BIN+'pip'
VENV_PYTHON = VENV + VENV_BIN+'python'
9d37b424   Etienne Pallier   refactorised and ...
44
45


2e0c8f94   Unknown   New install scrip...
46
class Colors:
3ea57509   Etienne Pallier   install.py cleanup
47
    if WINDOWS:
a075147a   aklotz   Windows 32 LXML
48
49
50
51
52
53
54
         ERROR = ''
         END = ''
         LOG_BLUE = ''
    else:
         ERROR = '\033[91m'
         END = '\033[0m'
         LOG_BLUE = '\033[94m'
2e0c8f94   Unknown   New install scrip...
55

3ea57509   Etienne Pallier   install.py cleanup
56

70fcc6ab   Etienne Pallier   install script re...
57
58
59
# GLOBAL_PYTHON = 'python3'
GLOBAL_PYTHON = os.path.split(sys.executable)[-1]
print(Colors.LOG_BLUE + "Python executable is " + GLOBAL_PYTHON + Colors.END)
3ea57509   Etienne Pallier   install.py cleanup
60
61
62
63
64
65
##if platform.dist()[0] == "centos": print("centos platform")





2e0c8f94   Unknown   New install scrip...
66
67
def replacePatternInFile(pattern, replace, file_path):
    try:
2e0c8f94   Unknown   New install scrip...
68
69
70
71
        with fileinput.FileInput(file_path, inplace=True, backup='.bak') as file:
            for line in file:
                print(line.replace(pattern, replace), end='')
    except:
89e9fa90   Etienne Pallier   bugfix stderr dan...
72
        sys.stderr.write(Colors.ERROR + "ERROR !: replacement in file failed !" + Colors.END + "\r\n")
2e0c8f94   Unknown   New install scrip...
73
74
75
76
        return 1
    return 0


2e0c8f94   Unknown   New install scrip...
77
def install_dependency_ubuntu(command, mode):
70fcc6ab   Etienne Pallier   install script re...
78
79
80
    '''
    Install dependency then check the return code
    '''
2e0c8f94   Unknown   New install scrip...
81
82
83
84
85
86
87
88
89
90
    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:
89e9fa90   Etienne Pallier   bugfix stderr dan...
91
        sys.stderr.write(Colors.ERROR + "ERROR !: installation of " + old + " failed !" + Colors.END + "\r\n")
2e0c8f94   Unknown   New install scrip...
92
93
94


def install_required_ubuntu():
2e0c8f94   Unknown   New install scrip...
95
    install_dependency_ubuntu("update", 'u')
2e0c8f94   Unknown   New install scrip...
96
97
98
99
100
101
    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
102
    #install_dependency_ubuntu("libmysqlclient-dev", 'i')
2e0c8f94   Unknown   New install scrip...
103
104
105


def install_dependency_centos(command, mode):
2e0c8f94   Unknown   New install scrip...
106
107
108
109
110
111
112
113
    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:
89e9fa90   Etienne Pallier   bugfix stderr dan...
114
        sys.stderr.write(Colors.ERROR + "ERROR !: installation of " + old + " failed !" + Colors.END + "\r\n")
2e0c8f94   Unknown   New install scrip...
115
116
117
118
119
120


def install_required_centos():
    install_dependency_centos("yum", 'u')
    install_dependency_centos("kernel", 'u')
    install_dependency_centos("", 'u')
2e0c8f94   Unknown   New install scrip...
121
122
123
124
125
126
127
128
129
130
131
    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:
89e9fa90   Etienne Pallier   bugfix stderr dan...
132
        sys.stderr.write(Colors.ERROR + "ERROR !" + Colors.END + "\r\n")
2e0c8f94   Unknown   New install scrip...
133
134
135
136

    process = subprocess.Popen("systemctl enable mariadb.service", shell=True)
    process.wait()
    if process.returncode != 0:
89e9fa90   Etienne Pallier   bugfix stderr dan...
137
        sys.stderr.write(Colors.ERROR + "ERROR !" + Colors.END + "\r\n")
2e0c8f94   Unknown   New install scrip...
138
139
140
141

    process = subprocess.Popen("mysql_secure_installation", shell=True)
    process.wait()
    if process.returncode != 0:
89e9fa90   Etienne Pallier   bugfix stderr dan...
142
        sys.stderr.write(Colors.ERROR + "ERROR !" + Colors.END + "\r\n")
2e0c8f94   Unknown   New install scrip...
143
144
145


def install_required():
70fcc6ab   Etienne Pallier   install script re...
146
147
    # Checking if user is sudo then install the needed dependencies
    # Find the linux distribution and call the related function
2e0c8f94   Unknown   New install scrip...
148
149
    distribution = platform.dist()
    if not 'SUDO_UID' in os.environ.keys():
89e9fa90   Etienne Pallier   bugfix stderr dan...
150
        sys.stderr.write("Super user rights are needed to install prerequisites\r\n")
2e0c8f94   Unknown   New install scrip...
151
152
153
154
155
156
157
158
159
160
        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)


4a8f058c   Alain Klotz   Merge branch 'dev...
161
def venv_pip_install(package_name:str, options:str=''):
3ea57509   Etienne Pallier   install.py cleanup
162
    os.system(VENV_PIP + ' install ' + options + ' ' + package_name)
9d37b424   Etienne Pallier   refactorised and ...
163
164
165
166


def install_venv(venv:str):

c1f53cef   Alain Klotz   Update install/in...
167
168
169
    # --------------------------------------------
    # --- Be aware not to create virtual environment in case of user root
    # --------------------------------------------
2e0c8f94   Unknown   New install scrip...
170
171
    if 'SUDO_UID' in os.environ.keys():
        answer = input(
9d37b424   Etienne Pallier   refactorised and ...
172
            "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")
2e0c8f94   Unknown   New install scrip...
173
174
175
176
177
178
        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)

c1f53cef   Alain Klotz   Update install/in...
179
    # --------------------------------------------
dbefae59   Etienne Pallier   Restructuring : p...
180
    # --- Create the (private) venv directory to put in files for virtual environment
c1f53cef   Alain Klotz   Update install/in...
181
    # --------------------------------------------
dbefae59   Etienne Pallier   Restructuring : p...
182
183
184
185
    if (os.path.basename(os.getcwd()) != "venv"):
        if not(os.path.isdir("../venv")):
            print(Colors.LOG_BLUE + "-----------------------------Creating \'venv\' directory-----------------------------" + Colors.END)
            os.mkdir("../venv")
2e0c8f94   Unknown   New install scrip...
186

5c5b079e   Etienne Pallier   Requirements harm...
187
    
c1f53cef   Alain Klotz   Update install/in...
188
189
190
    # --------------------------------------------
    # --- Deleting if already exist then creating the venv
    # --------------------------------------------
dbefae59   Etienne Pallier   Restructuring : p...
191
192
    #print(Colors.LOG_BLUE + "-----------------------------cd venv-----------------------------" + Colors.END)
    os.chdir("../venv/")
9d37b424   Etienne Pallier   refactorised and ...
193
    while True:
ca58ff86   Jeremy   Update
194
        try:
2e0c8f94   Unknown   New install scrip...
195
196
197
198
            if (os.path.isdir(venv)):
                print(Colors.LOG_BLUE + "-----------------------------Deleting existing venv-----------------------------" + Colors.END)
                shutil.rmtree(venv)
            break
3ea57509   Etienne Pallier   install.py cleanup
199
        # Exception on Windows WinError 145 : Cannot remove folder because files in folder not yet removed...
9d37b424   Etienne Pallier   refactorised and ...
200
        except Exception as e:
3ea57509   Etienne Pallier   install.py cleanup
201
            #print(e)
2e0c8f94   Unknown   New install scrip...
202
203
            continue

c1f53cef   Alain Klotz   Update install/in...
204
    # --------------------------------------------
dbefae59   Etienne Pallier   Restructuring : p...
205
    # --- Reinstall the virtual environment (from ../venv/)
c1f53cef   Alain Klotz   Update install/in...
206
    # --------------------------------------------
9d37b424   Etienne Pallier   refactorised and ...
207
    
3ea57509   Etienne Pallier   install.py cleanup
208
    print(Colors.LOG_BLUE + "-----------------------------Creating venv " + venv + "-----------------------------"+END_OF_LINE + Colors.END)
70fcc6ab   Etienne Pallier   install script re...
209
    os.system(GLOBAL_PYTHON+" -m venv " + venv)
9d37b424   Etienne Pallier   refactorised and ...
210
        
3ea57509   Etienne Pallier   install.py cleanup
211
    print(Colors.LOG_BLUE + "-----------------------------Upgrade pip, wheel, and setuptools" + "-----------------------------"+END_OF_LINE + Colors.END)
9d37b424   Etienne Pallier   refactorised and ...
212
    # Upgrade pip
3ea57509   Etienne Pallier   install.py cleanup
213
    os.system(VENV_PYTHON + ' -m pip install --upgrade pip')
9d37b424   Etienne Pallier   refactorised and ...
214
215
216
217
218
219
220
221
    '''
    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')
    '''
    
    # Pip upgrade wheel and setuptools
70fcc6ab   Etienne Pallier   install script re...
222
223
224
225
    venv_pip_install('wheel', '--upgrade')
    #os.system(VENV_PIP+' install --upgrade wheel')
    venv_pip_install('setuptools', '--upgrade')
    #os.system(VENV_PIP+' install --upgrade setuptools')
9d37b424   Etienne Pallier   refactorised and ...
226
    
9d37b424   Etienne Pallier   refactorised and ...
227
    # Pip install required packages from REQUIREMENTS file
3ea57509   Etienne Pallier   install.py cleanup
228
    print()
9d37b424   Etienne Pallier   refactorised and ...
229
    print(Colors.LOG_BLUE + "-----------------------------Installing python packages via pip-----------------------------" + Colors.END)
70fcc6ab   Etienne Pallier   install script re...
230
231
    venv_pip_install('../install/'+REQUIREMENTS, '-r')
    #os.system(VENV_PIP+' install -r ../install' + os.sep + REQUIREMENTS)
6cf96eb4   Etienne Pallier   bugfix install sc...
232
    
0b4ad555   Alain Klotz   windows.
233
234
235
    #print(Colors.LOG_BLUE + "-----------------------------cd ../install-----------------------------" + Colors.END)
    os.chdir("../install")

6cf96eb4   Etienne Pallier   bugfix install sc...
236
237
238
    if WINDOWS:
        ## moving voeventparse in site-packages directory
        try:
dbefae59   Etienne Pallier   Restructuring : p...
239
            site_packages = "..\\venv\\"+VENV+"\\Lib\\site-packages\\"
6cf96eb4   Etienne Pallier   bugfix install sc...
240
241
242
            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)
0b4ad555   Alain Klotz   windows.
243
244
                cmdline = "xcopy /i /y windows\\voeventparse " + site_packages + "voeventparse"
                process = subprocess.Popen(cmdline)
6cf96eb4   Etienne Pallier   bugfix install sc...
245
246
247
248
249
250
251
                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
                print(Colors.LOG_BLUE + "\r\n-----------------------------library successfully copied-----------------------------" + Colors.END)
        except Exception as e:
0b4ad555   Alain Klotz   windows.
252
253
254
            print(Colors.ERROR + "ERROR while Copying the voevent library in Lib/site-packages" + Colors.END) ; #, file=stderr)
            return False    
    return 0
9d37b424   Etienne Pallier   refactorised and ...
255
256
    
def install_database(venv):
c1f53cef   Alain Klotz   Update install/in...
257

3ea57509   Etienne Pallier   install.py cleanup
258
259
    print(Colors.LOG_BLUE + END_OF_LINE+"-----------------------------Launching mysql to create database and create and grant user pyros-----------------------------" + Colors.END)
  
c1f53cef   Alain Klotz   Update install/in...
260
261
262
263
264
265
266
267
268
269
270
271
272
    # --------------------------------------------
    # --- Determine the MySQL version
    # --------------------------------------------
    output = subprocess.check_output("mysql --version", shell=True)
    # output is something like: "mysql  Ver 15.1 Distrib 10.0.20-MariaDB, for Linux (x86_64) using  EditLine wrapper"
    tmp = (str(output).split()[4]).split('.')
    sql_version = float(tmp[0]+'.'+tmp[1])
    print(Colors.LOG_BLUE + "MySQL version is " + str(sql_version) + Colors.END)

    # --------------------------------------------
    # --- Prepare the SQL query to create and initialize the pyros database if needed
    # --------------------------------------------
    if sql_version<5.5:
50a00e8b   Alain Klotz   Explosion de la l...
273
274
275
276
277
278
279
280
281
        #sql_query = "drop database "+SQL_DATABASE+" ; CREATE DATABASE "+SQL_DATABASE+"; drop database "+SQL_DATABASE_TEST+" ; CREATE DATABASE "+SQL_DATABASE_TEST+"; CREATE USER "+SQL_USER+" ; GRANT USAGE ON *.* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"' WITH GRANT OPTION; DROP USER '"+SQL_USER+"'@'localhost'; GRANT ALL ON "+SQL_DATABASE+".* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"'; GRANT ALL PRIVILEGES ON "+SQL_DATABASE+".* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"' WITH GRANT OPTION;  GRANT ALL PRIVILEGES ON "+SQL_DATABASE_TEST+".* TO "+SQL_USER+"@localhost IDENTIFIED BY '"+SQL_PSWD+"' WITH GRANT OPTION;"
        sql_query = ""
        sql_query += "drop database "+SQL_DATABASE+" ; CREATE DATABASE "+SQL_DATABASE+"; "
        sql_query += "drop database "+SQL_DATABASE_TEST+" ; CREATE DATABASE "+SQL_DATABASE_TEST+"; "
        sql_query += "CREATE USER "+SQL_USER+" ; GRANT USAGE ON *.* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"' WITH GRANT OPTION; "
        sql_query += "DROP USER '"+SQL_USER+"'@'localhost'; "
        sql_query += "GRANT ALL ON "+SQL_DATABASE+".* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"'; "
        sql_query += "GRANT ALL PRIVILEGES ON "+SQL_DATABASE+".* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"' WITH GRANT OPTION; "
        sql_query += "GRANT ALL PRIVILEGES ON "+SQL_DATABASE_TEST+".* TO "+SQL_USER+"@localhost IDENTIFIED BY '"+SQL_PSWD+"' WITH GRANT OPTION; "
c1f53cef   Alain Klotz   Update install/in...
282
    else:
3ea57509   Etienne Pallier   install.py cleanup
283
        sql_query = "CREATE DATABASE IF NOT EXISTS "+SQL_DATABASE+";  CREATE DATABASE IF NOT EXISTS "+SQL_DATABASE_TEST+"; CREATE USER IF NOT EXISTS "+SQL_USER+"; GRANT USAGE ON *.* TO '"+SQL_USER+"'; DROP USER '"+SQL_USER+"'; GRANT ALL ON "+SQL_DATABASE+".* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"'; GRANT ALL ON "+SQL_DATABASE_TEST+".* TO '"+SQL_USER+"'@'localhost'; GRANT ALL PRIVILEGES ON "+SQL_DATABASE_TEST+".* TO '"+SQL_USER+"'@'localhost'; GRANT ALL ON "+SQL_DATABASE_TEST+".* TO '"+SQL_USER+"'@'localhost' IDENTIFIED BY '"+SQL_PSWD+"'"
c1f53cef   Alain Klotz   Update install/in...
284
285
286
287
288
289
290
291
292
293
        # NEWER MYSQL:
        # 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'"
        #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):
        #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;"
    
    # --- Prepare the SQL query to create and initialize the pyros database if needed
    #if platform.dist()[0] == "centos":
    #    req = sql_query
89e9fa90   Etienne Pallier   bugfix stderr dan...
294
    mysql_call_root = '"' + MYSQL_EXE_PATH + 'mysql" -u root -p'
3ea57509   Etienne Pallier   install.py cleanup
295
    mysql_call_pyros = "\"" + MYSQL_EXE_PATH+ "mysql\" -u "+SQL_USER+" -p"
f7b38c2d   Etienne Pallier   refactorised inst...
296
    
c1f53cef   Alain Klotz   Update install/in...
297
298
299
    # --------------------------------------------
    # --- Creating database and creating and granting user pyros
    # --------------------------------------------
db7a486d   Alain Klotz   Corrections MySQL.
300
    user_ros_is_created = True
7cf6c127   Etienne Pallier   bugfix stderr dan...
301
    
c1f53cef   Alain Klotz   Update install/in...
302
    if sql_version<5.5:
a00f954f   Alain Klotz   Update install fo...
303
       print(Colors.LOG_BLUE +"------------------ Check if the user pyros exists in MYSQL (type the pyros password) -----------------------------" + Colors.END)
c1f53cef   Alain Klotz   Update install/in...
304
       # --- We are testing if user pyros already exists in the database
a00f954f   Alain Klotz   Update install fo...
305
306
       process = subprocess.Popen("echo quit |" + mysql_call_pyros, shell=True)
       process.wait()
c1f53cef   Alain Klotz   Update install/in...
307
       if (process.returncode == 0):
db7a486d   Alain Klotz   Corrections MySQL.
308
309
           user_ros_is_created = False
    if user_ros_is_created:
c1f53cef   Alain Klotz   Update install/in...
310
       # --- The user pyros must be created in the database
a00f954f   Alain Klotz   Update install fo...
311
       print(Colors.LOG_BLUE +"-----------------------------Please enter your MYSQL root password-----------------------------" + Colors.END)
89e9fa90   Etienne Pallier   bugfix stderr dan...
312
313
       #process = subprocess.Popen("echo \"" + sql_query + "\" |"+ mysql_call_root, shell=True)
       process = subprocess.Popen('echo "' + sql_query + '" | '+ mysql_call_root, shell=True)
a00f954f   Alain Klotz   Update install fo...
314
       process.wait()
f7b38c2d   Etienne Pallier   refactorised inst...
315
    if (process.returncode != 0):
89e9fa90   Etienne Pallier   bugfix stderr dan...
316
        sys.stderr.write(Colors.ERROR + "ERROR !: db configuration failed !" + Colors.END + "\r\n")
f7b38c2d   Etienne Pallier   refactorised inst...
317
        return -1
3ea57509   Etienne Pallier   install.py cleanup
318
    print(Colors.LOG_BLUE + END_OF_LINE+"-----------------------------Database created and user pyros successfully created and granted-----------------------------" + Colors.END)
f7b38c2d   Etienne Pallier   refactorised inst...
319

c1f53cef   Alain Klotz   Update install/in...
320
321
322
    # --------------------------------------------
    # --- Replacing pattern in settings.py to use mysql
    # --------------------------------------------
f7b38c2d   Etienne Pallier   refactorised inst...
323
    print(Colors.LOG_BLUE + "-----------------------------setting MYSQL = True in settings-----------------------------" + Colors.END)
aea334a3   Alain Klotz   Correction du che...
324
    replacePatternInFile("MYSQL = False", "MYSQL = True", os.path.normpath("../src/core/pyros_django/pyros/settings.py"))
6cf96eb4   Etienne Pallier   bugfix install sc...
325
    
4a8f058c   Alain Klotz   Merge branch 'dev...
326
    #print(Colors.LOG_BLUE + "\r\n-----------------------------cd ..-----------------------------" + Colors.END)
244507e6   Unknown   adding migrations...
327
    os.chdir("..")
ca58ff86   Jeremy   Update
328

c1f53cef   Alain Klotz   Update install/in...
329
330
331
    # --------------------------------------------
    # ---  Executing migrations
    # --------------------------------------------
9d37b424   Etienne Pallier   refactorised and ...
332
    
56b06950   Unknown   Install script fi...
333
    print(Colors.LOG_BLUE + "\r\n\r\n-----------------------------Migrate : executing pyros.py init_database-----------------------------" + Colors.END)
9d37b424   Etienne Pallier   refactorised and ...
334
    #TODO: from venv !!!
244507e6   Unknown   adding migrations...
335
    try:
01348735   Etienne Pallier   Bugfix pyros.py s...
336
        #os.system(GLOBAL_PYTHON+" pyros.py init_database")
f4fd20f9   Etienne Pallier   pyros.py script i...
337
        os.system(GLOBAL_PYTHON+" pyros.py initdb")
9d37b424   Etienne Pallier   refactorised and ...
338
        '''
70fcc6ab   Etienne Pallier   install script re...
339
        process = subprocess.Popen(GLOBAL_PYTHON + " pyros.py init_database"  , shell=True)
244507e6   Unknown   adding migrations...
340
        process.wait()
9d37b424   Etienne Pallier   refactorised and ...
341
        '''
244507e6   Unknown   adding migrations...
342
    except Exception as e:
9d37b424   Etienne Pallier   refactorised and ...
343
        print("Exception ", e)
f4fd20f9   Etienne Pallier   pyros.py script i...
344
        print(Colors.ERROR + "Error while initializing database :" + Colors.END)
244507e6   Unknown   adding migrations...
345
        return -1
56b06950   Unknown   Install script fi...
346

244507e6   Unknown   adding migrations...
347
348
349
    print(Colors.LOG_BLUE + "\r\n\r\n-----------------------------Install successfull !-----------------------------" + Colors.END)
    return 0

9d37b424   Etienne Pallier   refactorised and ...
350

2e0c8f94   Unknown   New install scrip...
351
352
353
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
354
355


9d37b424   Etienne Pallier   refactorised and ...
356
357
358



2e0c8f94   Unknown   New install scrip...
359
if __name__ == '__main__':
9d37b424   Etienne Pallier   refactorised and ...
360
361
    if (len(sys.argv) > 1):
        if sys.argv[1] == "--prerequisites" or sys.argv[1] == "-p":
2e0c8f94   Unknown   New install scrip...
362
363
364
            install_required()
        else:
            _help()
9d37b424   Etienne Pallier   refactorised and ...
365
    elif len(sys.argv) == 1:
3ea57509   Etienne Pallier   install.py cleanup
366
367
        if INSTALL_VENV: install_venv(VENV)
        if INSTALL_DB: install_database(VENV)
9d37b424   Etienne Pallier   refactorised and ...
368

2e0c8f94   Unknown   New install scrip...
369
370
    else:
        _help()