Skip to content

Commit

Permalink
Merge pull request #110 from SelfhostedPro/develop
Browse files Browse the repository at this point in the history
Merge Develop
  • Loading branch information
SelfhostedPro authored Sep 26, 2020
2 parents a0bffaf + fda765c commit 396a59f
Show file tree
Hide file tree
Showing 30 changed files with 879 additions and 183 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ backend/*.pyo
backend/env
backend/venv
backend/*.env
backend/env*
backend/dist
backend/*.egg
backend/*.egg-info
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sql_app.db
venv
*.db
node_modules
config
config
*.pyc
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ RUN \
/root/.cache \
/tmp/*

COPY ./backend/api .
COPY root /
COPY ./backend/api ./
COPY ./backend/alembic /alembic
COPY root ./backend/alembic.ini /

# Vue
COPY --from=build-stage /app/dist /app
Expand Down
1 change: 0 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ config/data-dev.sqlite
env
venv
*.env
env*
dist
*.egg
*.egg-info
Expand Down
83 changes: 83 additions & 0 deletions backend/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = alembic
sourceless = true
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =

# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8


[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks=black
# black.type=console_scripts
# black.entrypoint=black
# black.options=-l 79

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
1 change: 1 addition & 0 deletions backend/alembic/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
91 changes: 91 additions & 0 deletions backend/alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config, MetaData
from sqlalchemy import pool
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
import os
import sys
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(abspath(__file__))))


from api.db import models
from api.settings import Settings
from api.auth import Base
# Combine metadata from auth and containers/templates
combined_meta_data = MetaData()
for declarative_base in [models.Base, Base]:
for (table_name, table) in declarative_base.metadata.tables.items():
combined_meta_data._add_table(table_name, table.schema, table)

target_metadata = combined_meta_data
config.set_main_option("sqlalchemy.url", os.environ.get('DATABASE_URL', 'sqlite:///config/data.sqlite'))

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions backend/alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
53 changes: 37 additions & 16 deletions backend/api/actions/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError

from fastapi import HTTPException
from ..db import models, schemas
from ..utils import *

Expand Down Expand Up @@ -70,36 +70,57 @@ def deploy_app(template: schemas.DeployForm):
try:
launch = launch_app(
template.name,
template.image,
conv_image2data(template.image),
conv_restart2data(template.restart_policy),
conv_ports2data(template.ports),
conv_portlabels2data(template.ports),
conv_volumes2data(template.volumes),
conv_env2data(template.env),
conv_devices2data(template.devices),
conv_labels2data(template.labels),
conv_sysctls2data(template.sysctls),
conv_caps2data(template.cap_add)
)

except Exception as exc:
raise
raise HTTPException(status_code=exc.response.status_code, detail=exc.explanation)
print('done deploying')

return schemas.DeployLogs(logs=launch.logs())

def Merge(dict1, dict2):
if dict1 and dict2:
return(dict2.update(dict1))
elif dict1:
return dict1
elif dict2:
return dict2
else:
return None

def launch_app(name, image, restart_policy, ports, volumes, env, sysctls, caps):
def launch_app(name, image, restart_policy, ports, portlabels, volumes, env, devices, labels, sysctls, caps):
dclient = docker.from_env()
lauch = dclient.containers.run(
name=name,
image=image,
restart_policy=restart_policy,
ports=ports,
volumes=volumes,
environment=env,
sysctls=sysctls,
cap_add=caps,
detach=True
)
print(lauch)
combined_labels = Merge(portlabels, labels)
try:
lauch = dclient.containers.run(
name=name,
image=image,
restart_policy=restart_policy,
ports=ports,
volumes=volumes,
environment=env,
sysctls=sysctls,
labels=combined_labels,
devices=devices,
cap_add=caps,
detach=True
)
except Exception as e:
if e.status_code == 500:
failed_app = dclient.containers.get(name)
failed_app.remove()
raise e

print(f'''Container started successfully.
Name: {name},
Image: {image},
Expand Down
Loading

0 comments on commit 396a59f

Please sign in to comment.