Skip to content

Commit

Permalink
Add packer HCL example
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Zhang committed Apr 5, 2023
1 parent 722d9e5 commit 236a550
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,84 @@ on [installing a plugin](https://www.packer.io/docs/extending/plugins/#installin
For more information on how to configure the plugin, please read the
documentation located in the [`docs/`](docs) directory.

## Usage Example

Before you try the example in this section, you should get public, private key
and project id from UCloud console and export the following environment varialbes:

- `UCLOUD_PRIVATE_KEY`
- `UCLOUD_PUBLIC_KEY`
- `UCLOUD_PROJECT_ID`

Then create a packer instruction file in HCL syntax as follows:


packer {
required_plugins {
ucloud = {
version = ">= 1.0.8"
source = "github.com/ucloud/ucloud"
}
}
}

variable "ucloud_private_key" {
type = string
default = "${env("UCLOUD_PRIVATE_KEY")}"
}

variable "ucloud_project_id" {
type = string
default = "${env("UCLOUD_PROJECT_ID")}"
}

variable "ucloud_public_key" {
type = string
default = "${env("UCLOUD_PUBLIC_KEY")}"
}

source "ucloud-uhost" "centos7-image-seed" {
availability_zone = "cn-sh2-02"
image_name = "uk8s-centos-7.9"
instance_type = "o-standard-2"
private_key = "${var.ucloud_private_key}"
project_id = "${var.ucloud_project_id}"
public_key = "${var.ucloud_public_key}"
region = "cn-sh2"
source_image_id = "uimage-vj5ui3"
ssh_username = "root"
}

build {
name = "UK8S-Centos7-BaseImage"
sources = ["source.ucloud-uhost.centos7-image-seed"]


provisioner "shell" {
inline = ["echo hello"]
}

provisioner "ansible" {
playbook_file = "./playbook-centos.yml"
}
}

Name the file as `xxx.pkr.hcl` and run `packer init` command under the
directory where the above file located:

packer init xxx.pkr.hcl

Packer will install the required plugins automatically. Then you run packer to
build the image for uhost. It is recommended that you enable the packer log to
aid diagnosis if something goes wrong. You run commands as follows:


export UCLOUD_PRIVATE_KEY="****************"
export UCLOUD_PUBLIC_KEY="******************"
export UCLOUD_PROJECT_ID="xxx"
export PACKER_LOG=1
export PACKER_LOG_PATH="packer.log"
packer build xxx.pkr.hcl

## Contributing

Expand Down

0 comments on commit 236a550

Please sign in to comment.