@@ -75,13 +75,18 @@ def select_app_name(self):
7575
7676 return name
7777
78- def save (self , ** kwargs ):
78+ def save (self , * args , * *kwargs ):
7979 if not self .id :
8080 self .id = generate_app_name ()
8181 while App .objects .filter (id = self .id ).exists ():
8282 self .id = generate_app_name ()
8383
84- return super (App , self ).save (** kwargs )
84+ application = super (App , self ).save (** kwargs )
85+
86+ # create all the required resources
87+ self .create (* args , ** kwargs )
88+
89+ return application
8590
8691 def __str__ (self ):
8792 return self .id
@@ -119,15 +124,31 @@ def log(self, message, level=logging.INFO):
119124 logger .log (level , "[{}]: {}" .format (self .id , message ))
120125
121126 def create (self , * args , ** kwargs ):
122- """Create a new application with an initial config and release"""
123- config = Config .objects .create (owner = self .owner , app = self )
124- Release .objects .create (version = 1 , owner = self .owner , app = self , config = config , build = None )
127+ """
128+ Create a application with an initial config, release, domain
129+ and k8s resource if needed
130+ """
131+ try :
132+ cfg = self .config_set .latest ()
133+ except Config .DoesNotExist :
134+ cfg = Config .objects .create (owner = self .owner , app = self )
135+
136+ # Only create if no release can be found
137+ try :
138+ rel = self .release_set .latest ()
139+ except Release .DoesNotExist :
140+ rel = Release .objects .create (
141+ version = 1 , owner = self .owner , app = self ,
142+ config = cfg , build = None
143+ )
125144
126145 # create required minimum resources in k8s for the application
127146 self ._scheduler .create (self .id )
128147
129148 # Attach the platform specific application sub domain to the k8s service
130- Domain (owner = self .owner , app = self , domain = self .id ).save ()
149+ # Only attach it on first release in case a customer has remove the app domain
150+ if rel .version == 1 and not Domain .objects .filter (domain = self .id ).exists ():
151+ Domain (owner = self .owner , app = self , domain = self .id ).save ()
131152
132153 def delete (self , * args , ** kwargs ):
133154 """Delete this application including all containers"""
@@ -231,6 +252,9 @@ def _clean_app_logs(self):
231252
232253 def scale (self , user , structure ): # noqa
233254 """Scale containers up or down to match requested structure."""
255+ # use create to make sure minimum resources are created
256+ self .create ()
257+
234258 if self .release_set .latest ().build is None :
235259 raise EnvironmentError ('No build associated with this release' )
236260
@@ -392,6 +416,9 @@ def _destroy_containers(self, to_destroy):
392416
393417 def deploy (self , user , release ):
394418 """Deploy a new release to this application"""
419+ # use create to make sure minimum resources are created
420+ self .create ()
421+
395422 existing = self .container_set .exclude (type = 'run' )
396423 new = []
397424 scale_types = set ()
0 commit comments