2015-02-17 17:28:33 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2015-12-21 10:54:24 +01:00
|
|
|
"regexp"
|
|
|
|
|
2015-02-17 17:28:33 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceDockerContainer() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceDockerContainerCreate,
|
|
|
|
Read: resourceDockerContainerRead,
|
|
|
|
Update: resourceDockerContainerUpdate,
|
|
|
|
Delete: resourceDockerContainerDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Indicates whether the container must be running.
|
|
|
|
//
|
|
|
|
// An assumption is made that configured containers
|
|
|
|
// should be running; if not, they should not be in
|
|
|
|
// the configuration. Therefore a stopped container
|
|
|
|
// should be started. Set to false to have the
|
|
|
|
// provider leave the container alone.
|
|
|
|
//
|
|
|
|
// Actively-debugged containers are likely to be
|
|
|
|
// stopped and started manually, and Docker has
|
|
|
|
// some provisions for restarting containers that
|
|
|
|
// stop. The utility here comes from the fact that
|
|
|
|
// this will delete and re-create the container
|
|
|
|
// following the principle that the containers
|
|
|
|
// should be pristine when started.
|
|
|
|
"must_run": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Default: true,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// ForceNew is not true for image because we need to
|
|
|
|
// sane this against Docker image IDs, as each image
|
|
|
|
// can have multiple names/tags attached do it.
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"hostname": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domainname": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"command": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
2015-10-26 22:24:48 +01:00
|
|
|
"entrypoint": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
2016-04-05 04:43:59 +02:00
|
|
|
"user": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
2015-02-17 17:28:33 +01:00
|
|
|
"dns": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-02-17 17:28:33 +01:00
|
|
|
},
|
|
|
|
|
2016-06-29 14:38:46 +02:00
|
|
|
"dns_opts": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
|
|
|
"dns_search": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
2015-02-17 17:28:33 +01:00
|
|
|
"publish_all_ports": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-10-27 17:08:57 +01:00
|
|
|
"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)
|
2016-02-26 04:38:31 +01:00
|
|
|
if !regexp.MustCompile(`^(no|on-failure|always|unless-stopped)$`).MatchString(value) {
|
2015-10-27 17:08:57 +01:00
|
|
|
es = append(es, fmt.Errorf(
|
2016-02-26 04:38:31 +01:00
|
|
|
"%q must be one of \"no\", \"on-failure\", \"always\" or \"unless-stopped\"", k))
|
2015-10-27 17:08:57 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"max_retry_count": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-02-17 17:28:33 +01:00
|
|
|
"volumes": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-12-03 11:51:59 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"from_container": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"container_path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"host_path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-01-15 22:59:33 +01:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if !regexp.MustCompile(`^/`).MatchString(value) {
|
|
|
|
es = append(es, fmt.Errorf(
|
|
|
|
"%q must be an absolute path", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-12-03 11:51:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"read_only": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceDockerVolumesHash,
|
2015-02-17 17:28:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"ports": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-12-03 11:51:59 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"internal": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"external": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Default: "tcp",
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceDockerPortsHash,
|
2015-02-17 17:28:33 +01:00
|
|
|
},
|
|
|
|
|
2016-01-15 03:59:07 +01:00
|
|
|
"host": &schema.Schema{
|
2015-10-09 15:05:43 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-10-27 11:54:05 +02:00
|
|
|
Required: true,
|
2015-10-09 15:05:43 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"host": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-10-27 11:54:05 +02:00
|
|
|
Required: true,
|
2015-10-09 15:05:43 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceDockerHostsHash,
|
2015-02-17 17:28:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"env": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-02-17 17:28:33 +01:00
|
|
|
},
|
2015-04-16 21:42:21 +02:00
|
|
|
|
|
|
|
"links": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-04-16 21:42:21 +02:00
|
|
|
},
|
2015-04-16 15:21:14 +02:00
|
|
|
|
2015-04-20 21:18:46 +02:00
|
|
|
"ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip_prefix_length": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"gateway": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"bridge": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-06-04 12:57:38 +02:00
|
|
|
|
|
|
|
"privileged": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-10-28 00:53:49 +01:00
|
|
|
|
2016-07-11 17:03:02 +02:00
|
|
|
"destroy_grace_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2015-11-03 21:20:58 +01:00
|
|
|
"labels": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-10-28 00:53:49 +01:00
|
|
|
"memory": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-11-10 01:36:23 +01:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < 0 {
|
|
|
|
es = append(es, fmt.Errorf("%q must be greater than or equal to 0", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-10-28 00:53:49 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"memory_swap": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-11-10 01:36:23 +01:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < -1 {
|
|
|
|
es = append(es, fmt.Errorf("%q must be greater than or equal to -1", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-10-28 00:53:49 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"cpu_shares": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-11-10 01:36:23 +01:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < 0 {
|
|
|
|
es = append(es, fmt.Errorf("%q must be greater than or equal to 0", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-10-28 00:53:49 +01:00
|
|
|
},
|
2015-11-04 18:42:55 +01:00
|
|
|
|
|
|
|
"log_driver": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: "json-file",
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if !regexp.MustCompile(`^(json-file|syslog|journald|gelf|fluentd)$`).MatchString(value) {
|
|
|
|
es = append(es, fmt.Errorf(
|
|
|
|
"%q must be one of \"json-file\", \"syslog\", \"journald\", \"gelf\", or \"fluentd\"", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"log_opts": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-01-01 09:57:21 +01:00
|
|
|
|
|
|
|
"network_mode": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-01-14 08:01:03 +01:00
|
|
|
|
2016-01-04 20:58:54 +01:00
|
|
|
"networks": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-01-05 03:46:24 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2016-01-04 20:58:54 +01:00
|
|
|
},
|
2016-12-05 12:06:34 +01:00
|
|
|
|
|
|
|
"upload": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"content": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
// This is intentional. The container is mutated once, and never updated later.
|
|
|
|
// New configuration forces a new deployment, even with the same binaries.
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"file": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceDockerUploadHash,
|
|
|
|
},
|
2015-02-17 17:28:33 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDockerPortsHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", m["internal"].(int)))
|
|
|
|
|
|
|
|
if v, ok := m["external"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(int)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["ip"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["protocol"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
2015-10-09 15:05:43 +02:00
|
|
|
func resourceDockerHostsHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
if v, ok := m["ip"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["host"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:28:33 +01:00
|
|
|
func resourceDockerVolumesHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
if v, ok := m["from_container"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["container_path"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["host_path"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
2016-01-15 22:59:33 +01:00
|
|
|
if v, ok := m["volume_name"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:28:33 +01:00
|
|
|
if v, ok := m["read_only"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(bool)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
2016-12-05 12:06:34 +01:00
|
|
|
|
|
|
|
func resourceDockerUploadHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
if v, ok := m["content"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["file"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v-", v.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|