|
1 | | -import click |
2 | | - |
3 | | -import sys |
4 | 1 | import os |
5 | | -import shutil |
6 | | -import yaml |
7 | | -import logging |
8 | | - |
9 | | -# Define version and directories *before* importing submodules |
10 | | -here = os.path.abspath(os.path.dirname(__file__)) |
11 | 2 |
|
12 | 3 | __version__ = '' |
13 | | - |
| 4 | +here = os.path.abspath(os.path.dirname(__file__)) |
14 | 5 | with open(os.path.join(here, 'VERSION')) as fp: |
15 | 6 | __version__ = fp.read() |
16 | 7 |
|
17 | | - |
18 | | -# default parameters for click.Path |
19 | | -pparams = { |
20 | | - 'exists': False, |
21 | | - 'file_okay': False, |
22 | | - 'dir_okay': True, |
23 | | -} |
24 | | - |
25 | | -from . import server |
26 | | -from .server import fixtures |
27 | | -from .server import db |
28 | | -from . import client |
29 | | -from . import utest |
30 | | -from . import util |
31 | | - |
32 | 8 | APPNAME = 'pytaskmanager' |
33 | | - |
34 | | -# ------------------------------------------------------------------------------ |
35 | | -# helper functions |
36 | | -# ------------------------------------------------------------------------------ |
37 | | -def get_config_location(ctx, config, force_create): |
38 | | - """Ensure configuration file exists and return its location.""" |
39 | | - if config is None: |
40 | | - # Get the location of config.yaml if not provided |
41 | | - filename = ctx.config_file |
42 | | - else: |
43 | | - # Use the config file provided as argument |
44 | | - filename = config |
45 | | - |
46 | | - # Check that the config file exists and create it if necessary, but |
47 | | - # only if it was not explicitly provided! |
48 | | - if not os.path.exists(filename): |
49 | | - # We will always create a configuration file at the default location |
50 | | - # when necessary. |
51 | | - if config and not force_create: |
52 | | - click.echo("Configuration file '{}' does not exist and '--force-create' not specified!".format(filename)) |
53 | | - click.echo("Aborting ...") |
54 | | - sys.exit(1) |
55 | | - |
56 | | - # Make sure the directory exists |
57 | | - dirname = os.path.dirname(filename) |
58 | | - |
59 | | - if dirname: |
60 | | - os.makedirs(dirname, exist_ok=True) |
61 | | - |
62 | | - # Copy a default config file |
63 | | - if ctx.instance_type == 'server': |
64 | | - skeleton_file = 'server_config_skeleton.yaml' |
65 | | - elif ctx.instance_type == 'client': |
66 | | - skeleton_file = 'client_config_skeleton.yaml' |
67 | | - elif ctx.instance_type == 'unittest': |
68 | | - skeleton_file = 'unittest_config_skeleton.yaml' |
69 | | - |
70 | | - src = os.path.join(here, '_data', skeleton_file) |
71 | | - dst = os.path.join(filename) |
72 | | - shutil.copy(src, dst) |
73 | | - |
74 | | - if ctx.instance_type == 'server': |
75 | | - with open(dst, 'r') as fp: |
76 | | - cfg = yaml.load(fp) |
77 | | - print('-' * 80) |
78 | | - print(cfg) |
79 | | - print('-' * 80) |
80 | | - |
81 | | - cfg['application']['logging']['file'] = ctx.instance_name + '.log' |
82 | | - |
83 | | - with open(dst, 'w') as fp: |
84 | | - yaml.dump(cfg, fp) |
85 | | - |
86 | | - return filename |
87 | | - |
88 | | - |
89 | | -@click.group() |
90 | | -def cli(): |
91 | | - """Main entry point for CLI scripts.""" |
92 | | - pass |
93 | | - |
94 | | - |
95 | | -# ------------------------------------------------------------------------------ |
96 | | -# ptm test |
97 | | -# ------------------------------------------------------------------------------ |
98 | | -@cli.command(name='test') |
99 | | -@click.option('-c', '--config', default=None, type=click.Path(), help='location of the config file') |
100 | | -def cli_test(config): |
101 | | - """Run unit tests.""" |
102 | | - ctx = util.AppContext(APPNAME, 'unittest') |
103 | | - cfg_filename = get_config_location(ctx, config=None, force_create=False) |
104 | | - ctx.init(cfg_filename) |
105 | | - utest.run() |
106 | | - |
107 | | - |
108 | | -# ------------------------------------------------------------------------------ |
109 | | -# ptm server |
110 | | -# ------------------------------------------------------------------------------ |
111 | | -@cli.group(name='server') |
112 | | -def cli_server(): |
113 | | - """Subcommand `ptm server`.""" |
114 | | - pass |
115 | | - |
116 | | - |
117 | | -@cli_server.command(name='start') |
118 | | -@click.option('-n', '--name', default='default', help='server instance to use') |
119 | | -@click.option('-c', '--config', default=None, help='filename of config file; overrides --name if provided') |
120 | | -@click.option('-e', '--environment', default='test', help='database environment to use') |
121 | | -@click.option('--ip', default='0.0.0.0', help='ip address to listen on') |
122 | | -@click.option('-p', '--port', default=5000, help='port to listen on') |
123 | | -@click.option('--debug/--no-debug', default=True, help='run server in debug mode (auto-restart)') |
124 | | -@click.option('--force-create', is_flag=True, help='Force creation of config file') |
125 | | -def cli_server_start(name, config, environment, ip, port, debug, force_create): |
126 | | - """Start the server.""" |
127 | | - click.echo("Starting server ...") |
128 | | - ctx = util.ServerContext(APPNAME, 'default') |
129 | | - # Load configuration and initialize logging system |
130 | | - cfg_filename = get_config_location(ctx, config, force_create) |
131 | | - ctx.init(cfg_filename, environment) |
132 | | - |
133 | | - # Load the flask.Resources |
134 | | - server.init_resources(ctx) |
135 | | - # Run the server |
136 | | - server.run(ctx, ip, port, debug=debug) |
137 | | - |
138 | | - |
139 | | -@cli_server.command(name='config_location') |
140 | | -@click.option('-n', '--name', default='default', help='server instance to use') |
141 | | -def cli_server_configlocation(name): |
142 | | - """Print the location of the default config file.""" |
143 | | - # ctx = util.AppContext(APPNAME, 'server', name) |
144 | | - ctx = util.ServerContext(APPNAME, 'default') |
145 | | - cfg_filename = get_config_location(ctx, config=None, force_create=False) |
146 | | - click.echo('{}'.format(cfg_filename)) |
147 | | - |
148 | | - |
149 | | -@cli_server.command(name='passwd') |
150 | | -@click.option('-n', '--name', default='default', help='server instance to use') |
151 | | -@click.option('-c', '--config', default=None, help='filename of config file; overrides --name if provided') |
152 | | -@click.option('-e', '--environment', default='test', help='database environment to use') |
153 | | -@click.option('-p', '--password', prompt='Password', hide_input=True) |
154 | | -def cli_server_passwd(name, config, environment, password): |
155 | | - """Set the root password.""" |
156 | | - log = logging.getLogger('ptm') |
157 | | - |
158 | | - ctx = util.AppContext(APPNAME, 'server', name) |
159 | | - |
160 | | - # Load configuration and initialize logging system |
161 | | - cfg_filename = get_config_location(ctx, config) |
162 | | - ctx.init(cfg_filename, environment) |
163 | | - |
164 | | - uri = ctx.get_database_location() |
165 | | - db.init(uri) |
166 | | - |
167 | | - try: |
168 | | - root = db.User.getByUsername('root') |
169 | | - except Exception as e: |
170 | | - log.info("Creating user root") |
171 | | - root = db.User(username='root') |
172 | | - |
173 | | - log.info("Setting password for root") |
174 | | - root.set_password(password) |
175 | | - root.save() |
176 | | - |
177 | | - log.info("[DONE]") |
178 | | - |
179 | | - |
180 | | -@cli_server.command(name='load_fixtures') |
181 | | -@click.option('-n', '--name', default='default', help='server instance to use') |
182 | | -@click.option('-e', '--environment', default='test', help='database environment to use') |
183 | | -@click.option('-c', '--config', default=None, help='filename of config file; overrides --name if provided') |
184 | | -def cli_server_load_fixtures(name, environment, config): |
185 | | - """Load fixtures for testing.""" |
186 | | - click.echo("Loading fixtures.") |
187 | | - # ctx = util.AppContext(APPNAME, 'server', name) |
188 | | - ctx = util.ServerContext(APPNAME, 'default') |
189 | | - |
190 | | - # Load configuration and initialize logging system |
191 | | - cfg_filename = get_config_location(ctx, config, force_create=False) |
192 | | - ctx.init(cfg_filename, environment) |
193 | | - |
194 | | - fixtures.init(ctx) |
195 | | - fixtures.create() |
196 | | - |
197 | | - |
198 | | -# ------------------------------------------------------------------------------ |
199 | | -# ptm client |
200 | | -# ------------------------------------------------------------------------------ |
201 | | -@cli.group(name='client') |
202 | | -def cli_client(): |
203 | | - """Subcommand `ptm client`.""" |
204 | | - pass |
205 | | - |
206 | | - |
207 | | -@cli_client.command(name='config_location') |
208 | | -@click.option('-n', '--name', default='default', help='client instance to use') |
209 | | -def cli_server_configlocation(name): |
210 | | - """Print the location of the default config file.""" |
211 | | - ctx = util.AppContext(APPNAME, 'client', name) |
212 | | - cfg_filename = get_config_location(ctx, config=None, force_create=False) |
213 | | - click.echo('{}'.format(cfg_filename)) |
214 | | - |
215 | | - |
216 | | -@cli_client.command(name='start') |
217 | | -@click.option('-n', '--name', default='default', help='client instance to use') |
218 | | -@click.option('-c', '--config', default=None, help='filename of config file; overrides --name if provided') |
219 | | -def cli_client_start(name, config): |
220 | | - """Start the client.""" |
221 | | - ctx = util.AppContext(APPNAME, 'client', name) |
222 | | - |
223 | | - # Load configuration and initialize logging system |
224 | | - cfg_filename = get_config_location(ctx, config, force_create=False) |
225 | | - ctx.init(cfg_filename) |
226 | | - |
227 | | - # Run the client |
228 | | - client.run(ctx) |
0 commit comments