github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/builtin/providers/docker/resource_docker_container_funcs.go (about)

     1  package docker
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"time"
     8  
     9  	dc "github.com/fsouza/go-dockerclient"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  var (
    14  	creationTime time.Time
    15  )
    16  
    17  func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) error {
    18  	var err error
    19  	client := meta.(*dc.Client)
    20  
    21  	var data Data
    22  	if err := fetchLocalImages(&data, client); err != nil {
    23  		return err
    24  	}
    25  
    26  	image := d.Get("image").(string)
    27  	if _, ok := data.DockerImages[image]; !ok {
    28  		if _, ok := data.DockerImages[image+":latest"]; !ok {
    29  			return fmt.Errorf("Unable to find image %s", image)
    30  		}
    31  		image = image + ":latest"
    32  	}
    33  
    34  	// The awesome, wonderful, splendiferous, sensical
    35  	// Docker API now lets you specify a HostConfig in
    36  	// CreateContainerOptions, but in my testing it still only
    37  	// actually applies HostConfig options set in StartContainer.
    38  	// How cool is that?
    39  	createOpts := dc.CreateContainerOptions{
    40  		Name: d.Get("name").(string),
    41  		Config: &dc.Config{
    42  			Image:      image,
    43  			Hostname:   d.Get("hostname").(string),
    44  			Domainname: d.Get("domainname").(string),
    45  		},
    46  	}
    47  
    48  	if v, ok := d.GetOk("env"); ok {
    49  		createOpts.Config.Env = stringSetToStringSlice(v.(*schema.Set))
    50  	}
    51  
    52  	if v, ok := d.GetOk("command"); ok {
    53  		createOpts.Config.Cmd = stringListToStringSlice(v.([]interface{}))
    54  		for _, v := range createOpts.Config.Cmd {
    55  			if v == "" {
    56  				return fmt.Errorf("values for command may not be empty")
    57  			}
    58  		}
    59  	}
    60  
    61  	if v, ok := d.GetOk("entrypoint"); ok {
    62  		createOpts.Config.Entrypoint = stringListToStringSlice(v.([]interface{}))
    63  	}
    64  
    65  	if v, ok := d.GetOk("user"); ok {
    66  		createOpts.Config.User = v.(string)
    67  	}
    68  
    69  	exposedPorts := map[dc.Port]struct{}{}
    70  	portBindings := map[dc.Port][]dc.PortBinding{}
    71  
    72  	if v, ok := d.GetOk("ports"); ok {
    73  		exposedPorts, portBindings = portSetToDockerPorts(v.(*schema.Set))
    74  	}
    75  	if len(exposedPorts) != 0 {
    76  		createOpts.Config.ExposedPorts = exposedPorts
    77  	}
    78  
    79  	extraHosts := []string{}
    80  	if v, ok := d.GetOk("host"); ok {
    81  		extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set))
    82  	}
    83  
    84  	volumes := map[string]struct{}{}
    85  	binds := []string{}
    86  	volumesFrom := []string{}
    87  
    88  	if v, ok := d.GetOk("volumes"); ok {
    89  		volumes, binds, volumesFrom, err = volumeSetToDockerVolumes(v.(*schema.Set))
    90  		if err != nil {
    91  			return fmt.Errorf("Unable to parse volumes: %s", err)
    92  		}
    93  	}
    94  	if len(volumes) != 0 {
    95  		createOpts.Config.Volumes = volumes
    96  	}
    97  
    98  	if v, ok := d.GetOk("labels"); ok {
    99  		createOpts.Config.Labels = mapTypeMapValsToString(v.(map[string]interface{}))
   100  	}
   101  
   102  	hostConfig := &dc.HostConfig{
   103  		Privileged:      d.Get("privileged").(bool),
   104  		PublishAllPorts: d.Get("publish_all_ports").(bool),
   105  		RestartPolicy: dc.RestartPolicy{
   106  			Name:              d.Get("restart").(string),
   107  			MaximumRetryCount: d.Get("max_retry_count").(int),
   108  		},
   109  		LogConfig: dc.LogConfig{
   110  			Type: d.Get("log_driver").(string),
   111  		},
   112  	}
   113  
   114  	if len(portBindings) != 0 {
   115  		hostConfig.PortBindings = portBindings
   116  	}
   117  	if len(extraHosts) != 0 {
   118  		hostConfig.ExtraHosts = extraHosts
   119  	}
   120  	if len(binds) != 0 {
   121  		hostConfig.Binds = binds
   122  	}
   123  	if len(volumesFrom) != 0 {
   124  		hostConfig.VolumesFrom = volumesFrom
   125  	}
   126  
   127  	if v, ok := d.GetOk("dns"); ok {
   128  		hostConfig.DNS = stringSetToStringSlice(v.(*schema.Set))
   129  	}
   130  
   131  	if v, ok := d.GetOk("dns_opts"); ok {
   132  		hostConfig.DNSOptions = stringSetToStringSlice(v.(*schema.Set))
   133  	}
   134  
   135  	if v, ok := d.GetOk("dns_search"); ok {
   136  		hostConfig.DNSSearch = stringSetToStringSlice(v.(*schema.Set))
   137  	}
   138  
   139  	if v, ok := d.GetOk("links"); ok {
   140  		hostConfig.Links = stringSetToStringSlice(v.(*schema.Set))
   141  	}
   142  
   143  	if v, ok := d.GetOk("memory"); ok {
   144  		hostConfig.Memory = int64(v.(int)) * 1024 * 1024
   145  	}
   146  
   147  	if v, ok := d.GetOk("memory_swap"); ok {
   148  		swap := int64(v.(int))
   149  		if swap > 0 {
   150  			swap = swap * 1024 * 1024
   151  		}
   152  		hostConfig.MemorySwap = swap
   153  	}
   154  
   155  	if v, ok := d.GetOk("cpu_shares"); ok {
   156  		hostConfig.CPUShares = int64(v.(int))
   157  	}
   158  
   159  	if v, ok := d.GetOk("log_opts"); ok {
   160  		hostConfig.LogConfig.Config = mapTypeMapValsToString(v.(map[string]interface{}))
   161  	}
   162  
   163  	if v, ok := d.GetOk("network_mode"); ok {
   164  		hostConfig.NetworkMode = v.(string)
   165  	}
   166  
   167  	createOpts.HostConfig = hostConfig
   168  
   169  	var retContainer *dc.Container
   170  	if retContainer, err = client.CreateContainer(createOpts); err != nil {
   171  		return fmt.Errorf("Unable to create container: %s", err)
   172  	}
   173  	if retContainer == nil {
   174  		return fmt.Errorf("Returned container is nil")
   175  	}
   176  
   177  	d.SetId(retContainer.ID)
   178  
   179  	if v, ok := d.GetOk("networks"); ok {
   180  		connectionOpts := dc.NetworkConnectionOptions{Container: retContainer.ID}
   181  
   182  		for _, rawNetwork := range v.(*schema.Set).List() {
   183  			network := rawNetwork.(string)
   184  			if err := client.ConnectNetwork(network, connectionOpts); err != nil {
   185  				return fmt.Errorf("Unable to connect to network '%s': %s", network, err)
   186  			}
   187  		}
   188  	}
   189  
   190  	creationTime = time.Now()
   191  	if err := client.StartContainer(retContainer.ID, nil); err != nil {
   192  		return fmt.Errorf("Unable to start container: %s", err)
   193  	}
   194  
   195  	return resourceDockerContainerRead(d, meta)
   196  }
   197  
   198  func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error {
   199  	client := meta.(*dc.Client)
   200  
   201  	apiContainer, err := fetchDockerContainer(d.Id(), client)
   202  	if err != nil {
   203  		return err
   204  	}
   205  	if apiContainer == nil {
   206  		// This container doesn't exist anymore
   207  		d.SetId("")
   208  		return nil
   209  	}
   210  
   211  	var container *dc.Container
   212  
   213  	loops := 1 // if it hasn't just been created, don't delay
   214  	if !creationTime.IsZero() {
   215  		loops = 30 // with 500ms spacing, 15 seconds; ought to be plenty
   216  	}
   217  	sleepTime := 500 * time.Millisecond
   218  
   219  	for i := loops; i > 0; i-- {
   220  		container, err = client.InspectContainer(apiContainer.ID)
   221  		if err != nil {
   222  			return fmt.Errorf("Error inspecting container %s: %s", apiContainer.ID, err)
   223  		}
   224  
   225  		if container.State.Running ||
   226  			!container.State.Running && !d.Get("must_run").(bool) {
   227  			break
   228  		}
   229  
   230  		if creationTime.IsZero() { // We didn't just create it, so don't wait around
   231  			return resourceDockerContainerDelete(d, meta)
   232  		}
   233  
   234  		if container.State.FinishedAt.After(creationTime) {
   235  			// It exited immediately, so error out so dependent containers
   236  			// aren't started
   237  			resourceDockerContainerDelete(d, meta)
   238  			return fmt.Errorf("Container %s exited after creation, error was: %s", apiContainer.ID, container.State.Error)
   239  		}
   240  
   241  		time.Sleep(sleepTime)
   242  	}
   243  
   244  	// Handle the case of the for loop above running its course
   245  	if !container.State.Running && d.Get("must_run").(bool) {
   246  		resourceDockerContainerDelete(d, meta)
   247  		return fmt.Errorf("Container %s failed to be in running state", apiContainer.ID)
   248  	}
   249  
   250  	// Read Network Settings
   251  	if container.NetworkSettings != nil {
   252  		d.Set("ip_address", container.NetworkSettings.IPAddress)
   253  		d.Set("ip_prefix_length", container.NetworkSettings.IPPrefixLen)
   254  		d.Set("gateway", container.NetworkSettings.Gateway)
   255  		d.Set("bridge", container.NetworkSettings.Bridge)
   256  	}
   257  
   258  	return nil
   259  }
   260  
   261  func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) error {
   262  	return nil
   263  }
   264  
   265  func resourceDockerContainerDelete(d *schema.ResourceData, meta interface{}) error {
   266  	client := meta.(*dc.Client)
   267  
   268  	removeOpts := dc.RemoveContainerOptions{
   269  		ID:            d.Id(),
   270  		RemoveVolumes: true,
   271  		Force:         true,
   272  	}
   273  
   274  	if err := client.RemoveContainer(removeOpts); err != nil {
   275  		return fmt.Errorf("Error deleting container %s: %s", d.Id(), err)
   276  	}
   277  
   278  	d.SetId("")
   279  	return nil
   280  }
   281  
   282  func stringListToStringSlice(stringList []interface{}) []string {
   283  	ret := []string{}
   284  	for _, v := range stringList {
   285  		if v == nil {
   286  			ret = append(ret, "")
   287  			continue
   288  		}
   289  		ret = append(ret, v.(string))
   290  	}
   291  	return ret
   292  }
   293  
   294  func stringSetToStringSlice(stringSet *schema.Set) []string {
   295  	ret := []string{}
   296  	if stringSet == nil {
   297  		return ret
   298  	}
   299  	for _, envVal := range stringSet.List() {
   300  		ret = append(ret, envVal.(string))
   301  	}
   302  	return ret
   303  }
   304  
   305  func mapTypeMapValsToString(typeMap map[string]interface{}) map[string]string {
   306  	mapped := make(map[string]string, len(typeMap))
   307  	for k, v := range typeMap {
   308  		mapped[k] = v.(string)
   309  	}
   310  	return mapped
   311  }
   312  
   313  func fetchDockerContainer(ID string, client *dc.Client) (*dc.APIContainers, error) {
   314  	apiContainers, err := client.ListContainers(dc.ListContainersOptions{All: true})
   315  
   316  	if err != nil {
   317  		return nil, fmt.Errorf("Error fetching container information from Docker: %s\n", err)
   318  	}
   319  
   320  	for _, apiContainer := range apiContainers {
   321  		if apiContainer.ID == ID {
   322  			return &apiContainer, nil
   323  		}
   324  	}
   325  
   326  	return nil, nil
   327  }
   328  
   329  func portSetToDockerPorts(ports *schema.Set) (map[dc.Port]struct{}, map[dc.Port][]dc.PortBinding) {
   330  	retExposedPorts := map[dc.Port]struct{}{}
   331  	retPortBindings := map[dc.Port][]dc.PortBinding{}
   332  
   333  	for _, portInt := range ports.List() {
   334  		port := portInt.(map[string]interface{})
   335  		internal := port["internal"].(int)
   336  		protocol := port["protocol"].(string)
   337  
   338  		exposedPort := dc.Port(strconv.Itoa(internal) + "/" + protocol)
   339  		retExposedPorts[exposedPort] = struct{}{}
   340  
   341  		external, extOk := port["external"].(int)
   342  		ip, ipOk := port["ip"].(string)
   343  
   344  		if extOk {
   345  			portBinding := dc.PortBinding{
   346  				HostPort: strconv.Itoa(external),
   347  			}
   348  			if ipOk {
   349  				portBinding.HostIP = ip
   350  			}
   351  			retPortBindings[exposedPort] = append(retPortBindings[exposedPort], portBinding)
   352  		}
   353  	}
   354  
   355  	return retExposedPorts, retPortBindings
   356  }
   357  
   358  func extraHostsSetToDockerExtraHosts(extraHosts *schema.Set) []string {
   359  	retExtraHosts := []string{}
   360  
   361  	for _, hostInt := range extraHosts.List() {
   362  		host := hostInt.(map[string]interface{})
   363  		ip := host["ip"].(string)
   364  		hostname := host["host"].(string)
   365  		retExtraHosts = append(retExtraHosts, hostname+":"+ip)
   366  	}
   367  
   368  	return retExtraHosts
   369  }
   370  
   371  func volumeSetToDockerVolumes(volumes *schema.Set) (map[string]struct{}, []string, []string, error) {
   372  	retVolumeMap := map[string]struct{}{}
   373  	retHostConfigBinds := []string{}
   374  	retVolumeFromContainers := []string{}
   375  
   376  	for _, volumeInt := range volumes.List() {
   377  		volume := volumeInt.(map[string]interface{})
   378  		fromContainer := volume["from_container"].(string)
   379  		containerPath := volume["container_path"].(string)
   380  		volumeName := volume["volume_name"].(string)
   381  		if len(volumeName) == 0 {
   382  			volumeName = volume["host_path"].(string)
   383  		}
   384  		readOnly := volume["read_only"].(bool)
   385  
   386  		switch {
   387  		case len(fromContainer) == 0 && len(containerPath) == 0:
   388  			return retVolumeMap, retHostConfigBinds, retVolumeFromContainers, errors.New("Volume entry without container path or source container")
   389  		case len(fromContainer) != 0 && len(containerPath) != 0:
   390  			return retVolumeMap, retHostConfigBinds, retVolumeFromContainers, errors.New("Both a container and a path specified in a volume entry")
   391  		case len(fromContainer) != 0:
   392  			retVolumeFromContainers = append(retVolumeFromContainers, fromContainer)
   393  		case len(volumeName) != 0:
   394  			readWrite := "rw"
   395  			if readOnly {
   396  				readWrite = "ro"
   397  			}
   398  			retVolumeMap[containerPath] = struct{}{}
   399  			retHostConfigBinds = append(retHostConfigBinds, volumeName+":"+containerPath+":"+readWrite)
   400  		default:
   401  			retVolumeMap[containerPath] = struct{}{}
   402  		}
   403  	}
   404  
   405  	return retVolumeMap, retHostConfigBinds, retVolumeFromContainers, nil
   406  }