42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import datetime
|
|
import pymongo
|
|
import os
|
|
|
|
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
|
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
|
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
|
|
|
|
|
class Mongo:
|
|
def __init__(self):
|
|
url = f"mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:27017/"
|
|
self.client: pymongo.MongoClient = pymongo.MongoClient(url)
|
|
self.database = self.client.get_database("certupdater")
|
|
self.hosts_collection.create_index([
|
|
("host", 1)
|
|
])
|
|
|
|
def __getitem__(self, item):
|
|
return self.database.get_collection(item)
|
|
|
|
@property
|
|
def hosts_collection(self):
|
|
return self["hosts"]
|
|
|
|
@property
|
|
def hosts(self):
|
|
hosts = {}
|
|
for host in self.hosts_collection.find({}):
|
|
hosts[host["host"]] = host
|
|
return hosts
|
|
|
|
def update_date(self, host: str):
|
|
now = datetime.datetime.now()
|
|
if self.hosts_collection.find_one({"host": host}):
|
|
self.hosts_collection.update_one({"host": host}, {"$set": {"update_time": now, "expire_time": now + datetime.timedelta(days=90)}})
|
|
else:
|
|
self.hosts_collection.insert_one({"host": host, "update_time": now, "expire_time": now + datetime.timedelta(days=90)})
|
|
|
|
|
|
mongo = Mongo()
|