1010import importlib
1111import json
1212
13+ from celery .canvas import group
1314from django .conf import settings
1415from django .db import models
1516from django .db .models .signals import post_save
2021
2122from api import fields
2223from celerytasks import controller
23- from celery .canvas import group
2424
2525
2626# define custom signals
2727scale_signal = Signal (providing_args = ['formation' , 'user' ])
2828release_signal = Signal (providing_args = ['formation' , 'user' ])
2929
30+
3031def import_tasks (provider_type ):
31- "Return Celery tasks for a given provider type"
32+ """ Return Celery tasks for a given provider type"" "
3233 try :
3334 tasks = importlib .import_module ('celerytasks.' + provider_type )
3435 except ImportError as e :
@@ -195,17 +196,19 @@ def publish(self, **kwargs):
195196 # TODO: add sharing-based key lookup, for now just owner's keys
196197 for formation in formations :
197198 keys = databag ['formations' ][formation .id ] = []
198- owner_keys = [ '{0}_{1}' .format (k .owner .username , k .id ) for k in formation .owner .key_set .all () ]
199+ owner_keys = ['{0}_{1}' .format (k .owner .username , k .id ) for k in formation .owner .key_set .all ()]
199200 keys .extend (owner_keys )
200201 # call a celery task to update gitosis
201202 if settings .CHEF_ENABLED :
202203 controller .update_gitosis .delay (databag ).wait () # @UndefinedVariable
203204
204205 def next_container_node (self , formation , container_type = 'web' ):
205206 count = []
206- backend_nodes = list (Node .objects .filter (formation = formation , type = 'backend' ).order_by ('created' ))
207- container_map = { n : [] for n in backend_nodes }
208- containers = list (Container .objects .filter (formation = formation , type = container_type ).order_by ('created' ))
207+ backend_nodes = list (Node .objects .filter (
208+ formation = formation , type = 'backend' ).order_by ('created' ))
209+ container_map = {n : [] for n in backend_nodes }
210+ containers = list (Container .objects .filter (
211+ formation = formation , type = container_type ).order_by ('created' ))
209212 for c in containers :
210213 container_map [c .node ].append (c )
211214 for n in container_map .keys ():
@@ -287,7 +290,7 @@ def _scale_backends(self, requested, **kwargs):
287290 funcs .append (b .node .launch )
288291 diff = requested - len (backends )
289292 # http://docs.celeryproject.org/en/latest/userguide/canvas.html#groups
290- job = [func () for func in funcs ]
293+ job = [func () for func in funcs ]
291294 return job
292295
293296 def _scale_proxies (self , requested , ** kwargs ):
@@ -307,7 +310,7 @@ def _scale_proxies(self, requested, **kwargs):
307310 funcs .append (p .node .launch )
308311 diff = requested - len (proxies )
309312 # http://docs.celeryproject.org/en/latest/userguide/canvas.html#groups
310- job = [ func () for func in funcs ]
313+ job = [func () for func in funcs ]
311314 return job
312315
313316 def _scale_containers (self , requested_counts , ** kwargs ):
@@ -341,7 +344,7 @@ def _scale_containers(self, requested_counts, **kwargs):
341344 containers .append (c )
342345 container_num += 1
343346 diff = requested - len (containers )
344- change += 1
347+ change += 1
345348 return change
346349
347350 def balance (self , ** kwargs ):
@@ -354,8 +357,9 @@ def balance(self, **kwargs):
354357 def _balance_containers (self , ** kwargs ):
355358 backends = self .backend_set .all ().order_by ('created' )
356359 if len (backends ) < 2 :
357- return # there's nothing to balance with 1 backend
358- all_containers = Container .objects .filter (formation = self ).order_by ('-created' )
360+ return # there's nothing to balance with 1 backend
361+ all_containers = Container .objects .filter (
362+ formation = self ).order_by ('-created' )
359363 # get the next container number (e.g. web.19)
360364 container_num = 1 if not all_containers else all_containers [0 ].num + 1
361365 changed = False
@@ -384,7 +388,7 @@ def _balance_containers(self, **kwargs):
384388 type = container_type ,
385389 num = container_num ,
386390 node = b_under .node )
387- container_num += 1
391+ container_num += 1
388392 # delete the oldest container from the most over-utilized node
389393 c = b_over .node .container_set .filter (type = container_type ).order_by ('created' )[0 ]
390394 c .delete ()
@@ -419,7 +423,7 @@ def calculate(self):
419423 d ['release' ]['build' ]['procfile' ] = release .build .procfile
420424 # calculate proxy
421425 d ['proxy' ] = {}
422- d ['proxy' ]['algorithm' ] = 'round_robin'
426+ d ['proxy' ]['algorithm' ] = 'round_robin'
423427 d ['proxy' ]['port' ] = 80
424428 d ['proxy' ]['backends' ] = []
425429 # calculate container formation
@@ -428,7 +432,8 @@ def calculate(self):
428432 # all container types get an exposed port starting at 5001
429433 port = 5000 + c .num
430434 d ['containers' ].setdefault (c .type , {})
431- d ['containers' ][c .type ].update ({ c .num : "{0}:{1}" .format (c .node .id , port ) })
435+ d ['containers' ][c .type ].update (
436+ {c .num : "{0}:{1}" .format (c .node .id , port )})
432437 # only proxy to 'web' containers
433438 if c .type == 'web' :
434439 d ['proxy' ]['backends' ].append ("{0}:{1}" .format (c .node .fqdn , port ))
@@ -467,9 +472,10 @@ def destroy(self):
467472 subtasks .extend ([b .node .terminate () for b in self .backend_set .all ()])
468473 subtasks .extend ([p .node .terminate () for p in self .proxy_set .all ()])
469474 job = group (* subtasks )
470- job .apply_async ().join () # block for termination
475+ job .apply_async ().join () # block for termination
471476 # purge other hosting provider infrastructure
472- tasks .cleanup_formation .delay (self .id ,
477+ tasks .cleanup_formation .delay (
478+ self .id ,
473479 self .flavor .provider .creds .copy (),
474480 self .flavor .params .copy ()).wait ()
475481
@@ -545,19 +551,21 @@ def _prepare_launch_args(self):
545551 # append to the default run_list
546552 run_list = chef .setdefault ('run_list' , [])
547553 attrs = chef ['initial_attributes' ] = {}
554+ formation_id = self .formation .id
548555 if self .type == 'backend' :
549556 run_list .append ('role[deis-backend]' )
550557 attrs .setdefault ('deis' , {}).setdefault (
551- 'runtime' , {}).setdefault ('formations' , [ self . formation . id ])
558+ 'runtime' , {}).setdefault ('formations' , [formation_id ])
552559 elif self .type == 'proxy' :
553560 run_list .append ('role[deis-proxy]' )
554561 attrs .setdefault ('deis' , {}).setdefault (
555- 'proxy' , {}).setdefault ('formations' , [ self . formation . id ])
562+ 'proxy' , {}).setdefault ('formations' , [formation_id ])
556563 # add the formation's ssh pubkey
557564 init .setdefault ('ssh_authorized_keys' , []).append (
558- self .formation .ssh_public_key )
565+ self .formation .ssh_public_key )
559566 # add all of the owner's SSH keys
560- init ['ssh_authorized_keys' ].extend ([k .public for k in self .formation .owner .key_set .all () ])
567+ init ['ssh_authorized_keys' ].extend (
568+ [k .public for k in self .formation .owner .key_set .all ()])
561569 ssh_username = self .formation .ssh_username
562570 ssh_private_key = self .formation .ssh_private_key
563571 args = (self .uuid , creds , params , init , ssh_username , ssh_private_key )
@@ -722,7 +730,8 @@ class Build(UuidAuditedModel):
722730 dockerfile = models .TextField (blank = True )
723731 config = fields .EnvVarsField (blank = True )
724732 # slug info, TODO: replace default URL with something more user friendly
725- url = models .URLField ('URL' , default = 'https://s3.amazonaws.com/gabrtv-slugs/nodejs.tar.gz' )
733+ url = models .URLField (
734+ 'URL' , default = 'https://s3.amazonaws.com/gabrtv-slugs/nodejs.tar.gz' )
726735 size = models .IntegerField (blank = True , null = True )
727736 checksum = models .CharField (max_length = 255 , blank = True )
728737
@@ -768,7 +777,7 @@ def rollback(self):
768777def new_release (sender , ** kwargs ):
769778 formation , user = kwargs ['formation' ], kwargs ['user' ]
770779 last_release = Release .objects .filter (
771- formation = formation ).order_by ('-created' )[0 ]
780+ formation = formation ).order_by ('-created' )[0 ]
772781 image = kwargs .get ('image' , last_release .image )
773782 config = kwargs .get ('config' , last_release .config )
774783 build = kwargs .get ('build' , last_release .build )
@@ -787,9 +796,9 @@ def new_release(sender, **kwargs):
787796 owner = user , formation = formation , values = new_values )
788797 # create new release and auto-increment version
789798 new_version = last_release .version + 1
790- release = Release .objects .create (owner = user , formation = formation ,
791- image = image , config = config , build = build , args = args , command = command ,
792- version = new_version )
799+ release = Release .objects .create (
800+ owner = user , formation = formation , image = image , config = config ,
801+ build = build , args = args , command = command , version = new_version )
793802 return release
794803
795804
@@ -847,4 +856,3 @@ def create_auth_token(sender, instance=None, created=False, **kwargs):
847856 if created :
848857 # pylint: disable=E1101
849858 Token .objects .create (user = instance )
850-
0 commit comments