Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3226 from james-wallis/odo-subdir-0.14.1
Browse files Browse the repository at this point in the history
[0.14.1] Refactor File-watcher projectName, add subdir to location when available
  • Loading branch information
johnmcollier committed Aug 12, 2020
2 parents c60b1fd + 797de34 commit 1966848
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function createProject(req: ICreateProjectParams): Promise<ICreateP
return { "statusCode": 400, "error": { "msg": "projectID, projectType and location are required parameters" }};
}

const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);

// create log storing directory for the project
logger.logInfo("Creating project logs directory");
Expand Down Expand Up @@ -658,7 +658,7 @@ export async function deleteProject(projectID: string): Promise<IDeleteProjectSu

const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
const projectLocation = projectInfo.location;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);

logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
Expand Down Expand Up @@ -842,7 +842,7 @@ export async function projectDeletion(projectID: string): Promise<number> {
const projectMetadata = getProjectMetadataById(projectID);
const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
const projectLocation = projectInfo.location;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);

logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
Expand Down Expand Up @@ -1154,7 +1154,7 @@ export function saveProjectInfo(projectID: string, projectInfo: ProjectInfo, sav
const projectJSON = JSON.stringify(projectInfo);
const infoFile = getProjectMetadataById(projectID).infoFile;
projectInfoCache[infoFile] = projectJSON;
const projectName = projectInfo.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectInfo.location);
logger.logProjectTrace(JSON.stringify(projectInfoCache), projectID);
if (saveIntoJsonFile) {
fs.writeFile(infoFile, projectJSON, "utf8", (err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function getAllProjectTypes(): Array<string> {
async function getExtensionProjectHandler(projectInfo: ProjectInfo): Promise<any> {

const key = projectInfo.projectID;
const projectName = path.basename(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
let handler = extensionProjectHandlers[key];

// is there an extension handler for the project?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ProjectInfo, BuildLog, AppLog, ProjectCapabilities, defaultProjectCapab
import { Validator } from "./Validator";
import { IExtensionProject } from "../extensions/IExtensionProject";
import { IFileChangeEvent } from "../utils/fileChanges";
import { getProjectNameFromPath } from "../utils/utils";

// skeleton for the logs originated from the extension json
const logsOrigin: logHelper.ILogTypes = {
Expand Down Expand Up @@ -207,7 +208,7 @@ export class OdoExtensionProject implements IExtensionProject {
*/
getContainerName = async (projectID: string, projectLocation: string): Promise<string> => {
let podName: string = undefined;
const projectName: string = path.basename(projectLocation);
const projectName: string = getProjectNameFromPath(projectLocation);
const componentName: string = await projectUtil.getComponentName(projectName);
const args: string[] = [
projectLocation,
Expand Down Expand Up @@ -246,7 +247,7 @@ export class OdoExtensionProject implements IExtensionProject {
let appName: string = undefined;
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
const projectLocation = projectInfo.location;
const projectName = path.basename(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);
const args: string[] = [
projectLocation,
"",
Expand Down Expand Up @@ -284,7 +285,7 @@ export class OdoExtensionProject implements IExtensionProject {
let appPort: string = undefined;
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
const projectLocation = projectInfo.location;
const projectName = path.basename(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);
const componentName: string = await projectUtil.getComponentName(projectName);
const args: string[] = [
projectLocation,
Expand Down Expand Up @@ -323,7 +324,7 @@ export class OdoExtensionProject implements IExtensionProject {
let appBaseURL: string = undefined;
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
const projectLocation = projectInfo.location;
const projectName = path.basename(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);
const componentName: string = await projectUtil.getComponentName(projectName);
const args: string[] = [
projectLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fs from "fs-extra";
import * as path from "path";

import * as projectUtil from "./projectUtil";
import { getProjectNameFromPath } from "../utils/utils";
import { Operation } from "./operation";
import { ProjectInfo, BuildLog, AppLog, ProjectCapabilities, defaultProjectCapabilities } from "./Project";
import { Validator } from "./Validator";
Expand Down Expand Up @@ -112,7 +113,7 @@ export class ShellExtensionProject implements IExtensionProject {
private setLanguage = async (projectInfo: ProjectInfo): Promise<void> => {

const logDir = await logHelper.getLogDir(
projectInfo.projectID, path.basename(projectInfo.location));
projectInfo.projectID, getProjectNameFromPath(projectInfo.location));

const args = [
projectInfo.location,
Expand Down
4 changes: 2 additions & 2 deletions src/pfe/file-watcher/server/src/projects/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Validator {
async validateRequiredFiles (requiredFiles: string[]): Promise<void> {
if (requiredFiles) {
const projectID = this.projectID;
const projectName = this.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(this.location);
const OR_SPLIT = "|";

try {
Expand Down Expand Up @@ -179,7 +179,7 @@ export class Validator {
*/
sendResult(): void {
const projectID = this.projectID;
const projectName = this.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(this.location);
logger.logProjectInfo("Sending validation result", projectID, projectName);
io.emitOnListener("projectValidated", this.result());
}
Expand Down
4 changes: 2 additions & 2 deletions src/pfe/file-watcher/server/src/projects/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const validate = async function(args: IProjectActionParams): Promise<{ op
"projectType": projectType,
"location": location
} as ProjectInfo;
const projectName = location.split("/").pop();
const projectName = utils.getProjectNameFromPath(location);
if (args.extensionID) {
projectInfo.extensionID = args.extensionID;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ export const enableautobuild = async function (args: IProjectActionParams): Prom
async function enableAndBuild(projectInfo: ProjectInfo): Promise<void> {
const projectHandler = await projectExtensions.getProjectHandler(projectInfo);
const projectID = projectInfo.projectID;
const projectName = projectInfo.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectInfo.location);

if (projectHandler.hasOwnProperty("setAutoBuild")) {
const operation = new Operation("enableautobuild", projectInfo);
Expand Down
28 changes: 14 additions & 14 deletions src/pfe/file-watcher/server/src/projects/projectUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function containerCreate(operation: Operation, script: string, comm
const event = "projectCreation";
const projectLocation = operation.projectInfo.location;
const projectID = operation.projectInfo.projectID;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectType = operation.projectInfo.projectType;
if (projectList.indexOf(projectID) === -1)
projectList.push(projectID);
Expand Down Expand Up @@ -211,7 +211,7 @@ export async function containerUpdate(operation: Operation, script: string, comm

const projectLocation = operation.projectInfo.location;
const projectID = operation.projectInfo.projectID;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectType = operation.projectInfo.projectType;
logger.logProjectInfo("Updating container for " + operation.projectInfo.projectType + " project " + projectLocation, projectID, projectName);
operation.containerName = await getContainerName(operation.projectInfo);
Expand Down Expand Up @@ -316,7 +316,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
async function executeBuildScript(operation: Operation, script: string, args: Array<string>, event: string): Promise<void> {
const projectID = operation.projectInfo.projectID;
const projectLocation = operation.projectInfo.location;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectInfo = {
operationId: operation.operationId,
projectID: operation.projectInfo.projectID
Expand Down Expand Up @@ -612,7 +612,7 @@ export async function getProjectMavenSettings(projectInfo: ProjectInfo): Promise
export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectLog> {
const projectID = projectInfo.projectID;
const projectLocation = projectInfo.location;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectType = projectInfo.projectType;
const projectLogDir = await logHelper.getLogDir(projectID, projectName);
const logDirectory = path.join(projectConstants.projectsLogDir, projectLogDir);
Expand Down Expand Up @@ -655,7 +655,7 @@ export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectL
export async function containerDelete(projectInfo: ProjectInfo, script: string): Promise<void> {

const projectID = projectInfo.projectID;
const projectName = projectInfo.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectInfo.location);
const containerName = await getContainerName(projectInfo);
const imagePushRegistry = projectInfo.deploymentRegistry;
logger.logProjectInfo("containerDelete: Kill running processes and remove container... ", projectID, projectName);
Expand Down Expand Up @@ -725,7 +725,7 @@ export function getLogName(projectID: string, projectLocation: string): string {
const hash = crypto.createHash("sha1", <TransformOptions>"utf8").update(projectLocation);

let logName = projectConstants.containerPrefix + projectID + "-" + hash.digest("hex");
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);

if (process.env.IN_K8 === "true" && logName.length > 53) {
logName = logName.substring(0, 53);
Expand All @@ -751,7 +751,7 @@ export function getDefaultContainerName(projectID: string, projectLocation: stri
}

// Sanitize project name to ensure project name only supports lower case letter and number
const projectNameOrigin: string = path.basename(projectLocation);
const projectNameOrigin: string = utils.getProjectNameFromPath(projectLocation);
const letterNumber: RegExp = /[A-Za-z0-9]/;
const upperCaseLetter: RegExp = /[A-Z]/;
const defaultProjectName: string = "cw";
Expand Down Expand Up @@ -1249,7 +1249,7 @@ export async function runScript(projectInfo: ProjectInfo, script: string, comman
const containerName = await getContainerName(projectInfo);
const logName = getLogName(projectInfo.projectID, projectInfo.location);
const logDir = await logHelper.getLogDir(projectInfo.projectID, projectInfo.projectName);
const projectName = path.basename(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
let args = [projectInfo.location, LOCAL_WORKSPACE, projectID, command, containerName, String(projectInfo.autoBuildEnabled), logName, projectInfo.startMode,
projectInfo.debugPort, "NONE", logDir];

Expand Down Expand Up @@ -1290,7 +1290,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis

const projectLocation = operation.projectInfo.location;
const projectID = operation.projectInfo.projectID;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);

if (projectList.indexOf(projectID) === -1)
projectList.push(projectID);
Expand Down Expand Up @@ -1446,7 +1446,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis
*/
async function containerBuildAndRun(event: string, buildInfo: BuildRequest, operation: Operation): Promise<void> {
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
const projectName = normalizedProjectLocation.split("/").reverse()[0];
const projectName = utils.getProjectNameFromPath(normalizedProjectLocation);
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
const dockerBuildLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.buildLogs.dockerBuild + logHelper.logExtension);
if (process.env.IN_K8 === "true") {
Expand Down Expand Up @@ -1668,7 +1668,7 @@ async function containerBuildAndRun(event: string, buildInfo: BuildRequest, oper
*/
async function runLocalContainer(buildInfo: BuildRequest): Promise<void> {
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
const projectName = normalizedProjectLocation.split("/").reverse()[0];
const projectName = utils.getProjectNameFromPath(normalizedProjectLocation);
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
const appLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.appLogs.app + logHelper.logExtension);
try {
Expand Down Expand Up @@ -1799,7 +1799,7 @@ export async function isApplicationPodUp(buildInfo: BuildRequest, projectName: s
export async function removeProject(projectInfo: ProjectInfo): Promise<void> {

const projectID = projectInfo.projectID;
const projectName = projectInfo.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectInfo.location);
const containerName = await getContainerName(projectInfo);
logger.logProjectInfo("removeProject: Kill running processes and remove container... ", projectID, projectName);
logger.logProjectInfo("Project ID: " + projectInfo.projectID, projectID, projectName);
Expand Down Expand Up @@ -1903,7 +1903,7 @@ async function getPODInfoAndSendToPortal(operation: Operation, event: string = "
const projectInfo = operation.projectInfo;
const projectLocation = projectInfo.location;
const projectID = projectInfo.projectID;
const projectName = projectLocation.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectLocation);
const keyValuePair: UpdateProjectInfoPair = {
key: "buildRequest",
value: false
Expand Down Expand Up @@ -2203,7 +2203,7 @@ export async function updateDetailedAppStatus(projectID: string, ip: string, por
export async function exposeOverIngress(projectInfo: ProjectInfo, appPort?: number): Promise<void> {
if (process.env.IN_K8) {
const projectID = projectInfo.projectID;
const projectName = projectInfo.location.split("/").pop();
const projectName = utils.getProjectNameFromPath(projectInfo.location);
projectInfo.appBaseURL = await kubeutil.exposeOverIngress(projectID, projectName, projectInfo.isHttps, appPort, projectInfo.appBaseURL);
await projectsController.saveProjectInfo(projectID, projectInfo, true);
}
Expand Down
11 changes: 6 additions & 5 deletions src/pfe/file-watcher/server/src/utils/kubeutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as processManager from "./processManager";
import { ProcessResult } from "./processManager";
import { ProjectInfo } from "../projects/Project";
import * as logHelper from "../projects/logHelper";
import { getProjectNameFromPath } from "./utils";

const k8s = require("@kubernetes/client-node"); // tslint:disable-line:no-require-imports

Expand Down Expand Up @@ -90,13 +91,13 @@ export async function getApplicationContainerInfo(projectInfo: ProjectInfo, oper
if (!projectInfo.compositeAppName) {
return undefined;
}
const componentName = path.basename(projectInfo.location);
const componentName = getProjectNameFromPath(projectInfo.location);
releaseLabel = "deploymentconfig=" + "cw-" + componentName + "-" + projectInfo.compositeAppName;
} else if (projectInfo.projectType == "odo-devfile") {
const componentName = path.basename(projectInfo.location);
const componentName = getProjectNameFromPath(projectInfo.location);
releaseLabel = "component=" + "cw-" + componentName;
}
const projectName = path.basename(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);

// Before deploying the application, we added a release label to the deployment, pod, and service,
// Use that to get the application's pod and service names.
Expand Down Expand Up @@ -127,7 +128,7 @@ export async function getApplicationContainerInfo(projectInfo: ProjectInfo, oper
logger.logProjectInfo("Pod name was not found while looking up service information for project.", projectID, projectName);
}
} catch (err) {
logger.logProjectError("Failed to get the pod name for: " + path.basename(projectLocation), projectID, projectName);
logger.logProjectError("Failed to get the pod name for: " + getProjectNameFromPath(projectLocation), projectID, projectName);
logger.logProjectError(err, projectID, projectName);
}

Expand Down Expand Up @@ -292,7 +293,7 @@ export async function isContainerActive(containerName: string, projectInfo?: Pro
try {
let releaseLabel = "release=" + containerName;
if (projectInfo && ["odo", "odo-devfile"].includes(projectInfo.projectType)) {
const componentName = path.basename(projectInfo.location);
const componentName = getProjectNameFromPath(projectInfo.location);
releaseLabel = projectInfo.projectType === "odo"
? "deploymentconfig=" + "cw-" + componentName + "-" + projectInfo.compositeAppName
: "component=" + "cw-" + componentName;
Expand Down
3 changes: 2 additions & 1 deletion src/pfe/file-watcher/server/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as path from "path";
import { promisify } from "util";
import * as constants from "../projects/constants";
import * as stackTrace from "stack-trace";
import { getProjectNameFromPath } from "./utils";
const chalk = require("chalk"); // tslint:disable-line:no-require-imports

const GENERAL_LOG_FILE_NAME = "Turbine.log";
Expand Down Expand Up @@ -101,7 +102,7 @@ export async function getProjectNameByProjectID(projectID: string): Promise<stri
const data = await readFileAsync(projectDataFile, "utf8");
const projectData = JSON.parse(data);
const location = projectData.location;
const projectName = location.split("/").pop();
const projectName = getProjectNameFromPath(location);
return projectName;
}

Expand Down

0 comments on commit 1966848

Please sign in to comment.