def run(c, cmd): """Start a service from the Docker Compose file, for example: docker django""" inv_logging.task(run.__name__) user, group = inv_base.uid_gid(c) inv_logging.cmd(cmd) inv_base.docker_compose(c, f"run -u {user}:{group} {cmd}", pty=True) inv_logging.success(run.__name__)
def read_backup(c, folder, docker_volume, host): """A function to read the database dump into the database.""" inv_docker.stop(c) inv_docker.start(c) settings = inv_base.read_settings("development") postgres_backup_folder = settings[folder] user, group = inv_base.uid_gid(c) postgresdata_backup_server_files = os.popen( f"ls {postgres_backup_folder}").read().strip().split("\n") file = postgresdata_backup_server_files[-1] file = file[:file.rfind(".tar.gz")] logging.info( f"The database dump archive is unpacked.: '{postgres_backup_folder}/{file}.tar.gz'" ) command = f"tar -xzf {postgres_backup_folder}/{file}.tar.gz -C {postgres_backup_folder}" if host == "development": c.run(command) elif host == "production": inv_rsync.ssh(c, settings["REMOTE_USER"], settings["REMOTE_HOST"], command) logging.info( f"The database dump was unpacked.: '{postgres_backup_folder}/{file}.out'" ) inv_base.docker_compose( c, f"run -u {user}:{group} --rm postgres bash -c 'psql -h postgres -U postgres -f {docker_volume}/{file}.out --quiet'" ) delete_db_dump(c, host, postgres_backup_folder, file) inv_docker.stop(c) inv_docker.start(c)
def npm(c, cmd): """This task is used to respond to the packet manager npm, for example: npm install date-fns""" inv_logging.task(npm.__name__) user, group = inv_base.uid_gid(c) inv_logging.cmd(cmd) inv_base.docker_compose(c, f"run -u {user}:{group} node npm {cmd}", pty=True) inv_logging.success(npm.__name__)
def build(c): """This task is used to build the Javascript components""" inv_logging.task(build.__name__) user, group = inv_base.uid_gid(c) inv_base.docker_compose(c, f"run -u {user}:{group} node npm run build", pty=True) logging.info("The Javascript components were built, minified and zipped.") inv_docker.stop(c) inv_logging.success(build.__name__)
def starttest(c): """This function is used to start the production test environment""" inv_logging.task(starttest.__name__) static_folder = os.path.join(os.getcwd(), "django/static") try: shutil.rmtree(static_folder) logging.info(f"{static_folder} folder was deleted.") except: logging.error(f"{static_folder} could not be deleted.") inv_install.setenvironment(c, "test") inv_django.makemigrations(c) inv_django.migrate(c) inv_django.collectstatic(c) inv_base.docker_compose(c, f"up") inv_install.setenvironment(c, "development") inv_logging.success(starttest.__name__)
def dump_backup(c, host): """A function for the database dump.""" inv_docker.stop(c) inv_docker.start(c) settings = inv_base.read_settings(host) if host == "development": postgres_backup_folder = f"{settings['postgres_backup_folder']}" elif host == "production": postgres_backup_folder = f"{settings['docker']['INSTALLFOLDER']}{settings['postgres_backup_server_folder']}" user, group = inv_base.uid_gid(c) file = f"postgres_{host}_backup_{datetime.datetime.now().strftime('%Y%m%d%H%M')}" inv_base.docker_compose( c, f"run -u {user}:{group} --rm postgres bash -c 'pg_dumpall -c -U postgres -h postgres > /var/backup/{file}.out'", pty=True) logging.info( f"A database dump was saved in the file: '{postgres_backup_folder}/{file}.out" ) command = f"tar -czf {postgres_backup_folder}/{file}.tar.gz -C {postgres_backup_folder} {file}.out" if host == "development": c.run(command) elif host == "production": inv_rsync.ssh(c, settings["REMOTE_USER"], settings["REMOTE_HOST"], command) logging.info( f"A database dump was saved in the file: '{postgres_backup_folder}/{file}.tar.gz'" ) delete_db_dump(c, host, postgres_backup_folder, file) logging.info( f"The uncompressed database dump was deleted: '{postgres_backup_folder}/{file}.out'" ) inv_docker.stop(c) inv_docker.start(c) return postgres_backup_folder, file, settings
def logs(c, cmd): """Show the log files from the Docker Services, for example: django""" inv_logging.task(logs.__name__) inv_base.docker_compose(c, 'logs -f {}'.format(cmd)) inv_logging.cmd(cmd) inv_logging.success(logs.__name__)
def stop(c): """Stop all running Docker Containers""" inv_logging.task(stop.__name__) inv_base.docker_compose(c, "down --remove-orphans") inv_logging.success(stop.__name__)
def start(c): """Start in detached Modus""" inv_logging.task(start.__name__) #inv_django.migrate(c) inv_base.docker_compose(c, "up -d") inv_logging.success(start.__name__)
def serve(c): """Serve the development environment""" inv_logging.task(serve.__name__) inv_base.docker_compose(c, "up") inv_logging.success(serve.__name__)
def rebuildhard(c): """Rebuild all containers with --no-cache""" inv_logging.task(rebuildhard.__name__) inv_base.docker_compose(c, "build --no-cache") fullrestart(c) inv_logging.success(rebuildhard.__name__)
def rebuild(c): """Rebuild all docker containers""" inv_logging.task(rebuild.__name__) inv_base.docker_compose(c, "build") restart(c) inv_logging.success(rebuild.__name__)
def fullrestart(c): """Restart all docker containers with force""" inv_logging.task(fullrestart.__name__) inv_base.docker_compose(c, "up -d --force-recreate") inv_logging.success(fullrestart.__name__)
def restart(c): """Restart all docker containers""" inv_logging.task(restart.__name__) inv_base.docker_compose(c, "up -d --remove-orphans") inv_logging.success(restart.__name__)
def lint(c): """This task is used to embellish the code""" inv_logging.task(lint.__name__) user, group = inv_base.uid_gid(c) inv_base.docker_compose(c, f"run -u {user}:{group} node npm run lint", pty=True) inv_logging.success(lint.__name__)