Esempio n. 1
0
  /**
   * Create Properties object with settings from the service object, if found on the given service.
   * The supported settings are: com.gs.application.dependsOn com.gs.service.type
   * com.gs.service.icon com.gs.service.network.protocolDescription
   *
   * @param service The service object the read the settings from
   * @return Properties object populated with the above properties, if found on the given service.
   */
  private Properties createServiceContextProperties(final Service service) {
    final Properties contextProperties = new Properties();

    // contextProperties.setProperty("com.gs.application.services",
    // serviceNamesString);
    if (service.getDependsOn() != null) {
      contextProperties.setProperty(
          CloudifyConstants.CONTEXT_PROPERTY_DEPENDS_ON, service.getDependsOn().toString());
    }
    if (service.getType() != null) {
      contextProperties.setProperty(
          CloudifyConstants.CONTEXT_PROPERTY_SERVICE_TYPE, service.getType());
    }
    if (service.getIcon() != null) {
      contextProperties.setProperty(
          CloudifyConstants.CONTEXT_PROPERTY_SERVICE_ICON,
          CloudifyConstants.SERVICE_EXTERNAL_FOLDER + service.getIcon());
    }
    if (service.getNetwork() != null) {
      if (service.getNetwork().getProtocolDescription() != null) {
        contextProperties.setProperty(
            CloudifyConstants.CONTEXT_PROPERTY_NETWORK_PROTOCOL_DESCRIPTION,
            service.getNetwork().getProtocolDescription());
      }
    }

    contextProperties.setProperty(
        CloudifyConstants.CONTEXT_PROPERTY_ELASTIC, Boolean.toString(service.isElastic()));

    return contextProperties;
  }
  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);
  }
 @Test
 public void testMissingTemplate() throws IOException, DSLException, PackagingException {
   Cloud cloud = ServiceReader.readCloud(new File(CLOUD_FILE_PATH));
   Service service = ServiceReader.readService(new File(NOT_EXIST_TEMPLATE_SERVICE_GROOVY));
   testValidator(
       cloud,
       service,
       service.getCompute().getTemplate(),
       CloudifyMessageKeys.MISSING_TEMPLATE.getName());
 }
Esempio n. 4
0
  /**
   * ************* 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;
  }
Esempio n. 5
0
 /** @return */
 private static File getUsmLibDir(final Service service) {
   final Map<String, String> customProperties = service.getCustomProperties();
   File usmLibDir = null;
   if (customProperties != null) {
     final String usmJarPathProp = customProperties.get(USM_JAR_PATH_PROP);
     if (usmJarPathProp != null) {
       usmLibDir = new File(usmJarPathProp);
     }
   }
   if (usmLibDir == null) {
     usmLibDir = new File(Environment.getHomeDirectory() + "/lib/platform/usm");
   }
   return usmLibDir;
 }
Esempio n. 6
0
  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;
  }
Esempio n. 7
0
 /**
  * Verifies the service configuration is valid.
  *
  * @param serviceFolder The Folder holding the service configuration files
  * @throws CLIException Reporting a failure to find or parse the configuration files
  */
 private void isServiceLifecycleNotNull(final File serviceFolder) throws CLIException {
   Service service;
   try {
     final File serviceFileDir = new File(serviceFolder, "ext");
     service =
         ServiceReader.getServiceFromDirectory(
                 serviceFileDir, CloudifyConstants.DEFAULT_APPLICATION_NAME)
             .getService();
     if (service.getLifecycle() == null) {
       throw new CLIException(getFormattedMessage("test_recipe_service_lifecycle_missing"));
     }
   } catch (final FileNotFoundException e) {
     logger.log(Level.SEVERE, "Service configuration file not found " + e.getMessage(), e);
     throw new CLIException("Failed to locate service configuration file. " + e.getMessage(), e);
   } catch (final PackagingException e) {
     logger.log(Level.SEVERE, "Packaging failed: " + e.getMessage(), e);
     e.printStackTrace();
     throw new CLIException("Packaging failed: " + e.getMessage(), e);
   } catch (final DSLException e) {
     logger.log(Level.SEVERE, "DSL Parsing failed: " + e.getMessage(), e);
     e.printStackTrace();
     throw new CLIException("Packaging failed: " + e.getMessage(), e);
   }
 }
Esempio n. 8
0
  private static void copyExtendedServiceFiles(
      final Service service, final File recipeFile, final File extFolder)
      throws FileNotFoundException, PackagingException, IOException {
    final LinkedList<String> extendedServicesPaths = service.getExtendedServicesPaths();

    File extendingScriptFile = new File(extFolder + "/" + recipeFile.getName());
    File currentExtendedServiceContext = recipeFile;

    for (final String extendedServicePath : extendedServicesPaths) {
      // Locate the extended service file in the destination path
      final File extendedServiceFile =
          locateServiceFile(currentExtendedServiceContext, extendedServicePath);
      // If the extended service exists in my directory, no need to copy or change anything
      // This can happen if we have extension of services inside application since the client
      // will prepare the extending service directory already and then it will be prepared fully at
      // the server
      if (extendedServiceFile.getParentFile().equals(recipeFile.getParentFile())) {
        continue;
      }
      // Copy it to local dir with new name if needed
      final File localExtendedServiceFile =
          copyExtendedServiceFileAndRename(extendedServiceFile, extFolder);
      logger.finer(
          "copying locally extended script "
              + extendedServiceFile
              + " to "
              + localExtendedServiceFile);
      // Update the extending script extend property with the location of the new extended service
      // script
      updateExtendingScriptFileWithNewExtendedScriptLocation(
          extendingScriptFile, localExtendedServiceFile);
      // Copy remote resources locally
      final File rootScriptDir = extendedServiceFile.getParentFile();
      FileUtils.copyDirectory(
          rootScriptDir,
          extFolder,
          new FileFilter() {

            @Override
            public boolean accept(final File pathname) {
              if (!SVNFileFilter.getFilter().accept(pathname)) {
                return false;
              }
              if (pathname.equals(extendedServiceFile)) {
                return false;
              }
              if (pathname.isDirectory()) {
                return true;
              }
              final String relativePath = pathname.getPath().replace(rootScriptDir.getPath(), "");
              final boolean accept = !new File(extFolder.getPath() + "/" + relativePath).exists();
              if (accept && logger.isLoggable(Level.FINEST)) {
                logger.finest("copying extended script resource [" + pathname + "] locally");
              }
              return accept;
            }
          });
      // Replace context extending script file for multiple level extension
      extendingScriptFile = localExtendedServiceFile;
      currentExtendedServiceContext = extendedServiceFile;
    }
  }
Esempio n. 9
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);
  }