Skip to content

Instantly share code, notes, and snippets.

View andgineer's full-sized avatar
💭
Andrey Sorokin, engineer

Andrey Sorokin andgineer

💭
Andrey Sorokin, engineer
View GitHub Profile
@andgineer
andgineer / db_fixture.py
Created June 9, 2020 15:38
pytest Fixture to rollback all DB changes after test
from sqlalchemy import event
from sqlalchemy.orm import sessionmaker
from app.db.session import engine
import pytest
import app.tests.config
@pytest.fixture(
scope='function',
autouse=True # New test DB session for each test todo we need it only for tests with Client fixture
@andgineer
andgineer / container_is_not_running.sh
Last active July 10, 2020 06:38
Check if a docker-compose service is running
#! /usr/bin/env bash
#
# Check if the $1 docker-compose service is running
#
# Usage:
# is_container_running <service name>
#
function container_is_not_running {
if [[ -z `docker ps -q --no-trunc | grep $(docker-compose ps -q $1)` ]]; then
@andgineer
andgineer / subst.sh
Created March 6, 2021 11:53
Substitute placeholders in config like ${name} with all env vars defined in current session
for VAR in $(compgen -e); do
# to work in zsh replace `${!VAR}` with `${(P)VAR}`
sed -i 's|${'$VAR'}|'"${!VAR}"'|g' /config.yml
done
@andgineer
andgineer / show_workers_ports.sh
Created March 12, 2021 13:55
Show port number of docker containers with specific name
for container_id in $(docker ps --filter "name=<ContainerName>" --quiet); do
docker inspect --format='{{(index (index .NetworkSettings.Ports "<PostNumber>/tcp") 0).HostPort}}' $container_id
done
@andgineer
andgineer / loadVarsFromText.groovy
Last active July 18, 2021 04:43
Jenkins groovy function. Loads into Jenkins env from lines "key=value" in `text` https://sorokin.engineer/posts/en/jenkins_load_env_vars_with_expanding.html
private void loadVarsFromText(String text) {
text.split('\n').each { envLine ->
def (key, value) = envLine.tokenize('=')
env."${key}" = "${value}"
}
}
"""
Loads a number of files listed in args:
python expand_vars.py file1 file2 ...
Expects line format like
key=value
Skips comments and empty lines.
Substitute vars $var / ${var} from system environment and from previous lines / files/
Universal Python 2 / 3 module to use on any Jenkins agent.
"""
private void loadVarsFromFile(String path) {
def file = readFile(path)
.replaceAll("(?m)^\\s*\\r?\\n", "") // skip empty line
.replaceAll("(?m)^#[^\\n]*\\r?\\n", "") // skip commented lines
file.split('\n').each { envLine ->
def (key, value) = envLine.tokenize('=')
env."${key}" = "${value.trim().replaceAll('^\"|\"$', '')}"
}
}
sh """
aws secretsmanager get-secret-value --secret-id accounts --query 'SecretString' --output text > secrets.json
""".stripIndent()
jsonfile = readJSON file: 'secrets.json'
env.MY_SECRET = jsonfile.MY_SECRET
@andgineer
andgineer / async_nullcontext.py
Created October 15, 2021 04:59
async version of nullcontext
session = get_session() if s3 is None else None
async with session.create_client(
"s3"
) if s3 is None else contextlib.AsyncExitStack() as s3_temp:
s3_object = await (s3 or s3_temp).get_object(Bucket=bucket, Key=key)
@andgineer
andgineer / aiobojtocore_client_context.py
Last active October 15, 2021 05:20
aiobojtocore client context
session = get_session()
async with session.create_client("s3") as s3_temp:
s3_object = await s3_temp.get_object(Bucket=bucket, Key=key)