Skip to main content

Docker library for xbot.framework

Project description

Introduction

xbot.plugins.docker provides Docker support for xbot.framework.

Installation

Install via pip:

pip install xbot.plugins.docker

Get Started

import os
import shutil

from xbot.plugins.docker.docker import DockerConnection

host = '192.168.8.8'

container_conn = DockerConnection()
container_conn.connect(
    host,
    container='existing-container',
    user='root',
)
container_conn.exec('echo hello', expect=0)
container_conn.disconnect()

image_conn = DockerConnection()
image_conn.connect(
    host,
    image='alpine:latest',
    runargs={'command': 'sleep infinity'},
    user='root',
)

image_conn.exec('echo hello', expect=0)
image_conn.exec('/bin/sh -c "exit 2"', expect=2)
image_conn.exec('echo hello', expect='hello')
image_conn.exec('/bin/sh -c "exit 7"', expect=None)

result = image_conn.exec('echo "jack 20"; echo "tom 30"')
assert str(result) == 'jack 20\ntom 30'
assert result.rc == 0
assert result.getfield('tom', 2) == '30'
assert result.getcol(2) == ['20', '30']

with image_conn.cd('/tmp'):
    assert image_conn.exec('pwd') == '/tmp'

local_home = os.environ.get('HOME') or os.environ['HOMEPATH']
local_put_dir = os.path.join(local_home, 'docker-put-dir')
local_get_dir = os.path.join(local_home, 'docker-get-dir')
remote_dir = '/tmp/docker-put-get-dir'
shutil.rmtree(local_put_dir, ignore_errors=True)
shutil.rmtree(local_get_dir, ignore_errors=True)
os.makedirs(os.path.join(local_put_dir, 'tree', 'nested'))
os.makedirs(local_get_dir)
with open(
    os.path.join(local_put_dir, 'file'),
    'w',
    encoding='utf-8',
) as local_file:
    local_file.write('xbot\n')

image_conn.makedirs(remote_dir)
image_conn.putfile(
    os.path.join(local_put_dir, 'file'),
    remote_dir,
)
image_conn.putfile(
    os.path.join(local_put_dir, 'file'),
    remote_dir,
    filename='renamed',
)
image_conn.putdir(
    os.path.join(local_put_dir, 'tree'),
    remote_dir,
)
image_conn.getfile(
    image_conn.join(remote_dir, 'file'),
    local_get_dir,
)
image_conn.getfile(
    image_conn.join(remote_dir, 'file'),
    local_get_dir,
    filename='renamed',
)
image_conn.getdir(
    image_conn.join(remote_dir, 'tree'),
    local_get_dir,
)

image_conn.disconnect()

Demo

Here is a demo project showing how to use xbot.plugins.docker in a test project.


简介

xbot.plugins.docker 是为 xbot.framework 提供 Docker 支持的插件。

安装

使用 pip 安装:

pip install xbot.plugins.docker

入门

import os
import shutil

from xbot.plugins.docker.docker import DockerConnection

host = '192.168.8.8'

container_conn = DockerConnection()
container_conn.connect(
    host,
    container='existing-container',
    user='root',
)
container_conn.exec('echo hello', expect=0)
container_conn.disconnect()

image_conn = DockerConnection()
image_conn.connect(
    host,
    image='alpine:latest',
    runargs={'command': 'sleep infinity'},
    user='root',
)

image_conn.exec('echo hello', expect=0)
image_conn.exec('/bin/sh -c "exit 2"', expect=2)
image_conn.exec('echo hello', expect='hello')
image_conn.exec('/bin/sh -c "exit 7"', expect=None)

result = image_conn.exec('echo "jack 20"; echo "tom 30"')
assert str(result) == 'jack 20\ntom 30'
assert result.rc == 0
assert result.getfield('tom', 2) == '30'
assert result.getcol(2) == ['20', '30']

with image_conn.cd('/tmp'):
    assert image_conn.exec('pwd') == '/tmp'

local_home = os.environ.get('HOME') or os.environ['HOMEPATH']
local_put_dir = os.path.join(local_home, 'docker-put-dir')
local_get_dir = os.path.join(local_home, 'docker-get-dir')
remote_dir = '/tmp/docker-put-get-dir'
shutil.rmtree(local_put_dir, ignore_errors=True)
shutil.rmtree(local_get_dir, ignore_errors=True)
os.makedirs(os.path.join(local_put_dir, 'tree', 'nested'))
os.makedirs(local_get_dir)
with open(
    os.path.join(local_put_dir, 'file'),
    'w',
    encoding='utf-8',
) as local_file:
    local_file.write('xbot\n')

image_conn.makedirs(remote_dir)
image_conn.putfile(
    os.path.join(local_put_dir, 'file'),
    remote_dir,
)
image_conn.putfile(
    os.path.join(local_put_dir, 'file'),
    remote_dir,
    filename='renamed',
)
image_conn.putdir(
    os.path.join(local_put_dir, 'tree'),
    remote_dir,
)
image_conn.getfile(
    image_conn.join(remote_dir, 'file'),
    local_get_dir,
)
image_conn.getfile(
    image_conn.join(remote_dir, 'file'),
    local_get_dir,
    filename='renamed',
)
image_conn.getdir(
    image_conn.join(remote_dir, 'tree'),
    local_get_dir,
)

image_conn.disconnect()

示例项目

展示如何在测试项目中使用 xbot.plugins.docker 的示例:demo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xbot_plugins_docker-0.1.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xbot_plugins_docker-0.1.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file xbot_plugins_docker-0.1.0.tar.gz.

File metadata

  • Download URL: xbot_plugins_docker-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for xbot_plugins_docker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9aa6026ce3a67818f60149125b655282271a5f15780d084ed80da2621cd43121
MD5 3e2940e4357946085c5d8ee9fa623f62
BLAKE2b-256 63761727bf90ec407eb3725fe5bafac26b62e4869c14caac329e54bb1a3d6f55

See more details on using hashes here.

File details

Details for the file xbot_plugins_docker-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for xbot_plugins_docker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25728b6eb0245af6ba593fc99a1a0006d26cf9ff3f1324ee9ed8b4371da7fd90
MD5 c5fec247ee86898657a910d219bfe11f
BLAKE2b-256 21c8cd24e35577b1dfe5595753a41b2ea2a3516c07dbd5dbc384bc58d2ae6b49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page