import subprocess import os import sys from doit.tools import LongRunning from dodo_utils import wait_net_service, wait_pgsql_db, \ up2date_hasimage, up2date_anyimages, \ run_task_func, scan_dir COMPOSE = 'docker-compose -p feedati' DOIT_CONFIG = {'default_tasks': ['up'], 'backend': 'sqlite3'} def task_build(): '''builda il container docker''' return { 'uptodate': [up2date_anyimages], 'file_dep': ['docker-compose.yml', 'rss-bridge/Dockerfile', 'panel/Dockerfile', ] + list(scan_dir('docker')), '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`. ''' } def task__build_rm(): '''rimuove container avviati''' return {'actions': [ "docker container ls -a --format '{{.ID}}\t{{.Names}}'|" "awk '$2 ~ /^feedati_/ { print $1 }' | " "xargs -r docker container rm", ]} def task__build_rmi(): '''rimuove immagini ottenute con build''' return { 'actions': [ r"docker images -q 'feedati/*' |" "xargs -r --verbose docker rmi", ] } def task__dbprepare_clean(): '''rimuove il dump caricato sul db''' return { 'actions': [ "docker ps -aqf name=feedati_db|xargs -r docker container rm ", "docker volume rm feedati_postgres_data || true", ] } def stop(): subprocess.check_call((COMPOSE + ' stop').split()) return True def task_dbprepare(): '''applica il dump sql al container del db''' panelcli = 'docker-compose -p feedati exec -T panel ./panelcli -db-pass panelpass -db-addr db:5432 ' psql = r'docker exec -i $(docker ps -aqf name=feedati_db) env PGPASSWORD=superpass psql -h 127.0.0.1 -f - -d feeds superadmin ' return { 'setup': ['_dbprepare_clean', 'build'], 'file_dep': list(scan_dir('docker', '*.sql')), 'actions': [ (COMPOSE + ' up -d db panel').split(), (wait_net_service, ('localhost', 5432, 300)), (wait_pgsql_db, ('feedati_db', 'feeds', 'superadmin')), 'echo LOADING DB', # dbconf.sql would be better put in /docker-entrypoint-initdb.d/init-user-db.sh # (see https://hub.docker.com/r/library/postgres/ ) psql + ' < docker/dbconf.sql', psql + ' < docker/ttrss.sql', panelcli + 'setup', panelcli + 'adduser -password password admin ad@m.in', psql + ' < docker/dbconf-perms.sql', 'echo DB RESTORED', ], 'teardown': [(stop, [])], 'uptodate': [up2date_hasimage('feedati_postgres_data')()], 'clean': [run_task_func(task__dbprepare_clean)] } def task__fix_perms(): '''fix permissions for shared www dir''' # currently empty, but keeping it for later use return { 'actions': None, } def task_up(): '''RUN that stuff!''' return { 'task_dep': ['build', 'dbprepare', '_fix_perms'], 'teardown': [(stop, [])], 'actions': [LongRunning( (COMPOSE + ' up').split(), shell=False) ] } def _get_valid_services(): srv = subprocess.check_output((COMPOSE + ' config --services').split()) 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: subprocess.check_call((COMPOSE + ' restart %s' % service).split()) return True 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"}], 'actions': [(restart, )], } def task__cleanall(): '''clean everything there is to clean''' return { 'task_dep': ['_build_rm', '_build_rmi', '_dbprepare_clean'], 'actions': None }