Merge branch 'develop' into ci
This commit is contained in:
commit
dc7c88885c
12 changed files with 220 additions and 3 deletions
|
|
@ -0,0 +1,28 @@
|
|||
import docker
|
||||
from docker.models.containers import Container
|
||||
|
||||
from app.model.containers import Tree, Compose, SimpleContainer
|
||||
|
||||
|
||||
class DockerService:
|
||||
def __init__(self):
|
||||
self._client = docker.from_env()
|
||||
self._tree = self._init_tree()
|
||||
|
||||
def _init_tree(self) -> Tree:
|
||||
tree = Tree()
|
||||
container: Container
|
||||
for container in self._client.containers.list():
|
||||
labels = container.labels
|
||||
working_dir = labels.get("com.docker.compose.project.working_dir")
|
||||
if working_dir:
|
||||
if tree.composes.get(working_dir) is None:
|
||||
tree.composes[working_dir] = Compose(working_dir)
|
||||
tree.composes[working_dir].containers.append(SimpleContainer.from_container(container))
|
||||
else:
|
||||
tree.containers.append(SimpleContainer.from_container(container))
|
||||
return tree
|
||||
|
||||
@property
|
||||
def tree(self) -> Tree:
|
||||
return self._tree
|
||||
38
app/services/passwords.py
Normal file
38
app/services/passwords.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import os.path
|
||||
|
||||
from pykeepass import PyKeePass, create_database, Group
|
||||
|
||||
|
||||
class Passwords:
|
||||
def __init__(self):
|
||||
from app.config import get_settings
|
||||
settings = get_settings()
|
||||
|
||||
with open(settings.kp.secret, "r") as fh:
|
||||
secret = fh.read()
|
||||
|
||||
self._kp_org = self.__get_or_create_store(settings.kp.file, secret)
|
||||
self._kp = self.__get_lock(settings.kp.file, secret)
|
||||
|
||||
@staticmethod
|
||||
def __get_or_create_store(path, passwd) -> PyKeePass:
|
||||
if os.path.exists(path):
|
||||
return PyKeePass(
|
||||
path,
|
||||
password=passwd,
|
||||
)
|
||||
return create_database(path, passwd)
|
||||
|
||||
@staticmethod
|
||||
def __get_lock(path, passwd) -> PyKeePass:
|
||||
lock_path = path + ".lock"
|
||||
import shutil
|
||||
shutil.copyfile(path, lock_path)
|
||||
return Passwords.__get_or_create_store(lock_path, passwd)
|
||||
|
||||
@property
|
||||
def store(self):
|
||||
return self._kp.root_group
|
||||
|
||||
def save(self, group: Group):
|
||||
pass
|
||||
1
app/services/system.py
Normal file
1
app/services/system.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
from git import Repo, Remote
|
||||
|
||||
from app.config import get_settings
|
||||
|
||||
|
||||
class GitService:
|
||||
def __init__(self):
|
||||
self._settings = get_settings()
|
||||
self._repo = Repo(self._settings.git.directory)
|
||||
self._origin: Remote = self._repo.remotes.origin
|
||||
|
||||
def get_modified_compose(self) -> str | None:
|
||||
self._update()
|
||||
return self._diff()
|
||||
|
||||
def _update(self):
|
||||
self._origin.pull()
|
||||
|
||||
def _diff(self) -> str | None:
|
||||
diff = self._repo.head.commit.diff("HEAD~1")
|
||||
composes = [f for f in diff if f.a_path.endswith("docker-compose.yml")]
|
||||
match len(composes):
|
||||
case 0:
|
||||
return None
|
||||
case 1:
|
||||
return composes[0].a_path
|
||||
case _:
|
||||
raise Exception("Multiple compose files modified")
|
||||
Loading…
Add table
Add a link
Reference in a new issue