hashicorp / packer-plugin-lxd

Packer plugin for LXD Builder

Home Page:https://www.packer.io/docs/builders/lxd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If builder builds on a remote LXD server, then also publish the image on the remote LXD server

xyzroller opened this issue · comments

Description

With LXD configured on a local machine (on which packer will run) and a pre-configured LXD remote, I can use the LXD builder to launch and provision a container on the remote LXD server by setting container_name = "remote-name:container-name".

The LXD builder then launches the image on the remote LXD server and I can use e.g. ansible with ansible_connection=lxd and ansible_lxd_remote=remote-name to provision it.

However, after the provisioning is completed, the LXD builder publishes the image on the remote LXD server but then copies it to the local machine and deletes the image on the remote server.

Even if I specify the remote server in output_image (e.g. output_image = "remote-name:output-image"), I still end up with an image on the local machine (which is named remote-name:output-image) and no image on the remote server.

Feature request:
If I launch and provision an LXD container on a remote LXD server, then I would like to be able to publish the image on the same remote LXD server instead of having it download to the local machine on which packer is running.

(Note: this would be the same behaviour as the Amazon AMI Builder, which launches an AMI on AWS, provisions it and then publishes the new image to the AWS account's image repository).

Use Case(s)

The use-case is to use packer on a local machine to build and publish images on a remote LXD server, which can then be launched on that server with Terraform.

Potential configuration

The existing configuration could be re-used so that output_image is parsed and if the form remote-name:output-image is found, then the lxd publish command is lxc publish container-name remote-name: --alias output-image and not lxc publish remote-name:container-name --alias output-image.

E.g.

build {

  source "lxd.container" {
    container_name = "remote-name:container-name"
    output_image = "remote-name:output-image"        ### This parsed to detect and identify the LXD remote
  }

  provisioner "ansible" {
    ...
  }
}

Alternatively, a new configuration parameter could be introduced. For example remote_publish: "remote-name", so that the lxd publish command is lxc publish container-name remote-name: --alias output-image

E.g.

build {

  source "lxd.container" {
    container_name = "remote-name:container-name"
    remote_publish = "remote-name"
    output_image = "output-image"
  }

  provisioner "ansible" {
    ...
  }
}

Maybe cleaner would then be to use a single remote configuration parameter for modifying both container_name and output_image.

E.g.

build {

  source "lxd.container" {
    remote = "remote-name"
    container_name = "container-name"
    output_image = "output-image"
  }

  provisioner "ansible" {
    ...
  }
}

...but this would then force both build and publish to be on the remote machine, which may not be wanted by some users, so for full flexibility we could have, e.g.

build {

  source "lxd.container" {
    remote_build = "remote-name"
    container_name = "container-name"
    remote_publish = "remote-name"
    output_image = "output-image"
  }

  provisioner "ansible" {
    ...
  }
}

Hope that makes sense.
Thanks in advance!