1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
import sys
|
9
|
from eolesql.log import log_console
|
10
|
from creole.eosfunc import gen_random, load_container_var
|
11
|
from creole.config import VIRTDISABLED
|
12
|
from eolesql.config import ROOT_USERNAME, HOST
|
13
|
from mysql_pwd import mysql_root_passwd
|
14
|
from pyeole.process import system_code
|
15
|
|
16
|
mysql_code="""CREATE DATABASE %s;
|
17
|
GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s';
|
18
|
flush privileges;
|
19
|
"""
|
20
|
|
21
|
if __name__ == "__main__":
|
22
|
|
23
|
if '-h' in sys.argv or '--help' in sys.argv:
|
24
|
cmd = "\"dbname\" \"dbuser\" \"dbpass\""
|
25
|
if not VIRTDISABLED:
|
26
|
cmd += " [\"container\"]"
|
27
|
print """Utilisation :
|
28
|
|
29
|
* Mode interactif :
|
30
|
%s
|
31
|
|
32
|
* Mode non interactif
|
33
|
%s %s
|
34
|
""" % (sys.argv[0], sys.argv[0], cmd)
|
35
|
sys.exit()
|
36
|
|
37
|
if len(sys.argv) == 4 or (not VIRTDISABLED and len(sys.argv) == 5):
|
38
|
database = sys.argv[1]
|
39
|
user = sys.argv[2]
|
40
|
password = sys.argv[3]
|
41
|
if not VIRTDISABLED and len(sys.argv) == 5:
|
42
|
container = sys.argv[4]
|
43
|
else:
|
44
|
container = "root"
|
45
|
else:
|
46
|
database = raw_input("Nom de la base de données à créer : ")
|
47
|
user = raw_input("Nom de l'utilisateur Mysql administrant la base : ")
|
48
|
password = raw_input("Mot de passe de l'utilisateur Mysql administrant \
|
49
|
la base : ")
|
50
|
if not VIRTDISABLED:
|
51
|
container = raw_input("Nom du conteneur source : ")
|
52
|
|
53
|
|
54
|
root_password = gen_random(10)
|
55
|
mysql_root_passwd(root_password)
|
56
|
|
57
|
|
58
|
log_console("## Création de la base %s ##" % (database, ))
|
59
|
container_ip = load_container_var().get('container_ip_%s' % container,
|
60
|
'localhost')
|
61
|
code = mysql_code % (database, database, user, container_ip, password)
|
62
|
|
63
|
system_code(["/usr/bin/mysql", "-p%s" % root_password,
|
64
|
"-u%s" % ROOT_USERNAME, "-h%s" % HOST, "mysql"], stdin=code)
|