add rebuild API endpoint.

Change-Id: I46b1b3eabec2dcac30a8b5f16e2e3895d7792fed
diff --git a/xos/api/utility/onboarding.py b/xos/api/utility/onboarding.py
index dd66d6d..345767a 100644
--- a/xos/api/utility/onboarding.py
+++ b/xos/api/utility/onboarding.py
@@ -20,6 +20,7 @@
         patterns = [] #super(CordSubscriberViewSet, self).get_urlpatterns(api_path=api_path)
 
         patterns.append( self.list_url("xos/ready/$", {"get": "get_xos_ready"}, "xos_ready") )
+        patterns.append( self.list_url("xos/rebuild/$", {"post": "post_rebuild"}, "xos_rebuild") )
 
         patterns.append( self.list_url("summary/$", {"get": "get_summary"}, "summary") )
 
@@ -42,6 +43,17 @@
         result = (xos.enacted is not None) and (xos.updated is not None) and (xos.enacted>=xos.updated) and (xos.backend_status.startswith("1"))
         return HttpResponse( json.dumps(result), content_type="application/javascript" )
 
+    def post_rebuild(self, request):
+        xos = XOS.objects.all()
+        if not xos:
+            raise Exception("There is no XOS object")
+
+        xos=xos[0]
+
+        xos.rebuild()
+
+        return Response(True)
+
     def get_summary(self, request):
         result = []
 
diff --git a/xos/core/models/xosmodel.py b/xos/core/models/xosmodel.py
index f17e3e9..c13115e 100644
--- a/xos/core/models/xosmodel.py
+++ b/xos/core/models/xosmodel.py
@@ -1,5 +1,5 @@
 import os
-from django.db import models
+from django.db import models, transaction
 from core.models import PlCoreBase
 from core.models.plcorebase import StrippedCharField
 
@@ -30,11 +30,12 @@
 #        return user.can_update_site(self.site, allow=['tech'])
 
     def rebuild(self):
-        for service_controller in self.service_controllers.all():
-            for scr in service_controller.service_controller_resources.all():
-               scr.save()
-            service_controller.save()
-        self.save()
+        with transaction.atomic():
+            for service_controller in self.service_controllers.all():
+                for scr in service_controller.service_controller_resources.all():
+                   scr.save()
+                service_controller.save()
+            self.save()
 
 class XOSVolume(PlCoreBase):
     xos = models.ForeignKey(XOS, related_name='volumes', help_text="The XOS object for this Volume")