restart policy support for docker_container

This commit is contained in:
ryane 2015-10-27 12:08:57 -04:00
parent 0ded14f160
commit 17d185808e
4 changed files with 44 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"regexp"
)
func resourceDockerContainer() *schema.Resource {
@ -92,6 +93,27 @@ func resourceDockerContainer() *schema.Resource {
ForceNew: true,
},
"restart": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "no",
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
value := v.(string)
if !regexp.MustCompile(`^(no|on-failure|always)$`).MatchString(value) {
es = append(es, fmt.Errorf(
"%q must be one of \"no\", \"on-failure\", or \"always\"", k))
}
return
},
},
"max_retry_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"volumes": &schema.Schema{
Type: schema.TypeSet,
Optional: true,

View File

@ -95,6 +95,10 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
hostConfig := &dc.HostConfig{
Privileged: d.Get("privileged").(bool),
PublishAllPorts: d.Get("publish_all_ports").(bool),
RestartPolicy: dc.RestartPolicy{
Name: d.Get("restart").(string),
MaximumRetryCount: d.Get("max_retry_count").(int),
},
}
if len(portBindings) != 0 {

View File

@ -25,7 +25,7 @@ func TestAccDockerContainer_basic(t *testing.T) {
})
}
func TestAccDockerContainer_entrypoint(t *testing.T) {
func TestAccDockerContainer_customized(t *testing.T) {
var c dc.Container
testCheck := func(*terraform.State) error {
@ -35,6 +35,15 @@ func TestAccDockerContainer_entrypoint(t *testing.T) {
c.Config.Entrypoint[2] != "ping localhost") {
return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint)
}
if c.HostConfig.RestartPolicy.Name == "on-failure" {
if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 {
return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount)
}
} else {
return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name)
}
return nil
}
@ -43,7 +52,7 @@ func TestAccDockerContainer_entrypoint(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDockerContainerEntrypointConfig,
Config: testAccDockerContainerCustomizedConfig,
Check: resource.ComposeTestCheckFunc(
testAccContainerRunning("docker_container.foo", &c),
testCheck,
@ -95,7 +104,7 @@ resource "docker_container" "foo" {
image = "${docker_image.foo.latest}"
}
`
const testAccDockerContainerEntrypointConfig = `
const testAccDockerContainerCustomizedConfig = `
resource "docker_image" "foo" {
name = "nginx:latest"
}
@ -104,5 +113,7 @@ resource "docker_container" "foo" {
name = "tf-test"
image = "${docker_image.foo.latest}"
entrypoint = ["/bin/bash", "-c", "ping localhost"]
restart = "on-failure"
max_retry_count = 5
}
`

View File

@ -48,6 +48,10 @@ The following arguments are supported:
connectivity between containers that are running on the same host.
* `hostname` - (Optional, string) Hostname of the container.
* `domainname` - (Optional, string) Domain name of the container.
* `restart` - (Optional, string) The restart policy for the container. Must be
one of "no", "on-failure", "always".
* `max_retry_count` - (Optional, int) The maximum amount of times to an attempt
a restart when `restart` is set to "on-failure"
* `must_run` - (Optional, bool) If true, then the Docker container will be
kept running. If false, then as long as the container exists, Terraform
assumes it is successful.