|
1 #!/usr/bin/env python2.7 |
|
2 |
|
3 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
4 # |
|
5 # This file is part of Iro. |
|
6 # |
|
7 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
8 # this software and associated documentation files (the "Software"), to deal in |
|
9 # the Software without restriction, including without limitation the rights to use, |
|
10 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
11 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
12 # subject to the following conditions: |
|
13 # |
|
14 # The above copyright notice and this permission notice shall be included in |
|
15 # all copies or substantial portions of the Software. |
|
16 # |
|
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
18 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
19 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
23 |
|
24 from twisted.python import log |
|
25 |
|
26 from sqlalchemy import create_engine, pool |
|
27 from sqlalchemy.exc import ArgumentError |
|
28 import sys,os |
|
29 import random |
|
30 |
|
31 import logging |
|
32 |
|
33 from iro.controller.pool import dbPool |
|
34 from iro import config, __version__, install |
|
35 from iro.model.schema import Offer, User, Userright |
|
36 from iro.model.utils import WithSession |
|
37 |
|
38 observer = log.PythonLoggingObserver() |
|
39 observer.start() |
|
40 |
|
41 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
|
42 |
|
43 import argparse |
|
44 |
|
45 parser = argparse.ArgumentParser(description='Iro adduser.') |
|
46 parser.add_argument('-v', '--version', action='version', version="%s %s"%(os.path.basename(sys.argv[0]),__version__)) |
|
47 parser.add_argument('name', help="username") |
|
48 parser.add_argument('offers', nargs='+', help="offers for user") |
|
49 args = parser.parse_args() |
|
50 |
|
51 if not install.checkConfig(): |
|
52 logging.error("iro.conf is not in right format.") |
|
53 logging.info("Please edit iro.conf") |
|
54 sys.exit(1) |
|
55 |
|
56 config.init() |
|
57 |
|
58 try: |
|
59 engine = create_engine(config.main.dburl, |
|
60 poolclass = pool.SingletonThreadPool, pool_size=dbPool.maxthreads, ) |
|
61 except ArgumentError: |
|
62 logging.error("Can't connect to database") |
|
63 logging.info("Please edit iro.conf") |
|
64 sys.exit(1) |
|
65 |
|
66 |
|
67 if not install.checkDatabaseConnection(): |
|
68 logging.error("Can't connect to database") |
|
69 logging.info("Please edit iro.conf") |
|
70 sys.exit(1) |
|
71 |
|
72 if not install.checkDatabase(): |
|
73 logging.error("Database not in right format.") |
|
74 logging.info("Please edit iro.conf and run iro-install --install") |
|
75 sys.exit(1) |
|
76 |
|
77 |
|
78 #test offers |
|
79 with WithSession(engine) as session: |
|
80 available_offers = [i[0] for i in session.query(Offer.name)] |
|
81 |
|
82 error = False |
|
83 for o in args.offers: |
|
84 if o not in available_offers: |
|
85 logging.error("Unknown offer: '%s'"%o) |
|
86 error = True |
|
87 |
|
88 if error: |
|
89 sys.exit(1) |
|
90 |
|
91 #create apikey |
|
92 apikey = "".join([random.choice("0123456789abcdef") for i in range(15)]) |
|
93 |
|
94 #adduser to database |
|
95 with WithSession(engine, False) as session: |
|
96 u = User(name=args.name, apikey=apikey) |
|
97 session.add(u) |
|
98 for i,offer in enumerate(args.offers): |
|
99 o = session.query(Offer).filter_by(name=offer).first() |
|
100 u.rights.append(Userright(o,default=i+1)) |
|
101 session.commit() |
|
102 logging.info("created user %s:"% args.name) |
|
103 logging.info("apikey = %s"% apikey) |
|
104 logging.info("offers = %s"% ", ".join(args.offers)) |