/** * ************* 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; }
public void testService( String serviceFolderPath, String overrideServiceName, final int timeoutMins) throws IOException, InterruptedException, RestException, PackagingException, DSLException { LogUtils.log("Reading Service from file : " + serviceFolderPath); Service service = ServiceReader.readService(new File(serviceFolderPath)); LogUtils.log("Succesfully read Service : " + service); serviceName = service.getName(); if (overrideServiceName != null) { LogUtils.log("Overriding service name with " + overrideServiceName); serviceName = overrideServiceName; } installServiceAndWait(serviceFolderPath, serviceName, timeoutMins); String restUrl = getRestUrl(); GSRestClient client = new GSRestClient("", "", new URL(restUrl), PlatformVersion.getVersionNumber()); Map<String, Object> entriesJsonMap = client.getAdminData("ProcessingUnits/Names/default." + serviceName + "/Status"); String serviceStatus = (String) entriesJsonMap.get(STATUS_PROPERTY); AssertUtils.assertTrue("service is not intact", serviceStatus.equalsIgnoreCase("INTACT")); uninstallServiceAndWait(serviceName); }
private static File createZippedPu( final Service service, final File puFolderToZip, final File recipeFile) throws IOException, PackagingException { logger.finer("trying to zip " + puFolderToZip.getAbsolutePath()); final String serviceName = service.getName() != null ? service.getName() : recipeFile.getParentFile().getName(); // create a temp dir under the system temp dir final File tmpFile = File.createTempFile("ServicePackage", null); tmpFile.delete(); tmpFile.mkdir(); final File zipFile = new File(tmpFile, serviceName + ".zip"); // files will be deleted in reverse order tmpFile.deleteOnExit(); zipFile.deleteOnExit(); ServiceReader.validateFolderSize(puFolderToZip, service.getMaxJarSize()); ZipUtils.zip(puFolderToZip, zipFile); logger.finer("zipped folder successfully to " + zipFile.getAbsolutePath()); return zipFile; }
/** {@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); }