3636}
3737"""
3838
39- RC_TEMPLATE = """\
39+ RCD_TEMPLATE = """\
4040 {
4141 "kind": "ReplicationController",
4242 "apiVersion": "$version",
6868 "containers": [
6969 {
7070 "name": "$containername",
71- "image": "$image"
71+ "image": "$image",
72+ "env": [
73+ {
74+ "name":"DEIS_APP",
75+ "value":"$id"
76+ },
77+ {
78+ "name":"DEIS_RELEASE",
79+ "value":"$appversion"
80+ }
81+ ]
82+ }
83+ ]
84+ }
85+ }
86+ }
87+ }
88+ """
89+
90+ RCB_TEMPLATE = """\
91+ {
92+ "kind": "ReplicationController",
93+ "apiVersion": "$version",
94+ "metadata": {
95+ "name": "$name",
96+ "labels": {
97+ "app": "$id",
98+ "heritage": "deis"
99+ }
100+ },
101+ "spec": {
102+ "replicas": $num,
103+ "selector": {
104+ "app": "$id",
105+ "version": "$appversion",
106+ "type": "$type",
107+ "heritage": "deis"
108+ },
109+ "template": {
110+ "metadata": {
111+ "labels": {
112+ "app": "$id",
113+ "version": "$appversion",
114+ "type": "$type",
115+ "heritage": "deis"
116+ }
117+ },
118+ "spec": {
119+ "containers": [
120+ {
121+ "name": "$containername",
122+ "image": "quay.io/deisci/slugrunner:v2-alpha",
123+ "env": [
124+ {
125+ "name":"PORT",
126+ "value":"5000"
127+ },
128+ {
129+ "name":"SLUG_URL",
130+ "value":"$image"
131+ },
132+ {
133+ "name":"DEBUG",
134+ "value":"1"
135+ },
136+ {
137+ "name":"DEIS_APP",
138+ "value":"$id"
139+ },
140+ {
141+ "name":"DEIS_RELEASE",
142+ "value":"$appversion"
143+ }
144+ ]
72145 }
73146 ]
74147 }
@@ -156,7 +229,7 @@ def _get_old_rc(self, name, app_type):
156229 exists = False
157230 prev_rc = []
158231 for rc in resp .json ()['items' ]:
159- if ('name ' in rc ['metadata ' ]['labels ' ] and name == rc ['metadata' ]['labels' ]['name ' ] and
232+ if ('app ' in rc ['spec ' ]['selector ' ] and name == rc ['metadata' ]['labels' ]['app ' ] and
160233 'type' in rc ['spec' ]['selector' ] and app_type == rc ['spec' ]['selector' ]['type' ]):
161234 exists = True
162235 prev_rc = rc
@@ -185,6 +258,7 @@ def deploy(self, name, image, command, **kwargs):
185258 app_type = name .split ("." )[1 ]
186259 old_rc = self ._get_old_rc (app_name , app_type )
187260 new_rc = self ._create_rc (name , image , command , ** kwargs )
261+ old_temp_rc = old_rc
188262 if old_rc :
189263 desired = int (old_rc ["spec" ]["replicas" ])
190264 old_rc_name = old_rc ["metadata" ]["name" ]
@@ -196,8 +270,8 @@ def deploy(self, name, image, command, **kwargs):
196270 count = 1
197271 while desired >= count :
198272 new_rc = self ._scale_app (new_rc_name , count , app_name )
199- if old_rc :
200- old_rc = self ._scale_app (old_rc_name , desired - count , app_name )
273+ if old_temp_rc :
274+ old_temp_rc = self ._scale_app (old_rc_name , desired - count , app_name )
201275 count += 1
202276 except Exception as e :
203277 self ._scale_app (new_rc ["metadata" ]["name" ], 0 , app_name )
@@ -207,7 +281,7 @@ def deploy(self, name, image, command, **kwargs):
207281
208282 raise RuntimeError ('{} (deploy): {}' .format (name , e ))
209283 if old_rc :
210- self ._delete_rc (old_rc_name , app_name )
284+ self ._delete_rc (app_name , old_rc_name )
211285
212286 def _get_events (self , namespace ):
213287 url = self ._api ("/namespaces/{}/events" , namespace )
@@ -305,6 +379,25 @@ def scale(self, name, image, command, **kwargs):
305379 self ._scale_app (name , old_replicas , app_name )
306380 raise RuntimeError ('{} (Scale): {}' .format (name , e ))
307381
382+ def _create_namespace (self , app_name ):
383+ url = self ._api ("/namespaces" )
384+ data = {
385+ "kind" : "Namespace" ,
386+ "apiVersion" : self .apiversion ,
387+ "metadata" : {
388+ "name" : app_name
389+ }
390+ }
391+ resp = self .session .post (url , json = data )
392+ if not resp .status_code == 201 :
393+ error (resp , "create Namespace {}" .format (app_name ))
394+
395+ def _check_status (self , resp , app_name ):
396+ if resp .status_code == 404 :
397+ self ._create_namespace (app_name )
398+ elif resp .status_code != 200 :
399+ error (resp , "locate Namespace {}" .format (app_name ))
400+
308401 def _create_rc (self , name , image , command , ** kwargs ):
309402 container_fullname = name
310403 app_name = kwargs .get ('aname' , {})
@@ -316,42 +409,36 @@ def _create_rc(self, name, image, command, **kwargs):
316409 # First ensure that the namespace was created
317410 url = self ._api ("/namespaces/{}" , app_name )
318411 resp = self .session .get (url )
319- if resp .status_code == 404 :
320- url = self ._api ("/namespaces" )
321- data = {
322- "kind" : "Namespace" ,
323- "apiVersion" : self .apiversion ,
324- "metadata" : {
325- "name" : app_name
326- }
327- }
328-
329- resp = self .session .post (url , json = data )
330- if not resp .status_code == 201 :
331- error (resp , "create Namespace {}" .format (app_name ))
332- elif resp .status_code != 200 :
333- error (resp , "locate Namespace {}" .format (app_name ))
334-
412+ self ._check_status (resp , app_name )
335413 num = kwargs .get ('num' , {})
414+ imgurl = self .registry + "/" + image
415+ TEMPLATE = RCD_TEMPLATE
416+ shalen = len (image [image .index (":" )+ 5 :])
417+ if image [image .index (":" )+ 1 :image .index (":" )+ 4 ] == "git" and shalen == 8 :
418+ imgurl = "http://" + settings .S3EP + "/git/home/" + image + "/slug"
419+ TEMPLATE = RCB_TEMPLATE
336420 l = {
337421 "name" : name ,
338422 "id" : app_name ,
339423 "appversion" : kwargs .get ("version" , {}),
340424 "version" : self .apiversion ,
341- "image" : self . registry + "/" + image ,
425+ "image" : imgurl ,
342426 "num" : kwargs .get ("num" , {}),
343427 "containername" : container_name ,
344428 "type" : app_type ,
345429 }
346-
347- template = string .Template (RC_TEMPLATE ).substitute (l )
430+ template = string .Template (TEMPLATE ).substitute (l )
348431 js_template = json .loads (template )
349432 containers = js_template ["spec" ]["template" ]["spec" ]["containers" ]
350433 containers [0 ]['args' ] = args
351434 loc = locals ().copy ()
352435 loc .update (re .match (MATCH , container_fullname ).groupdict ())
353436 mem = kwargs .get ('memory' , {}).get (loc ['c_type' ])
354437 cpu = kwargs .get ('cpu' , {}).get (loc ['c_type' ])
438+ env = kwargs .get ('envs' , {})
439+ if env :
440+ for k , v in env .iteritems ():
441+ containers [0 ]["env" ].append ({"name" : k , "value" : v })
355442 if mem or cpu :
356443 containers [0 ]["resources" ] = {"limits" : {}}
357444
@@ -365,13 +452,11 @@ def _create_rc(self, name, image, command, **kwargs):
365452 if cpu :
366453 cpu = float (cpu )/ 1024
367454 containers [0 ]["resources" ]["limits" ]["cpu" ] = cpu
368-
369455 url = self ._api ("/namespaces/{}/replicationcontrollers" , app_name )
370456 resp = self .session .post (url , json = js_template )
371457 if unhealthy (resp .status_code ):
372458 error (resp , 'create ReplicationController "{}" in Namespace "{}"' ,
373459 name , app_name )
374-
375460 create = False
376461 for _ in xrange (30 ):
377462 if not create and self ._get_rc_status (name , app_name ) == 404 :
@@ -483,7 +568,7 @@ def stop(self, name):
483568 """Stop a container."""
484569 pass
485570
486- def _delete_rc (self , name , namespace ):
571+ def _delete_rc (self , namespace , name ):
487572 url = self ._api ("/namespaces/{}/replicationcontrollers/{}" , namespace , name )
488573 resp = self .session .delete (url )
489574 if unhealthy (resp .status_code ):
0 commit comments