Skip to content

Commit

Permalink
Allow launching container with remote image (#34)
Browse files Browse the repository at this point in the history
* Allow image names with remote: prefix

Load default LXD remotes into LXD client config

* Improve documentation of supported remotes.

Document the supported remotes, which are currently only the hard codec LXD Default Image remotes + the LXD server defined in the provider.
  • Loading branch information
sl1pm4t authored Apr 7, 2017
1 parent 0e5b922 commit 614b6e4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
terraform-provider-lxd.iml
.idea
**/*.swp
settings.json
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ resource "lxd_container" "test1" {
}
```

You can also launch a container directly from a remote image, not locally cached, by referencing the remote image name using the format `[remote:]<image_alias|image_hash>`

```hcl
resource "lxd_container" "test1" {
name = "test1"
image = "images:ubuntu/xenial/amd64"
ephemeral = false
}
```

> NOTE:
> Currently the only supported remotes are:
> * remote named defined in LXD provider (same as omitting `<remote>:` prefix)
> * `images`
> * `ubuntu`
> * `ubuntu-daily`
> See the LXD (https://linuxcontainers.org/lxd/getting-started-cli/#using-the-built-in-image-remotes)[documentation] for more info on default image remotes.
#### Container Configuration & Devices

A container can also take a number of configuration and device options. A full reference can be found [here](https://github.com/lxc/lxd/blob/master/doc/configuration.md). For example, to create a container with 2 CPUs and to share the `/tmp` directory with the LXD host:

```hcl
Expand Down
6 changes: 6 additions & 0 deletions lxd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,17 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}

// Load static Public remotes
for k, v := range lxd.DefaultRemotes {
config.Remotes[k] = v
}

client, err := lxd.NewClient(&config, remote)
if err != nil {
err := fmt.Errorf("Could not create LXD client: %s", err)
return nil, err
}

log.Printf("[DEBUG] LXD Client: %#v", client)

if err := validateClient(client); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions lxd/resource_lxd_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func resourceLxdContainerCreate(d *schema.ResourceData, meta interface{}) error
name := d.Get("name").(string)
ephem := d.Get("ephemeral").(bool)
image := d.Get("image").(string)
if imgParts := strings.SplitN(image, ":", 2); len(imgParts) == 2 {
remote = imgParts[0]
image = imgParts[1]
}
config := resourceLxdConfigMap(d.Get("config"))
devices := resourceLxdDevices(d.Get("device"))

Expand Down
29 changes: 29 additions & 0 deletions lxd/resource_lxd_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ func TestAccContainer_basic(t *testing.T) {
})
}

func TestAccContainer_remoteImage(t *testing.T) {
var container api.Container
containerName := strings.ToLower(petname.Generate(2, "-"))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccContainer_remoteImage(containerName),
Check: resource.ComposeTestCheckFunc(
testAccContainerRunning(t, "lxd_container.container1", &container),
resource.TestCheckResourceAttr("lxd_container.container1", "name", containerName),
),
},
},
})
}

func TestAccContainer_config(t *testing.T) {
var container api.Container
containerName := strings.ToLower(petname.Generate(2, "-"))
Expand Down Expand Up @@ -620,3 +639,13 @@ resource "lxd_container" "container1" {
}
`, name)
}

func testAccContainer_remoteImage(name string) string {
return fmt.Sprintf(`
resource "lxd_container" "container1" {
name = "%s"
image = "images:ubuntu/xenial/amd64"
profiles = ["default"]
}
`, name)
}

0 comments on commit 614b6e4

Please sign in to comment.