feat: init

This commit is contained in:
montaghanmy
2023-03-23 11:03:16 +01:00
commit 10fe6f78d1
11518 changed files with 509786 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
plugins
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.env.development
config/local.json
config/production.json
config/development.json
jest.config.js
/config/test-local.json
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 207 KiB

+56
View File
@@ -0,0 +1,56 @@
# PluginsServer - This module is WIP and not ready for production ⚠️
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 !")
# How to use this image
Here is a picture representing how script works together to let you install and manage plugins easily
![Alt text](twake/plugins/HowScriptsWorks.png "Scripts")
## Start plugin server
docker-compose -f <docker-compose.yml> up -d 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
+75
View File
@@ -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
View File
@@ -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
View File
@@ -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`);
}
});
+18
View File
@@ -0,0 +1,18 @@
var fs = require("fs");
fs.readFile("/usr/src/app/plugins.json", function (err, data) {
if (err) throw err;
var json = JSON.parse(data).plugins;
var counter = 1;
if (json.length === 0) {
console.log("No plugins saved");
}
json.forEach((plugin) => {
console.log(
`${counter} - ${plugin.name} from ${plugin.repository} on local port ${plugin.port}`
);
counter++;
});
});
+20
View File
@@ -0,0 +1,20 @@
var fs = require("fs");
var child_process = require("child_process");
fs.readFile("/usr/src/app/plugins.json", async function (err, data) {
if (err) throw err;
var json = JSON.parse(data).plugins;
json.forEach((plugin) => {
console.log(`Preparing plugin ${plugin.name}`);
child_process.exec(
`cd /usr/src/app/plugins/${plugin.name} && build ${plugin.name} && up ${plugin.name} && cd ../..`,
(err) => {
if (err) throw err;
console.log(`Plugin ${plugin.name} running on port ${plugin.port}`);
child_process.exec(`cd /usr/src/app && start_nginx`, () => {});
}
);
});
});
+70
View File
@@ -0,0 +1,70 @@
var fs = require("fs");
var child_process = require("child_process");
fs.readFile("/usr/src/app/plugins.json", function (err, data) {
if (err) throw err;
var json = JSON.parse(data).plugins;
const conf = generate_config(json);
fs.writeFile("/usr/src/app/nginx/nginx.conf", conf, (err) => {
if (err) throw err;
console.log("Plugins reverse proxy ready for building");
});
child_process.exec(
`docker stop nginx_host && docker system prune -f -a --volumes`,
(err) => {
console.log(`Clean nginx container and rebuild`);
child_process.exec(
`cd nginx && docker build -t nginx_host . && docker run --name nginx_host --network=test-net --restart unless-stopped -dp 8080:80 nginx_host && cd ..`,
(err) => {
if (err) throw err;
console.log(
`Plugins container reverse proxy built and running on port 8080`
);
}
);
}
);
});
const generate_config = (json) => {
var conf = `
events {}
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =408;
}
location /api {
#proxy_set_header X-Forwarded-Host $host
proxy_pass http://172.21.0.1:3000;
}
`;
json.forEach((plugin) => {
conf += `
location /plugins/${plugin.name} {
proxy_pass http://172.64.0.1:${plugin.port};
}
`;
});
conf += `}}`;
return conf;
};
+22
View File
@@ -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 run --name ${plugin_name} --network=dind-net --restart unless-stopped -dp ${plugin.port}:${plugin.port} -e SERVER_PORT=${plugin.port} -e SERVER_PREFIX='/plugins/${plugin_name}' -e CREDENTIALS_ENDPOINT='http://172.64.0.1:8080' -e CREDENTIALS_SECRET=${plugin.secret} -e CREDENTIALS_ID=${plugin.id} -e SERVER_ORIGIN='http://localhost:8080' ${plugin.venv} ${plugin_name} && cd ../..`,
(err) => {
if (err) throw err;
}
);
}
});
});
+31
View File
@@ -0,0 +1,31 @@
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(
`delete ${plugin_name} && add https://github.com/linagora/Twake-plugins-${plugin_name} ${plugin.id} ${plugin.secret} ${plugin.venv}`,
(err) => {
if (err) throw err;
console.log(
`${plugin_name.toUpperCase()} is now updated, built, running and reachable from host on http://localhost:8080/plugins/${plugin_name}`
);
}
);
}
});
if (!existing) {
console.log(`${plugin_name.toUpperCase()} is not yet installed`);
}
});
+12
View File
@@ -0,0 +1,12 @@
FROM nginx:latest
#RUN rm /etc/nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY . /usr/share/nginx/html/
COPY nginx.conf /etc/nginx
VOLUME /usr/share/nginx/html
VOLUME /etc/nginx
EXPOSE 8080:80
+29
View File
@@ -0,0 +1,29 @@
events {}
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /api {
#proxy_set_header X-Forwarded-Host $host
proxy_pass http://twake_node_1:3000;
}
}
}
+1
View File
@@ -0,0 +1 @@
{"plugins":[]}
+26
View File
@@ -0,0 +1,26 @@
#!/bin/bash
GITURL=$1
FOLDERNAME=${GITURL##*-}
ID=$2
SECRET=$3
VENV=$4
docker network create \
--driver=bridge \
--subnet=172.64.0.0/16 \
--ip-range=172.64.0.0/24 \
--gateway=172.64.0.1 \
dind-net
node /usr/src/app/helpers/add.js $1 ${FOLDERNAME} ${ID} ${SECRET} "${VENV}"
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
node /usr/src/app/helpers/build.js $1
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
node /usr/src/app/helpers/delete.js $1
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
node /usr/src/app/helpers/list.js
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
docker network create \
--driver=bridge \
--subnet=172.64.0.0/16 \
--ip-range=172.64.0.0/24 \
--gateway=172.64.0.1 \
dind-net
node /usr/src/app/helpers/start.js
+12
View File
@@ -0,0 +1,12 @@
#!/bin/bash
docker network create \
--driver=bridge \
--subnet=172.64.0.0/16 \
--ip-range=172.64.0.0/24 \
--gateway=172.64.0.1 \
dind-net
node /usr/src/app/helpers/start_nginx.js
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
node /usr/src/app/helpers/up.js $1
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
node /usr/src/app/helpers/update.js $1