forked from harsxv/tinystatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
596 lines (497 loc) · 20.1 KB
/
manage.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import click
import os
import sys
from pathlib import Path
import yaml
from datetime import datetime
import json
from app.config import get_settings
import os
from dotenv import load_dotenv, set_key
from app.database import User, ServiceHealthCheck, get_db
from datetime import datetime, timedelta
from sqlalchemy.sql import func
import secrets
import string
# Add app directory to path
sys.path.append(str(Path(__file__).parent))
from app.database import init_db, get_db, Base, engine
from app.config import get_settings
settings = get_settings()
@click.group()
def cli():
"""StatusWatch Management CLI"""
pass
@cli.command()
@click.option('--username', prompt=True, help='Admin username')
@click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True, help='Admin password')
def createuser(username, password):
"""Create a new admin user"""
try:
from app.database import User
db = next(get_db())
# Check if user exists
if db.query(User).filter(User.username == username).first():
click.echo(f"User {username} already exists!", err=True)
return
# Create user
user = User(username=username)
user.set_password(password)
db.add(user)
user.generate_token()
db.commit()
click.echo(f"Created user: {username}")
except Exception as e:
click.echo(f"Error creating user: {str(e)}", err=True)
@cli.command()
def initdb():
"""Initialize the database"""
try:
init_db()
click.echo("Database initialized successfully!")
except Exception as e:
click.echo(f"Error initializing database: {str(e)}", err=True)
@cli.command()
def resetdb():
"""Reset the database (WARNING: This will delete all data)"""
if click.confirm('Are you sure you want to reset the database? This will delete all data!'):
try:
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
click.echo("Database reset successfully!")
except Exception as e:
click.echo(f"Error resetting database: {str(e)}", err=True)
@cli.command()
@click.argument('output', type=click.Path())
def backup(output):
"""Backup the database and configuration"""
try:
from app.database import ServiceHealthCheck
db = next(get_db())
# Get all data
checks = db.query(ServiceHealthCheck).all()
# Prepare backup data
backup_data = {
'timestamp': datetime.utcnow().isoformat(),
'version': '1.1.0',
'checks': [
{
'timestamp': check.timestamp.isoformat(),
'service_name': check.service_name,
'service_group': check.service_group,
'status': check.status,
'response_time': check.response_time,
'extra_data': check.extra_data
}
for check in checks
]
}
# Save backup
with open(output, 'w') as f:
json.dump(backup_data, f, indent=2)
click.echo(f"Backup saved to: {output}")
except Exception as e:
click.echo(f"Error creating backup: {str(e)}", err=True)
@cli.command()
@click.argument('backup_file', type=click.Path(exists=True))
def restore(backup_file):
"""Restore from a backup file"""
if click.confirm('This will overwrite existing data. Continue?'):
try:
from app.database import ServiceHealthCheck
db = next(get_db())
# Load backup
with open(backup_file, 'r') as f:
backup_data = json.load(f)
# Clear existing data
db.query(ServiceHealthCheck).delete()
# Restore checks
for check_data in backup_data['checks']:
check = ServiceHealthCheck(
timestamp=datetime.fromisoformat(check_data['timestamp']),
service_name=check_data['service_name'],
service_group=check_data['service_group'],
status=check_data['status'],
response_time=check_data['response_time'],
extra_data=check_data['extra_data']
)
db.add(check)
db.commit()
click.echo("Backup restored successfully!")
except Exception as e:
click.echo(f"Error restoring backup: {str(e)}", err=True)
@cli.command()
def checkconfig():
"""Validate configuration files"""
try:
# Check checks.yaml
if os.path.exists('checks.yaml'):
with open('checks.yaml', 'r') as f:
yaml.safe_load(f)
click.echo("✓ checks.yaml is valid")
else:
click.echo("⨯ checks.yaml not found", err=True)
# Check incidents.md
if os.path.exists('incidents.md'):
with open('incidents.md', 'r') as f:
f.read()
click.echo("✓ incidents.md is valid")
else:
click.echo("⨯ incidents.md not found", err=True)
# Check environment variables
required_vars = ['MONITOR_CONTINUOUSLY', 'CHECK_INTERVAL', 'MAX_HISTORY_ENTRIES']
for var in required_vars:
if hasattr(settings, var):
click.echo(f"✓ {var} is set")
else:
click.echo(f"⨯ {var} is not set", err=True)
except Exception as e:
click.echo(f"Error checking configuration: {str(e)}", err=True)
@cli.command()
def shell():
"""Start an interactive shell with app context"""
import code
from app.database import ServiceHealthCheck, User
from app.services.monitor import StatusMonitor
context = {
'db': next(get_db()),
'ServiceHealthCheck': ServiceHealthCheck,
'User': User,
'StatusMonitor': StatusMonitor,
'settings': settings
}
banner = "StatusWatch Interactive Shell\n" + \
"Available objects: db, ServiceHealthCheck, User, StatusMonitor, settings"
code.interact(banner=banner, local=context)
@cli.group()
def token():
"""Manage API tokens"""
pass
@token.command()
@click.argument('username')
@click.option('--expires', type=int, help='Token expiry in days')
def create(username, expires):
"""Create a new API token for a user"""
try:
db = next(get_db())
user = db.query(User).filter(User.username == username).first()
if not user:
click.echo(f"User {username} not found!", err=True)
return
token = user.generate_token(expires_in_days=expires)
db.commit()
click.echo(f"Token generated for {username}:")
click.echo(f"Token: {token}")
if expires:
click.echo(f"Expires: {user.token_expiry}")
else:
click.echo("Token never expires")
except Exception as e:
click.echo(f"Error generating token: {str(e)}", err=True)
@token.command()
@click.argument('username')
def revoke(username):
"""Revoke a user's API token"""
try:
db = next(get_db())
user = db.query(User).filter(User.username == username).first()
if not user:
click.echo(f"User {username} not found!", err=True)
return
if not user.api_token:
click.echo(f"User {username} has no active token!", err=True)
return
user.revoke_token()
db.commit()
click.echo(f"Token revoked for {username}")
except Exception as e:
click.echo(f"Error revoking token: {str(e)}", err=True)
@token.command()
@click.argument('username')
def info(username):
"""Show token information for a user"""
try:
db = next(get_db())
user = db.query(User).filter(User.username == username).first()
if not user:
click.echo(f"User {username} not found!", err=True)
return
if not user.api_token:
click.echo(f"User {username} has no active token")
return
click.echo(f"Token information for {username}:")
click.echo(f"Token: {user.api_token}")
if user.token_expiry:
remaining = user.token_expiry - datetime.utcnow()
if remaining.total_seconds() > 0:
click.echo(f"Expires in: {remaining.days} days, {remaining.seconds//3600} hours")
else:
click.echo("Token has expired")
else:
click.echo("Token never expires")
click.echo(f"Valid: {'Yes' if user.is_token_valid() else 'No'}")
except Exception as e:
click.echo(f"Error getting token info: {str(e)}", err=True)
@token.command()
def list():
"""List all users and their token status"""
try:
db = next(get_db())
users = db.query(User).all()
if not users:
click.echo("No users found")
return
click.echo("\nUser Token Status:")
click.echo("-" * 50)
for user in users:
status = "No token"
if user.api_token:
if user.is_token_valid():
status = "Valid"
if user.token_expiry:
remaining = user.token_expiry - datetime.utcnow()
if remaining.total_seconds() > 0:
status += f" (Expires in {remaining.days}d {remaining.seconds//3600}h)"
else:
status = "Expired"
click.echo(f"{user.username}: {status}")
except Exception as e:
click.echo(f"Error listing tokens: {str(e)}", err=True)
@cli.group()
def auth():
"""Manage authentication settings"""
pass
@auth.command()
def status():
"""Show current auth status"""
settings = get_settings()
click.echo(f"Authentication is currently: {'enabled' if settings.AUTH_ENABLED else 'disabled'}")
if settings.AUTH_ENABLED:
db = next(get_db())
users = db.query(User).count()
click.echo(f"Number of users: {users}")
@auth.command()
def enable():
"""Enable authentication"""
env_file = '.env'
try:
# Check if we can write to the file
try:
with open(env_file, 'a') as f:
pass
except (IOError, PermissionError):
click.echo(f"Error: Cannot write to {env_file}. Please check permissions.", err=True)
return
load_dotenv(env_file)
set_key(env_file, 'AUTH_ENABLED', 'true')
click.echo("Authentication enabled")
click.echo("Please restart the application for changes to take effect")
# Check if there are any users
db = next(get_db())
if db.query(User).count() == 0:
click.echo("\nWarning: No users exist. Create a user with:")
click.echo("python manage.py createuser")
except Exception as e:
click.echo(f"Error enabling authentication: {str(e)}", err=True)
click.echo("\nTip: You can manually set AUTH_ENABLED=true in your environment")
@auth.command()
def disable():
"""Disable authentication"""
if click.confirm('Are you sure you want to disable authentication? This will make your instance public!'):
env_file = '.env'
try:
# Check if we can write to the file
try:
with open(env_file, 'a') as f:
pass
except (IOError, PermissionError):
click.echo(f"Error: Cannot write to {env_file}. Please check permissions.", err=True)
return
load_dotenv(env_file)
set_key(env_file, 'AUTH_ENABLED', 'false')
click.echo("Authentication disabled")
click.echo("Please restart the application for changes to take effect")
except Exception as e:
click.echo(f"Error disabling authentication: {str(e)}", err=True)
click.echo("\nTip: You can manually set AUTH_ENABLED=false in your environment")
@auth.command()
def setup():
"""Interactive auth setup"""
click.echo("StatusWatch Authentication Setup")
click.echo("-" * 30)
# Check if we can write to .env
env_file = '.env'
try:
try:
with open(env_file, 'a') as f:
pass
except (IOError, PermissionError):
click.echo(f"Warning: Cannot write to {env_file}. Authentication settings must be set manually.", err=True)
click.echo("Please set AUTH_ENABLED=true in your environment")
except Exception as e:
click.echo(f"Warning: {str(e)}", err=True)
try:
# Enable auth if we can
if os.access(env_file, os.W_OK):
load_dotenv(env_file)
set_key(env_file, 'AUTH_ENABLED', 'true')
# Create admin user if none exists
db = next(get_db())
if db.query(User).count() == 0:
click.echo("\nNo users exist. Let's create an admin user.")
username = click.prompt("Username")
password = click.prompt("Password", hide_input=True, confirmation_prompt=True)
user = User(username=username)
user.set_password(password)
db.add(user)
db.commit()
# Generate API token
token = user.generate_token()
db.commit()
click.echo("\nUser created successfully!")
click.echo(f"API Token: {token}")
click.echo("\nAuthentication setup complete!")
if os.access(env_file, os.W_OK):
click.echo("Please restart the application for changes to take effect")
else:
click.echo("\nNote: You need to manually set AUTH_ENABLED=true in your environment")
except Exception as e:
click.echo(f"Error during setup: {str(e)}", err=True)
click.echo("\nTip: You can manually configure authentication settings in your environment")
@cli.group()
def db():
"""Database management commands"""
pass
@db.command()
@click.option('--group', help='Group name to remove entries for')
@click.option('--service', help='Service name to remove entries for')
@click.option('--older-than', type=int, help='Remove entries older than X days')
@click.option('--dry-run', is_flag=True, help='Show what would be deleted without actually deleting')
def cleanup(group, service, older_than, dry_run):
"""Remove entries from database by group or service name"""
try:
db = next(get_db())
query = db.query(ServiceHealthCheck)
# Build query based on filters
filters = []
if group:
filters.append(ServiceHealthCheck.service_group.contains(group))
if service:
filters.append(ServiceHealthCheck.service_name.contains(service))
if older_than:
cutoff = datetime.utcnow() - timedelta(days=older_than)
filters.append(ServiceHealthCheck.timestamp < cutoff)
if not filters:
click.echo("Error: Please specify at least one filter (--group, --service, or --older-than)", err=True)
return
# Apply all filters
query = query.filter(*filters)
# Count entries that would be deleted
count = query.count()
if count == 0:
click.echo("No matching entries found")
return
# Show what would be deleted
click.echo(f"\nFound {count} entries to delete:")
# Sample of entries to be deleted
sample = query.limit(5).all()
for entry in sample:
click.echo(f"- {entry.service_group}/{entry.service_name} ({entry.timestamp})")
if count > 5:
click.echo(f"... and {count - 5} more entries")
# Confirm and delete if not dry run
if not dry_run:
if click.confirm('\nDo you want to delete these entries?'):
deleted = query.delete()
db.commit()
click.echo(f"\nDeleted {deleted} entries")
else:
click.echo("\nOperation cancelled")
else:
click.echo("\nDry run - no entries were deleted")
except Exception as e:
click.echo(f"Error cleaning up database: {str(e)}", err=True)
@db.command()
def stats():
"""Show database statistics"""
try:
db = next(get_db())
# Get total entries
total_entries = db.query(ServiceHealthCheck).count()
# Get entries by group
group_stats = db.query(
ServiceHealthCheck.service_group,
func.count(ServiceHealthCheck.id).label('count')
).group_by(ServiceHealthCheck.service_group).all()
# Get date range
oldest = db.query(func.min(ServiceHealthCheck.timestamp)).scalar()
newest = db.query(func.max(ServiceHealthCheck.timestamp)).scalar()
# Display statistics
click.echo("\nDatabase Statistics")
click.echo("-" * 50)
click.echo(f"Total entries: {total_entries}")
click.echo(f"Date range: {oldest} to {newest}")
click.echo("\nEntries by group:")
for group, count in group_stats:
click.echo(f"- {group}: {count}")
# Calculate database size
db_path = settings.PRIMARY_DATABASE_URL.replace('sqlite:///', '')
if os.path.exists(db_path):
size = os.path.getsize(db_path)
click.echo(f"\nDatabase size: {size / 1024 / 1024:.2f} MB")
except Exception as e:
click.echo(f"Error getting database stats: {str(e)}", err=True)
@cli.command()
@click.option('--username', default='admin', help='Admin username (default: admin)')
def setupadmin(username):
"""Create default admin user with random password and API token"""
try:
from app.database import User
db = next(get_db())
# Generate a secure random password
password_chars = string.ascii_letters + string.digits + "!@#$%^&*"
password = ''.join(secrets.choice(password_chars) for _ in range(16))
# Check if user exists
user = db.query(User).filter(User.username == username).first()
if user:
click.echo(f"User {username} already exists!", err=True)
if click.confirm('Do you want to reset the password and generate a new token?'):
user.set_password(password)
token = user.generate_token()
else:
return
else:
# Create new user
user = User(username=username)
user.set_password(password)
db.add(user)
token = user.generate_token()
db.commit()
# Prepare credentials data
credentials = {
"username": username,
"password": password,
"api_token": token,
"created_at": datetime.utcnow().isoformat(),
"auth_enabled": settings.AUTH_ENABLED
}
# Save credentials to JSON file in data folder
credentials_file = settings.DATA_FOLDER / 'admin_credentials.json'
with open(credentials_file, 'w') as f:
json.dump(credentials, f, indent=2)
click.echo(f"\nDefault admin user setup complete:")
click.echo(f"Username: {username}")
click.echo(f"Password: {password}")
click.echo(f"API Token: {token}")
click.echo(f"Credentials saved to: {credentials_file.absolute()}")
# Show warning if authentication is disabled
if not settings.AUTH_ENABLED:
click.echo("\nWarning: Authentication is currently disabled!")
click.echo("Run 'python manage.py auth enable' to enable authentication")
# Show example curl command
click.echo("\nExample API call:")
click.echo(f"curl -H 'Authorization: Bearer {token}' http://localhost:8000/api/status")
except Exception as e:
click.echo(f"Error setting up admin user: {str(e)}", err=True)
if __name__ == '__main__':
cli()