Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
cf6b853
Add cron schedule to env.example
Xemka Jan 13, 2021
dc9d2c9
Update yarn.lock
Xemka Jan 13, 2021
8aa8aaa
Add cron schedule listener
Xemka Jan 13, 2021
6f17fd1
Add update message to types
Xemka Jan 13, 2021
456202e
Update package.json
Xemka Jan 13, 2021
6a87e1a
Add service status to types
Xemka Jan 13, 2021
d522262
Add update message to outgoing messages
Xemka Jan 13, 2021
e77b00e
Fix type of services
Xemka Jan 13, 2021
b9edd24
Added a scheduled launch to index.ts
Xemka Jan 13, 2021
befa352
Added statuses reader
Xemka Jan 13, 2021
1cd6462
Refactor statuses.ts
Xemka Jan 18, 2021
17d9318
Added a file for searching one workspace
Xemka Jan 18, 2021
94ecadb
Merge branch 'main' of https://github.com/codex-team/devops-toolbox-a…
Xemka Jan 21, 2021
12ad87d
Refactoring
Xemka Jan 26, 2021
a690d43
Add axios to package.json
Xemka Jan 26, 2021
ffcf8c4
Refactor statuses.ts
Xemka Jan 26, 2021
af9773a
Added services aggregation type
Xemka Jan 29, 2021
e78f88b
Added use of mongodb aggregations in statuses.ts
Xemka Jan 29, 2021
fe96048
Added aggregations function in workspaces service
Xemka Jan 29, 2021
d392ebd
Merge branch 'main' of https://github.com/codex-team/devops-toolbox-a…
Xemka Jan 29, 2021
b697b28
Moved aggregation condition to services
Xemka Feb 2, 2021
4be581c
Refactored types and promises
Xemka Feb 2, 2021
f722f38
Merge branch 'main' into feature/ping-services
fabled228 Feb 11, 2021
af93395
fixes
fabled228 Feb 16, 2021
869b489
Added ping of services
fabled228 Feb 24, 2021
5816b0d
eslint fixes
fabled228 Feb 24, 2021
3f427b2
fixes of commits
fabled228 Feb 25, 2021
4357669
removed logs
fabled228 Feb 25, 2021
5bcc722
Updating statuses for each workspace now not for users
fabled228 Mar 5, 2021
19cfef1
axios removed
fabled228 Mar 5, 2021
97f0726
changed collection name
fabled228 Mar 5, 2021
3fd12d3
Comments added
fabled228 Mar 5, 2021
5e9d454
removed excess cycle
fabled228 Mar 5, 2021
9b04c96
Update statuses.ts
fabled228 Mar 9, 2021
0fd1568
Update workspace.ts
fabled228 Mar 9, 2021
597ffcd
Update tsconfig.json
fabled228 Mar 10, 2021
6254fb5
fixed comments
fabled228 Mar 10, 2021
7e4ae5f
fixed finding of workspaces to return for 1 user
fabled228 Mar 10, 2021
5fcce7c
fixed names,comments and logic
fabled228 Mar 10, 2021
7ef4f60
fixed pinging
fabled228 Mar 12, 2021
6bdbf6c
remove logs
fabled228 Mar 15, 2021
3cc0457
fix of names and added new method to StatusesController
fabled228 Mar 16, 2021
9303915
fixed pingProjects
fabled228 Mar 18, 2021
00c9538
Update statuses.ts
fabled228 Mar 18, 2021
185d932
added export
fabled228 Mar 18, 2021
3670d6a
refactoring
fabled228 Mar 24, 2021
2c59cca
Update statuses.ts
fabled228 Mar 24, 2021
228d0ac
changed workspace aggregation
fabled228 Mar 24, 2021
cdfb9f3
refactoring
fabled228 Mar 25, 2021
3151c73
removed excess code
fabled228 Mar 25, 2021
2044caa
removed aggregations
fabled228 Mar 31, 2021
f6e58e9
fixed names
fabled228 Mar 31, 2021
f0c14e7
removed extend db operations
fabled228 Mar 31, 2021
1436e8f
refactoring
fabled228 Apr 5, 2021
b63e434
added docs
fabled228 Apr 9, 2021
28715eb
fixes
fabled228 Apr 9, 2021
2503fdc
Update servicePayload.ts
fabled228 Apr 9, 2021
8cc4b4f
docs
fabled228 May 5, 2021
b6f10b7
Update index.ts
fabled228 May 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ DB_URL=mongodb://mongodb:27017

# Workspaces config filename
CONFIG_FILE=config.yml

# Cron schedule for ping services
PING_SCHEDULE="* * * * *"
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
"@types/express": "^4.17.8",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.2",
"@types/node-cron": "^2.0.3",
"@types/ping": "^0.2.0",
"@types/ws": "^7.2.7",
"ctproto": "^0.0.7",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.10.11",
"nanoid": "^3.1.18",
"node-cron": "^2.0.3",
"ping": "^0.4.0",
"typescript": "^4.1.2",
"ws": "^7.3.1",
"yaml": "^1.10.0"
Expand Down
4 changes: 4 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export default class Config {
* Workspaces config-file path
*/
public static workspacesConfigPath: string = process.env.CONFIG_FILE || '';
/**
* Cron schedule for setting pinging interval
*/
public static pingSchedule: string = process.env.PING_SCHEDULE || '*/30 * * * * *';
}
108 changes: 108 additions & 0 deletions src/controllers/statuses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import ping from 'ping';
// eslint-disable-next-line @typescript-eslint/no-unused-vars-experimental,@typescript-eslint/no-unused-vars
import app from './../app';
import WorkspacesService from './../services/workspace';
import ServerProjectsStatuses, { ProjectStatus } from '../types/serverProjectsStatuses';
import Workspace from './../types/workspace';
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-unused-vars-experimental
import Client from 'ctproto/build/src/server/client';
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-unused-vars-experimental
import { DevopsToolboxAuthData } from '../types/api/responses/authorize';
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-unused-vars-experimental
import { ApiOutgoingMessage, ApiResponse } from '../types';
import Server from '../services/server';
import { NginxPayload } from '../types/servicePayload';

// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-unused-vars-experimental
Comment on lines +2 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

что за беспредел

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В следующем реквесте уберу..


/**
* Statuses controller to work with updating/checking/sending statuses of servers' statuses for each workspace
*/
export default class StatusesController {
/**
* Update statuses and send to logged users
*/
public static async updateStatuses(): Promise<void> {
const workspaces: Workspace[] | null = await WorkspacesService.find();

/**
* If workspaces exist,then updating statuses for them
*/
if (workspaces) {
for (const workspace of workspaces) {
/**
* For each server of workspace update statuses
*/
for (const s of workspace.servers) {
for (const service of s.services) {
/**
* If type of service is nginx, server's projects are pinged
*/
if (service.type === 'nginx') {
const projectsStatuses = await this.pingProjects(service.payload as NginxPayload[]);
const serverProjectsStatuses: ServerProjectsStatuses = {
projectsStatuses,
serverToken: s.token,
} as ServerProjectsStatuses;

await Server.updateServicesStatuses(serverProjectsStatuses);
}
}
}
}
}
}

/**
* Project availability check
*
* @param serverProject - array of workspace server's projects
*/
public static async checkProjectAvailability(serverProject: string): Promise<ProjectStatus> {
if (serverProject === '') {
return {
host: 'Unnamed host',
isOnline: false,
};
} else {
const pingServer = await ping.promise.probe(serverProject);

return {
host: serverProject,
isOnline: pingServer.alive,
};
}
}

/**
* Ping projects of some server
*
* @param payload - list of project of payload of some service of some server
*/
public static async pingProjects(payload: NginxPayload[]): Promise<ProjectStatus[]> {
const projectsStatuses:ProjectStatus[] = [];

for (const payloadElement of payload) {
const projectStatus = await StatusesController.checkProjectAvailability(payloadElement.serverName);

projectsStatuses.push(projectStatus);
}

return projectsStatuses;
}

/**
* Send statuses to clients
*
* @param statuses - array of statuses of all client services
* @param user - user
*/
// public static sendStatuses(statuses: ServerProjectsStatuses[], user: Client<DevopsToolboxAuthData, ApiResponse, ApiOutgoingMessage>): void {
// if (typeof user.authData.userToken === 'string') {
// app.context.transport
// .clients
// .find((client) => client.authData.userToken === user.authData.userToken)
// .send('statuses-updated', { statuses });
// }
// }
Comment on lines +100 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если это тебе не нужно, то можно убрать это вообще

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

комменты уберу в некст реквесте

}
36 changes: 36 additions & 0 deletions src/database/models/serverServicesStatuses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mongoose from '..';
import ServerProjectsStatuses from '../../types/serverProjectsStatuses';

/**
* Project status Schema
*/
const serverProjectsStatusesSchema: mongoose.Schema = new mongoose.Schema({
/**
* Workspace server's token
*/
serverToken: {
type: String,
required: true,
},
/**
* Workspace server's projects' names and statuses
*/
projectsStatuses: [ {
/**
* Host name
*/
host: {
type: String,
required: true,
},
/**
* host status
*/
isOnline: {
type: Boolean,
required: true,
},
} ],
});

export default mongoose.model<ServerProjectsStatuses>('server_projects_statuses', serverProjectsStatusesSchema);
1 change: 1 addition & 0 deletions src/database/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const workspaceSchema: mongoose.Schema = new mongoose.Schema({
type: String,
required: true,
},

/**
* Workspace servers
*/
Expand Down
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
/// <reference path="./types/app.d.ts" />
import { CTProtoServer } from 'ctproto';
import app from './app';
import cron from 'node-cron';
import Config from './config';
import { WorkspacesController, ApiRequest, ApiResponse, ApiOutgoingMessage } from './types';
import WorkspacesService from './services/workspace';
import StatusesController from './controllers/statuses';
import { AuthorizeMessagePayload } from './types/api/requests/authorize';
import { DevopsToolboxAuthData } from './types/api/responses/authorize';

Expand All @@ -16,14 +18,12 @@ app.listen(Config.httpPort, Config.host, () => {
* Initialize CTProto server for API
*/
const transport = new CTProtoServer<AuthorizeMessagePayload, DevopsToolboxAuthData, ApiRequest, ApiResponse, ApiOutgoingMessage>({
host: Config.host,
port: Config.wsPort,
async onAuth(authRequestPayload: AuthorizeMessagePayload): Promise<DevopsToolboxAuthData> {
/**
* Connected client's authorization token
*/
const authToken = authRequestPayload.token.toString();

/**
* Connected client's workspaces list
*/
Expand Down Expand Up @@ -63,3 +63,14 @@ const transport = new CTProtoServer<AuthorizeMessagePayload, DevopsToolboxAuthDa
* @example req.app.locals.transport
*/
app.context = { transport };

/**
* Ping availability of connected user's services
*/
cron.schedule(Config.pingSchedule, () => {
StatusesController.updateStatuses()
.then()
.catch(e => {
console.log(e);
});
});
43 changes: 43 additions & 0 deletions src/services/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import mongoose from '../database';
import IServerProjectsStatuses from '../types/serverProjectsStatuses';
import ServerProjectsStatuses from '../database/models/serverServicesStatuses';

/**
* Server of workspace with token,services and projects
*/
export default class Server {
/**
* Add new server
*
* @param server - new server
*/
public static async add(server: IServerProjectsStatuses): Promise<IServerProjectsStatuses | null> {
const newServer = new ServerProjectsStatuses(server);

return newServer.save();
}

/**
* Update of server projects' statuses in DB
*
* @param serverProjectsStatuses - server projects' statuses and server token
*/
public static async updateServicesStatuses(serverProjectsStatuses: IServerProjectsStatuses): Promise<mongoose.Document> {
const server = await ServerProjectsStatuses.findOne({ serverToken: serverProjectsStatuses.serverToken });

if (!server) {
await this.add(serverProjectsStatuses);
}

return ServerProjectsStatuses.updateOne({
serverToken: serverProjectsStatuses.serverToken,
}, {
$set: {
projectsStatuses: serverProjectsStatuses.projectsStatuses,
},

}, {
new: true,
});
}
}
12 changes: 11 additions & 1 deletion src/services/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoose from '../database';
import Workspace from '../database/models/workspace';
import { Workspace as IWorkspace, Service } from '../types';
import ServicePayload from '../types/servicePayload';

/**
* Workspace service
Expand All @@ -15,6 +16,15 @@ export default class WorkspacesService {
return Workspace.find(workspaceOptions);
}

/**
* Find one workspace with options
*
* @param workspaceOptions - Workspace options for looking for documents
*/
public static async findOne(workspaceOptions: mongoose.FilterQuery<typeof Workspace> = {}): Promise<IWorkspace | null> {
return Workspace.findOne(workspaceOptions);
}

/**
* Add new workspace
*
Expand All @@ -32,7 +42,7 @@ export default class WorkspacesService {
* @param token - Server token
* @param actualServices - Actual services
*/
public static async updateServices(token: string | undefined, actualServices: Service[]): Promise<IWorkspace | null> {
public static async updateServices(token: string | undefined, actualServices: Service<ServicePayload>[]): Promise<IWorkspace | null> {
return Workspace.findOneAndUpdate({ 'servers.token': token }, {
$set: {
'servers.$.services': actualServices,
Expand Down
2 changes: 2 additions & 0 deletions src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Authorize from './requests/authorize';
import AuthorizeResponse from './responses/authorize';
import GetWorkspacesResponse from './responses/getWorkspaces';
import WorkspaceUpdatedMessage from './outgoing/workspaceUpdated';
import StatusesUpdatedMessage from './outgoing/statusesUpdated';

/**
* This file uses Discriminating Unions types for our API
Expand Down Expand Up @@ -31,4 +32,5 @@ export type ApiResponse =
*/
export type ApiOutgoingMessage =
| WorkspaceUpdatedMessage
| StatusesUpdatedMessage
;
19 changes: 19 additions & 0 deletions src/types/api/outgoing/statusesUpdated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NewMessage } from 'ctproto/types';
import ServerProjectsStatuses from '../../serverProjectsStatuses';

/**
* Data about the updated statuses
*/
interface StatusesUpdatedPayload {
/**
* The updated workspace
*/
projectsStatuses: ServerProjectsStatuses;
}

/**
* Describes the outgoing message that will be sent when statuses of services will be updated
*/
export default interface StatusesUpdatedMessage extends NewMessage<StatusesUpdatedPayload> {
type: 'statuses-updated';
}
3 changes: 2 additions & 1 deletion src/types/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Service from './service';
import SSHConnectionInfo from './SSHConnectionInfo';
import ServicePayload from './servicePayload';

/**
* Interface for server
Expand All @@ -24,5 +25,5 @@ export default interface Server {
/**
* List of services running on the server
*/
services: Service[];
services: Service<ServicePayload>[];
}
28 changes: 28 additions & 0 deletions src/types/serverProjectsStatuses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import mongoose from 'mongoose';

/**
* Project status of the server
*/
export interface ProjectStatus {
/**
* Name of host
*/
host: string;
/**
* State of host (online/offline)
*/
isOnline: boolean;
}
/**
* Status of service in workspace
*/
export default interface ServerProjectsStatuses extends mongoose.Document {
/**
* Server's (containing the services) id
*/
serverToken: string;
/**
* Projects with their statuses of the server
*/
projectsStatuses: ProjectStatus[];
}
Loading