> ## Documentation Index
> Fetch the complete documentation index at: https://docs.siflow.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 镜像管理

## 创建镜像

支持通过 SDK 使用不同的构建方式创建 siflow 平台镜像

<img src="https://mintcdn.com/siflow/ntzNiCADsREJ4Kfh/products/siverse/sdk/media/sdk2.png?fit=max&auto=format&n=ntzNiCADsREJ4Kfh&q=85&s=6e93237572235e9feb968d2b1b35433f" alt="sdk" width="1754" height="330" data-path="products/siverse/sdk/media/sdk2.png" />

代码示例如下：

```python theme={null}
from siflow import SiFlow
from siflow.types import ImageBuildConfigRequest, InstanceRequest, ImageBuildConfigArgRequest

# export SIFLOW_ACCESS_KEY_ID=xxx
# export SIFLOW_ACCESS_KEY_SECRET=xxx

client = SiFlow(region="xx", cluster="xx")

image_build_config1 = ImageBuildConfigRequest(
    commit_id="1234",
    build_method="baseSiflowImage",
    basic_image_type="custom",
    basic_image_url="registry-ap-southeast.scitix.ai/ai-infra/ubuntu:0.1.4-qwer",
    description="CICD Method 1"
)

dockerfile_arg = [
    ImageBuildConfigArgRequest(
        hidden=True,
        key="VERSION",
        value="0.0.1",
    ),
    ImageBuildConfigArgRequest(
        hidden=True,
        key="NAME",
        value="ubuntu",
    )
]

image_build_config2 = ImageBuildConfigRequest(
    commit_id="1234",
    build_method="baseDockerfile",
    dockerfile_content="FROM ubuntu:20.04\nCMD [\"echo\", \"Hello from minimal Ubuntu!\"]",
    #dockerfile_path="examples/Dockerfile",
    description="CICD Method 2",
    dockerfile_arg=dockerfile_arg
)

image_build_config3 = ImageBuildConfigRequest(
    commit_id="1234",
    build_method="baseThirdImage",
    docker_hub_url="mysql:8.0",
    description="CICD Method 3"
)

image_build_config4 = ImageBuildConfigRequest(
    commit_id="1234",
    build_method="baseExistImage",
    docker_hub_url="registry-ap-southeast.scitix.ai/ai-infra/ubuntu:0.1.4-qwer",
    description="CICD Method 4"
)

instances_config = [
    InstanceRequest(
        name="sci.c23-2",
        countPerPod=10
    )
]

imageInfo = client.images.create(
    name="test",
    version="0.0.8",
    major_category="siflow",
    minor_category="cicd",
    image_build_type="custom",
    image_build_region="xx",
    image_build_cluster="xx",
    image_build_config=image_build_config2,
    resource_pool="build-image-cpu",
    instances=instances_config
)
print(imageInfo)
```

参数说明：

| 参数名                   | 类型  | 是否必填 | 说明                                                                                                                                                                                  |
| --------------------- | --- | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name                  | str | 是    | 镜像名称                                                                                                                                                                                |
| version               | str | 是    | 镜像版本                                                                                                                                                                                |
| minor\_category       | str | 是    | 镜像分类（class）                                                                                                                                                                         |
| image\_build\_type    | str | 是    | 镜像类型（custom 为自定义镜像，preset 为内置镜像）                                                                                                                                                    |
| image\_build\_region  | str | 是    | 构建镜像的资源地域                                                                                                                                                                           |
| image\_build\_cluster | str | 是    | 构建镜像的资源集群                                                                                                                                                                           |
| image\_build\_config  | obj | 是    | 构建配置                                                                                                                                                                                |
| resource\_pool        | str | 是    | 使用专属构建资源池时必填；请联系管理员确认资源池名称                                                                                                                                                          |
| instances             | obj | 是    | 使用专属构建资源池时必填；指定要使用的实例名称和实例数量<br /><br /> <pre><code>instances\_config = \[ <br />  InstanceRequest(<br />    name="sci.c23-2",<br />    countPerPod=10<br />  )<br />]</code></pre> |

image\_build\_config:

| 参数名                 | 类型         | 是否必填 | 说明                                                                                                     |
| ------------------- | ---------- | ---- | ------------------------------------------------------------------------------------------------------ |
| commit\_id          | str        | 是    | 镜像构建代码的 commit id                                                                                      |
| build\_method       | str        | 是    | 构建方式： <br /> - baseSiflowImage <br /> - baseDockerfile <br /> - baseThirdImage <br /> - baseExistImage |
| basic\_image\_type  | str        | 否    | 基础镜像类型（custom/preset），使用 baseSiflowImage 构建时必填                                                         |
| basic\_image\_url   | str        | 否    | 基础镜像 URL，使用 baseSiflowImage 方式构建时必填                                                                    |
| dockerfile\_content | str        | 否    | Dockerfile 内容；使用 baseDockerfile 构建时，dockerfile\_path 或本字段须提供其一                                         |
| dockerfile\_path    | str        | 否    | Dockerfile 的绝对路径。使用 baseDockerfile 构建时，本字段或 dockerfile\_content 须提供其一                                  |
| docker\_hub\_url    | str        | 否    | 第三方镜像 URL，使用 baseThirdImage 或 baseExistImage 构建时必填                                                     |
| description         | str        | 否    | 镜像描述详情                                                                                                 |
| pip                 | str        | 否    | 安装 pip，格式为：requests==2.31.0, flask==2.3.3                                                              |
| apt                 | str        | 否    | 安装 apt 软件包，格式为：curl=7.68.0-1ubuntu2.25, wget=1.20.3-1ubuntu2.1                                         |
| dockerfile\_arg     | list\[obj] | 否    | 使用 baseDockerfile 方式构建镜像时的构建参数                                                                         |

dockerfile\_arg:

| 参数名    | 类型   | 是否必填 | 说明                      |
| ------ | ---- | ---- | ----------------------- |
| key    | str  | 是    | 构建参数键                   |
| value  | str  | 是    | 构建参数值                   |
| hidden | bool | 否    | 是否隐藏构建参数值；未指定时默认为 false |

## 获取镜像列表

支持通过 SDK 查询镜像列表

```python theme={null}
from siflow import SiFlow
from siflow.types import ImageBuildConfigRequest

# export SIFLOW_ACCESS_KEY_ID=xxx
# export SIFLOW_ACCESS_KEY_SECRET=xxx

client = SiFlow(region="xx", cluster="xx")
images = client.images.list(
    page=1,
    pageSize=5,
    minor_category="",  # class
    image_build_type="custom",  # preset/custom
    image_build_cluster="",
    image_build_region="",
    keyword=""
)
for image in images:
    print(image)
```

参数说明：

| 参数名                   | 类型  | 是否必填 | 说明                               |
| --------------------- | --- | ---- | -------------------------------- |
| page                  | int | 否    | 分页参数                             |
| pageSize              | int | 否    | 分页参数                             |
| minor\_category       | str | 否    | 镜像分类（class）                      |
| image\_build\_type    | str | 否    | 镜像类型（custom 为自定义镜像，preset 为内置镜像） |
| image\_build\_region  | str | 否    | 构建镜像的资源地域                        |
| image\_build\_cluster | str | 否    | 构建镜像的资源集群                        |
| keyword               | str | 否    | 镜像名称或镜像版本（支持模糊搜索）                |

## 镜像共享

通过 SDK 新创建的镜像可以共享给所有组用户或特定指定用户

```bash theme={null}
from siflow import SiFlow

# export SIFLOW_ACCESS_KEY_ID=xxx
# export SIFLOW_ACCESS_KEY_SECRET=xxx

client = SiFlow(region="ap-southeast", cluster="aries")

share = client.images.share(
    image_id=xxx,
    is_show_all_users=False,
    users=["bslu", "bslu1"]
)

print(share)
```

| 参数名                  | 类型         | 是否必填 | 说明                                                                             |
| -------------------- | ---------- | ---- | ------------------------------------------------------------------------------ |
| image\_id            | int        | 是    | 镜像 ID，可通过查询镜像列表获取                                                              |
| is\_show\_all\_users | bool       | 是    | **普通用户不允许将该参数设置为 true，建议留空。对于管理员用户，设置为 true 时共享给所有组成员，设置为 false 时共享给指定用户。**    |
| users                | list\[str] | 否    | is\_show\_all\_users=false 时必填，指定要共享的用户列表；当 is\_show\_all\_users=true 时本字段将被忽略 |

```json theme={null}
# succeed
{
    "msg": "request succeed!",
    "status": true,
    "traceID": "9392e9724a48e3170dd1011d838071e6"
}

# failed
{
    "msg": "bslu2 not exist in org ai-infra, err : record not found",
    "status": false,
    "traceID": "04e0c47aeee2a4c4b7967894c550486a"
}
```

| 参数名     | 类型   | 说明      |
| ------- | ---- | ------- |
| status  | bool | 是否成功    |
| msg     | str  | 结果详情    |
| traceID | str  | Traceid |

## 结构定义

### RegisterImageResponse

| 参数名                     | 类型   | 说明                               |
| ----------------------- | ---- | -------------------------------- |
| id                      | int  |                                  |
| name                    | str  | 镜像名称                             |
| major\_category         | str  | "siflow"                         |
| minor\_category         | str  | 镜像分类（class）                      |
| version                 | str  | 镜像版本                             |
| owner                   | str  | 镜像创建者                            |
| description             | str  | 镜像详情                             |
| is\_deleted             | bool | 是否删除                             |
| image\_build\_type      | str  | 镜像类型（custom 为自定义镜像，preset 为内置镜像） |
| image\_build\_org\_name | str  | 镜像创建者所属租户                        |
| image\_build\_job\_name | str  | 构建镜像的作业名称                        |
| image\_build\_region    | str  | 构建镜像的资源地域                        |
| image\_build\_cluster   | str  | 构建镜像的资源集群                        |
| created\_at             | str  | 镜像创建时间                           |

### ImageListResponse

| 参数名                     | 类型         | 说明                               |
| ----------------------- | ---------- | -------------------------------- |
| id                      | int        |                                  |
| name                    | str        | 镜像名称                             |
| version                 | str        | 镜像版本                             |
| major\_category         | str        | "siflow"                         |
| minor\_category         | str        | 镜像分类（class）                      |
| owner                   | str        | 镜像创建者                            |
| description             | str        | 镜像详情                             |
| is\_deleted             | bool       | 是否删除                             |
| image\_build\_type      | str        | 镜像类型（custom 为自定义镜像，preset 为内置镜像） |
| image\_build\_org\_name | str        | 镜像创建者所属租户                        |
| image\_build\_job\_name | str        | 构建镜像的作业名称                        |
| image\_build\_region    | str        | 构建镜像的资源地域                        |
| image\_build\_cluster   | str        | 构建镜像的资源集群                        |
| created\_at             | str        | 镜像创建时间                           |
| updated\_at             | str        | 镜像修改时间                           |
| is\_show\_all\_users    | bool       | 是否对所有组成员可见                       |
| users                   | List\[str] | 可见用户列表                           |
| cluster\_images\_url    | str        | 镜像 URL                           |
| show\_clusters          | List\[str] | 已部署的集群列表                         |
| no\_show\_clusters      | List\[str] | 尚未部署的集群列表                        |
