private static String getReadMeFileKey(String relativePath) { String answer = relativePath; if (Strings.isNullOrBlank(answer)) { return "<root>"; } // remove leading path which can be either unix or windows style int pos = relativePath.indexOf('/'); int pos2 = relativePath.indexOf('\\'); if (pos > 0 && pos2 > 0) { pos = Math.max(pos, pos2); } else if (pos2 > 0) { pos = pos2; } if (pos > -1) { answer = relativePath.substring(pos); } // and remove any leading path separators answer = Files.stripLeadingSeparator(answer); if (Strings.isNullOrBlank(answer)) { answer = "<root>"; } return answer; }
@Override public void execute() throws MojoExecutionException, MojoFailureException { try { if (!project.isExecutionRoot()) { getLog().info("Not the execution root so ignoring this project"); return; } buildDir.mkdirs(); for (MavenProject reactorProject : reactorProjects) { // ignore the execution root which just aggregates stuff if (!reactorProject.isExecutionRoot()) { combineProfilesTo(reactorProject, buildDir); } } Zips.createZipFile(getLog(), buildDir, outputFile); projectHelper.attachArtifact(project, artifactType, artifactClassifier, outputFile); String relativePath = Files.getRelativePath(project.getBasedir(), outputFile); while (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } getLog().info("Created profile zip file: " + relativePath); } catch (MojoExecutionException e) { throw e; } catch (Exception e) { throw new MojoExecutionException("Error executing", e); } }
protected static String getChildProjectRelativePath(File projectBaseDir, MavenProject pomProject) throws IOException { // must include first dir as prefix String root = projectBaseDir.getName(); String relativePath = Files.getRelativePath(projectBaseDir, pomProject.getBasedir()); relativePath = root + File.separator + relativePath; return relativePath; }
@Override public File call() throws Exception { File download = download(path); if (download != null) { File tmpFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath()); Files.copy(download, tmpFile); return tmpFile; } else { return null; } }
/** Combines any files from the appSourceDir into the output directory */ public static void appendAppConfigFiles(File appSourceDir, File outputDir) throws IOException { if (appSourceDir.exists() && appSourceDir.isDirectory()) { File[] files = appSourceDir.listFiles(); if (files != null) { outputDir.mkdirs(); for (File file : files) { File outFile = new File(outputDir, file.getName()); if (file.isDirectory()) { appendAppConfigFiles(file, outFile); } else { if (outFile.exists() && file.getName().endsWith(".properties")) { System.out.println("Combining properties: file " + file.getAbsolutePath()); combinePropertiesFiles(file, outFile); } else { System.out.println("Copying file " + file.getAbsolutePath()); Files.copy(file, outFile); } } } } } }
protected Iterable<PipelineDTO> getPipelines(UIContext context, boolean filterPipelines) { Set<String> builders = null; ProjectOverviewDTO projectOveriew = null; if (filterPipelines) { projectOveriew = getProjectOverview(context); builders = projectOveriew.getBuilders(); } File dir = getJenkinsWorkflowFolder(context); Set<String> buildersFound = new HashSet<>(); if (dir != null) { Filter<File> filter = new Filter<File>() { @Override public boolean matches(File file) { return file.isFile() && Objects.equal(JENKINSFILE, file.getName()); } }; Set<File> files = Files.findRecursive(dir, filter); List<PipelineDTO> pipelines = new ArrayList<>(); for (File file : files) { try { String relativePath = Files.getRelativePath(dir, file); String value = Strings.stripPrefix(relativePath, "/"); String label = value; String postfix = "/" + JENKINSFILE; if (label.endsWith(postfix)) { label = label.substring(0, label.length() - postfix.length()); } if (label.startsWith(jenkinsFilePrefix)) { label = label.substring(jenkinsFilePrefix.length()); } // Lets ignore the fabric8 specific pipelines if (label.startsWith("fabric8-release/")) { continue; } String builder = null; int idx = label.indexOf("/"); if (idx > 0) { builder = label.substring(0, idx); if (filterPipelines && !builders.contains(builder)) { // ignore this builder continue; } else { buildersFound.add(builder); } } String descriptionMarkdown = null; File markdownFile = new File(file.getParentFile(), "ReadMe.md"); if (Files.isFile(markdownFile)) { descriptionMarkdown = IOHelpers.readFully(markdownFile); } PipelineDTO pipeline = new PipelineDTO(value, label, builder, descriptionMarkdown); File yamlFile = new File(file.getParentFile(), "metadata.yml"); if (Files.isFile(yamlFile)) { PipelineMetadata metadata = null; try { metadata = loadYaml(yamlFile, PipelineMetadata.class); } catch (IOException e) { LOG.warn("Failed to parse yaml file " + yamlFile + ". " + e, e); } if (metadata != null) { metadata.configurePipeline(pipeline); } } pipelines.add(pipeline); } catch (IOException e) { LOG.warn( "Failed to find relative path for folder " + dir + " and file " + file + ". " + e, e); } } if (buildersFound.size() == 1) { // lets trim the builder prefix from the labels for (String first : buildersFound) { String prefix = first + "/"; for (PipelineDTO pipeline : pipelines) { String label = pipeline.getLabel(); if (label.startsWith(prefix)) { label = label.substring(prefix.length()); pipeline.setLabel(label); } } break; } } Collections.sort(pipelines); return pipelines; } else { LOG.warn("No jenkinsWorkflowFolder!"); return new ArrayList<>(); } }
@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); }
@Override public void initializeUI(UIBuilder builder) throws Exception { final UIContext context = builder.getUIContext(); copyPipelineToProject.setValue(Boolean.TRUE); pipeline.setCompleter( new UICompleter<PipelineDTO>() { @Override public Iterable<PipelineDTO> getCompletionProposals( UIContext context, InputComponent<?, PipelineDTO> input, String value) { return getPipelines(context, true); } }); pipeline.setValueConverter( new Converter<String, PipelineDTO>() { @Override public PipelineDTO convert(String text) { return getPipelineForValue(context, text); } }); pipeline.addValueChangeListener( new ValueChangeListener() { @Override public void valueChanged(ValueChangeEvent event) { String value = event.getNewValue() != null ? event.getNewValue().toString() : null; if (value != null) { String description = getDescriptionForFlow(value); pipeline.setNote(description != null ? description : ""); } else { pipeline.setNote(""); } boolean canCopy = Strings.isNotBlank(value); copyPipelineToProject.setEnabled(canCopy); } }); if (getCurrentSelectedProject(context) != null) { PipelineDTO defaultValue = getPipelineForValue(context, DEFAULT_MAVEN_FLOW); if (defaultValue != null) { pipeline.setDefaultValue(defaultValue); pipeline.setValue(defaultValue); } } chatRoom.setCompleter( new UICompleter<String>() { @Override public Iterable<String> getCompletionProposals( UIContext context, InputComponent<?, String> input, String value) { return filterCompletions(getChatRoomNames(), value); } }); chatRoom.addValueChangeListener( new ValueChangeListener() { @Override public void valueChanged(ValueChangeEvent event) { String value = event.getNewValue() != null ? event.getNewValue().toString() : null; if (value != null) { String description = getDescriptionForChatRoom(value); chatRoom.setNote(description != null ? description : ""); } else { chatRoom.setNote(""); } } }); issueProjectName.setCompleter( new UICompleter<String>() { @Override public Iterable<String> getCompletionProposals( UIContext context, InputComponent<?, String> input, String value) { return filterCompletions(getIssueProjectNames(), value); } }); issueProjectName.addValueChangeListener( new ValueChangeListener() { @Override public void valueChanged(ValueChangeEvent event) { String value = event.getNewValue() != null ? event.getNewValue().toString() : null; if (value != null) { String description = getDescriptionForIssueProject(value); issueProjectName.setNote(description != null ? description : ""); } else { issueProjectName.setNote(""); } } }); // lets initialise the data from the current config if it exists ProjectConfig config = null; Project project = getCurrentSelectedProject(context); File configFile = getProjectConfigFile(context, getSelectedProject(context)); if (configFile != null && configFile.exists()) { config = ProjectConfigs.parseProjectConfig(configFile); } if (config != null) { PipelineDTO flow = getPipelineForValue(context, config.getPipeline()); if (flow != null) { CommandHelpers.setInitialComponentValue(this.pipeline, flow); } CommandHelpers.setInitialComponentValue(chatRoom, config.getChatRoom()); CommandHelpers.setInitialComponentValue(issueProjectName, config.getIssueProjectName()); CommandHelpers.setInitialComponentValue(codeReview, config.getCodeReview()); } inputComponents = new ArrayList<>(); File jenkinsFile = CommandHelpers.getProjectContextFile(context, project, "Jenkinsfile"); boolean hasJenkinsFile = Files.isFile(jenkinsFile); LOG.debug("Has Jenkinsfile " + hasJenkinsFile + " with file: " + jenkinsFile); if (!hasJenkinsFile) { inputComponents.addAll( CommandHelpers.addInputComponents(builder, pipeline, copyPipelineToProject)); } inputComponents.addAll( CommandHelpers.addInputComponents(builder, chatRoom, issueProjectName, codeReview)); }
@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); } }
protected void createAggregatedZip( File projectBaseDir, File projectBuildDir, String reactorProjectOutputPath, File projectOutputFile, boolean includeReadMe, Set<MavenProject> pomZipProjects) throws IOException { projectBuildDir.mkdirs(); for (MavenProject reactorProject : pomZipProjects) { // ignoreProject the execution root which just aggregates stuff if (!reactorProject.isExecutionRoot()) { Log log = getLog(); // TODO allow the project nesting to be defined via a property? String relativePath = getChildProjectRelativePath(projectBaseDir, reactorProject); File outDir = new File(projectBuildDir, relativePath); combineAppFilesToFolder(reactorProject, outDir, log, reactorProjectOutputPath); } } // we may want to include readme files for pom projects if (includeReadMe) { Map<String, File> pomNames = new HashMap<String, File>(); for (MavenProject pomProject : pomZipProjects) { File src = pomProject.getFile().getParentFile(); String relativePath = getChildProjectRelativePath(projectBaseDir, pomProject); File outDir = new File(projectBuildDir, relativePath); File copiedFile = copyReadMe(src, outDir); if (copiedFile != null) { String key = getReadMeFileKey(relativePath); pomNames.put(key, copiedFile); } } if (replaceReadmeLinksPrefix != null) { // now parse each readme file and replace github links for (Map.Entry<String, File> entry : pomNames.entrySet()) { File file = entry.getValue(); String key = entry.getKey(); boolean changed = false; List<String> lines = Files.readLines(file); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); String newLine = replaceGithubLinks(pomNames.keySet(), key, line); if (newLine != null) { lines.set(i, newLine); changed = true; } } if (changed) { Files.writeLines(file, lines); getLog().info("Replaced github links to fabric apps in reaadme file: " + file); } } } } Zips.createZipFile(getLog(), projectBuildDir, projectOutputFile, null); String relativePath = Files.getRelativePath(projectBaseDir, projectOutputFile); while (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } getLog().info("Created app zip file: " + relativePath); }
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); } }
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"); } } }