Skip to content

Commit

Permalink
Merge pull request #25 from rtCamp/cli-typer
Browse files Browse the repository at this point in the history
Docker Stack Migration: Old Site to New Site
  • Loading branch information
Xieyt authored Sep 7, 2023
2 parents 1f1133b + 0405fba commit c3a7261
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 104 deletions.
25 changes: 0 additions & 25 deletions docker-files/.devcontainer.json

This file was deleted.

1 change: 0 additions & 1 deletion docker-files/build.amd64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

docker build --push --platform linux/amd64 -t xieytx/fm-nginx:amd64 docker-images/nginx-docker/.
docker build --push --platform linux/amd64 -t xieytx/fm-mailhog:amd64 docker-images/mailhog-docker/.
docker build --push --platform linux/amd64 -t xieytx/fm-rqdashboard:amd64 docker-images/rq-dashboard-docker/.
docker build --push --platform linux/amd64 -t xieytx/fm-frappe:amd64 docker-images/frappe-docker/.
1 change: 0 additions & 1 deletion docker-files/build.arm64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

docker build --push --platform linux/arm64 -t xieytx/fm-nginx:arm64 docker-images/nginx-docker/.
docker build --push --platform linux/arm64 -t xieytx/fm-mailhog:arm64 docker-images/mailhog-docker/.
docker build --push --platform linux/arm64 -t xieytx/fm-rqdashboard:arm64 docker-images/rq-dashboard-docker/.
docker build --push --platform linux/arm64 -t xieytx/fm-frappe:arm64 docker-images/frappe-docker/.
6 changes: 0 additions & 6 deletions docker-files/combine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ docker manifest create xieytx/fm-mailhog:latest \

docker manifest push xieytx/fm-mailhog:latest

docker manifest create xieytx/fm-rqdashboard:latest \
--amend xieytx/fm-rqdashboard:amd64 \
--amend xieytx/fm-rqdashboard:arm64

docker manifest push xieytx/fm-rqdashboard:latest

docker manifest create xieytx/fm-frappe:latest \
--amend xieytx/fm-frappe:amd64 \
--amend xieytx/fm-frappe:arm64
Expand Down
3 changes: 1 addition & 2 deletions docker-files/docker-images/nginx-docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/bash

# wait for all the programs to load first
echo "Waiting for mailhog,adminer,rq to start"
echo "Waiting for mailhog adminer to start"
wait-for-it -t 120 mailhog:8025
wait-for-it -t 120 adminer:8080
wait-for-it -t 120 rq-dashboard:9181

/config/jinja2 -D SITENAME="$SITENAME" /config/template.conf > /etc/nginx/conf.d/default.conf

Expand Down
15 changes: 0 additions & 15 deletions docker-files/docker-images/nginx-docker/template.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ upstream mailhog {
upstream adminer {
server adminer:8080 fail_timeout=120;
}
upstream rq-dash {
server rq-dashboard:9181 fail_timeout=120;
}
server {

{%- if ENABLE_SSL == False %}
Expand Down Expand Up @@ -123,18 +120,6 @@ server {
proxy_pass http://adminer/;
}

# rq-dashboard
location ^~ /rq-dash/ {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header X-Use-X-Accel-Redirect True;
proxy_redirect off;
proxy_pass http://rq-dash/rq-dash/;

}

# error pages
error_page 502 /502.html;
location /502.html {
Expand Down
15 changes: 0 additions & 15 deletions docker-files/docker-images/rq-dashboard-docker/Dockerfile

This file was deleted.

3 changes: 1 addition & 2 deletions fm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from fm.site_manager.manager import SiteManager
import os
import requests
from fm.site_manager.Richprint import richprint

app = typer.Typer(no_args_is_help=True,rich_markup_mode='rich')

Expand Down Expand Up @@ -202,7 +201,7 @@ def code(
callback=code_callback,
),
] = default_extension,
force_start: Annotated[bool , typer.Option(help="Force start the site before attaching to container.")] = False
force_start: Annotated[bool , typer.Option('--force-start','-f',help="Force start the site before attaching to container.")] = False
):
"""Open site in vscode. :sparkles:"""
sites.init(sitename)
Expand Down
64 changes: 54 additions & 10 deletions fm/site_manager/SiteCompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pathlib import Path
import yaml
from typing import List
import json
import typer

from fm.site_manager.Richprint import richprint

def represent_none(self, _):
return self.represent_scalar('tag:yaml.org,2002:null', '')
Expand All @@ -11,36 +13,71 @@ def represent_none(self, _):
class SiteCompose:
def __init__(self,loadfile: Path):
self.compose_path:Path = loadfile
self.exists = loadfile.exists()
self.yml: yaml | None = None
self.init()

def init(self):
# if the load file not found then the site not exits
if self.exists:
if self.exists():
with open(self.compose_path,'r') as f:
self.yml = yaml.safe_load(f)
else:
# see if template exits
template =self.__get_template('docker-compose.tmpl')
if template == None:
print("Template not found!")
exit()
# load the template file
self.yml = yaml.safe_load(template)

def exists(self):
return self.compose_path.exists()

def get_compose_path(self):
return self.compose_path

def migrate_compose(self,version):
if self.exists():
frappe_envs = self.get_envs('frappe')
nginx_envs = self.get_envs('nginx')
extra_hosts = self.get_extrahosts('frappe')

template =self.__get_template('docker-compose.tmpl')
self.yml = yaml.safe_load(template)

self.set_version(version)
self.set_envs('frappe',frappe_envs)
self.set_envs('nginx',nginx_envs)
self.set_extrahosts('frappe',extra_hosts)
self.write_to_file()

def __get_template(self,file_name: str)-> None | str:
file_name = f"templates/{file_name}"
try:
data = pkgutil.get_data(__name__,file_name)
except:
return None
richprint.error(f"{file_name} template not found!")
raise typer.Exit(1)
yml = data.decode()
return yml

def get_services_list(self):
return list(self.yml['services'].keys())

def is_services_name_same_as_template(self):
template = self.__get_template('docker-compose.tmpl')
template_yml = yaml.safe_load(template)
template_service_name_list = list(template_yml['services'].keys())
template_service_name_list.sort()
current_service_name_list = list(self.yml['services'].keys())
current_service_name_list.sort()
return current_service_name_list == template_service_name_list

def get_version(self):
try:
compose_version = self.yml['x-version']
except KeyError:
return None
return compose_version

def set_version(self, version):
self.yml['x-version'] = version

def set_envs(self,container:str,env:dict):
"""Sets env to given container."""
self.yml['services'][container]['environment'] = env
Expand All @@ -55,14 +92,21 @@ def set_labels(self,container:str, labels:dict):
def get_labels(self,container:str) -> dict:
try:
labels = self.yml['services'][container]['labels']
except Exception:
except KeyError:
return {}
return labels

def set_extrahosts(self,container:str,extrahosts:list):
"""Sets extrahosts to contianer."""
self.yml['services'][container]['extra_hosts'] = extrahosts

def get_extrahosts(self,container:str) -> list:
try:
extra_hosts = self.yml['services'][container]['extra_hosts']
except KeyError:
return []
return extra_hosts

def write_to_file(self):
# saving the docker compose to the directory
with open(self.compose_path,'w') as f:
Expand Down
17 changes: 13 additions & 4 deletions fm/site_manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def init(self, sitename: str| None = None,createdir: bool = False):
sitename = sitename + ".localhost"
sitepath: Path = self.sitesdir / sitename
self.site: Site = Site(sitepath, sitename)

self.migrate_site()

def __get_all_sites_path(self, exclude: List[str] = []):
sites_path = []
Expand Down Expand Up @@ -144,8 +144,11 @@ def stop_site(self):
richprint.exit(
f"Site {self.site.name} doesn't exists! Aborting!"
)
self.stop_sites()
richprint.change_head(f"Stopping site")
#self.stop_sites()
self.site.stop()
richprint.update_head(f"Stopped site")
richprint.stop()

def start_site(self):
if not self.site.exists():
Expand Down Expand Up @@ -173,9 +176,7 @@ def attach_to_site(self, user: str, extensions: List[str]):
f"--folder-uri=vscode-remote://attached-container+{container_hex}+/workspace",
]
)

extensions.sort()

labels = {
"devcontainer.metadata": json.dumps([
{
Expand Down Expand Up @@ -311,3 +312,11 @@ def info(self):
bench_apps_list_table.add_row(app,apps_json[app]['version'])
richprint.stdout.print(bench_apps_list_table)
richprint.stop()

def migrate_site(self):
if not self.site.composefile.is_services_name_same_as_template():
self.site.down()
self.site.migrate_site()
if self.site.running():
self.site.start()

41 changes: 27 additions & 14 deletions fm/site_manager/site.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
from python_on_whales import DockerClient, DockerException
import typer
import rich
import importlib
import shutil
import re
from typing import List, Type
from pathlib import Path

from fm.site_manager.SiteCompose import SiteCompose
from fm.site_manager.Richprint import richprint

from time import sleep
from rich.live import Live
from rich.table import Table,Row
from rich.progress import Progress

def handle_DockerException():
pass

Expand Down Expand Up @@ -67,11 +62,22 @@ def get_frappe_container_hex(self) -> None | str:
return frappe_container.string.encode().hex()
return None

def migrate_site(self) -> None:
if self.composefile.exists():
richprint.change_head("Checking Envrionment Version")
compose_version = self.composefile.get_version()
fm_version = importlib.metadata.version('fm')
if not compose_version == fm_version:
richprint.change_head("Migrating Environment")
self.composefile.migrate_compose(fm_version)
richprint.print("Migrated Environment")

def generate_compose(self,inputs:dict) -> None:
self.composefile.set_envs('frappe',inputs['frappe_env'])
self.composefile.set_envs('nginx',inputs['nginx_env'])
self.composefile.set_extrahosts('frappe',inputs['extra_hosts'])
fm_version = importlib.metadata.version('fm')
self.composefile.set_version(fm_version)
self.composefile.write_to_file()

def create_dirs(self) -> bool:
Expand Down Expand Up @@ -118,41 +124,48 @@ def frappe_logs_till_start(self):

def stop(self) -> bool:
try:
self.docker.compose.stop(timeout=200)
self.docker.compose.stop(timeout=10)
except DockerException as e:
richprint.error(f"{e.stdout}{e.stderr}")
richprint.exit(f"{e.stdout}{e.stderr}")

def status(self) -> str:
try:
ps_output =self.docker.compose.ps()
ps_output = self.docker.compose.ps()
for container in ps_output:
print(container.state.status)
except DockerException as e:
richprint.error(f"{e.stdout}{e.stderr}")


def running(self) -> bool:
try:
ls_output = self.docker.compose.ls()
if ls_output:
for composeproject in ls_output:
if composeproject.config_files[0] == self.composefile.compose_path.absolute() and composeproject.running >= 9:
if composeproject.config_files[0] == self.composefile.compose_path.absolute() and composeproject.running >= len(self.composefile.get_services_list()):
return True
return False
except DockerException as e:
richprint.exit(f"{e.stdout}{e.stderr}")

def down(self) -> bool:
if self.composefile.exists():
try:
richprint.change_head(f"Removing Containers")
self.docker.compose.down(remove_orphans=True,timeout=2)
# TODO handle low leverl error like read only, write only etc
except DockerException as e:
richprint.exit(f"{e.stdout}{e.stderr}")

def remove(self) -> bool:
if self.composefile.exists:
if self.composefile.exists():
try:
richprint.change_head(f"Removing Containers")
self.docker.compose.down(remove_orphans=True,volumes=True,timeout=2)
# TODO handle low leverl error like read only, write only etc
richprint.change_head(f"Removing Dirs")
shutil.rmtree(self.path)
except DockerException as e:
richprint.error(f"{e.stdout}{e.stderr}")
richprint.exit(f"{e.stdout}{e.stderr}")

def shell(self,container:str, user:str | None = None):
# TODO check user exists
Expand Down
8 changes: 0 additions & 8 deletions fm/site_manager/templates/docker-compose.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ services:
ports:
- 8080

rq-dashboard:
image: xieytx/fm-rqdashboard:latest
environment:
- RQ_DASHBOARD_REDIS_URL=redis://redis-queue:6379
ports:
- 9181


redis-cache:
image: redis:alpine
volumes:
Expand Down
Loading

0 comments on commit c3a7261

Please sign in to comment.