Beispiel #1
0
 /**
  * Returns true if the remote git url is defined or the local git repo has a remote url defined
  */
 protected boolean hasRemoteRepo() {
   if (Strings.isNotBlank(gitUrl)) {
     return true;
   }
   Repository repository = git.getRepository();
   StoredConfig config = repository.getConfig();
   String url = config.getString("remote", remoteName, "url");
   if (Strings.isNotBlank(url)) {
     return true;
   }
   return false;
 }
Beispiel #2
0
 protected void configureBranch(String branch) {
   // lets update the merge config
   if (Strings.isNotBlank(branch) && hasRemoteRepo()) {
     StoredConfig config = git.getRepository().getConfig();
     if (Strings.isNullOrBlank(config.getString("branch", branch, "remote"))
         || Strings.isNullOrBlank(config.getString("branch", branch, "merge"))) {
       config.setString("branch", branch, "remote", remoteName);
       config.setString("branch", branch, "merge", "refs/heads/" + branch);
       try {
         config.save();
       } catch (IOException e) {
         getLog()
             .error(
                 "Failed to save the git configuration into "
                     + new File(buildDir, ".git")
                     + " with branch "
                     + branch
                     + " on remote repo: "
                     + gitUrl
                     + " due: "
                     + e.getMessage()
                     + ". This exception is ignored.",
                 e);
       }
     }
   }
 }
Beispiel #3
0
 public void filter(ClientRequestContext requestContext) throws IOException {
   MultivaluedMap<String, Object> headers = requestContext.getHeaders();
   String authenticationHeader = getAuthenticationHeader();
   if (Strings.isNotBlank(authenticationHeader)) {
     headers.add("Authorization", authenticationHeader);
   }
 }
 protected boolean overrideFromEnvVar(String name, boolean defaultValue) {
   String envVar = envVarPrefix + name;
   String value = null;
   try {
     value = System.getenv(envVar);
     LOG.debug("Checking env var: " + envVar);
     if (Strings.isNullOrBlank(value)) {
       // lets try the generic value for all entities
       value = System.getProperty(ENV_VAR_PREFIX + name);
     }
     if (Strings.isNotBlank(value)) {
       Boolean b = Boolean.parseBoolean(value);
       if (b != null) {
         return b.booleanValue();
       }
     }
   } catch (Exception e) {
     LOG.warn(
         "Failed to parse boolean value for env var "
             + envVar
             + " with value `"
             + value
             + "`. "
             + e,
         e);
   }
   return defaultValue;
 }
 /** If a slug is not supplied then lets generate it from the project name */
 protected String validateSlug(String slug, String name) {
   if (Strings.isNotBlank(slug)) {
     return slug;
   } else {
     return getUsername() + "-" + name;
   }
 }
Beispiel #6
0
  public static final String toAlias(
      String serviceName,
      String serviceProtocol,
      String servicePort,
      String servicePath,
      Boolean endpoint,
      Boolean external,
      String suffix) {
    StringBuilder sb = new StringBuilder();

    if (external) {
      sb.append("external");
    } else {
      sb.append("internal");
    }

    sb.append("-");

    if (endpoint) {
      sb.append("endpoint");
    } else {
      sb.append("service");
    }

    sb.append("-").append(serviceName);

    if (Strings.isNotBlank(serviceProtocol)) {
      sb.append("-").append(serviceProtocol);
    } else {
      sb.append("-").append("tcp");
    }

    if (Strings.isNotBlank(servicePort)) {
      sb.append("-").append(serviceProtocol);
    } else {
      sb.append("-").append("single");
    }

    if (Strings.isNotBlank(servicePath)) {
      sb.append("-").append(servicePath);
    } else {
      sb.append("-").append("root");
    }

    sb.append("-").append(suffix);
    return sb.toString();
  }
Beispiel #7
0
 public static String or(String... candidates) {
   for (String candidate : candidates) {
     if (Strings.isNotBlank(candidate)) {
       return candidate;
     }
   }
   return null;
 }
Beispiel #8
0
 private String getAuthenticationHeader() {
   String token = getAuthToken();
   if (Strings.isNotBlank(token)) {
     return "Bearer " + token;
   } else {
     return null;
   }
 }
 protected PipelineDTO getPipelineForValue(UIContext context, String value) {
   if (Strings.isNotBlank(value)) {
     Iterable<PipelineDTO> pipelines = getPipelines(context, false);
     for (PipelineDTO pipelineDTO : pipelines) {
       if (pipelineDTO.getValue().equals(value) || pipelineDTO.toString().equals(value)) {
         return pipelineDTO;
       }
     }
   }
   return null;
 }
  public void addController(ReplicationController controller) {
    String id = getId(controller);
    if (Strings.isNotBlank(id)) {
      controllers.put(id, controller);

      // now lets find all the pods that are active for this
      List<Pod> pods = snapshot.podsForReplicationController(controller);
      for (Pod pod : pods) {
        addPod(pod);
      }
    }
  }
 /**
  * Returns the webhook URL for the given module.
  *
  * <p>The module webhook might not use the correct public host name so lets convert it.
  */
 public String getPublicWebhookUrl(ModuleDTO module) {
   if (module != null) {
     String webhooksUrl = module.getWebhooksUrl();
     if (Strings.isNotBlank(webhooksUrl)) {
       int idx = webhooksUrl.indexOf("/api/v");
       if (idx > 0) {
         return URLUtils.pathJoin(getAddress(), webhooksUrl.substring(idx));
       }
     }
     return webhooksUrl;
   }
   return null;
 }
 public KubernetesClient getKubernetes() {
   if (kubernetes == null) {
     String kubernetesAddress = kubernetesUrl.getValue();
     if (Strings.isNotBlank(kubernetesAddress)) {
       kubernetes =
           new DefaultKubernetesClient(
               new ConfigBuilder().withMasterUrl(kubernetesAddress).build());
     } else {
       kubernetes = new DefaultKubernetesClient();
     }
   }
   Objects.notNull(kubernetes, "kubernetes");
   return kubernetes;
 }
Beispiel #13
0
 /**
  * Applies the kubernetes json url to the configuration.
  *
  * @param map The arquillian configuration.
  */
 private static URL getKubernetesConfigurationUrl(Map<String, String> map)
     throws MalformedURLException {
   if (map.containsKey(ENVIRONMENT_CONFIG_URL)) {
     return new URL(map.get(ENVIRONMENT_CONFIG_URL));
   } else if (map.containsKey(ENVIRONMENT_CONFIG_RESOURCE_NAME)) {
     String resourceName = map.get(ENVIRONMENT_CONFIG_RESOURCE_NAME);
     return findConfigResource(resourceName);
   } else if (Strings.isNotBlank(Utils.getSystemPropertyOrEnvVar(ENVIRONMENT_CONFIG_URL, ""))) {
     return new URL(Utils.getSystemPropertyOrEnvVar(ENVIRONMENT_CONFIG_URL, ""));
   } else {
     String defaultValue = "/" + DEFAULT_CONFIG_FILE_NAME;
     String resourceName =
         Utils.getSystemPropertyOrEnvVar(ENVIRONMENT_CONFIG_RESOURCE_NAME, defaultValue);
     URL answer = findConfigResource(resourceName);
     if (answer == null) {}
     return answer;
   }
 }
Beispiel #14
0
  /**
   * Iterates through all nested directories and generates archetypes for all found, non-pom Maven
   * projects.
   *
   * @param baseDir a directory to look for projects which may be converted to Maven Archetypes
   * @param outputDir target directory where Maven Archetype projects will be generated
   * @param clean regenerate the archetypes (clean the archetype target dir)?
   * @throws IOException
   */
  public void generateArchetypes(
      String containerType, File baseDir, File outputDir, boolean clean, List<String> dirs)
      throws IOException {
    LOG.debug(
        "Generating archetypes from {} to {}",
        baseDir.getCanonicalPath(),
        outputDir.getCanonicalPath());
    File[] files = baseDir.listFiles();
    if (files != null) {
      for (File file : files) {
        if (file.isDirectory()) {
          File projectDir = file;
          File projectPom = new File(projectDir, "pom.xml");
          if (projectPom.exists() && !skipImport(projectDir)) {
            if (archetypeUtils.isValidProjectPom(projectPom)) {
              String fileName = file.getName();
              String archetypeDirName = fileName.replace("example", "archetype");
              if (fileName.equals(archetypeDirName)) {
                archetypeDirName += "-archetype";
              }
              archetypeDirName = containerType + "-" + archetypeDirName;

              File archetypeDir = new File(outputDir, archetypeDirName);
              generateArchetype(projectDir, projectPom, archetypeDir, clean, dirs);

              File archetypePom = new File(archetypeDir, "pom.xml");
              if (archetypePom.exists()) {
                addArchetypeMetaData(archetypePom, archetypeDirName);
              }
            } else {
              // lets iterate through the children
              String childContainerType = file.getName();
              if (Strings.isNotBlank(containerType)) {
                childContainerType = containerType + "-" + childContainerType;
              }
              generateArchetypes(childContainerType, file, outputDir, clean, dirs);
            }
          }
        }
      }
    }
  }
Beispiel #15
0
  protected void generateZip()
      throws DependencyTreeBuilderException, MojoExecutionException, IOException,
          MojoFailureException {

    File appBuildDir = buildDir;
    if (Strings.isNotBlank(pathInZip)) {
      appBuildDir = new File(buildDir, pathInZip);
    }
    appBuildDir.mkdirs();

    if (hasConfigDir()) {
      copyAppConfigFiles(appBuildDir, appConfigDir);

    } else {
      getLog()
          .info(
              "The app configuration files directory "
                  + appConfigDir
                  + " doesn't exist, so not copying any additional project documentation or configuration files");
    }
    MavenProject project = getProject();

    if (!ignoreProject) {
      File kubernetesJson = getKubernetesJson();
      if (kubernetesJson != null && kubernetesJson.isFile() && kubernetesJson.exists()) {
        File jsonFile = new File(appBuildDir, "kubernetes.json");
        jsonFile.getParentFile().mkdirs();
        Files.copy(kubernetesJson, jsonFile);
      }

      // TODO if no iconRef is specified we could try guess based on the project?

      // lets check if we can use an icon reference
      copyIconToFolder(appBuildDir);
    }

    // lets only generate a app zip if we have a requirement (e.g. we're not a parent pom packaging
    // project) and
    // we have defined some configuration files or dependencies
    // to avoid generating dummy apps for parent poms
    if (hasConfigDir() || !ignoreProject) {

      if (includeReadMe) {
        copyReadMe(appBuildDir);
      }

      if (generateSummaryFile) {
        copySummaryText(appBuildDir);
      }

      if (generateAppPropertiesFile) {
        String name = project.getName();
        if (Strings.isNullOrBlank(name)) {
          name = project.getArtifactId();
        }
        String description = project.getDescription();
        Properties appProperties = new Properties();
        appProperties.put("name", name);
        if (Strings.isNotBlank(description)) {
          appProperties.put("description", description);
        }
        appProperties.put("groupId", project.getGroupId());
        appProperties.put("artifactId", project.getArtifactId());
        appProperties.put("version", project.getVersion());
        File appPropertiesFile = new File(appBuildDir, "fabric8.properties");
        appPropertiesFile.getParentFile().mkdirs();
        if (!appPropertiesFile.exists()) {
          appProperties.store(new FileWriter(appPropertiesFile), "Fabric8 Properties");
        }
      }

      File outputZipFile = getZipFile();
      File legalDir = null;
      if (includeLegal) {
        legalDir = new File(project.getBuild().getOutputDirectory(), "META-INF");
      }
      Zips.createZipFile(getLog(), buildDir, outputZipFile, legalDir);

      projectHelper.attachArtifact(project, artifactType, artifactClassifier, outputZipFile);
      getLog().info("Created app zip file: " + outputZipFile);
    }
  }
 private String messagePrefix(String message) {
   return message + (Strings.isNotBlank(message) ? ". " : "");
 }
Beispiel #17
0
  protected void generateAggregatedZip(
      MavenProject rootProject,
      List<MavenProject> reactorProjects,
      Set<MavenProject> pomZipProjects)
      throws IOException, MojoExecutionException {
    File projectBaseDir = rootProject.getBasedir();
    String rootProjectGroupId = rootProject.getGroupId();
    String rootProjectArtifactId = rootProject.getArtifactId();
    String rootProjectVersion = rootProject.getVersion();

    String aggregatedZipFileName =
        "target/" + rootProjectArtifactId + "-" + rootProjectVersion + "-app.zip";
    File projectOutputFile = new File(projectBaseDir, aggregatedZipFileName);
    getLog()
        .info(
            "Generating "
                + projectOutputFile.getAbsolutePath()
                + " from root project "
                + rootProjectArtifactId);
    File projectBuildDir = new File(projectBaseDir, reactorProjectOutputPath);

    if (projectOutputFile.exists()) {
      projectOutputFile.delete();
    }
    createAggregatedZip(
        projectBaseDir,
        projectBuildDir,
        reactorProjectOutputPath,
        projectOutputFile,
        includeReadMe,
        pomZipProjects);
    if (rootProject.getAttachedArtifacts() != null) {
      // need to remove existing as otherwise we get a WARN
      Artifact found = null;
      for (Artifact artifact : rootProject.getAttachedArtifacts()) {
        if (artifactClassifier != null
            && artifact.hasClassifier()
            && artifact.getClassifier().equals(artifactClassifier)) {
          found = artifact;
          break;
        }
      }
      if (found != null) {
        rootProject.getAttachedArtifacts().remove(found);
      }
    }

    getLog()
        .info(
            "Attaching aggregated zip "
                + projectOutputFile
                + " to root project "
                + rootProject.getArtifactId());
    projectHelper.attachArtifact(rootProject, artifactType, artifactClassifier, projectOutputFile);

    // if we are doing an install goal, then also install the aggregated zip manually
    // as maven will install the root project first, and then build the reactor projects, and at
    // this point
    // it does not help to attach artifact to root project, as those artifacts will not be installed
    // so we need to install manually
    List<String> activeProfileIds = new ArrayList<>();
    List<Profile> activeProfiles = rootProject.getActiveProfiles();
    if (activeProfiles != null) {
      for (Profile profile : activeProfiles) {
        String id = profile.getId();
        if (Strings.isNotBlank(id)) {
          activeProfileIds.add(id);
        }
      }
    }
    if (rootProject.hasLifecyclePhase("install")) {
      getLog().info("Installing aggregated zip " + projectOutputFile);
      InvocationRequest request = new DefaultInvocationRequest();
      request.setBaseDirectory(rootProject.getBasedir());
      request.setPomFile(new File("./pom.xml"));
      request.setGoals(Collections.singletonList("install:install-file"));
      request.setRecursive(false);
      request.setInteractive(false);
      request.setProfiles(activeProfileIds);

      Properties props = new Properties();
      props.setProperty("file", aggregatedZipFileName);
      props.setProperty("groupId", rootProjectGroupId);
      props.setProperty("artifactId", rootProjectArtifactId);
      props.setProperty("version", rootProjectVersion);
      props.setProperty("classifier", "app");
      props.setProperty("packaging", "zip");
      props.setProperty("generatePom", "false");
      request.setProperties(props);

      getLog()
          .info(
              "Installing aggregated zip using: mvn install:install-file"
                  + serializeMvnProperties(props));
      Invoker invoker = new DefaultInvoker();
      try {
        InvocationResult result = invoker.execute(request);
        if (result.getExitCode() != 0) {
          throw new IllegalStateException("Error invoking Maven goal install:install-file");
        }
      } catch (MavenInvocationException e) {
        throw new MojoExecutionException("Error invoking Maven goal install:install-file", e);
      }
    }

    if (rootProject.hasLifecyclePhase("deploy")) {

      if (deploymentRepository == null && Strings.isNullOrBlank(altDeploymentRepository)) {
        String msg =
            "Cannot run deploy phase as Maven project has no <distributionManagement> with the maven url to use for deploying the aggregated zip file, neither an altDeploymentRepository property.";
        getLog().warn(msg);
        throw new MojoExecutionException(msg);
      }

      getLog()
          .info(
              "Deploying aggregated zip "
                  + projectOutputFile
                  + " to root project "
                  + rootProject.getArtifactId());
      getLog()
          .info(
              "Using deploy goal: "
                  + deployFileGoal
                  + " with active profiles: "
                  + activeProfileIds);

      InvocationRequest request = new DefaultInvocationRequest();
      request.setBaseDirectory(rootProject.getBasedir());
      request.setPomFile(new File("./pom.xml"));
      request.setGoals(Collections.singletonList(deployFileGoal));
      request.setRecursive(false);
      request.setInteractive(false);
      request.setProfiles(activeProfileIds);
      request.setProperties(getProject().getProperties());

      Properties props = new Properties();
      props.setProperty("file", aggregatedZipFileName);
      props.setProperty("groupId", rootProjectGroupId);
      props.setProperty("artifactId", rootProjectArtifactId);
      props.setProperty("version", rootProjectVersion);
      props.setProperty("classifier", "app");
      props.setProperty("packaging", "zip");
      String deployUrl = null;

      if (!Strings.isNullOrBlank(deployFileUrl)) {
        deployUrl = deployFileUrl;
      } else if (altDeploymentRepository != null && altDeploymentRepository.contains("::")) {
        deployUrl =
            altDeploymentRepository.substring(altDeploymentRepository.lastIndexOf("::") + 2);
      } else {
        deployUrl = deploymentRepository.getUrl();
      }

      props.setProperty("url", deployUrl);
      props.setProperty("repositoryId", deploymentRepository.getId());
      props.setProperty("generatePom", "false");
      request.setProperties(props);

      getLog()
          .info(
              "Deploying aggregated zip using: mvn deploy:deploy-file"
                  + serializeMvnProperties(props));
      Invoker invoker = new DefaultInvoker();
      try {
        InvocationResult result = invoker.execute(request);
        if (result.getExitCode() != 0) {
          throw new IllegalStateException("Error invoking Maven goal deploy:deploy-file");
        }
      } catch (MavenInvocationException e) {
        throw new MojoExecutionException("Error invoking Maven goal deploy:deploy-file", e);
      }
    }
  }
Beispiel #18
0
  @Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    if (isIgnoreProject()) return;

    try {
      boolean newUserAdded = false;

      fabricServer = mavenSettings.getServer(serverId);

      if (Strings.isNullOrBlank(consoleUrl)) {
        consoleUrl = DEFAULT_CONSOLE_URL;
      }

      // we may have username and password from consoleUrl
      String jolokiaUsername = null;
      String jolokiaPassword = null;
      try {
        URL url = new URL(consoleUrl);
        String s = url.getUserInfo();
        if (Strings.isNotBlank(s) && s.indexOf(':') > 0) {
          int idx = s.indexOf(':');
          jolokiaUsername = s.substring(0, idx);
          jolokiaPassword = s.substring(idx + 1);
        }
      } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Option consoleUrl is invalid due " + e.getMessage());
      }

      // jolokia url overrides username/password configured in maven settings
      if (jolokiaUsername != null) {
        if (fabricServer == null) {
          fabricServer = new Server();
        }
        getLog()
            .info(
                "Using username: "******" and password from provided consoleUrl option");
        fabricServer.setUsername(jolokiaUsername);
        fabricServer.setPassword(jolokiaPassword);
      }

      if (fabricServer == null) {
        boolean create = false;
        if (mavenSettings.isInteractiveMode() && mavenSettingsWriter != null) {
          System.out.println("Maven settings file: " + mavenSettingsFile.getAbsolutePath());
          System.out.println();
          System.out.println();
          System.out.println(
              "There is no <server> section in your ~/.m2/settings.xml file for the server id: "
                  + serverId);
          System.out.println();
          System.out.println(
              "You can enter the username/password now and have the settings.xml updated or you can do this by hand if you prefer.");
          System.out.println();
          while (true) {
            String value =
                readInput("Would you like to update the settings.xml file now? (y/n): ")
                    .toLowerCase();
            if (value.startsWith("n")) {
              System.out.println();
              System.out.println();
              break;
            } else if (value.startsWith("y")) {
              create = true;
              break;
            }
          }
          if (create) {
            System.out.println("Please let us know the login details for this server: " + serverId);
            System.out.println();
            String userName = readInput("Username: "******"Password: "******"Repeat Password: "******"Passwords do not match, please try again.");
              password = readPassword("Password: "******"Repeat Password: "******".backup-" + counter++ + ".xml");
                if (!backupFile.exists()) {
                  System.out.println(
                      "Copied original: "
                          + mavenSettingsFile.getAbsolutePath()
                          + " to: "
                          + backupFile.getAbsolutePath());
                  Files.copy(mavenSettingsFile, backupFile);
                  break;
                }
              }
            }
            Map<String, Object> config = new HashMap<String, Object>();
            mavenSettingsWriter.write(mavenSettingsFile, config, mavenSettings);
            System.out.println("Updated settings file: " + mavenSettingsFile.getAbsolutePath());
            System.out.println();

            newUserAdded = true;
          }
        }
      }
      if (fabricServer == null) {
        String message =
            "No <server> element can be found in ~/.m2/settings.xml for the server <id>"
                + serverId
                + "</id> so we cannot connect to fabric8!\n\n"
                + "Please add the following to your ~/.m2/settings.xml file (using the correct user/password values):\n\n"
                + "<servers>\n"
                + "  <server>\n"
                + "    <id>"
                + serverId
                + "</id>\n"
                + "    <username>admin</username>\n"
                + "    <password>admin</password>\n"
                + "  </server>\n"
                + "</servers>\n";
        getLog().error(message);
        throw new MojoExecutionException(message);
      }

      if (!isIgnoreProject()) {
        uploadAppZip(newUserAdded);
      } else {
        getLog().info("Ignoring this project so not uploading the App Zip");
      }
    } catch (MojoExecutionException e) {
      throw e;
    } catch (Exception e) {
      throw new MojoExecutionException("Error executing", e);
    }
  }
 public void addService(Service service) {
   String id = getId(service);
   if (Strings.isNotBlank(id)) {
     services.put(id, service);
   }
 }
  @Override
  public Result execute(UIExecutionContext context) throws Exception {
    LOG.info("Creating the fabric8.yml file");

    String fileName = ProjectConfigs.FILE_NAME;
    Project project = getSelectedProject(context);
    File configFile = getProjectConfigFile(context.getUIContext(), getSelectedProject(context));
    if (configFile == null) {
      // lets not fail as we typically want to execute SaveDevOpsStep next...
      return Results.success();
    }
    ProjectConfig config = null;
    boolean hasFile = false;
    if (configFile.exists()) {
      config = ProjectConfigs.parseProjectConfig(configFile);
      hasFile = true;
    }
    if (config == null) {
      config = new ProjectConfig();
    }

    CommandHelpers.putComponentValuesInAttributeMap(context, inputComponents);
    updateConfiguration(context, config);
    LOG.info("Result: " + config);

    String message;
    if (config.isEmpty() && !hasFile) {
      message = "No " + fileName + " need be generated as there is no configuration";
      return Results.success(message);
    } else {
      String operation = "Updated";
      if (!configFile.exists()) {
        operation = "Created";
      }
      ProjectConfigs.saveConfig(config, configFile);
      message = operation + " " + fileName;
    }

    // now lets update the devops stuff
    UIContext uiContext = context.getUIContext();
    Map<Object, Object> attributeMap = uiContext.getAttributeMap();

    String gitUrl = getStringAttribute(attributeMap, "gitUrl");
    if (Strings.isNullOrBlank(gitUrl)) {
      gitUrl = getStringAttribute(attributeMap, "gitAddress");
    }

    Object object = attributeMap.get(Project.class);
    String user = getStringAttribute(attributeMap, "gitUser");
    String named = getStringAttribute(attributeMap, "projectName");
    ;
    File basedir = CommandHelpers.getBaseDir(project);
    if (basedir == null && configFile != null) {
      basedir = configFile.getParentFile();
    }

    if (object instanceof Project) {
      Project newProject = (Project) object;
      MetadataFacet facet = newProject.getFacet(MetadataFacet.class);
      if (facet != null) {
        if (Strings.isNullOrBlank(named)) {
          named = facet.getProjectName();
        }
        if (Strings.isNullOrBlank(gitUrl)) {
          String address = getStringAttribute(attributeMap, "gitAddress");
          gitUrl = address + user + "/" + named + ".git";
        }
      } else {
        LOG.error("No MetadataFacet for newly created project " + newProject);
      }
    } else {
      // updating an existing project - so lets try find the git url from the current source code
      if (Strings.isNullOrBlank(gitUrl)) {
        gitUrl = GitHelpers.extractGitUrl(basedir);
      }
      if (basedir != null) {
        if (Strings.isNullOrBlank(named)) {
          named = basedir.getName();
        }
      }
    }
    // lets default the environments from the pipeline
    PipelineDTO pipelineValue = pipeline.getValue();
    LOG.info("Using pipeline " + pipelineValue);
    String buildName = config.getBuildName();
    if (Strings.isNotBlank(buildName)) {
      if (pipelineValue != null) {
        List<String> environments = pipelineValue.getEnvironments();
        if (environments == null) {
          environments = new ArrayList<>();
        }
        LinkedHashMap<String, String> environmentMap = new LinkedHashMap<>();
        if (environments.isEmpty()) {
          environmentMap.put("Current", namespace);
        } else {
          for (String environment : environments) {
            String envNamespace = namespace + "-" + environment.toLowerCase();
            environmentMap.put(environment, envNamespace);
          }
        }
        config.setEnvironments(environmentMap);
      }
    }
    LOG.info("Configured project " + buildName + " environments: " + config.getEnvironments());
    ProjectConfigs.defaultEnvironments(config, namespace);

    String projectName = config.getBuildName();
    if (Strings.isNullOrBlank(projectName)) {
      projectName = named;
      config.setBuildName(projectName);
    }

    LOG.info("Project name is: " + projectName);
    if (Strings.isNotBlank(projectName) && project != null) {
      MavenFacet maven = project.getFacet(MavenFacet.class);
      Model pom = maven.getModel();
      if (pom != null
          && !isFunktionParentPom(project)
          && !isFabric8MavenPlugin3OrGreater(project)) {
        Properties properties = pom.getProperties();
        boolean updated = false;
        updated =
            MavenHelpers.updatePomProperty(
                properties, "fabric8.label.project", projectName, updated);
        updated =
            MavenHelpers.updatePomProperty(
                properties, "fabric8.label.version", "${project.version}", updated);
        if (updated) {
          LOG.info("Updating pom.xml properties!");
          maven.setModel(pom);
        } else {
          LOG.warn("Did not update pom.xml properties!");
        }
      } else {
        LOG.warn("No pom.xml found!");
      }
    }

    Boolean copyFlowToProjectValue = copyPipelineToProject.getValue();
    if (copyFlowToProjectValue != null && copyFlowToProjectValue.booleanValue()) {
      if (basedir == null || !basedir.isDirectory()) {
        LOG.warn("Cannot copy the pipeline to the project as no basedir!");
      } else {
        String flow = null;
        PipelineDTO pipelineDTO = pipelineValue;
        if (pipelineDTO != null) {
          flow = pipelineDTO.getValue();
        }
        if (Strings.isNullOrBlank(flow)) {
          LOG.warn("Cannot copy the pipeline to the project as no pipeline selected!");
        } else {
          String flowText = getFlowContent(flow, uiContext);
          if (Strings.isNullOrBlank(flowText)) {
            LOG.warn(
                "Cannot copy the pipeline to the project as no pipeline text could be loaded!");
          } else {
            flowText = Strings.replaceAllWithoutRegex(flowText, "GIT_URL", "'" + gitUrl + "'");
            File newFile = new File(basedir, ProjectConfigs.LOCAL_FLOW_FILE_NAME);
            Files.writeToFile(newFile, flowText.getBytes());
            LOG.info("Written pipeline to " + newFile);
            if (config != null) {
              config.setPipeline(null);
              config.setUseLocalFlow(true);
            }
          }
        }
      }
    }

    final DevOpsConnector connector = new DevOpsConnector();
    connector.setProjectConfig(config);
    connector.setTryLoadConfigFileFromRemoteGit(false);
    connector.setUsername(user);
    connector.setPassword(getStringAttribute(attributeMap, "gitPassword"));
    connector.setBranch(getStringAttribute(attributeMap, "gitBranch", "master"));
    connector.setBasedir(basedir);
    connector.setGitUrl(gitUrl);
    connector.setRepoName(named);

    connector.setRegisterWebHooks(true);

    // lets not trigger the jenkins webhook yet as the git push should trigger the build
    connector.setTriggerJenkinsJob(false);

    LOG.info("Using connector: " + connector);

    /*
            attributeMap.put("registerWebHooks", new Runnable() {
                @Override
                public void run() {
                    LOG.info("Now registering webhooks!");
                    connector.registerWebHooks();
                }
            });
    */
    try {
      connector.execute();
    } catch (Exception e) {
      LOG.error("Failed to update DevOps resources: " + e, e);
    }

    return Results.success(message);
  }
 public void addPod(Pod pod) {
   String id = getId(pod);
   if (Strings.isNotBlank(id)) {
     pods.put(id, pod);
   }
 }
    public DockerCreateContainerParameters invoke() throws Exception {
      assertValid();

      String containerId = options.getName();
      containerConfig = createContainerConfig(options);

      // allow values to be extracted from the profile configuration
      // such as the image
      Set<String> profileIds = options.getProfiles();
      String versionId = options.getVersion();
      FabricService service = getFabricService();
      Map<String, String> configOverlay = new HashMap<>();
      Map<String, String> ports = null;
      Map<String, String> dockerProviderConfig = new HashMap<>();

      List<Profile> profileOverlays = new ArrayList<>();
      Version version = null;
      if (profileIds != null && versionId != null) {
        ProfileService profileService = service.adapt(ProfileService.class);
        version = profileService.getVersion(versionId);
        if (version != null) {
          for (String profileId : profileIds) {
            Profile profile = version.getRequiredProfile(profileId);
            if (profile != null) {
              Profile overlay = profileService.getOverlayProfile(profile);
              profileOverlays.add(overlay);
              Map<String, String> dockerConfig =
                  overlay.getConfiguration(DockerConstants.DOCKER_PROVIDER_PID);
              if (dockerConfig != null) {
                configOverlay.putAll(dockerConfig);
              }
              if (ports == null || ports.size() == 0) {
                ports = overlay.getConfiguration(Constants.PORTS_PID);
              }
            }
          }
          if (version.hasProfile(DockerConstants.DOCKER_PROVIDER_PROFILE_ID)) {
            Profile profile =
                version.getRequiredProfile(DockerConstants.DOCKER_PROVIDER_PROFILE_ID);
            if (profile != null) {
              Profile overlay = profileService.getOverlayProfile(profile);
              Map<String, String> dockerConfig =
                  overlay.getConfiguration(DockerConstants.DOCKER_PROVIDER_PID);
              if (dockerConfig != null) {
                dockerProviderConfig.putAll(dockerConfig);
              }
            }
          }
        }
      }
      if (ports == null || ports.size() == 0) {
        // lets find the defaults from the docker profile
        if (version == null) {
          version = service.getRequiredDefaultVersion();
        }
        Profile dockerProfile = version.getRequiredProfile("docker");
        ports = dockerProfile.getConfiguration(Constants.PORTS_PID);
        if (ports == null || ports.size() == 0) {
          LOG.warn("Could not a docker ports configuration for: " + Constants.PORTS_PID);
          ports = new HashMap<String, String>();
        }
      }
      LOG.info("Got port configuration: " + ports);

      environmentVariables =
          ChildContainers.getEnvironmentVariables(service, options, DockerConstants.SCHEME);

      DockerProviderConfig configOverlayDockerProvider =
          createDockerProviderConfig(configOverlay, environmentVariables);

      CuratorFramework curatorOptional = getCuratorFramework();
      String image =
          JolokiaAgentHelper.substituteVariableExpression(
              containerConfig.getImage(), environmentVariables, service, curatorOptional, true);

      if (Strings.isNullOrBlank(image)) {
        image = configOverlayDockerProvider.getImage();
        if (Strings.isNullOrBlank(image)) {
          DockerProviderConfig dockerProviderConfigObject =
              createDockerProviderConfig(dockerProviderConfig, environmentVariables);
          image = dockerProviderConfigObject.getImage();
        }
        if (Strings.isNullOrBlank(image)) {
          image = System.getenv(DockerConstants.EnvironmentVariables.FABRIC8_DOCKER_DEFAULT_IMAGE);
        }
        if (Strings.isNullOrBlank(image)) {
          image = DockerConstants.DEFAULT_IMAGE;
        }
        containerConfig.setImage(image);
      }
      containerType = "docker " + image;
      Container container = service.getContainer(containerId);
      if (container != null) {
        container.setType(containerType);
      }

      String[] cmd = containerConfig.getCmd();
      if (cmd == null || cmd.length == 0) {
        String value = configOverlayDockerProvider.getCmd();
        if (Strings.isNullOrBlank(value)) {
          cmd = null;
        } else {
          cmd = new String[] {value};
        }
        containerConfig.setCmd(cmd);
      }

      Map<String, Integer> internalPorts = options.getInternalPorts();
      Map<String, Integer> externalPorts = options.getExternalPorts();

      Map<String, Object> exposedPorts = new HashMap<>();
      Set<Integer> usedPortByHost = findUsedPortByHostAndDocker();
      Map<String, String> emptyMap = new HashMap<>();

      SortedMap<Integer, String> sortedInternalPorts = new TreeMap<>();
      for (Map.Entry<String, String> portEntry : ports.entrySet()) {
        String portName = portEntry.getKey();
        String portText = portEntry.getValue();
        if (portText != null && !Strings.isNullOrBlank(portText)) {
          Integer port = null;
          try {
            port = Integer.parseInt(portText);
          } catch (NumberFormatException e) {
            LOG.warn(
                "Ignoring bad port number for "
                    + portName
                    + " value '"
                    + portText
                    + "' in PID: "
                    + Constants.PORTS_PID);
          }
          if (port != null) {
            sortedInternalPorts.put(port, portName);
            internalPorts.put(portName, port);
            exposedPorts.put(portText + "/tcp", emptyMap);
          } else {
            LOG.info("No port for " + portName);
          }
        }
      }

      String dockerHost = dockerFactory.getDockerHost();
      jolokiaUrl = null;

      Map<String, String> javaContainerConfig =
          Profiles.getOverlayConfiguration(
              service, profileIds, versionId, Constants.JAVA_CONTAINER_PID);
      JavaContainerConfig javaConfig = new JavaContainerConfig();
      getConfigurer().configure(javaContainerConfig, javaConfig);

      boolean isJavaContainer = ChildContainers.isJavaContainer(getFabricService(), options);

      // lets create the ports in sorted order
      for (Map.Entry<Integer, String> entry : sortedInternalPorts.entrySet()) {
        Integer port = entry.getKey();
        String portName = entry.getValue();
        int externalPort = createExternalPort(containerId, portName, usedPortByHost, options);
        externalPorts.put(portName, externalPort);
        environmentVariables.put("FABRIC8_" + portName + "_PORT", "" + port);
        environmentVariables.put("FABRIC8_" + portName + "_PROXY_PORT", "" + externalPort);

        if (portName.equals(JolokiaAgentHelper.JOLOKIA_PORT_NAME)) {
          jolokiaUrl = "http://" + dockerHost + ":" + externalPort + "/jolokia/";
          LOG.info("Found Jolokia URL: " + jolokiaUrl);

          JolokiaAgentHelper.substituteEnvironmentVariables(
              javaConfig,
              environmentVariables,
              isJavaContainer,
              JolokiaAgentHelper.getJolokiaPortOverride(port),
              JolokiaAgentHelper.getJolokiaAgentIdOverride(getFabricService().getEnvironment()));
        } else {
          JolokiaAgentHelper.substituteEnvironmentVariables(
              javaConfig,
              environmentVariables,
              isJavaContainer,
              JolokiaAgentHelper.getJolokiaAgentIdOverride(getFabricService().getEnvironment()));
        }
      }
      javaConfig.updateEnvironmentVariables(environmentVariables, isJavaContainer);

      LOG.info("Passing in manual ip: " + dockerHost);
      environmentVariables.put(EnvironmentVariables.FABRIC8_MANUALIP, dockerHost);
      if (container != null) {
        container.setManualIp(dockerHost);
      }
      if (!environmentVariables.containsKey(EnvironmentVariables.FABRIC8_LISTEN_ADDRESS)) {
        environmentVariables.put(EnvironmentVariables.FABRIC8_LISTEN_ADDRESS, dockerHost);
      }
      environmentVariables.put(EnvironmentVariables.FABRIC8_GLOBAL_RESOLVER, ZkDefs.MANUAL_IP);
      environmentVariables.put(
          EnvironmentVariables.FABRIC8_FABRIC_ENVIRONMENT, DockerConstants.SCHEME);

      // now the environment variables are all set lets see if we need to make a custom image
      String libDir = configOverlayDockerProvider.getJavaLibraryPath();
      String deployDir = configOverlayDockerProvider.getJavaDeployPath();
      String homeDir = configOverlayDockerProvider.getHomePath();
      if (Strings.isNotBlank(libDir) || Strings.isNotBlank(deployDir)) {
        if (container != null) {
          container.setProvisionResult("preparing");
          container.setAlive(true);
        }
        String imageRepository = configOverlayDockerProvider.getImageRepository();
        String entryPoint = configOverlayDockerProvider.getImageEntryPoint();
        List<String> names = new ArrayList<String>(profileIds);
        names.add(versionId);
        String newImageName = "fabric8-" + Strings.join(names, "-").replace('.', '-');
        String customImageUserName = configOverlayDockerProvider.getCustomImageUserName();

        DockerProviderConfig currentContainerDockerProviderConfig = null;
        Container currentContainer = service.getCurrentContainer();
        if (currentContainer != null) {
          Map<String, String> configuration =
              currentContainer
                  .getOverlayProfile()
                  .getConfiguration(DockerConstants.DOCKER_PROVIDER_PID);
          if (configuration != null && !configuration.isEmpty()) {
            Map<String, Map<String, String>> configurations = new HashMap<>();
            configurations.put(DockerConstants.DOCKER_PROVIDER_PID, configuration);
            service.substituteConfigurations(configurations);
            configuration = configurations.get(DockerConstants.DOCKER_PROVIDER_PID);
            configuration =
                JolokiaAgentHelper.substituteEnvironmentVariableExpressionKeysAndValues(
                    configuration, System.getenv());
            currentContainerDockerProviderConfig = createDockerProviderConfig(configuration);
          }
        }
        if (Strings.isNullOrBlank(customImageUserName)
            && currentContainerDockerProviderConfig != null) {
          customImageUserName = currentContainerDockerProviderConfig.getCustomImageUserName();
        }
        Boolean customImagePushValue = configOverlayDockerProvider.getCustomImagePush();
        if (customImagePushValue == null && currentContainerDockerProviderConfig != null) {
          customImagePushValue = currentContainerDockerProviderConfig.getCustomImagePush();
        }
        boolean customImagePush =
            customImagePushValue != null && customImagePushValue.booleanValue();
        if (Strings.isNotBlank(customImageUserName)) {
          newImageName = customImageUserName + "/" + newImageName;
        } else {
          customImagePush = false;
        }

        CustomDockerContainerImageBuilder builder = new CustomDockerContainerImageBuilder();
        CustomDockerContainerImageOptions customDockerContainerImageOptions =
            new CustomDockerContainerImageOptions(
                image,
                imageRepository,
                newImageName,
                libDir,
                deployDir,
                homeDir,
                entryPoint,
                configOverlayDockerProvider.getOverlayFolder(),
                configOverlayDockerProvider.isMavenJavaLibraryPathLayout());

        String actualImage =
            builder.generateContainerImage(
                service,
                container,
                profileOverlays,
                docker,
                customDockerContainerImageOptions,
                options,
                downloadExecutor,
                environmentVariables);
        if (actualImage != null) {
          containerConfig.setImage(actualImage);

          if (customImagePush) {
            LOG.info(
                "Pushing image to repository " + newImageName + " actualImage: " + actualImage);
            Auth authConfig = new Auth();
            authConfig.setEmail("*****@*****.**");
            authConfig.setUsername(service.getZooKeeperUser());
            authConfig.setPassword(service.getZookeeperPassword());
            try {
              docker.imagePush(newImageName, "latest", authConfig);
              LOG.info("Image pushed to repository " + newImageName);
            } catch (Exception e) {
              LOG.info(
                  "Failed to push image " + newImageName + ": " + e + Dockers.dockerErrorMessage(e),
                  e);
              throw e;
            }
          }
        }
      }

      JolokiaAgentHelper.substituteEnvironmentVariableExpressions(
          environmentVariables, environmentVariables, service, curatorOptional, false);

      List<String> env = containerConfig.getEnv();
      if (env == null) {
        env = new ArrayList<>();
      }
      Dockers.addEnvironmentVariablesToList(env, environmentVariables);
      containerConfig.setExposedPorts(exposedPorts);
      containerConfig.setEnv(env);

      String name = options.getName();

      LOG.info(
          "Creating container on docker: "
              + getDockerAddress()
              + " name: "
              + name
              + " env vars: "
              + env);
      LOG.info("Creating container with config: " + containerConfig);
      return this;
    }
Beispiel #23
0
  protected void initGitRepo() throws MojoExecutionException, IOException, GitAPIException {
    buildDir.mkdirs();
    File gitDir = new File(buildDir, ".git");
    if (!gitDir.exists()) {
      String repo = gitUrl;
      if (Strings.isNotBlank(repo)) {
        getLog()
            .info(
                "Cloning git repo "
                    + repo
                    + " into directory "
                    + getGitBuildPathDescription()
                    + " cloneAllBranches: "
                    + cloneAll);
        CloneCommand command =
            Git.cloneRepository()
                .setCloneAllBranches(cloneAll)
                .setURI(repo)
                .setDirectory(buildDir)
                .setRemote(remoteName);
        // .setCredentialsProvider(getCredentials()).
        try {
          git = command.call();
          return;
        } catch (Throwable e) {
          getLog().error("Failed to command remote repo " + repo + " due: " + e.getMessage(), e);
          // lets just use an empty repo instead
        }
      } else {
        InitCommand initCommand = Git.init();
        initCommand.setDirectory(buildDir);
        git = initCommand.call();
        getLog()
            .info("Initialised an empty git configuration repo at " + getGitBuildPathDescription());

        // lets add a dummy file
        File readMe = new File(buildDir, "ReadMe.md");
        getLog().info("Generating " + readMe);
        Files.writeToFile(
            readMe,
            "fabric8 git repository created by fabric8-maven-plugin at " + new Date(),
            Charset.forName("UTF-8"));
        git.add().addFilepattern("ReadMe.md").call();
        commit("Initial commit");
      }
      String branch = git.getRepository().getBranch();
      configureBranch(branch);
    } else {
      getLog().info("Reusing existing git repository at " + getGitBuildPathDescription());

      FileRepositoryBuilder builder = new FileRepositoryBuilder();
      Repository repository =
          builder
              .setGitDir(gitDir)
              .readEnvironment() // scan environment GIT_* variables
              .findGitDir() // scan up the file system tree
              .build();

      git = new Git(repository);
      if (pullOnStartup) {
        doPull();
      } else {
        getLog().info("git pull from remote config repo on startup is disabled");
      }
    }
  }