Repository: deis/deis

Issue:
Scaling up from layers=0 leads to error
I created an app, scaled runtime and proxy layers to 1, scaled them both down to 0, scaled them back up to 1 and got this error:

``` python
[20/Aug/2013 11:15:58] "POST /api/formations/fabled-greenery/scale/layers HTTP/1.0" 200 294
ERROR Internal Server Error: /api/formations/fabled-greenery/scale/layers
Traceback (most recent call last):
  File "/opt/deis/controller/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/opt/deis/controller/venv/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 78, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/deis/controller/venv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/deis/controller/venv/local/lib/python2.7/site-packages/rest_framework/views.py", line 327, in dispatch
    response = self.handle_exception(exc)
  File "/opt/deis/controller/venv/local/lib/python2.7/site-packages/rest_framework/views.py", line 324, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/deis/controller/api/views.py", line 184, in scale_layers
    databag = formation.scale_layers()
  File "/opt/deis/controller/api/models.py", line 291, in scale_layers
    if new_nodes or containers_balanced:
UnboundLocalError: local variable 'new_nodes' referenced before assignment
```



Discussion:

Resolving patch:
diff --git a/api/models.py b/api/models.py
--- a/api/models.py
+++ b/api/models.py
@@ -261,13 +261,13 @@ def scale_layers(self, **kwargs):
         """Scale layers up or down to match requested."""
         layers = self.layers.copy()
         funcs = []
+        new_nodes = False
         for layer_id, requested in layers.items():
             layer = self.layer_set.get(id=layer_id)
             nodes = list(layer.node_set.all().order_by('created'))
             diff = requested - len(nodes)
             if diff == 0:
                 continue
-            new_nodes = False
             while diff < 0:
                 node = nodes.pop(0)
                 funcs.append(node.terminate)
diff --git a/api/views.py b/api/views.py
--- a/api/views.py
+++ b/api/views.py
@@ -304,7 +304,6 @@ def get_object(self, *args, **kwargs):
     def destroy(self, request, **kwargs):
         node = self.get_object()
         node.destroy()
-        node.delete()
         return Response(status=status.HTTP_204_NO_CONTENT)
 
 
diff --git a/celerytasks/ec2.py b/celerytasks/ec2.py
--- a/celerytasks/ec2.py
+++ b/celerytasks/ec2.py
@@ -136,13 +136,10 @@ def terminate_node(node_id, creds, params, provider_id):
             i.update()
             if i.state == "terminated":
                 break
-    # pull the node from the database
+    # delete the node from the database
     node = Node.objects.get(uuid=node_id)
     chef_id = node.id
-    node.provider_id = None
-    node.fqdn = None
-    node.metadata = {}
-    node.save()
+    node.delete()
     # purge the node & client records from chef server
     client = ChefAPI(settings.CHEF_SERVER_URL,
                      settings.CHEF_CLIENT_NAME,


Test patch: