示例#1
0
 /**
  * Packages the recipe files and other required files in a zip.
  *
  * @param recipeDirOrFile The recipe service DSL file or containing folder
  * @return A zip file
  * @throws CLIException Reporting a failure to find or parse the given DSL file, or pack the zip
  *     file
  */
 public File doPack(final File recipeDirOrFile) throws CLIException {
   try {
     return Packager.pack(recipeDirOrFile);
   } catch (final IOException e) {
     throw new CLIException(e);
   } catch (final PackagingException e) {
     throw new CLIException(e);
   } catch (final DSLException e) {
     throw new CLIException(e);
   }
 }
示例#2
0
 /**
  * *********** 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);
 }
示例#3
0
  /** {@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);
  }