Skip to content

Commit

Permalink
🐳 #2391 plugin server (#2439)
Browse files Browse the repository at this point in the history
* merge side project in twake working with jitsi

* all plugins now work

* litle correction in readme

* Update README.md

Co-authored-by: tissarni <tissarni>
Co-authored-by: Romaric Mourgues <rmourgues@linagora.com>
  • Loading branch information
tissarni and RomaricMourgues committed Nov 29, 2022
1 parent c3a3628 commit 700080d
Show file tree
Hide file tree
Showing 41 changed files with 499 additions and 8,047 deletions.
2 changes: 1 addition & 1 deletion twake/backend/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion twake/backend/node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9769,4 +9769,4 @@ yn@3.1.1:
yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
30 changes: 30 additions & 0 deletions twake/docker-compose.dev.mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,27 @@ services:
- ./docker-data/mongo:/data/db
ports:
- 27017:27017
networks:
- twake_network

plugins:
container_name: plugins
image: twaketech/twake-plugins
privileged: true
volumes:
- ./plugins/plugins:/usr/src/app/plugins
- ./plugins/plugins.json:/usr/src/app/plugins.json
build:
context: .
dockerfile: ./docker/twake-plugins/Dockerfile
ports:
- 8080:8080
networks:
- twake_network

node:
image: twaketech/twake-node
container_name: node
ports:
- 4000:3000
- 8000:3000
Expand All @@ -30,3 +49,14 @@ services:
- mongo
links:
- mongo
networks:
- twake_network

networks:
twake_network:
driver: bridge
ipam:
config:
- subnet: 172.21.0.0/16
gateway: 172.21.0.1

10 changes: 5 additions & 5 deletions twake/docker/twake-nginx/site.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ server {
try_files $uri /index.html;
}

location ~ ^/(api/v1).* {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
location ~ ^/(internal).* {
proxy_set_header X-Forwarded-Host $host;
proxy_pass ${NODE_HOST};
}

location ~ ^/(internal|plugins).* {
location ~ ^/(plugins).* {
proxy_set_header X-Forwarded-Host $host;
proxy_pass ${NODE_HOST};
proxy_pass http://plugins_container:8080;
}

location ~ ^/(api|administration/v2/).* {
Expand Down
42 changes: 10 additions & 32 deletions twake/docker/twake-plugins/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,48 +1,26 @@
# Common node machine
FROM node:16-bullseye-slim as node-base
# syntax=docker/dockerfile:1

### Install dependancies

#Docker mac issue
RUN apt-get update && apt-get install -y libc6
RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
FROM docker:20.10.17-dind-alpine3.16

### Install Twake

WORKDIR /usr/src/app
COPY backend/node/package*.json ./
RUN apk add --update nodejs-current npm bash curl

# Test Stage
FROM node-base as test
WORKDIR /usr/src/app

RUN npm install
COPY backend/node/ .
COPY plugins/ .

# Development Stage
FROM node-base as development
VOLUME /usr/src/app
RUN chmod -R +x scripts
RUN mv scripts/* /bin
RUN apk update
RUN apk add git

RUN npm install -g pino-pretty
RUN npm install -g tsc-watch
RUN npm install
COPY backend/node/ .

CMD ["npm", "run", "dev"]

# Production Stage
FROM node-base as production

ARG NODE_ENV=production

ENV NODE_ENV=development
RUN npm install #Install dev dependancies for build
COPY backend/node/ .
ENV NODE_ENV=${NODE_ENV}
RUN npm run build #Build in production mode
RUN rm -R node_modules
RUN npm install #Install prod dependancies after build

EXPOSE 3000

CMD ["npm", "run", "serve"]


1 change: 1 addition & 0 deletions twake/plugins/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lerna-debug.log*
# Tests
/coverage
/.nyc_output
plugins

# IDEs and editors
/.idea
Expand Down
1 change: 0 additions & 1 deletion twake/plugins/.nvmrc

This file was deleted.

11 changes: 0 additions & 11 deletions twake/plugins/.prettierignore

This file was deleted.

9 changes: 0 additions & 9 deletions twake/plugins/.prettierrc

This file was deleted.

Binary file added twake/plugins/HowItWorks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added twake/plugins/HowScriptsWorks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 50 additions & 7 deletions twake/plugins/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
# Twake plugins server
# PluginsServer - This module is WIP and not ready for production ⚠️

### Run plugin server
A simple to use docker image for Twake plugins. This image use docker in docker to permit to deploy plugin easily and quickly everywhere. Docker in Docker is useful but is not ready for production environment because you need to use the root container with privileged. Furthermore, with Dockerception 127.0.0.1 is not home so this is a little schema to help understanding how this container work with Twake.
![Alt text](twake/plugins/HowItWorks.png "How it works !")

1. Update config (TODO)
# How to use this image

2. `docker-compose up -d plugins`
Here is a picture representing how script works together to let you install and manage plugins easily
![Alt text](twake/plugins/HowScriptsWorks.png "Scripts")

### Install new plugin
## Start plugin server

1. `docker-compose exec plugins yarn install-plugin https://github.com/linagora/Twake-Plugins-Jitsi`
docker-compose -f <docker-compose.yml> up -d plugins

2. `docker-compose restart plugins`
## Add a plugin

By using the command below, you clone a plugins repo, add it to a list of already installed plugins, build and run it and start the nginx reverse proxy. After this the plugin will be up and running on a port specified in the plugins.json list.
If you need for example to add environment variable to your plugin you can add it with a string as <environnemen-variable> like this: "-e GIPHY_APIKEY=toto -e GIPHY_SERET=tata"

docker-compose -f <docker-compose.yml> exec plugins add <plugin-git-repo> <twake-plugin-id> <twake-plugin-secret> <environnement-variable>

## Start all plugins installed

This script will start all plugins installed and saved in plugin.json

docker-compose -f <docker-compose.yml> exec plugins start

### build already installed plugin

When you already have installed a plugin, Add script will not install a plugin a second time. So you can check the configuration in plugin.json a use build script to build your image.

docker-compose -f <docker-compose.yml> exec plugins build <plugin-git-repo>

### Run already installed and built plugin

If your plugin is installed and the image associated is built, just run the plugin. Before running a plugin manually check if the network (docker network ls) is already created, if not restart your nginx with script start_nginx.

docker-compose -f <docker-compose.yml> exec plugins start_nginx && docker-compose -f <docker-compose.yml> exec plugins up <plugin-name>

## Update a plugin

To update a plugin, use the update script that will pull last version of the plugin on github.

docker-compose -f <docker-compose.yml> exec plugins update <plugin-name>

## delete a plugin

docker-compose -f <docker-compose.yml> exec plugins delete <plugin-name>

## list all plugin installed

docker-compose -f <docker-compose.yml> exec plugins list

## Start Nginx

docker-compose -f <docker-compose.yml> exec plugins start_nginx
9 changes: 0 additions & 9 deletions twake/plugins/babel.config.js

This file was deleted.

1 change: 0 additions & 1 deletion twake/plugins/config/custom-environment-variables.json

This file was deleted.

5 changes: 0 additions & 5 deletions twake/plugins/config/default.json

This file was deleted.

75 changes: 75 additions & 0 deletions twake/plugins/helpers/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
var fs = require("fs");
var child_process = require("child_process");

const plugin_repository = process.argv[2];
const plugin_name = process.argv[3];
var plugin_id = "";
var plugin_secret = "";
if (process.argv[4] && process.argv[5]) {
plugin_id = process.argv[4];
plugin_secret = process.argv[5];
}
var venv = "";
if (process.argv[6]) venv = process.argv[6];

var port = 8001;

fs.readFile("/usr/src/app/plugins.json", function (err, data) {
if (err) throw err;

var json = JSON.parse(data).plugins;
var existing = false;

json.forEach((plugin) => {
if (plugin.port >= port) {
port = plugin.port + 1;
}
if (plugin.name === plugin_name) {
console.log(`${plugin_name.toUpperCase()} is already saved`);
existing = true;

return existing;
}
});

if (existing) return;

let obj = {
name: plugin_name,
repository: plugin_repository,
port: port,
id: plugin_id,
secret: plugin_secret,
venv: venv,
};

json.push(obj);

child_process.exec(
`git clone ${plugin_repository} /usr/src/app/plugins/${plugin_name}`,
(err) => {
if (err) throw err;
fs.writeFile(
"/usr/src/app/plugins.json",
JSON.stringify({ plugins: json }),
(err) => {
if (err) throw err;
console.log(
`${plugin_name.toUpperCase()} is now cloned and saved \nWaiting for ${plugin_name.toUpperCase()} build`
);
}
);
child_process.exec(`build ${plugin_name} && up ${plugin_name}`, (err) => {
if (err) throw err;
console.log(
`${plugin_name.toUpperCase()} built and running on port ${port}`
);
child_process.exec(`start_nginx`, () => {
console.log(
`${plugin_name.toUpperCase()} Reachable from host on http://localhost:8080/plugins/${plugin_name}`
);
});
});
}
);
});
22 changes: 22 additions & 0 deletions twake/plugins/helpers/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var fs = require("fs");
var child_process = require("child_process");

const plugin_name = process.argv[2];

fs.readFile("/usr/src/app/plugins.json", function (err, data) {
if (err) throw err;

var json = JSON.parse(data).plugins;

json.forEach((plugin) => {
if (plugin.name === plugin_name) {
existing = true;
child_process.exec(
`cd /usr/src/app/plugins/${plugin_name} && docker build -t ${plugin_name} . && cd ../..`,
(err) => {
if (err) throw err;
}
);
}
});
});
37 changes: 37 additions & 0 deletions twake/plugins/helpers/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var fs = require("fs");
var child_process = require("child_process");

const plugin_name = process.argv[2];

fs.readFile("/usr/src/app/plugins.json", function (err, data) {
if (err) throw err;

var json = JSON.parse(data).plugins;
var existing = false;

json.forEach((plugin) => {
if (plugin.name === plugin_name) {
existing = true;
child_process.exec(
`cd /usr/src/app/plugins/ && rm -fr ${plugin_name} && docker stop ${plugin_name} && docker system prune -f -a --volumes `,
(err) => {
const new_json = json.filter((plugin) => plugin.name !== plugin_name);
fs.writeFile(
"/usr/src/app/plugins.json",
JSON.stringify({ plugins: new_json }),
(err) => {
if (err) throw err;
child_process.exec(`start_nginx`, () => {
console.log(`nginx restarted`);
});
console.log(`${plugin_name.toUpperCase()} deleted`);
}
);
}
);
}
});
if (!existing) {
console.log(`${plugin_name.toUpperCase()} is not yet installed`);
}
});
Loading

0 comments on commit 700080d

Please sign in to comment.