Skip to content

Commit fec12a7

Browse files
fix: configurable db setup and users for workdir
1 parent 67ff3b3 commit fec12a7

File tree

13 files changed

+96
-46
lines changed

13 files changed

+96
-46
lines changed

.github/workflows/app-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ jobs:
116116
context: ./app
117117
# Note: tags has to be all lower-case
118118
tags: ${{ env.REGISTRY }}/${{ github.repository }}/app:${{ github.run_id }}
119-
# ${{ steps.md.metadata.outputs.tags }}
120-
labels: ${{ steps.md.metadata.outputs.labels }}
119+
# ${{ steps.metadata.outputs.tags }}
120+
labels: ${{ steps.metadata.outputs.labels }}
121121
# build on feature branches, push only on main branch
122122
push: true
123123
# - uses: goodwithtech/dockle-action@main

.github/workflows/core-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ jobs:
115115
with:
116116
# relative path to the place where source code with Dockerfile is located
117117
context: ./core
118-
# Note: tags has to be all lower-case
118+
# Note: tags have to be all lower-case
119119
tags: ${{ env.REGISTRY }}/${{ github.repository }}/core:${{ github.run_id }}
120-
# ${{ steps.md.metadata.outputs.tags }}
121-
labels: ${{ steps.md.metadata.outputs.labels }}
122-
# build on feature branches, push only on main branch
120+
# ${{ steps.metadata.outputs.tags }}
121+
labels: ${{ steps.metadata.outputs.labels }}
122+
# build on feature branches, push only on the main branch
123123
push: true
124124
# - uses: goodwithtech/dockle-action@main
125125
# with:

app/Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ RUN apk update && apk add git
1616
COPY --from=builder /app/dist /server/dist
1717
COPY --from=builder /app/node_modules /server/node_modules
1818
COPY --from=builder /app/protobufs /server/protobufs
19-
RUN mkdir -p /compage/workdir
20-
RUN chmod -R 777 /compage/workdir
19+
### Add new user
20+
RUN adduser -D -g '' compageuser
21+
RUN mkdir -p $HOME/.compage/workdir
22+
RUN chmod -R 777 $HOME/.compage/workdir
23+
USER compageuser
2124
ENV NODE_ENV=production
2225
EXPOSE 5000
2326
CMD [ "node", "dist/src/app.js" ]

app/README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
# app
2-
- This is a component which connects to different git-platforms and push-pull repositories.
3-
- This also uses Cassandra or SQLite (based on configuration) databases to persist projects, gitplatforms and users related details.
4-
- It's an Express.js based REST server and gRPC client to core component.
2+
- This is a component that connects to different git-platforms and push-pull repositories.
3+
- This also uses Cassandra or SQLite (based on configuration) databases to persist projects, git platforms, and users related details.
4+
- It's an Express.js based REST server and gRPC client to a core component.
5+
- The sqlite is used as a database for development. To use cassandra, you need to set the following environment variables in your terminal along with a `environment DB_TYPE='cassandra'`.
6+
```bash
7+
export CASSANDRA_CONTACT_POINTS = 'localhost';
8+
export CASSANDRA_LOCAL_DATA_CENTER = 'datacenter1';
9+
export CASSANDRA_KEYSPACE = 'compage';
10+
export CASSANDRA_USERNAME = 'cassandra';
11+
export CASSANDRA_PASSWORD = 'cassandra';
12+
```
13+
- To start the cassandra server on local, you can fire command `docker-compose up -d` from the setup directory of compage/app.
514

615
#### How to run this component?
716
- Navigate to app directory [`cd app`] from root directory of compage
817
- Fire `npm install` to install the dependencies
918
- Run `npm run dev` command to start the express-server. This command will auto-reload the changes you make to app directory.
10-
- Kill process on port 5000
19+
- If something goes wrong, you want to kill the process on port 5000, run the following command
1120
```sudo kill -9 `sudo lsof -t -i:5000```

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app",
3-
"version": "0.0.1",
3+
"version": "1.0.0",
44
"main": "app.ts",
55
"repository": "https://github.com/intelops/compage.git",
66
"author": "Mahendra <[email protected]>",

app/src/integrations/git-platforms/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {GitPlatformEntity} from '../../models/gitPlatform';
55
import {NewProjectGitServerRequest} from '../simple-git/models';
66
import {pushNewProjectToGitServer} from '../simple-git/newProject';
77
import {GitPlatformService} from '../../services/gitPlatformService';
8+
import {WORKDIR_PATH} from '../../utils/constants';
89

910
const gitPlatformService = new GitPlatformService();
1011

@@ -23,7 +24,7 @@ export const createRepository = async (projectEntity: ProjectEntity) => {
2324
};
2425

2526
export const makeInitialCommit = async (projectEntity: ProjectEntity) => {
26-
const createdProjectPath = `/compage/workdir/workdir/${projectEntity.id}`;
27+
const createdProjectPath = `${WORKDIR_PATH}/${projectEntity.id}`;
2728
fs.mkdirSync(createdProjectPath, {recursive: true});
2829
fs.writeFileSync(`${createdProjectPath}/README.md`, `# ${projectEntity.repository_name}`);
2930
const gitPlatform: GitPlatformEntity = await gitPlatformService.getGitPlatform(projectEntity.owner_email, projectEntity.git_platform_name);
@@ -45,6 +46,7 @@ export const makeInitialCommit = async (projectEntity: ProjectEntity) => {
4546
if (error.length !== 0) {
4647
throw new Error(error);
4748
}
49+
// TODO: uncomment this line
4850
// delete the project from temp folder once it's pushed to git server.
4951
// fs.rmSync(createdProjectPath, {recursive: true});
5052
};

app/src/integrations/simple-git/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const gitOperations = async (git: SimpleGit, repositoryBranch?: string, p
3737

3838
let branchName: string = '';
3939
if (!repositoryBranch && !projectVersion) {
40-
branchName = 'master';
40+
branchName = 'main';
4141
} else {
4242
branchName = repositoryBranch + '-' + projectVersion;
4343
}

app/src/integrations/simple-git/newProject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Logger from '../../utils/logger';
44
import {NewProjectGitServerRequest} from './models';
55

66
export const pushNewProjectToGitServer = async (newProjectGitServerRequest: NewProjectGitServerRequest): Promise<string> => {
7+
console.log('pushNewProjectToGitServer' + newProjectGitServerRequest.generatedProjectPath);
78
const options: Partial<SimpleGitOptions> = {
89
baseDir: newProjectGitServerRequest.generatedProjectPath,
910
binary: 'git',
@@ -27,7 +28,6 @@ export const pushNewProjectToGitServer = async (newProjectGitServerRequest: NewP
2728
if (error.length > 0) {
2829
return error;
2930
}
30-
Logger.debug('git init done');
3131

3232
// add local git config like username and email
3333
await git.addConfig('user.email', newProjectGitServerRequest.gitProviderDetails.platformEmail);

app/src/routes/codeOperations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {requireEmailMiddleware} from '../middlewares/auth';
1010
import Logger from '../utils/logger';
1111
import {rimraf} from 'rimraf';
1212
import tar from 'tar';
13-
import {X_EMAIL_HEADER} from '../utils/constants';
13+
import {WORKDIR_PATH, X_EMAIL_HEADER} from '../utils/constants';
1414
import {GenerateCodeRequest, getGenerateCodeError, getGenerateCodeResponse, Project} from '../models/code';
1515
import {GitPlatformEntity} from '../models/gitPlatform';
1616
import {Metadata, OldVersion, ProjectEntity} from '../models/project';
@@ -87,7 +87,7 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
8787
}
8888

8989
// create directory hierarchy here itself as creating it after receiving data will not be proper.
90-
const originalProjectPath = `/compage/workdir/${projectEntity.display_name}`;
90+
const originalProjectPath = `${WORKDIR_PATH}/${projectEntity.display_name}`;
9191
// this is a path where the project will be downloaded
9292
const downloadedProjectPath = `${originalProjectPath}_downloaded`;
9393
// this is a path where the project will be cloned

app/src/utils/constants.ts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import os from 'os';
88
export const X_EMAIL_HEADER = 'X-Email-ID';
99
export const DEVELOPMENT = 'development';
1010
export const TEST = 'test';
11+
export const CONFIG_PATH = `${os.homedir()}/.compage`;
12+
export const WORKDIR_PATH = `${CONFIG_PATH}/workdir`;
1113

14+
// read secret files from the given path - happens only in production [not in development or test] in containers
1215
const readSecretFile = (secretPath: string) => {
1316
const keyValuePairs: Map<string, string> = new Map();
1417
const entries = fs.readdirSync(secretPath, {withFileTypes: true});
@@ -20,6 +23,7 @@ const readSecretFile = (secretPath: string) => {
2023
return keyValuePairs;
2124
};
2225

26+
// set NODE_ENV to development if not set
2327
if (!process.env.NODE_ENV) {
2428
process.env.NODE_ENV = DEVELOPMENT;
2529
}
@@ -31,25 +35,41 @@ let config: Config;
3135
if (isDevelopment) {
3236
// use verbose logging
3337
// use server in development mode
34-
dotenv.config({path: `${os.homedir()}/.compage/.env`});
35-
assert.ok(process.env.COMPAGE_CORE_URL, `The 'COMPAGE_CORE_URL' environment variable is required`);
36-
config = {
37-
// db config
38-
db: {
38+
dotenv.config({path: `${CONFIG_PATH}/.env`});
39+
let db: DbConfig;
40+
if (process.env.DB_TYPE === 'cassandra') {
41+
db = {
42+
cassandra: {
43+
contactPoints: process.env.CASSANDRA_CONTACT_POINTS?.split(','),
44+
localDataCenter: process.env.CASSANDRA_LOCAL_DATA_CENTER,
45+
keyspace: process.env.CASSANDRA_KEYSPACE,
46+
credentials: {
47+
username: process.env.CASSANDRA_USERNAME,
48+
password: process.env.CASSANDRA_PASSWORD
49+
}
50+
}
51+
};
52+
} else {
53+
db = {
3954
sqlite: {
4055
storage: 'db.sqlite'
4156
}
42-
},
57+
};
58+
}
59+
assert.ok(process.env.COMPAGE_CORE_URL, `The 'COMPAGE_CORE_URL' environment variable is required`);
60+
config = {
61+
// db config
62+
db,
4363
// app server config
4464
serverPort: process.env.PORT || 5000,
45-
// core url
65+
// compage-core url
4666
compageCoreUrl: process.env.COMPAGE_CORE_URL,
4767
};
4868
} else if (isTest) {
4969
config = {
5070
// app server config
5171
serverPort: 5000,
52-
// db config
72+
// db config for testing is sqlite
5373
db: {
5474
sqlite: {
5575
storage: 'db.sqlite'
@@ -101,22 +121,30 @@ if (isDevelopment) {
101121
};
102122
}
103123

124+
export interface CassandraDBConfig {
125+
contactPoints?: string[];
126+
localDataCenter?: string;
127+
keyspace?: string;
128+
credentials?: {
129+
username?: string;
130+
password?: string;
131+
};
132+
}
133+
134+
export interface SqliteDBConfig {
135+
storage: string;
136+
}
137+
138+
export interface DbConfig {
139+
cassandra?: CassandraDBConfig;
140+
sqlite?: SqliteDBConfig;
141+
}
142+
104143
export interface Config {
105144
// app server config
106145
serverPort?: string | number;
107-
// cassandra config
108-
db?: {
109-
cassandra?: {
110-
contactPoints?: string[];
111-
localDataCenter?: string;
112-
keyspace?: string;
113-
credentials?: {
114-
username: string;
115-
password: string;
116-
}
117-
},
118-
sqlite?: {}
119-
};
146+
// db config
147+
db?: DbConfig;
120148
// core url
121149
compageCoreUrl?: string;
122150
}

0 commit comments

Comments
 (0)