private ServiceConfiguration handleDsl() throws DSLException {
    File dslFile = null;

    if (serviceFileName != null) {
      dslFile = new File(this.puExtDir, this.serviceFileName);
    }

    DSLReader dslReader = new DSLReader();
    dslReader.setAdmin(USMUtils.getAdmin());
    dslReader.setClusterInfo(clusterInfo);
    dslReader.setPropertiesFileName(propertiesFileName);
    dslReader.setRunningInGSC(isRunningInGSC);
    dslReader.setDslFile(dslFile);
    dslReader.setWorkDir(this.puExtDir);
    dslReader.setDslFileNameSuffix(DSLReader.SERVICE_DSL_FILE_NAME_SUFFIX);

    // When loading a service in the USM, expect the jar files to
    // be available in the pu lib dir, and ignore the contents of usmlib
    dslReader.setLoadUsmLib(false);

    logger.info("Loading Service configuration from DSL File");
    Service service = dslReader.readDslEntity(Service.class);
    return new ServiceConfiguration(
        service, dslReader.getContext(), this.puExtDir, dslReader.getDslFile());
  }
Ejemplo n.º 2
0
  private static Object testDSLOverrides(
      final String servicePath,
      final String overridesFilePath,
      final Class<?> clazz,
      final Map<String, Object> expectedFields)
      throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
          DSLException {
    final File workDir = new File(servicePath);

    final boolean isApplication = clazz.equals(Application.class);
    final boolean isService = clazz.equals(Service.class);
    final boolean isCloud = clazz.equals(Cloud.class);

    File overridesFile = null;
    if (overridesFilePath != null) {
      overridesFile = new File(overridesFilePath);
    }
    final DSLReader reader = new DSLReader();
    reader.setWorkDir(workDir);
    reader.setRunningInGSC(true);
    if (isApplication) {
      reader.setDslFileNameSuffix(DSLUtils.APPLICATION_DSL_FILE_NAME_SUFFIX);
      reader.addProperty(DSLUtils.APPLICATION_DIR, workDir.getAbsolutePath());
      reader.setCreateServiceContext(false);
    } else if (isService) {
      reader.setDslFileNameSuffix(DSLUtils.SERVICE_DSL_FILE_NAME_SUFFIX);
    } else if (isCloud) {
      reader.setDslFileNameSuffix(DSLUtils.CLOUD_DSL_FILE_NAME_SUFFIX);
      reader.setCreateServiceContext(false);
    } else {
      throw new DSLException("Class " + clazz.getName() + " does not exist in the DSL Domain");
    }
    reader.setOverridesFile(overridesFile);
    final Object object = reader.readDslEntity(clazz);
    assertOverrides(object, reader, expectedFields, workDir);
    return object;
  }
Ejemplo n.º 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);
  }