-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
113 lines (96 loc) · 3.05 KB
/
init_db.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
#!/usr/bin/python2.7
# -*- coding: us-ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
#
# Copyright (C) 2009 Keith Dart <keith@dartworks.biz>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
"""
Place initial values in database.
"""
import sys
import grp
from pycopia import aid
from router.db import models
from router.db import config
#def do_XXX(session):
# for name, desc, in (
# ("XXX", "XXX"),
# ):
# session.add(models.create(models.XXX, name=name, description=desc))
# session.commit()
SYSTEMGROUPS = [
"users",
"audio",
"cdrom",
"games",
"wheel",
"dialout",
"tape",
"usb",
"video",
"cdrw",
"cron",
"plugdev",
"tcpdump",
]
def do_config(session):
rn = models.create(models.Config, name="root", user=None, parent=None, value=aid.NULL)
session.add(rn)
session.commit()
root = config.Container(session, rn)
root["logfiledir"] = "/var/log"
root["documentroot"] = "/var/www/localhost"
session.commit()
def do_groups(session):
for gid, name in (
(1010, "quagga"),
(1011, "admin"),
):
session.add(models.create(models.Group, groupname=name, gid=gid))
for grname in SYSTEMGROUPS:
try:
grent = grp.getgrnam(grname)
except KeyError:
continue
session.add(models.create(models.Group, groupname=grent.gr_name, gid=grent.gr_gid))
session.commit()
def do_users(session):
for username, uid, gid, gecos, homedir, shell, admin, first, last, pw in (
("admin", 1001, 1011, "Administrator", "/home/admin", "/usr/bin/vtysh", True, "Admin", "User", "gentoo"),
):
session.add(models.create(
models.User, username=username, uid=uid, gid=gid, gecos=gecos, is_admin=admin,
first_name=first, last_name=last, shell=shell, homedir=homedir, password=pw))
session.commit()
def do_assign(session):
glist = list(session.query(models.Group).all())
admin = session.query(models.User).filter(models.User.username=="admin").one()
admin.groups = glist
session.commit()
def init_database(argv):
try:
url = argv[1]
except IndexError:
from pycopia import basicconfig
cf = basicconfig.get_config("database.conf")
url = cf["DATABASE_URL"]
Maker = models.create_session(url)
dbsession = Maker()
try:
do_config(dbsession)
do_groups(dbsession)
do_users(dbsession)
do_assign(dbsession)
finally:
dbsession.close()
if __name__ == "__main__":
init_database(sys.argv)