Skip to content

Instantly share code, notes, and snippets.

Avatar
😀

Kostja P kpavlovsky

😀
  • Europe
View GitHub Profile
@kpavlovsky
kpavlovsky / deployment_v3.py
Created Oct 4, 2020
Deployments with temouts, abort, non-blocking with paramiko
View deployment_v3.py
def run_deployment_v3(deployment: Deployment):
"""Runs deployment as 'app' user with separate commands passing commands via base64 scripts,
with timeouts and possibility to abort deployment"""
if deployment.is_aborted:
print("deployment is aborted")
deployment.log("Deployment was aborted")
deployment.status = DEPLOYMENT_STATUS.aborted
deployment.save(update_fields=['status', ])
return "Aborted"
server = deployment.application.server # type: Server
@kpavlovsky
kpavlovsky / paramiko_long_running_command_non_blocking_output_timeout.py
Created Sep 29, 2020
Using paramiko run long running command non blocking with timeout of 60 seconds
View paramiko_long_running_command_non_blocking_output_timeout.py
import paramiko
import time
from io import StringIO
import sys
import select
def main():
"""Run cat command and tries to cancel it """
ip_address = '52.28.75.66'
with open('/Users/k/.ssh/id_rsa', 'r') as f:
@kpavlovsky
kpavlovsky / deployment.py
Created Sep 28, 2020
Run the deployment: Celery, Paramiko
View deployment.py
def run_deployment(deployment: Deployment):
"""Runs deployment as 'app' user with separate commands passing commands via base64 scripts"""
server = deployment.application.server # type: Server
rsa_key = server.user.private_key # type: str
deployment.log(f"ran run_deployment_v2 for deployment {deployment.id}")
username = 'app'
deployment.log(f'??? Connecting to {username}@{server.ip_address}')
try:
pkey = paramiko.RSAKey.from_private_key(StringIO(rsa_key))
client = paramiko.SSHClient()
@kpavlovsky
kpavlovsky / Dockerfile
Created Jul 3, 2020
Dockerfile Python 3.8 Simplified
View Dockerfile
FROM python:3.8
SHELL ["/bin/bash", "-c"]
ENV PIP_NO_CACHE_DIR off
ENV PIP_DISABLE_PIP_VERSION_CHECK on
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 0
WORKDIR /code/
COPY ./code/requirements.txt /code/
RUN pip install -r requirements.txt
COPY ./code/ /code/
@kpavlovsky
kpavlovsky / Dockerfile
Created Jul 3, 2020
Dockerfile for PYPY3
View Dockerfile
FROM pypy:3
SHELL ["/bin/bash", "-c"]
ENV PIP_NO_CACHE_DIR off
ENV PIP_DISABLE_PIP_VERSION_CHECK on
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 0
RUN apt-get update \
&& apt-get install -y --force-yes \
nano python-pip gettext chrpath libssl-dev libxft-dev \
libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev\
@kpavlovsky
kpavlovsky / Dockerfile
Created Jul 3, 2020
Dockerfile for PHP-7
View Dockerfile
FROM python:3.8
SHELL ["/bin/bash", "-c"]
ENV PIP_NO_CACHE_DIR off
ENV PIP_DISABLE_PIP_VERSION_CHECK on
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 0
RUN apt-get update \
&& apt-get install -y --force-yes \
nano python-pip gettext chrpath libssl-dev libxft-dev \
libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev\
@kpavlovsky
kpavlovsky / Dockerfile
Created Jul 3, 2020
Dockerfile for Node 14
View Dockerfile
FROM python:3.8
SHELL ["/bin/bash", "-c"]
ENV PIP_NO_CACHE_DIR off
ENV PIP_DISABLE_PIP_VERSION_CHECK on
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 0
RUN apt-get update \
&& apt-get install -y --force-yes \
nano python-pip gettext chrpath libssl-dev libxft-dev \
libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev\
@kpavlovsky
kpavlovsky / Dockerfile
Created Jul 3, 2020
Dockerfile for Python3.8
View Dockerfile
FROM python:3.8
SHELL ["/bin/bash", "-c"]
ENV PIP_NO_CACHE_DIR off
ENV PIP_DISABLE_PIP_VERSION_CHECK on
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 0
RUN apt-get update \
&& apt-get install -y --force-yes \
nano python-pip gettext chrpath libssl-dev libxft-dev \
libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev\
@kpavlovsky
kpavlovsky / settings.py
Last active Jun 5, 2020
Nginx Cache Setup for Django Site. Uses Django Dummy Cache Backend by default to set required Cache-Control Headers for downstream Nginx Cache
View settings.py
REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:6379')
CACHES = {
"redis": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
"default": {
@kpavlovsky
kpavlovsky / server_setup.py
Created Oct 28, 2019
Connect to a remote SSH server with private key in a string, download a bash script and run it with CURL
View server_setup.py
def server_setup(server_id: int):
"""Connects to server and runs installation of required software"""
server = Server.objects.get(pk=server_id)
server.setup_status = SETUP_STATUS.started
server.save(update_fields=['setup_status', ])
rsa_key = server.user.private_key
print(f"ran server_setup server_id={server_id}")
setup_command = "curl -sSL https://app.appliku.com/server_api/initial_installation.sh | bash"
try:
pkey = paramiko.RSAKey.from_private_key(StringIO(rsa_key))