python automation fabric docker - ghdrako/doc_snipets GitHub Wiki


tags:

  • containers
  • docker
  • programming
  • python

Python automation fabric docker

Fabric library designed to execute shell commands remotely over SSH, yielding useful Python objects in return. It builds on top of Invoke (subprocess command execution and command-line features) and Paramiko (SSH protocol implementation), extending their APIs to complement one another and provide additional functionality.

from fabric import Connection,task
@task
  c.run('git pull origin master')
  c.run('docker-compose down')
  c.run('docker-compose up -d')
  c.run('systemctl restart nginx')

hosts = [server1.example.com','server2.example.com']
deploy_to_server(host,'admin','path/to/ssh_key')
from fabric import Connection,task

@task
  c.run('git pull origin master', warn=True)
  c.run('source venv/bin/activate && pip install -r requirements.txt', warn=True)
  c.run(systemctl restart my_web_app', warn=True)
  print("Deploy succesfully")
deploy()
import docker

client = docker.from_env()
# Pulling image
image = client.images.pull('python:3.8-slim')
# Running containers
container = client.containers.run(image,detach=True,name='my_python_app')
# Listening container
print(container.name)
container = client.containers.get('my_python_app')
container.stop()
from kubernetes import client,config

config.load_kube_config()

api_instance = client.CoreV1Api()

pod_list = api_instance.list_namespace_pod(namespace='default')
print(pod.metadata_name)

# ceate new namespace
namespace_bpdy = client.V1Namespace(metadata=client.V1ObjectMeta(name='new-namespace'))
api_instance.create_namespace(namespace_body)