def build_main_image(args: dict, main_image: str = None) -> None: """Builds the main image. Args: args (dict): Parsed command-line arguments passed to the script. main_image (str): Optional; Main image name. Defaults to the value from config file. """ main_image = main_image if main_image else CONFIG['DOCKER']['main_image'] if not args['--rebuild'] and docker_utils.item_exists('image', main_image): utils.warn(f"Image '{main_image}' already exists, not building") return utils.log(f"Building '{main_image}' image") main_image_cmd = [ 'docker', 'build', '--tag', main_image, '--file', os.path.join('docker', 'Dockerfile'), '.', ] if args['--no-cache']: main_image_cmd.insert(2, '--no-cache') elif args['--cache-from']: for item in args['--cache-from']: main_image_cmd[2:2] = ['--cache-from', item] if args['--suspend'] or args['--debug']: main_image_cmd[2:2] = ['--build-arg', 'suspend=true' if args['--suspend'] else 'debug=true'] utils.execute_cmd(main_image_cmd)
def build_build_image(args: dict, build_image: str = None) -> None: """Builds the build image. Args: args (dict): Parsed command-line arguments passed to the script. build_image (str): Optional; Build image name. Defaults to the value from config file. """ build_image = build_image if build_image else CONFIG['DOCKER']['build_image'] if not args['--rebuild'] and docker_utils.item_exists('image', build_image): utils.warn(f"Image '{build_image}' already exists, not building") return utils.log(f"Building '{build_image}' image") build_image_cmd = [ 'docker', 'build', '--tag', build_image, '--file', os.path.join('docker', 'Dockerfile-gradle'), '.', ] if args['--no-cache']: build_image_cmd.insert(2, '--no-cache') elif args['--cache-from']: for item in args['--cache-from']: build_image_cmd[2:2] = ['--cache-from', item] utils.execute_cmd(build_image_cmd)
def apply_migrations() -> None: """Applies database migrations.""" liquibase_cmd = database_helper.fetch_dependencies() liquibase_cmd.append('update') utils.log('Applying database migrations') utils.execute_cmd(liquibase_cmd)
def main() -> None: args = docopt.docopt(__doc__, version=CONFIG['DEFAULT']['script_version']) utils.verify_envars(REQUIRED_ENVARS, 'pgAdmin', __doc__) pg_username = os.environ.get('PG_USERNAME') pg_password = os.environ.get('PG_PASSWORD') if not pg_username or not pg_password: utils.raise_error('One or more envars have not been specified', usage=lambda: print(__doc__.strip('\n'))) image_name = CONFIG['DATABASE']['pgadmin_image'] container_name = CONFIG['DATABASE']['pgadmin_container'] network = args['--network'] if args['--network'] else CONFIG['DOCKER'][ 'network'] if not docker_utils.item_exists('network', network): utils.raise_error(f"Docker network '{network}' doesn't exist") def create_pgadmin_container() -> None: utils.log(f"Creating '{image_name}' container, name: {container_name}") utils.execute_cmd([ 'docker', 'create', '--interactive', '--tty', '--env', f"PGADMIN_DEFAULT_EMAIL={pg_username}", '--env', f"PGADMIN_DEFAULT_PASSWORD={pg_password}", '--publish', f"{CONFIG['DATABASE']['pgadmin_port']}:80", '--network', network, '--name', container_name, image_name, ]) if docker_utils.item_exists('container', container_name): if args['--rebuild']: docker_utils.rm_container( docker_utils.DockerContainer(container_name, rm_volumes=True)) create_pgadmin_container() else: create_pgadmin_container() utils.log(f"Starting '{container_name}' container") start_cmd = [ 'docker', 'start', container_name, ] if not args['--detach']: start_cmd[2:2] = ['--interactive'] utils.execute_cmd(start_cmd)
def roll_back(count: str) -> None: """Rolls back the given number of database change sets. Args: count (str): Number of latest change sets to roll back. """ liquibase_cmd = database_helper.fetch_dependencies() liquibase_cmd.extend(['rollbackCount', count]) utils.log('Applying database migrations') utils.execute_cmd(liquibase_cmd)
def start_main_container(args: dict) -> None: """Builds the main image. Args: args (dict): Parsed command-line arguments passed to the script. """ main_container = CONFIG['DOCKER']['main_image'] utils.log(f"Starting '{main_container}'") main_start_cmd = ['docker', 'start', main_container] if not args['--detach']: main_start_cmd[2:2] = ['--attach', '--interactive'] utils.execute_cmd(main_start_cmd)
def main() -> None: docopt.docopt(__doc__, version=CONFIG['DEFAULT']['script_version']) utils.log('Running API tests') response = requests.get(URL) if 200 != response.status_code: container = CONFIG['DOCKER']['main_image'] utils.log(f"Logs from '{container}':") utils.execute_cmd([ 'docker', 'exec', container, 'tail', '-n', '500', f"/home/project/log/{container}.log" ]) utils.raise_error( f"::error::Expected 200 response code but received {response.status_code} from '{URL}', see logs above" )
def create_main_container(args: dict, main_image: str = None) -> None: """Creates main Docker container. Args: args (dict): Parsed command-line arguments passed to the script. main_image (str): Optional; Main image name. Defaults to the value from config file. """ main_image = main_image if main_image else CONFIG['DOCKER']['main_image'] main_container = main_image if not args['--rebuild'] and docker_utils.item_exists('container', main_container): utils.warn(f"Container '{main_container}' already exists, not creating") return utils.log(f"Creating '{main_container}' container") spring_port = CONFIG['SPRING']['port'] main_container_cmd = [ 'docker', 'create', '--publish', f"{spring_port}:{spring_port}", '--name', main_container, '--network', CONFIG['DOCKER']['network'], '--env', f"SPRING_DATASOURCE_URL={os.environ.get('SPRING_DATASOURCE_URL')}", '--env', f"SPRING_DATASOURCE_USERNAME={os.environ.get('SPRING_DATASOURCE_USERNAME')}", '--env', f"SPRING_DATASOURCE_PASSWORD={os.environ.get('SPRING_DATASOURCE_PASSWORD')}", main_image, ] if args['--suspend'] or args['--debug']: debug_port = CONFIG['SPRING']['debug_port'] main_container_cmd[2:2] = ['--publish', f"{debug_port}:{debug_port}"] if not args['--detach']: main_container_cmd[2:2] = ['--interactive', '--tty'] utils.execute_cmd(main_container_cmd) utils.log(f"Copying JAR into '{main_container}'") utils.execute_cmd([ 'docker', 'cp', os.path.join('build', 'libs', 'app.jar'), f"{main_container}:/home/project/app.jar", ])
def run_build_image(args: dict, build_image: str = None) -> None: """Runs the build image and copies the compiled JAR file out of the container. Args: args (dict): Parsed command-line arguments passed to the script. build_image (str): Optional; Build image name. Defaults to the value from config file. """ build_image = build_image if build_image else CONFIG['DOCKER']['build_image'] build_container = build_image if not args['--rebuild'] and docker_utils.item_exists('container', build_container): utils.warn(f"Container '{build_container}' already exists, not running") return utils.log(f"Running '{build_image}' image") build_container_cmd = [ 'docker', 'run', '--name', build_container, '--volume', f"{CONFIG['DOCKER']['cache_volume']}:/home/gradle/.gradle", '--user', 'gradle', build_image, ] build_container_cmd.extend(CONFIG['DOCKER']['build_command'].split(' ')) if not args['--detach']: build_container_cmd[2:2] = ['--interactive', '--tty'] utils.execute_cmd(build_container_cmd) utils.log(f"Copying JAR from '{build_container}' container") shutil.rmtree(os.path.join('build', 'libs'), ignore_errors=True) pathlib.Path(os.path.join('build')).mkdir(parents=True, exist_ok=True) utils.execute_cmd([ 'docker', 'cp', f"{build_container}:/home/gradle/project/build/libs", os.path.join('build'), ]) for file in glob.glob(os.path.join('build', 'libs', '*.jar')): if file.endswith('.jar'): os.rename(file, os.path.join('build', 'libs', 'app.jar')) break
def create_pgadmin_container() -> None: utils.log(f"Creating '{image_name}' container, name: {container_name}") utils.execute_cmd([ 'docker', 'create', '--interactive', '--tty', '--env', f"PGADMIN_DEFAULT_EMAIL={pg_username}", '--env', f"PGADMIN_DEFAULT_PASSWORD={pg_password}", '--publish', f"{CONFIG['DATABASE']['pgadmin_port']}:80", '--network', network, '--name', container_name, image_name, ])
def run_db_container(container_name: str, network: str, port: str) -> None: """Runs the database Docker container. Args: container_name (str): Name to use for the database container. network (str): Name of a Docker network to plug the database into. port (str): Host port at which the database will be listening on. """ docker_image = CONFIG['DATABASE']['docker_image'] if docker_utils.item_exists('container', container_name): utils.log( f"Container '{container_name}' already exists, not running '{docker_image}' image" ) return if not docker_utils.item_exists('network', network): utils.raise_error(f"Docker network '{network}' doesn't exist") utils.log(f"Running '{docker_image}' container, name: {container_name}") utils.execute_cmd([ 'docker', 'run', '--detach', '--name', container_name, '--publish', f"{port}:5432", # <host_port>:<container_port> '--network', network, '--env', f"POSTGRES_DB={os.environ.get('POSTGRES_DB')}", '--env', f"POSTGRES_USER={os.environ.get('POSTGRES_USER')}", '--env', f"POSTGRES_PASSWORD={os.environ.get('POSTGRES_PASSWORD')}", docker_image, ]) time.sleep(3) # Wait for the database to come up
def main() -> None: args = docopt.docopt(__doc__, version=CONFIG['DEFAULT']['script_version']) utils.verify_envars(REQUIRED_ENVARS, 'Postgres', __doc__) container_name = args['--container'] if args['--container'] else CONFIG[ 'DATABASE']['database_container'] network_name = args['--network'] if args['--network'] else CONFIG[ 'DOCKER']['network'] if args['--rollback']: port = args['--port'] if args['--port'] else CONFIG['DATABASE']['port'] run_db_container(container_name, network_name, port) roll_back(args['--rollback']) else: start(container=container_name, network=network_name, port=args['--port'], migrations=args['--apply-migrations'], start_db=args['--start-db']) if args['--attach'] and docker_utils.item_exists('container', container_name): utils.execute_cmd(['docker', 'attach', container_name])
def main() -> None: docopt.docopt(__doc__, version=CONFIG['DEFAULT']['script_version']) db_container = CONFIG['DATABASE']['database_test_container'] network = CONFIG['DOCKER']['test_network'] db_port = CONFIG['DATABASE']['test_port'] set_envars(db_port) docker_utils.create_network(network) database.start(container=db_container, network=network, port=db_port, migrations=True) utils.log('Running integration tests') completed_process = utils.execute_cmd( ['./gradlew', 'integrationTest', '--info'], pipe_stderr=True) docker_utils.rm_container( docker_utils.DockerContainer(db_container, rm_volumes=True)) docker_utils.rm_network(network) if completed_process.stderr: utils.raise_error(completed_process.stderr.decode('utf8'))