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

Commit 1966848

Browse files
authored
Merge pull request #3226 from james-wallis/odo-subdir-0.14.1
[0.14.1] Refactor File-watcher projectName, add subdir to location when available
2 parents c60b1fd + 797de34 commit 1966848

File tree

16 files changed

+161
-46
lines changed

16 files changed

+161
-46
lines changed

src/pfe/file-watcher/server/src/controllers/projectsController.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export async function createProject(req: ICreateProjectParams): Promise<ICreateP
146146
return { "statusCode": 400, "error": { "msg": "projectID, projectType and location are required parameters" }};
147147
}
148148

149-
const projectName = projectLocation.split("/").pop();
149+
const projectName = utils.getProjectNameFromPath(projectLocation);
150150

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

659659
const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
660660
const projectLocation = projectInfo.location;
661-
const projectName = projectLocation.split("/").pop();
661+
const projectName = utils.getProjectNameFromPath(projectLocation);
662662

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

847847
logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
848848
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
@@ -1154,7 +1154,7 @@ export function saveProjectInfo(projectID: string, projectInfo: ProjectInfo, sav
11541154
const projectJSON = JSON.stringify(projectInfo);
11551155
const infoFile = getProjectMetadataById(projectID).infoFile;
11561156
projectInfoCache[infoFile] = projectJSON;
1157-
const projectName = projectInfo.location.split("/").pop();
1157+
const projectName = utils.getProjectNameFromPath(projectInfo.location);
11581158
logger.logProjectTrace(JSON.stringify(projectInfoCache), projectID);
11591159
if (saveIntoJsonFile) {
11601160
fs.writeFile(infoFile, projectJSON, "utf8", (err) => {

src/pfe/file-watcher/server/src/extensions/projectExtensions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export function getAllProjectTypes(): Array<string> {
164164
async function getExtensionProjectHandler(projectInfo: ProjectInfo): Promise<any> {
165165

166166
const key = projectInfo.projectID;
167-
const projectName = path.basename(projectInfo.location);
167+
const projectName = utils.getProjectNameFromPath(projectInfo.location);
168168
let handler = extensionProjectHandlers[key];
169169

170170
// is there an extension handler for the project?

src/pfe/file-watcher/server/src/projects/OdoExtensionProject.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ProjectInfo, BuildLog, AppLog, ProjectCapabilities, defaultProjectCapab
2222
import { Validator } from "./Validator";
2323
import { IExtensionProject } from "../extensions/IExtensionProject";
2424
import { IFileChangeEvent } from "../utils/fileChanges";
25+
import { getProjectNameFromPath } from "../utils/utils";
2526

2627
// skeleton for the logs originated from the extension json
2728
const logsOrigin: logHelper.ILogTypes = {
@@ -207,7 +208,7 @@ export class OdoExtensionProject implements IExtensionProject {
207208
*/
208209
getContainerName = async (projectID: string, projectLocation: string): Promise<string> => {
209210
let podName: string = undefined;
210-
const projectName: string = path.basename(projectLocation);
211+
const projectName: string = getProjectNameFromPath(projectLocation);
211212
const componentName: string = await projectUtil.getComponentName(projectName);
212213
const args: string[] = [
213214
projectLocation,
@@ -246,7 +247,7 @@ export class OdoExtensionProject implements IExtensionProject {
246247
let appName: string = undefined;
247248
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
248249
const projectLocation = projectInfo.location;
249-
const projectName = path.basename(projectLocation);
250+
const projectName = getProjectNameFromPath(projectLocation);
250251
const args: string[] = [
251252
projectLocation,
252253
"",
@@ -284,7 +285,7 @@ export class OdoExtensionProject implements IExtensionProject {
284285
let appPort: string = undefined;
285286
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
286287
const projectLocation = projectInfo.location;
287-
const projectName = path.basename(projectLocation);
288+
const projectName = getProjectNameFromPath(projectLocation);
288289
const componentName: string = await projectUtil.getComponentName(projectName);
289290
const args: string[] = [
290291
projectLocation,
@@ -323,7 +324,7 @@ export class OdoExtensionProject implements IExtensionProject {
323324
let appBaseURL: string = undefined;
324325
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
325326
const projectLocation = projectInfo.location;
326-
const projectName = path.basename(projectLocation);
327+
const projectName = getProjectNameFromPath(projectLocation);
327328
const componentName: string = await projectUtil.getComponentName(projectName);
328329
const args: string[] = [
329330
projectLocation,

src/pfe/file-watcher/server/src/projects/ShellExtensionProject.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import fs from "fs-extra";
1414
import * as path from "path";
1515

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

114115
const logDir = await logHelper.getLogDir(
115-
projectInfo.projectID, path.basename(projectInfo.location));
116+
projectInfo.projectID, getProjectNameFromPath(projectInfo.location));
116117

117118
const args = [
118119
projectInfo.location,

src/pfe/file-watcher/server/src/projects/Validator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Validator {
5757
async validateRequiredFiles (requiredFiles: string[]): Promise<void> {
5858
if (requiredFiles) {
5959
const projectID = this.projectID;
60-
const projectName = this.location.split("/").pop();
60+
const projectName = utils.getProjectNameFromPath(this.location);
6161
const OR_SPLIT = "|";
6262

6363
try {
@@ -179,7 +179,7 @@ export class Validator {
179179
*/
180180
sendResult(): void {
181181
const projectID = this.projectID;
182-
const projectName = this.location.split("/").pop();
182+
const projectName = utils.getProjectNameFromPath(this.location);
183183
logger.logProjectInfo("Sending validation result", projectID, projectName);
184184
io.emitOnListener("projectValidated", this.result());
185185
}

src/pfe/file-watcher/server/src/projects/actions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const validate = async function(args: IProjectActionParams): Promise<{ op
5757
"projectType": projectType,
5858
"location": location
5959
} as ProjectInfo;
60-
const projectName = location.split("/").pop();
60+
const projectName = utils.getProjectNameFromPath(location);
6161
if (args.extensionID) {
6262
projectInfo.extensionID = args.extensionID;
6363
}
@@ -131,7 +131,7 @@ export const enableautobuild = async function (args: IProjectActionParams): Prom
131131
async function enableAndBuild(projectInfo: ProjectInfo): Promise<void> {
132132
const projectHandler = await projectExtensions.getProjectHandler(projectInfo);
133133
const projectID = projectInfo.projectID;
134-
const projectName = projectInfo.location.split("/").pop();
134+
const projectName = utils.getProjectNameFromPath(projectInfo.location);
135135

136136
if (projectHandler.hasOwnProperty("setAutoBuild")) {
137137
const operation = new Operation("enableautobuild", projectInfo);

src/pfe/file-watcher/server/src/projects/projectUtil.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export async function containerCreate(operation: Operation, script: string, comm
104104
const event = "projectCreation";
105105
const projectLocation = operation.projectInfo.location;
106106
const projectID = operation.projectInfo.projectID;
107-
const projectName = projectLocation.split("/").pop();
107+
const projectName = utils.getProjectNameFromPath(projectLocation);
108108
const projectType = operation.projectInfo.projectType;
109109
if (projectList.indexOf(projectID) === -1)
110110
projectList.push(projectID);
@@ -211,7 +211,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
211211

212212
const projectLocation = operation.projectInfo.location;
213213
const projectID = operation.projectInfo.projectID;
214-
const projectName = projectLocation.split("/").pop();
214+
const projectName = utils.getProjectNameFromPath(projectLocation);
215215
const projectType = operation.projectInfo.projectType;
216216
logger.logProjectInfo("Updating container for " + operation.projectInfo.projectType + " project " + projectLocation, projectID, projectName);
217217
operation.containerName = await getContainerName(operation.projectInfo);
@@ -316,7 +316,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
316316
async function executeBuildScript(operation: Operation, script: string, args: Array<string>, event: string): Promise<void> {
317317
const projectID = operation.projectInfo.projectID;
318318
const projectLocation = operation.projectInfo.location;
319-
const projectName = projectLocation.split("/").pop();
319+
const projectName = utils.getProjectNameFromPath(projectLocation);
320320
const projectInfo = {
321321
operationId: operation.operationId,
322322
projectID: operation.projectInfo.projectID
@@ -612,7 +612,7 @@ export async function getProjectMavenSettings(projectInfo: ProjectInfo): Promise
612612
export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectLog> {
613613
const projectID = projectInfo.projectID;
614614
const projectLocation = projectInfo.location;
615-
const projectName = projectLocation.split("/").pop();
615+
const projectName = utils.getProjectNameFromPath(projectLocation);
616616
const projectType = projectInfo.projectType;
617617
const projectLogDir = await logHelper.getLogDir(projectID, projectName);
618618
const logDirectory = path.join(projectConstants.projectsLogDir, projectLogDir);
@@ -655,7 +655,7 @@ export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectL
655655
export async function containerDelete(projectInfo: ProjectInfo, script: string): Promise<void> {
656656

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

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

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

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

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

12911291
const projectLocation = operation.projectInfo.location;
12921292
const projectID = operation.projectInfo.projectID;
1293-
const projectName = projectLocation.split("/").pop();
1293+
const projectName = utils.getProjectNameFromPath(projectLocation);
12941294

12951295
if (projectList.indexOf(projectID) === -1)
12961296
projectList.push(projectID);
@@ -1446,7 +1446,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis
14461446
*/
14471447
async function containerBuildAndRun(event: string, buildInfo: BuildRequest, operation: Operation): Promise<void> {
14481448
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
1449-
const projectName = normalizedProjectLocation.split("/").reverse()[0];
1449+
const projectName = utils.getProjectNameFromPath(normalizedProjectLocation);
14501450
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
14511451
const dockerBuildLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.buildLogs.dockerBuild + logHelper.logExtension);
14521452
if (process.env.IN_K8 === "true") {
@@ -1668,7 +1668,7 @@ async function containerBuildAndRun(event: string, buildInfo: BuildRequest, oper
16681668
*/
16691669
async function runLocalContainer(buildInfo: BuildRequest): Promise<void> {
16701670
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
1671-
const projectName = normalizedProjectLocation.split("/").reverse()[0];
1671+
const projectName = utils.getProjectNameFromPath(normalizedProjectLocation);
16721672
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
16731673
const appLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.appLogs.app + logHelper.logExtension);
16741674
try {
@@ -1799,7 +1799,7 @@ export async function isApplicationPodUp(buildInfo: BuildRequest, projectName: s
17991799
export async function removeProject(projectInfo: ProjectInfo): Promise<void> {
18001800

18011801
const projectID = projectInfo.projectID;
1802-
const projectName = projectInfo.location.split("/").pop();
1802+
const projectName = utils.getProjectNameFromPath(projectInfo.location);
18031803
const containerName = await getContainerName(projectInfo);
18041804
logger.logProjectInfo("removeProject: Kill running processes and remove container... ", projectID, projectName);
18051805
logger.logProjectInfo("Project ID: " + projectInfo.projectID, projectID, projectName);
@@ -1903,7 +1903,7 @@ async function getPODInfoAndSendToPortal(operation: Operation, event: string = "
19031903
const projectInfo = operation.projectInfo;
19041904
const projectLocation = projectInfo.location;
19051905
const projectID = projectInfo.projectID;
1906-
const projectName = projectLocation.split("/").pop();
1906+
const projectName = utils.getProjectNameFromPath(projectLocation);
19071907
const keyValuePair: UpdateProjectInfoPair = {
19081908
key: "buildRequest",
19091909
value: false
@@ -2203,7 +2203,7 @@ export async function updateDetailedAppStatus(projectID: string, ip: string, por
22032203
export async function exposeOverIngress(projectInfo: ProjectInfo, appPort?: number): Promise<void> {
22042204
if (process.env.IN_K8) {
22052205
const projectID = projectInfo.projectID;
2206-
const projectName = projectInfo.location.split("/").pop();
2206+
const projectName = utils.getProjectNameFromPath(projectInfo.location);
22072207
projectInfo.appBaseURL = await kubeutil.exposeOverIngress(projectID, projectName, projectInfo.isHttps, appPort, projectInfo.appBaseURL);
22082208
await projectsController.saveProjectInfo(projectID, projectInfo, true);
22092209
}

src/pfe/file-watcher/server/src/utils/kubeutil.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as processManager from "./processManager";
2121
import { ProcessResult } from "./processManager";
2222
import { ProjectInfo } from "../projects/Project";
2323
import * as logHelper from "../projects/logHelper";
24+
import { getProjectNameFromPath } from "./utils";
2425

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

@@ -90,13 +91,13 @@ export async function getApplicationContainerInfo(projectInfo: ProjectInfo, oper
9091
if (!projectInfo.compositeAppName) {
9192
return undefined;
9293
}
93-
const componentName = path.basename(projectInfo.location);
94+
const componentName = getProjectNameFromPath(projectInfo.location);
9495
releaseLabel = "deploymentconfig=" + "cw-" + componentName + "-" + projectInfo.compositeAppName;
9596
} else if (projectInfo.projectType == "odo-devfile") {
96-
const componentName = path.basename(projectInfo.location);
97+
const componentName = getProjectNameFromPath(projectInfo.location);
9798
releaseLabel = "component=" + "cw-" + componentName;
9899
}
99-
const projectName = path.basename(projectLocation);
100+
const projectName = getProjectNameFromPath(projectLocation);
100101

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

@@ -292,7 +293,7 @@ export async function isContainerActive(containerName: string, projectInfo?: Pro
292293
try {
293294
let releaseLabel = "release=" + containerName;
294295
if (projectInfo && ["odo", "odo-devfile"].includes(projectInfo.projectType)) {
295-
const componentName = path.basename(projectInfo.location);
296+
const componentName = getProjectNameFromPath(projectInfo.location);
296297
releaseLabel = projectInfo.projectType === "odo"
297298
? "deploymentconfig=" + "cw-" + componentName + "-" + projectInfo.compositeAppName
298299
: "component=" + "cw-" + componentName;

src/pfe/file-watcher/server/src/utils/logger.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as path from "path";
1515
import { promisify } from "util";
1616
import * as constants from "../projects/constants";
1717
import * as stackTrace from "stack-trace";
18+
import { getProjectNameFromPath } from "./utils";
1819
const chalk = require("chalk"); // tslint:disable-line:no-require-imports
1920

2021
const GENERAL_LOG_FILE_NAME = "Turbine.log";
@@ -101,7 +102,7 @@ export async function getProjectNameByProjectID(projectID: string): Promise<stri
101102
const data = await readFileAsync(projectDataFile, "utf8");
102103
const projectData = JSON.parse(data);
103104
const location = projectData.location;
104-
const projectName = location.split("/").pop();
105+
const projectName = getProjectNameFromPath(location);
105106
return projectName;
106107
}
107108

0 commit comments

Comments
 (0)