Examples¶
Run a container¶
For running a container, you need to have image initialized. Image holds information of repository and tag and provides other methods. To run image using docker binary (as user would) use conu.apidefs.image.Image.run_via_binary()
method with conu.backend.docker.container.DockerRunBuilder
as parameter.
from conu import DockerBackend
from conu.helpers import get_container_output
with DockerBackend() as backend:
# This will run the container using the supplied command, collects output and
# cleans the container
output = get_container_output(backend, "registry.fedoraproject.org/fedora", ["ls", "-1", "/etc"],
image_tag="27")
assert "passwd" in output
Wait for service to be ready¶
conu.backend.docker.container.DockerContainer.wait_for_port()
tries to reach 8080 till it’s opened. You can use your own timeout to limit time spent on waiting.
from conu import DockerBackend, DockerRunBuilder
with DockerBackend() as backend:
image = backend.ImageClass('centos/httpd-24-centos7')
additional_opts = ["-p", "8080:8080"]
container = image.run_via_binary(additional_opts=additional_opts)
container.wait_for_port(port=8080, timeout=-1)
container.stop()
container.delete()
Extend image using source-to-image¶
Extends acts as s2i binary. It extends builder image in form of conu.backend.docker.image.S2IDockerImage
using provided source and desired name of resulting image.
from conu import S2IDockerImage, DockerBackend
# to make sure that temporary directory is cleaned
with DockerBackend():
source = 'https://github.com/dbarnett/python-helloworld'
image = S2IDockerImage("centos/python-35-centos7")
extended_image = image.extend(source, "myapp")
container = image.run_via_binary()
container.stop()
container.delete()
Run image in pod¶
Run image inside k8s conu.backend.k8s.pod.Pod
import logging
from conu import K8sBackend, \
DockerBackend
from conu.backend.k8s.pod import PodPhase
from conu.utils import get_oc_api_token
api_key = get_oc_api_token()
with K8sBackend(api_key=api_key, logging_level=logging.DEBUG) as k8s_backend:
namespace = k8s_backend.create_namespace()
with DockerBackend(logging_level=logging.DEBUG) as backend:
image = backend.ImageClass("openshift/hello-openshift")
pod = image.run_in_pod(namespace=namespace)
try:
pod.wait(200)
assert pod.is_ready()
assert pod.get_phase() == PodPhase.RUNNING
finally:
pod.delete()
assert pod.get_phase() == PodPhase.TERMINATING
k8s_backend.delete_namespace(namespace)
Deploy new application in OpenShift using remote source¶
Build and deploy new application in OpenShift using centos/python-36-centos7
image and remote source.
import logging
from conu import OpenshiftBackend
from conu.utils import get_oc_api_token
api_key = get_oc_api_token()
with OpenshiftBackend(api_key=api_key, logging_level=logging.DEBUG) as openshift_backend:
openshift_backend.get_status()
python_image = openshift_backend.import_image("python-36-centos7",
"docker.io/centos/python-36-centos7")
# create new app from remote source in OpenShift cluster
app_name = openshift_backend.create_new_app_from_source(
python_image,
source="https://github.com/openshift/django-ex.git",
project='myproject')
try:
# wait until service is ready to accept requests
openshift_backend.wait_for_service(
app_name=app_name,
port=8080,
expected_output='Welcome to your Django application on OpenShift',
timeout=300)
finally:
openshift_backend.get_logs(app_name)