77import socket
88import re
99import time
10+ from django .conf import settings
1011
1112
1213MATCH = re .compile (
@@ -112,6 +113,8 @@ def _create_container(self, name, image, command, unit, **kwargs):
112113 l .update ({'cpu' : '-c {}' .format (cpu )})
113114 else :
114115 l .update ({'cpu' : '' })
116+ # set unit hostname
117+ l .update ({'hostname' : self ._get_hostname (name )})
115118 # should a special entrypoint be used
116119 entrypoint = kwargs .get ('entrypoint' )
117120 if entrypoint :
@@ -137,6 +140,19 @@ def _create_container(self, name, image, command, unit, **kwargs):
137140 if attempt == (RETRIES - 1 ): # account for 0 indexing
138141 raise
139142
143+ def _get_hostname (self , application_name ):
144+ hostname = settings .UNIT_HOSTNAME
145+ if hostname == "default" :
146+ return ''
147+ elif hostname == "application" :
148+ # replace underscore with dots, since underscore is not valid in DNS hostnames
149+ dns_name = application_name .replace ("_" , "." )
150+ return '-h ' + dns_name
151+ elif hostname == "server" :
152+ return '-h %H'
153+ else :
154+ raise RuntimeError ('Unsupported hostname: ' + hostname )
155+
140156 def start (self , name ):
141157 """Start a container"""
142158 self ._wait_for_container (name )
@@ -294,7 +310,7 @@ def attach(self, name):
294310 {"section" : "Unit" , "name" : "Description" , "value" : "{name}" },
295311 {"section" : "Service" , "name" : "ExecStartPre" , "value" : '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE"''' }, # noqa
296312 {"section" : "Service" , "name" : "ExecStartPre" , "value" : '''/bin/sh -c "docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true"''' }, # noqa
297- {"section" : "Service" , "name" : "ExecStart" , "value" : '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} -P -e PORT=$port $IMAGE {command}"''' }, # noqa
313+ {"section" : "Service" , "name" : "ExecStart" , "value" : '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} {hostname} -P -e PORT=$port $IMAGE {command}"''' }, # noqa
298314 {"section" : "Service" , "name" : "ExecStop" , "value" : '''/usr/bin/docker rm -f {name}''' },
299315 {"section" : "Service" , "name" : "TimeoutStartSec" , "value" : "20m" },
300316 {"section" : "Service" , "name" : "RestartSec" , "value" : "5" },
0 commit comments