Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ The container expects the following environment variables:

`PRIVATE_KEY` - Private key content

`PASSWORD` - Password overrides a set PRIVATE_KEY (not recommended)

`PASSPHRASE` - Passphrase to your private key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be not mandatory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty should work - I however did not explicitly test it without


### GitLab Runner variables

`RUNNER_TAG_LIST` - Tag list
Expand Down Expand Up @@ -90,6 +94,8 @@ NETWORK=<your value>
KEY_PAIR_NAME=<your value>
SECURITY_GROUP=<your value>
USERNAME=<your value>
PASSWORD=<your value>
PASSPHRASE=<your value>

OS_AUTH_URL=<your value>
OS_PROJECT_NAME=<your value>
Expand Down
2 changes: 2 additions & 0 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
KEY_PAIR_NAME = os.getenv("CUSTOM_ENV_KEY_PAIR_NAME") or os.getenv("KEY_PAIR_NAME")
SECURITY_GROUP = os.getenv("CUSTOM_ENV_SECURITY_GROUP") or os.getenv("SECURITY_GROUP")
USERNAME = os.getenv("CUSTOM_ENV_USERNAME") or os.getenv("USERNAME")
PASSWORD = os.getenv("CUSTOM_ENV_PASSWORD") or os.getenv("PASSWORD")
PRIVATE_KEY_PATH = f"{os.getenv('HOME')}/priv_key"
PASSPHRASE = os.getenv("CUSTOM_ENV_PASSPHRASE") or os.getenv("PASSPHRASE")

BUILD_FAILURE_EXIT_CODE = os.getenv("BUILD_FAILURE_EXIT_CODE")
SYSTEM_FAILURE_EXIT_CODE = os.getenv("SYSTEM_FAILURE_EXIT_CODE")
26 changes: 26 additions & 0 deletions env.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
RUNNER_TAG_LIST=<your value>
REGISTRATION_TOKEN=<your value>
RUNNER_NAME=<your value>
CI_SERVER_URL=<your value>
RUNNER_BUILDS_DIR=<your value>
RUNNER_CACHE_DIR=<your value>
CONCURRENT=<your value>

FLAVOR=<your value>
BUILDER_IMAGE=<your value>
NETWORK=<your value>
KEY_PAIR_NAME=<your value>
SECURITY_GROUP=<your value>
USERNAME=<your value>
PASSWORD=<your value>
PASSPHRASE=<your value>

OS_AUTH_URL=<your value>
OS_PROJECT_NAME=<your value>
OS_USERNAME=<your value>
OS_PASSWORD=<your value>
OS_PROJECT_DOMAIN_NAME=<your value>
OS_USER_DOMAIN_NAME=<your value>
OS_REGION_NAME=<your value>
OS_IDENTITY_API_VERSION=<your value>
OS_INTERFACE=<your value>
27 changes: 19 additions & 8 deletions prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,25 @@ def check_ssh(ip: str) -> None:

@retry(reraise=True, stop=stop_after_attempt(3), wait=wait_fixed(10))
def connect():
ssh_client.connect(
hostname=ip,
username=env.USERNAME,
pkey=pkey,
look_for_keys=False,
allow_agent=False,
timeout=20,
)
if len(env.PASSWORD) > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should take into account the case when PRIVATE_KEY_PATH is not provided and PASSWORD is the only secret.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. PRIVATE_KEY_PATH is always provided by env.py even if PRIVATE_KEY was empty.

Copy link
Contributor

@quarckster quarckster Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRIVATE_KEY_PATH is always provided by env.py even if PRIVATE_KEY was empty.

Sorry, I meant PRIVATE_KEY. It doesn't make sense to provide PRIVATE_KEY if you have PASSWORD and vice versa. They are mutually exclusive arguments.

def get_cred():
    if env.PASSWORD:
        return {"password": env.PASSWORD}
    if env.PRIVATE_KEY:
        pkey = paramiko.rsakey.RSASHA256Key.from_private_key_file(env.PRIVATE_KEY_PATH)
        return {"passphrase": env.PASSPHRASE, "pkey": pkey}
    raise ValueError("Either PASSWORD or PRIVATE_KEY must be defined")
...

ssh_client.connect(
    hostname=ip,
    username=env.USERNAME,
    look_for_keys=False,
    allow_agent=False,
    timeout=60,
    **get_cred()
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can of course write it like that. How about you merge it and change this style detail then?

ssh_client.connect(
hostname=ip,
username=env.USERNAME,
password=env.PASSWORD,
look_for_keys=False,
allow_agent=False,
timeout=60,
)
else:
ssh_client.connect(
hostname=ip,
username=env.USERNAME,
pkey=pkey,
passphrase=env.PASSPHRASE,
look_for_keys=False,
allow_agent=False,
timeout=60,
)

connect()
ssh_client.close()
Expand Down
29 changes: 21 additions & 8 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ 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)
ssh_client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
ssh_client.connect(
hostname=ip,
username=env.USERNAME,
pkey=pkey,
look_for_keys=False,
allow_agent=False,
timeout=60,
)
def connect():
if len(env.PASSWORD) > 1:
ssh_client.connect(
hostname=ip,
username=env.USERNAME,
password=env.PASSWORD,
look_for_keys=False,
allow_agent=False,
timeout=60,
)
else:
ssh_client.connect(
hostname=ip,
username=env.USERNAME,
pkey=pkey,
passphrase=env.PASSPHRASE,
look_for_keys=False,
allow_agent=False,
timeout=60,
)
connect()
return ssh_client


Expand Down