定制 utopia-php/orchestration 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

utopia-php/orchestration

Composer 安装命令:

composer require utopia-php/orchestration

包简介

Lite & fast micro PHP abstraction library for container orchestration

README 文档

README

Build Status Total Downloads Discord

Utopia framework orchestration library is simple and lite library for abstracting the interaction with multiple container orchestrators. This library is aiming to be as simple and easy to learn and use. This library is maintained by the Appwrite team.

Although this library is part of the Utopia Framework project it is dependency free and can be used as standalone with any other PHP project or framework.

Getting Started

Install using composer:

composer require utopia-php/orchestration

Example

<?php

require_once 'vendor/autoload.php';

use Utopia\Orchestration\Orchestration;
use Utopia\Orchestration\Adapter\DockerCLI;

// Initialise Orchestration with Docker CLI adapter.
$orchestration = new Orchestration(new DockerCLI());

// Pull the image.
$orchestration->pull('ubuntu:latest');

// Launch a ubuntu container that doesn't end using the tail command.
$containerID = $orchestration->run('ubuntu:latest', 'testContainer', ['tail', '-f', '/dev/null']);

$stderr = '';
$stdout = '';

// Execute a hello world command in the container
$orchestration->execute($containerID, ['echo', 'Hello World!'], $stdout, $stderr);

// Remove the container forcefully since it's still running.
$orchestration->remove($containerID, true);

Usage

Initialisation

There are currently two orchestrator adapters available and each of them has slightly different parameters:

  • DockerAPI

    Directly communicates to the Docker Daemon using the Docker UNIX socket.

    use Utopia\Orchestration\Orchestration;
    use Utopia\Orchestration\Adapter\DockerAPI;
    
    $orchestration = new Orchestration(new DockerAPI($username, $password, $email));

    $username, $password and $email are optional and are only used to pull private images from Docker Hub.

  • DockerCLI

    Uses the Docker CLI to communicate to the Docker Daemon.

    use Utopia\Orchestration\Orchestration;
    use Utopia\Orchestration\Adapter\DockerCLI;
    
    $orchestration = new Orchestration(new DockerCLI($username, $password));

    $username and $password are optional and are only used to pull private images from Docker Hub.

Once you have initialised your Orchestration object the following methods can be used:

  • Pulling an image

    This method pulls the image requested from the orchestrators registry. It will return a boolean value indicating if the image was pulled successfully.

    $orchestration->pull('image:tag');
    Parameters
    • image [String] [Required]

      The image to pull from the registry.


  • Running a container

    This method creates and runs a new container. On success, it will return a string containing the container ID. On failure, it will throw an exception.

    $orchestration->run(
        'image:tag',
        'name',
        ['echo', 'hello world!'],
        'entrypoint',
        'workdir',
        ['tmp:/tmp:rw', 'cooldirectory:/home/folder:rw'],
        ['ENV_VAR' => 'value'],
        '/tmp',
        ['label' => 'value'],
        'hostname',
        true,
    );
    Parameters
    • image [String] [Required]

      The image to base the container off.

    • name [String] [Required]

      The name given to the container.

    • command [Array]

      The command to run in the container seperated into a array.

    • entrypoint [String]

      The executable to run in the container.

    • workdir [String]

      The default directory in which the container commands will run.

    • volumes [Array]

      The volumes to attach to the container.

    • env [Array]

      The environment variables to set in the container.

    • mountFolder [String]

      A folder that will be automatically mounted to /tmp in the container

    • labels [Array]

      The labels to set on the container.

    • hostname [String]

      The hostname to set on the container.

    • remove [Boolean]

      Whether to remove the container once it exits.

  • Executing a command in a running container

    This method executes a command in an already running container and returns a boolean value indicating if the command was executed successfully.

    $stdout = '';
    $stderr = '';
    
    $orchestraton->execute(
        'container_id',
        ['echo', 'Hello World!'],
        $stdout,
        $stderr,
        ['VAR' => 'VALUE'],
        10,
    )
    Parameters
    • container_id [String] [Required]

      The ID of the container to execute the command in.

    • command [Array] [Required]

      The command to execute in the container.

    • stdout [String] [Reference]

      The variable to store the stdout of the command in.

    • stderr [String] [Reference]

      The variable to store the stderr of the command in.

    • env [Array]

      The environment variables to set while executing the command.

    • timeout [Integer]

      The timeout in seconds to wait for the command to finish.

  • Removing a container

    This method removes a container and returns a boolean value indicating if the container was removed successfully.

    $orchestration->remove('container_id', true);
    Parameters
    • container_id [String] [Required]

      The ID of the container to remove.

    • force [Boolean]

      Whether to remove the container forcefully.

  • List containers

    This method returns an array of containers.

    $orchestration->list(['label' => 'value']);
    Parameters
    • filters [Array]

      Filters to apply to the list of containers.

  • List Networks

    This method returns an array of networks.

    $orchestration->listNetworks();
    Parameters

    This method has no parameters

  • Create a Network

    This method creates a new network and returns a boolean value indicating if the network was created successfully.

    $orchestration->createNetwork('name', false);
    Parameters
    • name [String] [Required]

      The name of the network.

    • internal [Boolean]

      Whether to set the network to be an internal network.

  • Remove a Network

    This method removes a network and returns a boolean value indicating if the network was removed successfully.

    $orchestration->removeNetwork('network_id');
    Parameters
    • network_id [String] [Required]

      The ID of the network to remove.

  • Connect a container to a network

    This method connects a container to a network and returns a boolean value indicating if the connection was successful.

    $orchestration->connect('container_id', 'network_id');
    Parameters
    • container_id [String] [Required]

      The ID of the container to connect to the network.

    • network_id [String] [Required]

      The ID of the network to connect to.

  • Disconnect a container from a network

    This method disconnects a container from a network and returns a boolean value indicating if the removal was successful.

    $orchestration->disconnect('container_id', 'network_id', false);
    Parameters
    • container_id [String] [Required]

      The ID of the container to disconnect from the network.

    • network_id [String] [Required]

      The ID of the network to disconnect from.

    • force [Boolean]

      Whether to disconnect the container forcefully.

System Requirements

Utopia Framework requires PHP 7.3 or later. We recommend using the latest PHP version whenever possible.

Copyright and license

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php

utopia-php/orchestration 适用场景与选型建议

utopia-php/orchestration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 225.9k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2021 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「php」 「upf」 「docker」 「kubernetes」 「orchestration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 utopia-php/orchestration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 utopia-php/orchestration 我们能提供哪些服务?
定制开发 / 二次开发

基于 utopia-php/orchestration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 225.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 38
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 6
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-16