1616import org .apache .commons .io .FileUtils ;
1717import org .apache .commons .io .IOUtils ;
1818import org .eclipse .jetty .io .EofException ;
19+ import org .json .simple .JSONObject ;
1920import org .json .simple .parser .ParseException ;
2021import org .slf4j .Logger ;
2122import org .slf4j .LoggerFactory ;
4647import java .util .regex .Matcher ;
4748import java .util .regex .Pattern ;
4849
49-
5050@ RestController
5151public class ExtenderController {
52+ public enum InstanceType {
53+ MIXED ,
54+ FRONTEND_ONLY ,
55+ BUILDER_ONLY
56+ };
57+
5258 private static final Logger LOGGER = LoggerFactory .getLogger (ExtenderController .class );
5359
5460 private static final String LATEST = "latest" ;
@@ -65,6 +71,8 @@ public class ExtenderController {
6571
6672 private final RemoteEngineBuilder remoteEngineBuilder ;
6773 private Map <String , RemoteInstanceConfig > remoteBuilderPlatformMappings ;
74+ @ Value ("${extender.instance-type:MIXED}" )
75+ private InstanceType instanceType ;
6876 private final boolean remoteBuilderEnabled ;
6977
7078 private static long maxPackageSize = 1024 *1024 *1024 ;
@@ -148,7 +156,7 @@ public void buildEngineAsync(HttpServletRequest _request,
148156 HttpServletResponse response ,
149157 @ PathVariable ("platform" ) String platform ,
150158 @ PathVariable ("sdkVersion" ) String sdkVersionString )
151- throws ExtenderException , IOException , ParseException {
159+ throws ExtenderException , IOException , ParseException , VersionNotSupportedException , PlatformNotSupportedException {
152160
153161 boolean isMultipart = JakartaServletFileUpload .isMultipartContent (_request );
154162 if (!isMultipart ) {
@@ -198,17 +206,33 @@ public void buildEngineAsync(HttpServletRequest _request,
198206 // Regardless of success/fail status, we want to cache the uploaded files
199207 DataCacheService .DataCacheServiceInfo uploadResultInfo = dataCacheService .cacheFiles (uploadDirectory );
200208 metricsWriter .measureCacheUpload (uploadResultInfo .cachedFileSize .longValue (), uploadResultInfo .cachedFileCount .intValue ());
201-
202- String [] buildEnvDescription = ExtenderUtil .getSdksForPlatform (platform , defoldSdkService .getPlatformSdkMappings (sdkVersion ));
203- // Build engine locally or on remote builder
204- if (remoteBuilderEnabled && isRemotePlatform (buildEnvDescription [0 ], buildEnvDescription [1 ])) {
205- LOGGER .info ("Building engine on remote builder" );
206- RemoteInstanceConfig remoteInstanceConfig = getRemoteBuilderConfig (buildEnvDescription [0 ], buildEnvDescription [1 ]);
207- this .remoteEngineBuilder .buildAsync (remoteInstanceConfig , uploadDirectory , platform , sdkVersion , jobDirectory , buildDirectory , metricsWriter );
208- } else {
209+
210+ if (instanceType .equals (InstanceType .BUILDER_ONLY )) {
209211 asyncBuilder .asyncBuildEngine (metricsWriter , platform , sdkVersion , jobDirectory , uploadDirectory , buildDirectory );
212+ } else {
213+ String [] buildEnvDescription = null ;
214+ try {
215+ JSONObject mappings = defoldSdkService .getPlatformSdkMappings (sdkVersion );
216+ buildEnvDescription = ExtenderUtil .getSdksForPlatform (platform , mappings );
217+ } catch (ExtenderException exc ) {
218+ if (instanceType .equals (InstanceType .FRONTEND_ONLY )) {
219+ LOGGER .error ("Unsupported engine version {}" , sdkVersion );
220+ throw new VersionNotSupportedException (sdkVersion );
221+ }
222+ }
223+ // Build engine locally or on remote builder
224+ if (remoteBuilderEnabled && buildEnvDescription != null && isRemotePlatform (buildEnvDescription [0 ], buildEnvDescription [1 ])) {
225+ LOGGER .info ("Building engine on remote builder" );
226+ RemoteInstanceConfig remoteInstanceConfig = getRemoteBuilderConfig (buildEnvDescription [0 ], buildEnvDescription [1 ]);
227+ this .remoteEngineBuilder .buildAsync (remoteInstanceConfig , uploadDirectory , platform , sdkVersion , jobDirectory , buildDirectory , metricsWriter );
228+ } else if (instanceType .equals (InstanceType .MIXED )) {
229+ asyncBuilder .asyncBuildEngine (metricsWriter , platform , sdkVersion , jobDirectory , uploadDirectory , buildDirectory );
230+ } else {
231+ // no remote builder was found and current instance can't build
232+ LOGGER .error ("Unsupported build platform {}" , platform );
233+ throw new PlatformNotSupportedException (platform );
234+ }
210235 }
211-
212236 response .getWriter ().write (jobDirectory .getName ());
213237 response .getWriter ().flush ();
214238 response .getWriter ().close ();
@@ -218,6 +242,8 @@ public void buildEngineAsync(HttpServletRequest _request,
218242 throw new ExtenderException (e , "Client closed connection prematurely, build aborted" );
219243 } catch (FileUploadException e ) {
220244 throw new ExtenderException (e , "Bad request: " + e .getMessage ());
245+ } catch (VersionNotSupportedException |PlatformNotSupportedException exc ) {
246+ throw exc ;
221247 } catch (Exception e ) {
222248 LOGGER .error (String .format ("Exception while building or sending response - SDK: %s" , sdkVersion ));
223249 throw e ;
0 commit comments