/** * ************* Packs an application folder into a zip file. * * @param application the application object as read from the application file. * @param applicationDir the directory where the application was read from. * @param additionalServiceFiles additional files that should be packaged into each service * directory. * @return the packaged zip file. * @throws IOException . * @throws PackagingException . */ public static File packApplication( final Application application, final File applicationDir, final File[] additionalServiceFiles) throws IOException, PackagingException { boolean hasExtendedServices = false; for (final Service service : application.getServices()) { if (!service.getExtendedServicesPaths().isEmpty()) { hasExtendedServices = true; break; } } File applicationFolderToPack = applicationDir; // If there are no extended service we don't need to prepare an application folder to pack with // all the // extended services content. if (hasExtendedServices) { final File destApplicationFolder = createCopyDirectory(applicationFolderToPack); for (final Service service : application.getServices()) { final File extFolder = new File(destApplicationFolder + "/" + service.getName()); final File recipeFile = DSLReader.findDefaultDSLFile( DSLReader.SERVICE_DSL_FILE_NAME_SUFFIX, new File(applicationDir + "/" + service.getName())); copyExtendedServiceFiles(service, recipeFile, extFolder); } // Pack the prepared folder instead of the original application folder. applicationFolderToPack = destApplicationFolder; } if ((additionalServiceFiles != null) && (additionalServiceFiles.length > 0)) { // if a copy directory was already created, use the existing one, otherwise // create a new one. if (applicationFolderToPack == applicationDir) { applicationFolderToPack = createCopyDirectory(applicationFolderToPack); } List<Service> services = application.getServices(); for (Service service : services) { File serviceDir = new File(applicationFolderToPack, service.getName()); if (!serviceDir.exists()) { throw new PackagingException("Could not find service folder at: " + serviceDir); } if (!serviceDir.isDirectory()) { throw new PackagingException("Was expecting a directory at: " + serviceDir); } for (File fileToCopy : additionalServiceFiles) { FileUtils.copyFileToDirectory(fileToCopy, serviceDir); } } } // zip the application folder. final File zipFile = File.createTempFile("application", ".zip"); zipFile.deleteOnExit(); ZipUtils.zip(applicationFolderToPack, zipFile); return zipFile; }
/** * *********** Pack a service recipe folder into a zip file. * * @param recipeDirOrFile the recipe directory or recipe file. * @return the packed file. * @throws IOException . * @throws PackagingException . * @throws DSLException . */ public static File pack(final File recipeDirOrFile) throws IOException, PackagingException, DSLException { // Locate recipe file final File recipeFile = recipeDirOrFile.isDirectory() ? DSLReader.findDefaultDSLFile(DSLReader.SERVICE_DSL_FILE_NAME_SUFFIX, recipeDirOrFile) : recipeDirOrFile; // Parse recipe into service final Service service = ServiceReader.readService(recipeFile); return Packager.pack(recipeFile, service); }
private static File locateServiceFile(final File recipeFile, final String extendedServicePath) throws FileNotFoundException, PackagingException { File extendedServiceFile = new File(extendedServicePath); if (!extendedServiceFile.isAbsolute()) { extendedServiceFile = new File(recipeFile.getParent() + "/" + extendedServicePath); } if (extendedServiceFile.isDirectory()) { extendedServiceFile = DSLReader.findDefaultDSLFile(DSLReader.SERVICE_DSL_FILE_NAME_SUFFIX, extendedServiceFile); } return extendedServiceFile; }
/** {@inheritDoc} */ @Override protected Object doExecute() throws Exception { if (cloudOverrides != null) { if (cloudOverrides.length() >= TEN_K) { throw new CLIStatusException(CloudifyErrorMessages.CLOUD_OVERRIDES_TO_LONG.getName()); } } RecipePathResolver pathResolver = new RecipePathResolver(); if (pathResolver.resolveService(recipe)) { recipe = pathResolver.getResolved(); } else { throw new CLIStatusException( "service_file_doesnt_exist", StringUtils.join(pathResolver.getPathsLooked().toArray(), ", ")); } File packedFile; final File cloudConfigurationZipFile = createCloudConfigurationZipFile(); // TODO: this logics should not be done twice. should be done directly // in the rest server. // also figure out how to treat war/jar files that have no .groovy file. // create default? Service service = null; try { if (recipe.getName().endsWith(".jar") || recipe.getName().endsWith(".war")) { // legacy XAP Processing Unit packedFile = recipe; } else if (recipe.isDirectory()) { // Assume that a folder will contain a DSL file? final List<File> additionFiles = new LinkedList<File>(); if (cloudConfigurationZipFile != null) { additionFiles.add(cloudConfigurationZipFile); } File recipeFile = recipe; if (serviceFileName != null) { final File fullPathToRecipe = new File(recipe.getAbsolutePath() + "/" + serviceFileName); if (!fullPathToRecipe.exists()) { throw new CLIStatusException("service_file_doesnt_exist", fullPathToRecipe.getPath()); } // locate recipe file recipeFile = fullPathToRecipe.isDirectory() ? DSLReader.findDefaultDSLFile( DSLUtils.SERVICE_DSL_FILE_NAME_SUFFIX, fullPathToRecipe) : fullPathToRecipe; } else { recipeFile = DSLReader.findDefaultDSLFile(DSLUtils.SERVICE_DSL_FILE_NAME_SUFFIX, recipe); } final DSLReader dslReader = createDslReader(recipeFile); service = dslReader.readDslEntity(Service.class); // lookup service properties file File servicePropertiesFile = new File(recipe, service.getName() + "-service" + DSLUtils.PROPERTIES_FILE_SUFFIX); /* * name the merged properties file as the original properties file. * this will allow all properties to be available by anyone who parses the default * properties file. (like Lifecycle scripts) */ File tempFile = File.createTempFile("__cloudify", ""); String tempFileName = tempFile.getName(); tempFile.delete(); // create the file in a unique folder under the temp directory File uniqueFolder = new File(TEMP_FOLDER + File.separator + tempFileName); uniqueFolder.mkdir(); File finalPropsFile = new File(uniqueFolder, servicePropertiesFile.getName()); finalPropsFile.deleteOnExit(); // this will actually create an empty props file. FileAppender appender = new FileAppender(finalPropsFile); if (overrides != null) { // merge the service properties file with the overrides file. appender.append("Service Properties File", servicePropertiesFile); appender.append("Overrides Properties File", overrides); appender.flush(); additionFiles.add(finalPropsFile); } packedFile = Packager.pack(recipeFile, false, service, additionFiles); packedFile.deleteOnExit(); finalPropsFile.delete(); } else { // serviceFile is a zip file packedFile = recipe; service = ServiceReader.readServiceFromZip(packedFile); } } catch (final IOException e) { throw new CLIException(e); } catch (final PackagingException e) { throw new CLIException(e); } final String currentApplicationName = getCurrentApplicationName(); Properties props = null; if (service != null) { props = createServiceContextProperties(service); if (serviceFileName != null) { props.setProperty(CloudifyConstants.CONTEXT_PROPERTY_SERVICE_FILE_NAME, serviceFileName); } if (serviceName == null || serviceName.isEmpty()) { serviceName = service.getName(); } } else { if (serviceName == null || serviceName.isEmpty()) { serviceName = recipe.getName(); final int endIndex = serviceName.lastIndexOf('.'); if (endIndex > 0) { serviceName = serviceName.substring(0, endIndex); } } } if (zone == null || zone.isEmpty()) { zone = serviceName; } String templateName; // service is null when a simple deploying war for example if (service == null || service.getCompute() == null) { templateName = ""; } else { templateName = service.getCompute().getTemplate(); if (templateName == null) { templateName = ""; } } try { final String lifecycleEventContainerPollingID = adminFacade.installElastic( packedFile, currentApplicationName, serviceName, zone, props, templateName, authGroups, getTimeoutInMinutes(), !disableSelfHealing, cloudOverrides); pollForLifecycleEvents(lifecycleEventContainerPollingID); } finally { // if a zip file was created, delete it at the end of use. if (recipe.isDirectory()) { FileUtils.deleteQuietly(packedFile.getParentFile()); } } // TODO - server may have failed! We should check the service state and // decide accordingly // which message to display. return getFormattedMessage("service_install_ended", Color.GREEN, serviceName); }