72 lines
3.0 KiB
Python
72 lines
3.0 KiB
Python
import json
|
|
import os
|
|
import sqlite3
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
|
|
def data_dir():
|
|
root = Path(os.environ.get('AUTODEPLOY_DATA', str(Path(os.environ.get('LOCALAPPDATA', str(Path.home()))) / 'AutoDeploy')))
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
return root
|
|
|
|
|
|
DEFAULTS = dict(name='新服务器', host='', port=22, username='root', password='',
|
|
front='/home/dist', war='/usr/local/tomcat/webapps/ROOT.war',
|
|
tomcat='/usr/local/tomcat', startup='/usr/local/tomcat/bin/startup.sh',
|
|
shutdown='/usr/local/tomcat/bin/shutdown.sh', temp='/home/deploy/tmp',
|
|
backups='/home/deploy/backups', health='', stop_timeout=30,
|
|
start_timeout=120, java_home='', service='', owner='', auto_rollback=True,
|
|
nginx_service='nginx.service', nginx_binary='/usr/sbin/nginx',
|
|
nginx_cert_dir='/home/ssl', nginx_cert_name='', nginx_key_name='',
|
|
local_war='', local_front='', local_cert='', local_key='')
|
|
|
|
|
|
class Store:
|
|
def __init__(self, path=None):
|
|
self.path = path or data_dir() / 'config.db'
|
|
with self.connect() as db:
|
|
db.executescript('''
|
|
CREATE TABLE IF NOT EXISTS profiles(id INTEGER PRIMARY KEY, config TEXT NOT NULL);
|
|
CREATE TABLE IF NOT EXISTS runs(id INTEGER PRIMARY KEY, profile_id INTEGER,
|
|
created TEXT, action TEXT, status TEXT, log TEXT, backup TEXT, config TEXT);
|
|
''')
|
|
|
|
@contextmanager
|
|
def connect(self):
|
|
db = sqlite3.connect(self.path)
|
|
db.row_factory = sqlite3.Row
|
|
try:
|
|
with db:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
def profiles(self):
|
|
with self.connect() as db:
|
|
return [(r['id'], {**DEFAULTS, **json.loads(r['config'])}) for r in db.execute('SELECT * FROM profiles ORDER BY id')]
|
|
|
|
def save(self, pid, config):
|
|
with self.connect() as db:
|
|
raw = json.dumps(config, ensure_ascii=False)
|
|
if pid:
|
|
db.execute('UPDATE profiles SET config=? WHERE id=?', (raw, pid))
|
|
return pid
|
|
return db.execute('INSERT INTO profiles(config) VALUES(?)', (raw,)).lastrowid
|
|
|
|
def delete(self, pid):
|
|
with self.connect() as db:
|
|
db.execute('DELETE FROM profiles WHERE id=?', (pid,))
|
|
|
|
def record(self, pid, action, status, log, backup, config):
|
|
safe = {k: v for k, v in config.items() if k != 'password'}
|
|
with self.connect() as db:
|
|
db.execute('INSERT INTO runs(profile_id,created,action,status,log,backup,config) VALUES(?,?,?,?,?,?,?)',
|
|
(pid, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), action, status, log, backup,
|
|
json.dumps(safe, ensure_ascii=False)))
|
|
|
|
def history(self, pid):
|
|
with self.connect() as db:
|
|
return [dict(r) for r in db.execute('SELECT * FROM runs WHERE profile_id=? ORDER BY id DESC LIMIT 100', (pid,))]
|