Archived
2
0
Fork 0
forked from blallo/Feedati
This repository has been archived on 2024-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
Feedati/dodo.py

156 lines
4.8 KiB
Python
Raw Normal View History

2018-08-09 10:34:34 +02:00
import subprocess
2018-08-17 18:05:13 +02:00
import os
2018-09-21 12:35:33 +02:00
import sys
2018-08-09 10:34:34 +02:00
from doit.tools import LongRunning
from dodo_utils import wait_net_service, wait_pgsql_db, \
up2date_hasimage, up2date_anyimages, \
2018-09-05 16:53:04 +02:00
run_task_func, scan_dir
2018-08-09 10:34:34 +02:00
COMPOSE = 'docker-compose -p feedati'
2018-09-21 12:13:27 +02:00
DOIT_CONFIG = {'default_tasks': ['up'], 'backend': 'sqlite3'}
2018-08-09 10:34:34 +02:00
def task_build():
'''builda il container docker'''
return {
'uptodate': [up2date_anyimages],
2018-08-17 18:05:13 +02:00
'file_dep': ['docker-compose.yml',
'rss-bridge/Dockerfile',
2018-09-05 16:53:04 +02:00
] + list(scan_dir('docker')),
2018-08-09 10:34:34 +02:00
'actions': [COMPOSE + ' build'],
'clean': [run_task_func(task__build_rm),
run_task_func(task__build_rmi)],
'doc': '''
This task recreates every docker container. While it is automatically run for most changes in the
development environment, please remember that if you want to run it manually to grab changes in the
docker hub, you need to run `doit run -a build`.
'''
2018-08-09 10:34:34 +02:00
}
def task__build_rm():
2018-08-09 10:34:34 +02:00
'''rimuove container avviati'''
return {'actions': [
"docker container ls -a --format '{{.ID}}\t{{.Names}}'|"
"awk '$2 ~ /^feedati_/ { print $1 }' | "
"xargs -r docker container rm",
]}
2018-08-09 10:34:34 +02:00
def task__build_rmi():
2018-08-09 10:34:34 +02:00
'''rimuove immagini ottenute con build'''
return {
'actions': [
r"docker images -q 'feedati/*' |"
"xargs -r --verbose docker rmi",
2018-08-09 10:34:34 +02:00
]
}
def task__dbprepare_clean():
2018-08-09 10:34:34 +02:00
'''rimuove il dump caricato sul db'''
return {
'actions': [
"docker ps -aqf name=feedati_db|xargs -r docker container rm ",
2018-08-09 10:34:34 +02:00
"docker volume rm feedati_postgres_data || true",
]
}
def stop():
subprocess.check_call((COMPOSE + ' stop').split())
return True
def task_dbprepare():
2018-08-09 10:34:34 +02:00
'''applica il dump sql al container del db'''
return {
'setup': ['_dbprepare_clean', 'build'],
2018-09-25 16:35:42 +02:00
'file_dep': list(scan_dir('docker', '*.sql')),
2018-08-09 10:34:34 +02:00
'actions': [
(COMPOSE + ' up -d db').split(),
(wait_net_service, ('localhost', 5432, 300)),
2018-09-25 16:35:42 +02:00
(wait_pgsql_db, ('feedati_db', 'feeds', 'superadmin')),
'echo LOADING DB',
2018-09-25 16:35:42 +02:00
# dbconf.sql would be better put in /docker-entrypoint-initdb.d/init-user-db.sh
# (see https://hub.docker.com/r/library/postgres/ )
r'docker exec -i $(docker ps -aqf name=feedati_db) '
2018-09-25 16:35:42 +02:00
'env PGPASSWORD=superpass psql -h 127.0.0.1 -f - -d feeds superadmin < docker/dbconf.sql',
r'docker exec -i $(docker ps -aqf name=feedati_db) '
'env PGPASSWORD=ttpass psql -h 127.0.0.1 -f - -d feeds ttrss < docker/ttrss.sql',
2018-09-29 23:57:36 +02:00
'docker run feedati/feedspanel -db-pass panelpass -db-addr db:5432 batch',
r'env PGPASSWORD=ttpass psql -h 127.0.0.1 -f - -d feeds ttrss <<< '
'"GRANT SELECT ON TABLE users.users TO \'apache\'"',
'echo DB RESTORED',
],
'teardown': [(stop, [])],
'uptodate': [up2date_hasimage('feedati_postgres_data')()],
'clean': [run_task_func(task__dbprepare_clean)]
2018-08-09 10:34:34 +02:00
}
def task__fix_perms():
2018-08-09 10:34:34 +02:00
'''fix permissions for shared www dir'''
2018-08-19 14:57:31 +02:00
# currently empty, but keeping it for later use
2018-08-09 10:34:34 +02:00
return {
'actions': None,
2018-08-09 10:34:34 +02:00
}
def task_up():
'''RUN that stuff!'''
2018-08-09 10:34:34 +02:00
return {
'task_dep': ['build', 'dbprepare', '_fix_perms'],
2018-08-09 17:55:12 +02:00
'teardown': [(stop, [])],
2018-08-09 10:34:34 +02:00
'actions': [LongRunning(
2018-08-09 17:55:12 +02:00
(COMPOSE + ' up').split(),
shell=False)
2018-08-09 10:34:34 +02:00
]
}
2018-09-21 12:35:33 +02:00
def _get_valid_services():
srv = subprocess.check_output((COMPOSE + ' config --services').split())
2018-09-21 12:35:33 +02:00
return srv.decode('utf-8').strip().split()
def restart(services):
'''This actually restart the container(s).'''
valid = _get_valid_services()
err = False
for service in services:
if service not in valid:
print('ERROR: invalid service %s' % service, file=sys.stderr)
err = True
if err or not services:
print('Valid services are: ' + ', '.join(valid), file=sys.stderr)
return False
for service in services:
2018-09-21 16:39:20 +02:00
subprocess.check_call((COMPOSE + ' restart %s' % service).split())
return True
2018-09-21 12:35:33 +02:00
def task_restart():
'''Restarts a container specified via commandline.'''
return {
'params': [{'name': 'services',
'short': 's',
'long': 'service',
'type': list,
'default': [],
'help': "the list of services to be restarted"}],
2018-09-21 12:35:33 +02:00
'actions': [(restart, )],
}
def task__cleanall():
2018-08-09 10:34:34 +02:00
'''clean everything there is to clean'''
return {
'task_dep': ['_build_rm', '_build_rmi', '_dbprepare_clean'],
2018-08-09 10:34:34 +02:00
'actions': None
}