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.1.tar.gz (18.5 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.1-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xbot_plugins_docker-0.1.1.tar.gz
  • Upload date:
  • Size: 18.5 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.1.tar.gz
Algorithm Hash digest
SHA256 1b6255e838a8a9d0fc75acf8543db5cbf334db597375468118fc820477431af8
MD5 0da9d6e6bacf07c3abe7ff48859c0817
BLAKE2b-256 ae2036112e27dfad8aed19a852b8f22246ee4a168928b8bdbc04b0042cfea1ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for xbot_plugins_docker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b76ea600ad3225b7f70b362006e171e4b84fbe8a4153b5eb238a59a3352cd9f
MD5 3975fa94c2832426da479dae3740d2ad
BLAKE2b-256 c8ca8dd03c38484584bd41c18567b376cd03fbabf2e9d87bcc91c1a2d694f1ff

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