Update: dockerize app, rename pkg to mgrctl, fix imports

This commit is contained in:
Stepan Zhukovsky 2024-06-03 18:58:27 +09:00
parent 34e8118327
commit 0f96d0f956
50 changed files with 309 additions and 94 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
docker-compose.yml
.gitignore
.env
.git
.cache

1
.gitignore vendored
View File

@ -163,3 +163,4 @@ cython_debug/
# Project specific
config.json
**/dummy_platform

View File

@ -1,10 +1,59 @@
# app/Dockerfile
# pull the official docker image
FROM python:3.11-alpine as poetry-base
# default build args
ARG APP_VERSION=0.1.0 \
APP_DIR=/app \
SRC_DIR=./mgrctl \
PKG_NAME=mgrctl \
PKG_VERSION=0.1.0
# set env variables
ENV APP_NAME=mgrctl \
# Python3:
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
# Pip:
PIP_NO_CACHE_DIR=on \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_ROOT_USER_ACTION=ignore \
PIP_DEFAULT_TIMEOUT=100 \
# Poetry:
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local' \
POETRY_VERSION=1.7.1
# install system deps
RUN apk --no-cache add curl \
&& curl -sSL https://install.python-poetry.org | python3 - \
&& mkdir ${APP_DIR}
COPY ./pyproject.toml ./poetry.lock ./README.md ${APP_DIR}
COPY ${SRC_DIR} ${APP_DIR}/${PKG_NAME}
# set workdir
WORKDIR ${APP_DIR}
# build pkg whl
RUN poetry build --format wheel \
&& pip install ${APP_DIR}/dist/${PKG_NAME}-${PKG_VERSION}-py3-none-any.whl \
&& rm -r /usr/local/venv
# now multistage builds
FROM python:3.11-alpine
WORKDIR /app
# copy app and dependences
COPY --from=poetry-base /usr/local/ /usr/local/
COPY requirements.txt ./
RUN pip install --no-cache-dir --root-user-action=ignore -r requirements.txt
# install bash and mgrctl shell completion
RUN apk --no-cache add bash \
&& echo 'eval "$(_MGRCTL_COMPLETE=bash_source mgrctl)"' > ~/.bashrc
COPY main.py .
CMD [ "python", "-u", "main.py" ]
# "demonize" container
# use docker attach mgrctl or docker exec -it mgrctl mgrctl --help for example
CMD [ "bash"]

59
docker-compose.yaml Normal file
View File

@ -0,0 +1,59 @@
# ? docker-compose.yml for development environment
# ! To start development you need to create a directory ./dummy_platform.
# ? Place files from the test platform into it:
# ? VM6:
# ? /opt/ispsystem/vm/config.json - configuration file
# ? /opt/ispsystem/vm/mysql - database directory
# ? DCI6:
# ? /opt/ispsystem/dci/config.json - configuration file
# ? /opt/ispsystem/dci/mysql - database directory
# ? Create ./.env file and fill it with required vars:
# ? PLATFORM_TYPE='vm'
# ? Database container:
# ? MYSQL_DATABASE="database name"
# ? MYSQL_ROOT_PASSWORD="super secret password from config.json"
# ? Launch:
# ? docker-compose up -d --force-recreate
# ? docker attach mgrctl
services:
mgrctl:
container_name: mgrctl
restart: unless-stopped
build:
context: .
args:
- APP_VERSION=${APP_VERSION}
- APP_DIR=${APP_DIR}
- SRC_DIR=${SRC_DIR}
- PKG_NAME=${PKG_NAME}
- PKG_VERSION=${PKG_VERSION}
networks:
vm_box_net: null
volumes:
- type: bind
source: ./dummy_platform/opt/ispsystem/${PLATFORM_TYPE}/config.json
target: /opt/ispsystem/${PLATFORM_TYPE}/config.json
env_file:
- ./.env
tty: true
stdin_open: true
mysql:
container_name: mysql
image: docker-registry.ispsystem.com/mysql:5
volumes:
- ./dummy_platform/opt/ispsystem/${PLATFORM_TYPE}/mysql:/var/lib/mysql
env_file:
- ./.env
labels:
autoconf_mysql: "true"
networks:
vm_box_net: null
command: --group-concat-max-len=131072 --max-connections=1000 --optimizer-search-depth=0
networks:
vm_box_net:
driver: bridge

View File

@ -1,12 +0,0 @@
import click
from cli.lazy_group import LazyGroup
from settings.general import INSTALLED_APPS
@click.group(
cls=LazyGroup,
lazy_subcommands=INSTALLED_APPS['vm6'],
help='vm6 command for lazy example',
)
def cli():
pass

View File

@ -1,13 +0,0 @@
import click
@click.group(help='nodes command for lazy example')
def cli():
pass
@cli.command(name='list')
def nodes_list():
click.echo('NODES LIST: etc...')
for num in range(1, 10):
click.echo(num)

View File

@ -1,21 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import click
from cli.lazy_group import LazyGroup
@click.group(
cls=LazyGroup,
lazy_subcommands={
'vm6': 'apps.vm6.commands.cli',
'dci6': 'apps.dci6.commands.cli',
},
help='main CLI command for lazy example',
)
def cli():
pass
if __name__ == '__main__':
cli()

View File

@ -1,13 +0,0 @@
from settings.environment import env
from settings.general import BASE_DIR
from utils.helpers import parse_json_file
PLATFORM_TYPE = env.str('PLATFORM_TYPE', 'vm')
PLATFORM_CONFIG = parse_json_file(f'{BASE_DIR}/config.json')
PLATFORM_URL = env.url(
'PLATFORM_URL',
f"https://{PLATFORM_CONFIG.get('DomainName' ,'replace.me')}"
)

15
mgrctl/__init__.py Normal file
View File

@ -0,0 +1,15 @@
# █▀▄▀█ █▀▀ ▀█▀ ▄▀█ ▀
# █░▀░█ ██▄ ░█░ █▀█ ▄
# -- -- -- -- -- -- -
__author__ = "MOIS3Y, a.garaev, Failak3, Ann_M"
__credits__ = [
"Stepan Zhukovsky",
"Arthur Garaev",
"Vladislav Shmidt",
"Anna Moskovkina"
]
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Stepan Zhukovsky"
__email__ = "stepan@zhukovsky.me"
__status__ = "Development"

View File

@ -3,7 +3,7 @@ import json
import urllib
import requests
from settings.api import INPUT_HOSTNAME, INPUT_PORT, HEADERS
from mgrctl.settings.api import INPUT_HOSTNAME, INPUT_PORT, HEADERS
class BaseAPI(object):
@ -61,6 +61,12 @@ class BaseAPI(object):
print(response)
raise sys.exit()
def get(self, url, headers={}, data={}):
return self.call_api(url, headers, data, method='GET')
def post(self, url, headers={}, data={}):
return self.call_api(url, headers, data, method='POST')
class BaseAuthAPI(BaseAPI):
def __init__(self, api_url=None, verify_ssl=True):

View File

@ -1,4 +1,4 @@
from api.base import BaseAPI, BaseAuthAPI, BaseDnsProxyAPI, BaseIpAPI
from mgrctl.api.base import BaseAPI, BaseAuthAPI, BaseDnsProxyAPI, BaseIpAPI
class AuthAPI(BaseAuthAPI):

View File

@ -1,4 +1,4 @@
from api.base import BaseAPI, BaseAuthAPI, BaseDnsProxyAPI, BaseIpAPI
from mgrctl.api.base import BaseAPI, BaseAuthAPI, BaseDnsProxyAPI, BaseIpAPI
class AuthAPI(BaseAuthAPI):

15
mgrctl/apps/__init__.py Normal file
View File

@ -0,0 +1,15 @@
# █▀▄▀█ █▀▀ ▀█▀ ▄▀█ ▀
# █░▀░█ ██▄ ░█░ █▀█ ▄
# -- -- -- -- -- -- -
__author__ = "MOIS3Y, a.garaev, Failak3, Ann_M"
__credits__ = [
"Stepan Zhukovsky",
"Arthur Garaev",
"Vladislav Shmidt",
"Anna Moskovkina"
]
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Stepan Zhukovsky"
__email__ = "stepan@zhukovsky.me"
__status__ = "Development"

View File

@ -0,0 +1,15 @@
# █▀▄▀█ █▀▀ ▀█▀ ▄▀█ ▀
# █░▀░█ ██▄ ░█░ █▀█ ▄
# -- -- -- -- -- -- -
__author__ = "MOIS3Y, a.garaev, Failak3, Ann_M"
__credits__ = [
"Stepan Zhukovsky",
"Arthur Garaev",
"Vladislav Shmidt",
"Anna Moskovkina"
]
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Stepan Zhukovsky"
__email__ = "stepan@zhukovsky.me"
__status__ = "Development"

View File

@ -1,7 +1,7 @@
import click
from cli.lazy_group import LazyGroup
from settings.general import INSTALLED_APPS
from mgrctl.cli.lazy_group import LazyGroup
from mgrctl.settings.general import INSTALLED_APPS
@click.group(

View File

@ -0,0 +1,15 @@
# █▀▄▀█ █▀▀ ▀█▀ ▄▀█ ▀
# █░▀░█ ██▄ ░█░ █▀█ ▄
# -- -- -- -- -- -- -
__author__ = "MOIS3Y, a.garaev, Failak3, Ann_M"
__credits__ = [
"Stepan Zhukovsky",
"Arthur Garaev",
"Vladislav Shmidt",
"Anna Moskovkina"
]
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Stepan Zhukovsky"
__email__ = "stepan@zhukovsky.me"
__status__ = "Development"

View File

@ -0,0 +1,10 @@
# █▀▄▀█ █▀▀ ▀█▀ ▄▀█ ▀
# █░▀░█ ██▄ ░█░ █▀█ ▄
# -- -- -- -- -- -- -
__author__ = "MOIS3Y"
__credits__ = ["Stepan Zhukovsky"]
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Stepan Zhukovsky"
__email__ = "stepan@zhukovsky.me"
__status__ = "Development"

View File

@ -1,10 +1,17 @@
import click
from db.vm6.databases import isp_database
from db.vm6.models import AuthUser
from mgrctl.db.vm6.databases import isp_database
from mgrctl.db.vm6.models import AuthUser
from mgrctl.apps.vm6.auth import __version__
@click.group(help='access command for lazy example')
@click.group(help='auth cmd for auth in VMmanager 6')
@click.version_option(
version=__version__,
package_name='mgrctl.apps.vm6.auth',
message=__version__
)
def cli():
pass

View File

@ -0,0 +1,19 @@
import click
from mgrctl.cli.lazy_group import LazyGroup
from mgrctl.settings.general import INSTALLED_APPS
from mgrctl.apps.vm6 import __version__
@click.group(
cls=LazyGroup,
lazy_subcommands=INSTALLED_APPS['vm6'],
help='vm6 command for lazy example',
)
@click.version_option(
version=__version__,
package_name='mgrctl',
message=__version__
)
def cli():
pass

View File

@ -1,5 +1,5 @@
from peewee import MySQLDatabase, PostgresqlDatabase
from settings.db import (
from mgrctl.settings.db import (
DB_ENGINE,
DB_HOST,
DB_PORT,

View File

@ -1,4 +1,4 @@
from db.connection import guess_database
from mgrctl.db.connection import guess_database
# The variable will contain an object of

View File

@ -11,8 +11,8 @@ from peewee import (
TextField,
SQL
)
from db.common import UnknownField
from db.dci6.databases import auth_database
from mgrctl.db.common import UnknownField
from mgrctl.db.dci6.databases import auth_database
class BaseModel(Model):

View File

@ -1,4 +1,4 @@
from db.connection import guess_database
from mgrctl.db.connection import guess_database
# The variable will contain an object of

View File

@ -12,8 +12,8 @@ from peewee import (
TextField,
SQL
)
from db.common import UnknownField, JSONField
from db.vm6.databases import isp_database
from mgrctl.db.common import UnknownField, JSONField
from mgrctl.db.vm6.databases import isp_database
class BaseModel(Model):

32
mgrctl/mgrctl.py Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# █▀▄▀█ █▀▀ █▀█ █▀▀ ▀█▀ █░░ ▀
# █░▀░█ █▄█ █▀▄ █▄▄ ░█░ █▄▄ ▄
# -- -- -- -- -- -- -- -- --
import click
from mgrctl import __version__
from mgrctl.cli.lazy_group import LazyGroup
@click.group(
cls=LazyGroup,
lazy_subcommands={
'vm6': 'mgrctl.apps.vm6.commands.cli',
'dci6': 'mgrctl.apps.dci6.commands.cli',
},
help='main CLI command for lazy example',
)
@click.version_option(
version=__version__,
package_name='mgrctl',
message=__version__
)
def cli():
pass
if __name__ == '__main__':
cli()

View File

@ -1,12 +1,12 @@
from settings.general import BASE_DIR
from mgrctl.settings.general import BASE_DIR
from settings.platform import (
from mgrctl.settings.platform import (
PLATFORM_TYPE,
PLATFORM_URL,
PLATFORM_CONFIG
)
from settings.db import(
from mgrctl.settings.db import(
DB_ENGINE,
DB_HOST,
DB_PORT,

View File

@ -1,4 +1,4 @@
from settings.platform import PLATFORM_TYPE
from mgrctl.settings.platform import PLATFORM_TYPE
# Name of nginx container:
INPUT_HOSTNAME = 'input' if PLATFORM_TYPE == 'vm' else 'dci_input_1'

View File

@ -1,5 +1,5 @@
from settings.environment import env
from settings.platform import PLATFORM_CONFIG
from mgrctl.settings.environment import env
from mgrctl.settings.platform import PLATFORM_CONFIG
# ! Required because some instance use psql db:

View File

@ -6,10 +6,9 @@ BASE_DIR = pathlib.Path(__file__).resolve().parent.parent
INSTALLED_APPS = {
'vm6': {
'access': 'apps.vm6.access.commands.cli',
'nodes': 'apps.vm6.nodes.commands.cli',
'auth': 'mgrctl.apps.vm6.auth.commands.cli',
},
'dci6': {
'access': 'apps.dci6.access.commands.cli',
'auth': 'mgrctl.apps.dci6.auth.commands.cli',
},
}

View File

@ -0,0 +1,17 @@
from mgrctl.settings.environment import env
from mgrctl.utils.helpers import parse_json_file
PLATFORM_TYPE = env.str('PLATFORM_TYPE', 'vm')
PLATFORM_CONFIG = env.str(
'PLATFORM_CONFIG',
f'/opt/ispsystem/{PLATFORM_TYPE}/config.json'
)
PLATFORM_CONFIG = parse_json_file(PLATFORM_CONFIG)
PLATFORM_URL = env.url(
'PLATFORM_URL',
f"https://{PLATFORM_CONFIG.get('DomainName' ,'replace.me')}"
)

View File

@ -1,8 +1,9 @@
import json
import sys
import click
def parse_json_file(file_path):
def parse_json_file(file_path: str) -> dict:
"""
Function read json file as usual config.json then parse it to python dict
@ -16,5 +17,7 @@ def parse_json_file(file_path):
with open(file_path, 'r') as f:
return json.load(f)
except Exception as error:
print(error)
click.echo(error)
click.echo("Required: /opt/ispsystem/PLATFORM_TYPE/config.json")
click.echo("Note: don't forget to mount this file into the container")
sys.exit(1)

View File

@ -1,8 +1,13 @@
[tool.poetry]
name = "isp-maintenance"
name = "mgrctl"
version = "0.1.0"
description = "Maintenance service for ISPsystem platforms"
authors = ["MOIS3Y <s.zhukovskii@ispsystem.com>", "Failak3 <v.shmidt@ispsystem.com>", "a.garaev <a.garaev@ispsystem.com>", "Ann_M <a.moskovkina@ispsystem.com>"]
authors = [
"MOIS3Y <s.zhukovskii@ispsystem.com>",
"Failak3 <v.shmidt@ispsystem.com>",
"a.garaev <a.garaev@ispsystem.com>",
"Ann_M <a.moskovkina@ispsystem.com>"
]
license = "MIT"
readme = "README.md"
@ -19,7 +24,9 @@ sh = "^2.0.7"
[tool.poetry.group.dev.dependencies]
flake8 = "^7.0.0"
[tool.poetry.scripts]
mgrctl = 'mgrctl.mgrctl:cli'
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"