33from django .db import models
44from django .contrib .auth import get_user_model
55from api .exceptions import DryccException , Conflict
6+ from api .tasks import run_pipeline
67from .base import UuidAuditedModel
78
89User = get_user_model ()
@@ -22,6 +23,7 @@ class Build(UuidAuditedModel):
2223 # optional fields populated by builder
2324 sha = models .CharField (max_length = 40 , blank = True )
2425 procfile = models .JSONField (default = dict , blank = True )
26+ dryccfile = models .JSONField (default = dict , blank = True )
2527 dockerfile = models .TextField (blank = True )
2628
2729 class Meta :
@@ -40,6 +42,12 @@ def type(self):
4042 # container image (or any sort of image) used via drycc pull
4143 return 'image'
4244
45+ @property
46+ def procfile_types (self ):
47+ if self .dryccfile :
48+ return list (self .dryccfile ['deploy' ].keys ())
49+ return list (self .procfile .keys ())
50+
4351 @property
4452 def source_based (self ):
4553 """
@@ -53,7 +61,16 @@ def source_based(self):
5361 def version (self ):
5462 return 'git-{}' .format (self .sha ) if self .source_based else 'latest'
5563
56- def create (self , user , * args , ** kwargs ):
64+ def get_image (self , procfile_type , default_image = None ):
65+ docker = self .dryccfile .get ('build' , {}).get ('docker' , {})
66+ if procfile_type in docker :
67+ if procfile_type == 'web' :
68+ return self .image
69+ else :
70+ return f'{ self .image } -{ procfile_type } '
71+ return default_image if default_image else self .image
72+
73+ def create_release (self , user , * args , ** kwargs ):
5774 app_settings = self .app .appsettings_set .latest ()
5875 latest_release = self .app .release_set .filter (failed = False ).latest ()
5976 latest_version = self .app .release_set .latest ().version
@@ -64,22 +81,21 @@ def create(self, user, *args, **kwargs):
6481 config = latest_release .config ,
6582 canary = len (app_settings .canaries ) > 0 ,
6683 )
67- self . app . deploy (new_release )
84+ run_pipeline . delay (new_release )
6885 return new_release
6986 except Exception as e :
7087 # check if the exception is during create or publish
7188 if ('new_release' not in locals () and
7289 self .app .release_set .latest ().version == latest_version + 1 ):
7390 new_release = self .app .release_set .latest ()
74- if ' new_release' in locals ():
91+ new_release . state = "crashed"
7592 new_release .failed = True
7693 new_release .summary = "{} deployed {} which failed" .format (self .owner , str (self .uuid )[:7 ]) # noqa
7794 # Get the exception that has occured
7895 new_release .exception = "error: {}" .format (str (e ))
7996 new_release .save ()
80- else :
97+ if 'new_release' not in locals () :
8198 self .delete ()
82-
8399 raise DryccException (str (e )) from e
84100
85101 def save (self , ** kwargs ):
@@ -90,14 +106,14 @@ def save(self, **kwargs):
90106 # previous release had a Procfile and the current one does not
91107 (
92108 previous_release .build is not None and
93- len (previous_release .build . procfile ) > 0 and
94- len (self .procfile ) == 0
109+ len (previous_release .procfile_types ) > 0 and
110+ len (self .procfile_types ) == 0
95111 )
96112 ):
97113 # Reject deployment
98114 raise Conflict (
99- 'Last deployment had a Procfile but is missing in this deploy. '
100- 'For a successful deployment provide a Procfile .'
115+ 'Last deployment had process types but is missing in this deploy. '
116+ 'For a successful deployment provide process types .'
101117 )
102118
103119 # See if processes are permitted to be removed
@@ -107,16 +123,16 @@ def save(self, **kwargs):
107123 # previous release had a Procfile and the current one does as well
108124 (
109125 previous_release .build is not None and
110- len (previous_release .build . procfile ) > 0 and
111- len (self .procfile ) > 0
126+ len (previous_release .procfile_types ) > 0 and
127+ len (self .procfile_types ) > 0
112128 )
113129 )
114130
115131 # spin down any proc type removed between the last procfile and the newest one
116132 if remove_procs and previous_release .build is not None :
117133 removed = {}
118- for proc in previous_release .build . procfile :
119- if proc not in self .procfile and self .app .structure .get (proc , 0 ) > 0 :
134+ for proc in previous_release .procfile_types :
135+ if proc not in self .procfile_types and self .app .structure .get (proc , 0 ) > 0 :
120136 # Scale proc type down to 0
121137 removed [proc ] = 0
122138
@@ -127,10 +143,11 @@ def save(self, **kwargs):
127143 if (
128144 settings .DRYCC_DEPLOY_PROCFILE_MISSING_REMOVE is False and
129145 previous_release .build is not None and
130- len (previous_release .build . procfile ) > 0 and
131- len (self .procfile ) == 0
146+ len (previous_release .procfile_types ) > 0 and
147+ len (self .procfile_types ) == 0
132148 ):
133149 self .procfile = previous_release .build .procfile
150+ self .dryccfile = previous_release .build .dryccfile
134151
135152 return super (Build , self ).save (** kwargs )
136153
0 commit comments