diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c0e0ad5716aa7751aa836fad78b8bfd9c8c66ce9..f6647569c4a06f69020629a54a9841e7923a424b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,11 +1,11 @@ repos: - repo: https://github.com/asottile/reorder_python_imports - rev: v2.5.0 + rev: v3.0.1 hooks: - id: reorder-python-imports language_version: python3 - repo: https://github.com/psf/black - rev: 21.6b0 + rev: 22.1.0 hooks: - id: black args: [--safe, --quiet, --line-length, "100"] @@ -20,7 +20,7 @@ repos: - --max-line-length=100 - --ignore=W503,E203 - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.1.0 hooks: - id: trailing-whitespace language_version: python3 @@ -29,7 +29,7 @@ repos: - id: debug-statements language_version: python3 - repo: https://github.com/asottile/pyupgrade - rev: v2.19.3 + rev: v2.31.1 hooks: - id: pyupgrade language_version: python3 diff --git a/Containerfile b/Containerfile index 866d0bf706d272d1b52056fa6155a10a9da1a500..5ffbe8adfba5509fe6554e9fcfc3f48dff654e3a 100644 --- a/Containerfile +++ b/Containerfile @@ -1,6 +1,6 @@ ARG GITLAB_RUNNER_VERSION=v13.12.0 -FROM registry.access.redhat.com/ubi8:8.4 AS builder +FROM registry.access.redhat.com/ubi8:8.5 AS builder ARG GITLAB_RUNNER_VERSION @@ -14,7 +14,7 @@ RUN dnf install -y git-core make go ncurses && \ chmod a+x out/binaries/gitlab-runner && \ out/binaries/gitlab-runner --version -FROM registry.access.redhat.com/ubi8:8.4 +FROM registry.access.redhat.com/ubi8:8.5 ARG GITLAB_RUNNER_VERSION diff --git a/README.md b/README.md index 58f2510cb3bc1c0eff43c21d191ea38d040e5c60..2e7a3f9ae24da7cf4d06a3adbcbc89e87b99daa1 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ The container expects the following environment variables: `PRIVATE_KEY` - Private key content +`SSH_TIMEOUT` - Timeout for establishing SSH connection + ### GitLab Runner variables `RUNNER_TAG_LIST` - Tag list diff --git a/config.sh b/config.sh index 0f5175154af71d2335f8bf1803378e32f0541ffc..7b0d29a1bc9bbec43589bd6b0b0cfee5cdad1728 100644 --- a/config.sh +++ b/config.sh @@ -4,7 +4,7 @@ cat << EOS { "driver": { "name": "Openstack", - "version": "2021.10.07.0" + "version": "2022.03.28.0" } } EOS diff --git a/env.py b/env.py index 110e7122c413e9b23dc4f86994b51eeba341c5da..79add3fcc8cc868d1b524fd480ed28c24c37bbfb 100644 --- a/env.py +++ b/env.py @@ -9,6 +9,7 @@ KEY_PAIR_NAME = os.getenv("CUSTOM_ENV_KEY_PAIR_NAME") or os.getenv("KEY_PAIR_NAM SECURITY_GROUP = os.getenv("CUSTOM_ENV_SECURITY_GROUP") or os.getenv("SECURITY_GROUP") USERNAME = os.getenv("CUSTOM_ENV_USERNAME") or os.getenv("USERNAME") PRIVATE_KEY_PATH = f"{os.getenv('HOME')}/priv_key" +SSH_TIMEOUT = os.getenv("CUSTOM_ENV_SSH_TIMEOUT") or os.getenv("SSH_TIMEOUT") or "30" BUILD_FAILURE_EXIT_CODE = os.getenv("BUILD_FAILURE_EXIT_CODE") SYSTEM_FAILURE_EXIT_CODE = os.getenv("SYSTEM_FAILURE_EXIT_CODE") diff --git a/prepare.py b/prepare.py index bc96a12ec72e9d45fd1542ecf81124e8b13f7657..782f131b43420bdb9509ea42c80b493c81ebaf9d 100644 --- a/prepare.py +++ b/prepare.py @@ -5,13 +5,16 @@ import traceback import openstack import paramiko from tenacity import retry +from tenacity import RetryCallState from tenacity import stop_after_attempt from tenacity import wait_fixed import env -def provision_server(conn: openstack.connection.Connection) -> openstack.compute.v2.server.Server: +def provision_server( + conn: openstack.connection.Connection, +) -> openstack.compute.v2.server.Server: image = conn.compute.find_image(env.BUILDER_IMAGE) flavor = conn.compute.find_flavor(env.FLAVOR) network = conn.network.find_network(env.NETWORK) @@ -34,10 +37,21 @@ def get_server_ip( def check_ssh(ip: str) -> None: ssh_client = paramiko.client.SSHClient() - pkey = paramiko.rsakey.RSASHA256Key.from_private_key_file(env.PRIVATE_KEY_PATH) + pkey = paramiko.rsakey.RSAKey.from_private_key_file(env.PRIVATE_KEY_PATH) ssh_client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy()) - @retry(reraise=True, stop=stop_after_attempt(10), wait=wait_fixed(10)) + def before_callback(retry_state: RetryCallState): + print( + f"Attempt: {retry_state.attempt_number}; timeout: {env.SSH_TIMEOUT} seconds", + flush=True, + ) + + @retry( + reraise=True, + stop=stop_after_attempt(10), + wait=wait_fixed(int(env.SSH_TIMEOUT)), + before=before_callback, + ) def connect(): ssh_client.connect( hostname=ip, @@ -45,7 +59,7 @@ def check_ssh(ip: str) -> None: pkey=pkey, look_for_keys=False, allow_agent=False, - timeout=20, + timeout=int(env.SSH_TIMEOUT), ) connect() @@ -53,6 +67,10 @@ def check_ssh(ip: str) -> None: def main() -> None: + print( + "Source code of this driver https://github.com/RedHatQE/openstack-gitlab-executor", + flush=True, + ) print("Connecting to Openstack", flush=True) try: conn = openstack.connect() @@ -61,7 +79,7 @@ def main() -> None: ip = get_server_ip(conn, server) print(f"Instance {env.VM_NAME} is running on address {ip}", flush=True) conn.close() - print("Checking SSH connection", flush=True) + print("Waiting for SSH connection", flush=True) check_ssh(ip) print("SSH connection has been established", flush=True) except Exception: diff --git a/requirements.txt b/requirements.txt index 4ba8a2cde3064c03e2ac7fc33dfbf6e27d2d4814..a3b3fe64b2ae6c24c4d4631b600fd3b33d3f0d5a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,7 @@ munch==2.5.0 netifaces==0.11.0 openstacksdk==0.57.0 os-service-types==1.7.0 -paramiko @ git+https://github.com/kkovaacs/paramiko.git@d43ed76a44c451b2b9031883a1a7e55de59fdf79 # adds support of modern RSA keys +paramiko==2.10.3 pbr==5.6.0 pycparser==2.20 PyNaCl==1.4.0 diff --git a/run.py b/run.py index ecba68df66a079e5d22ac248b4d13575b1a28563..7a4ba7bfb9007550df54669734822e70cc774231 100644 --- a/run.py +++ b/run.py @@ -28,7 +28,7 @@ def execute_script_on_server(ssh: paramiko.client.SSHClient, script_path: str) - def get_ssh_client(ip: str) -> paramiko.client.SSHClient: ssh_client = paramiko.client.SSHClient() - pkey = paramiko.rsakey.RSASHA256Key.from_private_key_file(env.PRIVATE_KEY_PATH) + pkey = paramiko.rsakey.RSAKey.from_private_key_file(env.PRIVATE_KEY_PATH) ssh_client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy()) ssh_client.connect( hostname=ip, @@ -36,7 +36,7 @@ def get_ssh_client(ip: str) -> paramiko.client.SSHClient: pkey=pkey, look_for_keys=False, allow_agent=False, - timeout=60, + timeout=int(env.SSH_TIMEOUT), ) return ssh_client