2020-03-18 18:46:47 -04:00
---
description: |
The `ucloud-uhost` Packer builder plugin provides the capability to build
2020-06-24 08:31:05 -04:00
customized images based on an existing base image for use in UHost Instance.
2020-03-18 18:46:47 -04:00
page_title: UCloud Image Builder
2020-04-02 19:39:47 -04:00
sidebar_title: UCloud
2020-03-18 18:46:47 -04:00
---
# UCloud Image Builder
Type: `ucloud-uhost`
2021-02-05 19:49:28 -05:00
Artifact BuilderId: `ucloud.uhost`
2020-03-18 18:46:47 -04:00
The `ucloud-uhost` Packer builder plugin provides the capability to build
2020-06-24 08:31:05 -04:00
customized images based on an existing base image for use in UHost Instance.
2020-03-18 18:46:47 -04:00
This builder builds an UCloud image by launching an UHost instance from a source image,
provisioning that running machine, and then creating an image from that machine.
## Configuration Reference
The following configuration options are available for building UCloud images. They are
segmented below into two categories: required and optional parameters.
In addition to the options listed here, a
2021-02-05 19:49:28 -05:00
[communicator](/docs/templates/legacy_json_templates/communicator) can be configured for this
2020-03-18 18:46:47 -04:00
builder.
2020-03-23 20:02:12 -04:00
~> **Note:** The builder doesn't support Windows images for now and only supports CentOS and Ubuntu images via SSH authentication with `ssh_username` (Required) and `ssh_password` (Optional). The `ssh_username` must be `root` for CentOS images and `ubuntu` for Ubuntu images. The `ssh_password` may contain 8-30 characters, and must consist of at least 2 items out of the capital letters, lower case letters, numbers and special characters. The special characters include `()~!@#\$%^&\*-+=\_|{}\[]:;'<>,.?/`.
2020-03-18 18:46:47 -04:00
### Required:
2020-06-24 08:31:05 -04:00
@include 'builder/ucloud/common/AccessConfig-required.mdx'
2020-03-18 18:46:47 -04:00
2020-06-24 08:31:05 -04:00
@include 'builder/ucloud/common/RunConfig-required.mdx'
2020-03-18 18:46:47 -04:00
2020-06-24 08:31:05 -04:00
@include 'builder/ucloud/common/ImageConfig-required.mdx'
2020-03-18 18:46:47 -04:00
### Optional:
2020-06-24 08:31:05 -04:00
@include 'builder/ucloud/common/AccessConfig-not-required.mdx'
2020-03-18 18:46:47 -04:00
2020-06-24 08:31:05 -04:00
@include 'builder/ucloud/common/RunConfig-not-required.mdx'
2020-03-18 18:46:47 -04:00
2020-06-24 08:31:05 -04:00
@include 'builder/ucloud/common/ImageConfig-not-required.mdx'
2020-03-18 18:46:47 -04:00
## Examples
Here is a basic example for build UCloud CentOS image:
2020-06-24 08:31:05 -04:00
<Tabs>
<Tab heading="JSON">
2020-03-18 18:46:47 -04:00
```json
{
"variables": {
"ucloud_public_key": "{{env `UCLOUD_PUBLIC_KEY`}}",
"ucloud_private_key": "{{env `UCLOUD_PRIVATE_KEY`}}",
"ucloud_project_id": "{{env `UCLOUD_PROJECT_ID`}}"
},
"builders": [
{
"type": "ucloud-uhost",
"public_key": "{{user `ucloud_public_key`}}",
"private_key": "{{user `ucloud_private_key`}}",
"project_id": "{{user `ucloud_project_id`}}",
"region": "cn-bj2",
"availability_zone": "cn-bj2-02",
"instance_type": "n-basic-2",
"source_image_id": "uimage-f1chxn",
"ssh_username": "root",
2020-06-24 08:31:05 -04:00
"image_name": "packer-test{{timestamp}}"
2020-03-18 18:46:47 -04:00
}
]
}
```
2020-06-24 08:31:05 -04:00
</Tab>
<Tab heading="HCL2">
```hcl
// .pkr.hcl file
variable "ucloud_public_key" {
type = string
default = "xxx"
}
variable "ucloud_private_key" {
type = string
default = "xxx"
}
variable "ucloud_project_id" {
type = string
default = "xxx"
}
source "ucloud-uhost" "basic-example" {
public_key = var.ucloud_public_key
private_key = var.ucloud_private_key
project_id = var.ucloud_project_id
region = "cn-bj2"
availability_zone = "cn-bj2-02"
instance_type = "n-basic-2"
source_image_id = "uimage-f1chxn"
ssh_username = "root"
}
build {
source "sources.ucloud-uhost.basic-example" {
image_name = "packer-test-${timestamp()}"
}
}
```
</Tab>
</Tabs>
2020-03-18 18:46:47 -04:00
Here is a example for build UCloud Ubuntu image:
2020-06-24 08:31:05 -04:00
<Tabs>
<Tab heading="JSON">
2020-03-18 18:46:47 -04:00
```json
{
"variables": {
"ucloud_public_key": "{{env `UCLOUD_PUBLIC_KEY`}}",
"ucloud_private_key": "{{env `UCLOUD_PRIVATE_KEY`}}",
"ucloud_project_id": "{{env `UCLOUD_PROJECT_ID`}}",
2020-06-24 08:31:05 -04:00
"password": "ucloud_2020"
2020-03-18 18:46:47 -04:00
},
"builders": [
{
"type": "ucloud-uhost",
"public_key": "{{user `ucloud_public_key`}}",
"private_key": "{{user `ucloud_private_key`}}",
"project_id": "{{user `ucloud_project_id`}}",
"region": "cn-bj2",
"availability_zone": "cn-bj2-02",
"instance_type": "n-basic-2",
"source_image_id": "uimage-irofn4",
"ssh_password": "{{user `password`}}",
"ssh_username": "ubuntu",
2020-06-24 08:31:05 -04:00
"image_name": "packer-test-ubuntu{{timestamp}}"
2020-03-18 18:46:47 -04:00
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | sudo -S '{{.Path}}'",
"inline": ["sleep 30", "sudo apt update", "sudo apt install nginx -y"]
}
]
}
```
2020-06-24 08:31:05 -04:00
</Tab>
<Tab heading="HCL2">
```hcl
// .pkr.hcl file
variable "ucloud_public_key" {
type = string
default = "xxx"
}
variable "ucloud_private_key" {
type = string
default = "xxx"
}
variable "ucloud_project_id" {
type = string
default = "xxx"
}
variable "password" {
type = string
default = "ucloud_2020"
}
source "ucloud-uhost" "basic-example" {
public_key = var.ucloud_public_key
private_key = var.ucloud_private_key
project_id = var.ucloud_project_id
region = "cn-bj2"
availability_zone = "cn-bj2-02"
instance_type = "n-basic-2"
ssh_password = var.password
source_image_id = "uimage-irofn4"
ssh_username = "ubuntu"
}
build {
source "sources.ucloud-uhost.basic-example" {
image_name = "packer-test-ubuntu-${timestamp()}"
}
provisioner "shell" {
execute_command = "echo '${var.password}' | sudo -S '{{.Path}}'"
inline = ["sleep 30", "sudo apt update", "sudo apt install nginx -y"]
}
}
```
</Tab>
</Tabs>
2020-03-23 20:02:12 -04:00
-> **Note:** Packer can also read the public key and private key from
2020-03-18 18:46:47 -04:00
environmental variables. See the configuration reference in the section above
for more information on what environmental variables Packer will look for.
2020-06-24 08:31:05 -04:00
~> **Note:** Source image may be deprecated after a while, you can use the tools like [UCloud CLI](https://docs.ucloud.cn/cli/intro) to run `ucloud image list` to find one that exists.