@wocker/ws 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +155 -0
  3. package/bin/ws.js +13 -0
  4. package/lib/App.d.ts +13 -0
  5. package/lib/App.js +52 -0
  6. package/lib/controllers/ImageController.d.ts +8 -0
  7. package/lib/controllers/ImageController.js +29 -0
  8. package/lib/controllers/ProjectController.d.ts +64 -0
  9. package/lib/controllers/ProjectController.js +561 -0
  10. package/lib/controllers/index.d.ts +2 -0
  11. package/lib/controllers/index.js +27 -0
  12. package/lib/env.d.ts +7 -0
  13. package/lib/env.js +24 -0
  14. package/lib/index.d.ts +3 -0
  15. package/lib/index.js +31 -0
  16. package/lib/makes/Controller.d.ts +5 -0
  17. package/lib/makes/Controller.js +12 -0
  18. package/lib/makes/Docker.d.ts +58 -0
  19. package/lib/makes/Docker.js +418 -0
  20. package/lib/makes/FS.d.ts +37 -0
  21. package/lib/makes/FS.js +317 -0
  22. package/lib/makes/LineConvertStream.d.ts +13 -0
  23. package/lib/makes/LineConvertStream.js +45 -0
  24. package/lib/makes/Logger.d.ts +7 -0
  25. package/lib/makes/Logger.js +39 -0
  26. package/lib/makes/Model.d.ts +5 -0
  27. package/lib/makes/Model.js +16 -0
  28. package/lib/makes/MySQL.d.ts +15 -0
  29. package/lib/makes/MySQL.js +31 -0
  30. package/lib/makes/Plugin.d.ts +13 -0
  31. package/lib/makes/Plugin.js +43 -0
  32. package/lib/makes/Repository.d.ts +11 -0
  33. package/lib/makes/Repository.js +25 -0
  34. package/lib/makes/index.d.ts +6 -0
  35. package/lib/makes/index.js +71 -0
  36. package/lib/models/Preset.d.ts +19 -0
  37. package/lib/models/Preset.js +60 -0
  38. package/lib/models/Project.d.ts +38 -0
  39. package/lib/models/Project.js +151 -0
  40. package/lib/models/index.d.ts +2 -0
  41. package/lib/models/index.js +27 -0
  42. package/lib/plugins/LocaltunnelPlugin.d.ts +36 -0
  43. package/lib/plugins/LocaltunnelPlugin.js +273 -0
  44. package/lib/plugins/MaildevPlugin.d.ts +10 -0
  45. package/lib/plugins/MaildevPlugin.js +45 -0
  46. package/lib/plugins/MariadbPlugin.d.ts +25 -0
  47. package/lib/plugins/MariadbPlugin.js +297 -0
  48. package/lib/plugins/MongodbPlugin.d.ts +20 -0
  49. package/lib/plugins/MongodbPlugin.js +271 -0
  50. package/lib/plugins/NgrokPlugin.d.ts +37 -0
  51. package/lib/plugins/NgrokPlugin.js +248 -0
  52. package/lib/plugins/PageKitePlugin.d.ts +31 -0
  53. package/lib/plugins/PageKitePlugin.js +158 -0
  54. package/lib/plugins/PostgresPlugin.d.ts +16 -0
  55. package/lib/plugins/PostgresPlugin.js +94 -0
  56. package/lib/plugins/PresetPlugin.d.ts +19 -0
  57. package/lib/plugins/PresetPlugin.js +164 -0
  58. package/lib/plugins/ProjectPlugin.d.ts +12 -0
  59. package/lib/plugins/ProjectPlugin.js +54 -0
  60. package/lib/plugins/ProxmoxPlugin.d.ts +9 -0
  61. package/lib/plugins/ProxmoxPlugin.js +55 -0
  62. package/lib/plugins/ProxyPlugin.d.ts +33 -0
  63. package/lib/plugins/ProxyPlugin.js +257 -0
  64. package/lib/plugins/RedisPlugin.d.ts +10 -0
  65. package/lib/plugins/RedisPlugin.js +42 -0
  66. package/lib/plugins/ServeoPlugin.d.ts +36 -0
  67. package/lib/plugins/ServeoPlugin.js +260 -0
  68. package/lib/plugins/TestPlugin.d.ts +10 -0
  69. package/lib/plugins/TestPlugin.js +75 -0
  70. package/lib/plugins/index.d.ts +14 -0
  71. package/lib/plugins/index.js +159 -0
  72. package/lib/services/AppConfigService.d.ts +18 -0
  73. package/lib/services/AppConfigService.js +77 -0
  74. package/lib/services/AppEventsService.d.ts +13 -0
  75. package/lib/services/AppEventsService.js +29 -0
  76. package/lib/services/ProjectService.d.ts +13 -0
  77. package/lib/services/ProjectService.js +91 -0
  78. package/lib/services/index.d.ts +3 -0
  79. package/lib/services/index.js +38 -0
  80. package/lib/types/Config.d.ts +11 -0
  81. package/lib/types/Config.js +5 -0
  82. package/lib/types/EnvConfig.d.ts +3 -0
  83. package/lib/types/EnvConfig.js +5 -0
  84. package/lib/types/index.d.ts +2 -0
  85. package/lib/types/index.js +27 -0
  86. package/lib/utils/buildOptions.d.ts +1 -0
  87. package/lib/utils/buildOptions.js +12 -0
  88. package/lib/utils/demuxOutput.d.ts +2 -0
  89. package/lib/utils/demuxOutput.js +25 -0
  90. package/lib/utils/escapeRegExp.d.ts +2 -0
  91. package/lib/utils/escapeRegExp.js +10 -0
  92. package/lib/utils/exec.d.ts +2 -0
  93. package/lib/utils/exec.js +44 -0
  94. package/lib/utils/fetch.d.ts +5 -0
  95. package/lib/utils/fetch.js +37 -0
  96. package/lib/utils/followProgress.d.ts +6 -0
  97. package/lib/utils/followProgress.js +80 -0
  98. package/lib/utils/format-size-units.d.ts +1 -0
  99. package/lib/utils/format-size-units.js +19 -0
  100. package/lib/utils/get-config.d.ts +2 -0
  101. package/lib/utils/get-config.js +20 -0
  102. package/lib/utils/get-cursor-position.d.ts +4 -0
  103. package/lib/utils/get-cursor-position.js +34 -0
  104. package/lib/utils/image-build.d.ts +13 -0
  105. package/lib/utils/image-build.js +34 -0
  106. package/lib/utils/index.d.ts +21 -0
  107. package/lib/utils/index.js +236 -0
  108. package/lib/utils/injectVariables.d.ts +5 -0
  109. package/lib/utils/injectVariables.js +17 -0
  110. package/lib/utils/parse-table.d.ts +2 -0
  111. package/lib/utils/parse-table.js +27 -0
  112. package/lib/utils/promptConfirm.d.ts +6 -0
  113. package/lib/utils/promptConfirm.js +21 -0
  114. package/lib/utils/promptGroup.d.ts +16 -0
  115. package/lib/utils/promptGroup.js +39 -0
  116. package/lib/utils/promptSelect.d.ts +12 -0
  117. package/lib/utils/promptSelect.js +47 -0
  118. package/lib/utils/promptText.d.ts +13 -0
  119. package/lib/utils/promptText.js +53 -0
  120. package/lib/utils/set-config.d.ts +2 -0
  121. package/lib/utils/set-config.js +18 -0
  122. package/lib/utils/spawn.d.ts +2 -0
  123. package/lib/utils/spawn.js +24 -0
  124. package/lib/utils/tty.d.ts +2 -0
  125. package/lib/utils/tty.js +10 -0
  126. package/lib/utils/volumeFormat.d.ts +6 -0
  127. package/lib/utils/volumeFormat.js +15 -0
  128. package/lib/utils/volumeParse.d.ts +2 -0
  129. package/lib/utils/volumeParse.js +16 -0
  130. package/package.json +90 -0
  131. package/plugins/localtunnel/Dockerfile +37 -0
  132. package/plugins/localtunnel/Ubuntu.dockerfile +34 -0
  133. package/plugins/localtunnel/config.json +3 -0
  134. package/plugins/maildev/Dockerfile +5 -0
  135. package/plugins/maildev/config.json +3 -0
  136. package/plugins/mariadb/admin/conf/config.user.inc.php +9 -0
  137. package/plugins/ngrok/Dockerfile +2 -0
  138. package/plugins/pagekite/Dockerfile +3 -0
  139. package/plugins/proxmox/Dockerfile +35 -0
  140. package/plugins/proxy/Dockerfile +7 -0
  141. package/plugins/proxy/config.json +3 -0
  142. package/plugins/serveo/Dockerfile +17 -0
  143. package/presets/apache/.docker/apache2/apache2.conf +230 -0
  144. package/presets/apache/.docker/apache2/mods-available/mpm_prefork.conf +16 -0
  145. package/presets/apache/.docker/apache2/sites-available/000-default.conf.conf +21 -0
  146. package/presets/apache/Dockerfile +110 -0
  147. package/presets/apache/bin/compare-version +3 -0
  148. package/presets/apache/config.json +47 -0
  149. package/presets/go/Dockerfile +19 -0
  150. package/presets/go/config.json +13 -0
  151. package/presets/node/Dockerfile +37 -0
  152. package/presets/node/config.json +36 -0
  153. package/presets/php-apache/Dockerfile +133 -0
  154. package/presets/php-apache/bin/compare-version +3 -0
  155. package/presets/php-apache/config.json +56 -0
  156. package/presets/php-apache/etc/apache2/apache2.conf +230 -0
  157. package/presets/php-apache/etc/apache2/mods-available/mpm_prefork.conf +16 -0
  158. package/presets/php-apache/etc/apache2/sites-available/000-default.conf +21 -0
  159. package/presets/shopify/Dockerfile +160 -0
  160. package/presets/shopify/bin/compare-version +3 -0
  161. package/presets/shopify/config.json +24 -0
  162. package/presets/shopify/etc/apache2/apache2.conf +230 -0
  163. package/presets/shopify/etc/apache2/mods-available/mpm_prefork.conf +16 -0
  164. package/presets/shopify/etc/apache2/sites-available/000-default.conf +21 -0
@@ -0,0 +1,297 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.MariadbPlugin = void 0;
7
+ var Path = _interopRequireWildcard(require("path"));
8
+ var _format = _interopRequireDefault(require("date-fns/format"));
9
+ var _env = require("../env");
10
+ var _makes = require("../makes");
11
+ var _utils = require("../utils");
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+ class MariadbPlugin extends _makes.Plugin {
16
+ user = "root";
17
+ password = "toor";
18
+ constructor() {
19
+ super("mariadb");
20
+ this.dbDir = Path.join(_env.DATA_DIR, "db/mariadb");
21
+ // this.dataDir = Path.join(PLUGINS_DIR, "mariadb");
22
+ }
23
+
24
+ install(cli) {
25
+ super.install(cli);
26
+ cli.command("mariadb:start").action(() => this.start());
27
+ cli.command("mariadb:stop").action(() => this.stop());
28
+ cli.command("mariadb:backup [database]").completion("database", () => this.getDatabases()).action((options, database) => this.backup(database));
29
+ cli.command("mariadb:delete-backup [database] [filename]").option("yes", {
30
+ type: "boolean",
31
+ alias: "y"
32
+ }).completion("database", () => this.getDumpDatabases()).completion("filename", (options, database) => this.getDumpFiles(database)).action((options, database, filename) => this.deleteBackup(options, database, filename));
33
+ cli.command("mariadb:restore [database] [filename]").completion("database", () => this.getDumpDatabases()).completion("filename", (options, database) => this.getDumpFiles(database)).action((options, database, filename) => this.restore(options, database, filename));
34
+ cli.command("mariadb:import <database>").completion("database", () => this.getDatabases()).action((options, database) => this.import(options, database));
35
+ cli.command("mariadb:dump <database>").completion("database", () => this.getDatabases()).action((options, database) => this.dump(options, database));
36
+ cli.command("mariadb:exec [...command]").action((options, command) => this.exec(options, command));
37
+ }
38
+ async getDatabases() {
39
+ const stream = await _makes.Docker.exec("mariadb.workspace", ["mysql", `-u${this.user}`, `-p${this.password}`, "-e", "SHOW DATABASES;"], false);
40
+ let res = "";
41
+ stream.on("data", data => {
42
+ res += (0, _utils.demuxOutput)(data).toString();
43
+ });
44
+ await new Promise((resolve, reject) => {
45
+ stream.on("end", resolve);
46
+ stream.on("error", reject);
47
+ });
48
+ return res.split(/\r?\n/).filter(database => {
49
+ return !/Database$/.test(database);
50
+ }).filter(database => {
51
+ return !/_schema/.test(database);
52
+ }).filter(database => {
53
+ return !!database;
54
+ });
55
+ }
56
+ async getDumpDatabases() {
57
+ return _makes.FS.readdir(this.dataPath("dump"));
58
+ }
59
+ async getDumpFiles(database) {
60
+ if (!database) {
61
+ return [];
62
+ }
63
+ const dirPath = this.dataPath("dump", database);
64
+ if (!_makes.FS.existsSync(dirPath)) {
65
+ return [];
66
+ }
67
+ return _makes.FS.readdirFiles(dirPath);
68
+ }
69
+ async start() {
70
+ await this.startMariadb();
71
+ await this.startAdmin();
72
+ }
73
+ async startMariadb() {
74
+ console.info("Mariadb starting...");
75
+ await _makes.Docker.pullImage("mariadb:10.5");
76
+ await _makes.Docker.containerRun({
77
+ name: "mariadb.workspace",
78
+ restart: "always",
79
+ ports: ["3306:3306"],
80
+ env: {
81
+ MYSQL_ROOT_PASSWORD: "toor"
82
+ },
83
+ volumes: [`${this.dbDir}:/var/lib/mysql`],
84
+ image: "mariadb:10.5"
85
+ });
86
+ }
87
+ async startAdmin() {
88
+ console.info("Phpmyadmin starting...");
89
+ const configPath = "conf/config.user.inc.php";
90
+ if (!_makes.FS.existsSync(this.dataPath(configPath))) {
91
+ await _makes.FS.mkdir(Path.dirname(this.dataPath(configPath)), {
92
+ recursive: true
93
+ });
94
+ await _makes.FS.copyFile(this.pluginPath("admin", configPath), this.dataPath(configPath));
95
+ }
96
+ await _makes.FS.mkdir(this.dataPath("save"), {
97
+ recursive: true
98
+ });
99
+ await _makes.FS.mkdir(this.dataPath("upload"), {
100
+ recursive: true
101
+ });
102
+ await _makes.Docker.pullImage("phpmyadmin/phpmyadmin:latest");
103
+ await _makes.Docker.containerRun({
104
+ name: "dbadmin-mariadb.workspace",
105
+ restart: "always",
106
+ env: {
107
+ PMA_USER: "root",
108
+ PMA_PASSWORD: "toor",
109
+ VIRTUAL_HOST: "dbadmin-mariadb.workspace",
110
+ VIRTUAL_PORT: "80"
111
+ },
112
+ volumes: [`${this.dataPath("conf/config.user.inc.php")}:/etc/phpmyadmin/config.user.inc.php`, `${this.dataPath("save")}:/etc/phpmyadmin/save`, `${this.dataPath("upload")}:/etc/phpmyadmin/upload`],
113
+ links: ["mariadb.workspace:db"],
114
+ image: "phpmyadmin/phpmyadmin:latest"
115
+ });
116
+ }
117
+ async stop() {
118
+ await this.stopMariadb();
119
+ await this.stopAdmin();
120
+ }
121
+ async stopMariadb() {
122
+ console.info("Mariadb stopping...");
123
+ const container = await _makes.Docker.getContainer("mariadb.workspace");
124
+ if (container) {
125
+ try {
126
+ await container.stop();
127
+ } catch (err) {
128
+ _makes.Logger.warning(err.message, {
129
+ class: "MariadbPlugin"
130
+ });
131
+ }
132
+ try {
133
+ await container.remove();
134
+ } catch (err) {
135
+ _makes.Logger.warning(err.message, {
136
+ class: "MariadbPlugin"
137
+ });
138
+ }
139
+ }
140
+ }
141
+ async stopAdmin() {
142
+ console.info("Phpmyadmin stopping...");
143
+ const container = await _makes.Docker.getContainer("dbadmin-mariadb.workspace");
144
+ if (container) {
145
+ try {
146
+ await container.stop();
147
+ } catch (err) {
148
+ _makes.Logger.warning(err.message, {
149
+ class: "MariadbPlugin"
150
+ });
151
+ }
152
+ try {
153
+ await container.remove();
154
+ } catch (err) {
155
+ _makes.Logger.warning(err.message, {
156
+ class: "MariadbPlugin"
157
+ });
158
+ }
159
+ }
160
+ }
161
+ async exec(options, command) {
162
+ await _makes.Docker.exec("mariadb.workspace", command);
163
+ }
164
+ async backup(database) {
165
+ if (!database) {
166
+ database = await (0, _utils.promptSelect)({
167
+ message: "Database",
168
+ options: await this.getDatabases()
169
+ });
170
+ }
171
+ const date = (0, _format.default)(new Date(), "yyyy-MM-dd HH-mm");
172
+ const filePath = this.dataPath("dump", database, `${date}.sql`);
173
+ if (!_makes.FS.existsSync(Path.dirname(filePath))) {
174
+ _makes.FS.mkdirSync(Path.dirname(filePath), {
175
+ recursive: true
176
+ });
177
+ }
178
+ const file = _makes.FS.createWriteStream(filePath);
179
+ const stream = await _makes.Docker.exec("mariadb.workspace", ["mysqldump", "--add-drop-table", `-u${this.user}`, `-p${this.password}`, database], false);
180
+ stream.on("data", data => {
181
+ file.write((0, _utils.demuxOutput)(data));
182
+ });
183
+ }
184
+ async deleteBackup(options, database, filename) {
185
+ const {
186
+ yes
187
+ } = options;
188
+ if (!database) {
189
+ const dumps = await this.getDumpDatabases();
190
+ if (dumps.length === 0) {
191
+ throw new Error("No dump databases");
192
+ }
193
+ database = await (0, _utils.promptSelect)({
194
+ message: "Database",
195
+ options: dumps
196
+ });
197
+ }
198
+ if (!filename) {
199
+ const files = await _makes.FS.readdirFiles(this.dataPath("dump", database));
200
+ if (files.length === 0) {
201
+ throw new Error(`No backups for ${database}`);
202
+ }
203
+ filename = await (0, _utils.promptSelect)({
204
+ message: "File",
205
+ options: files
206
+ });
207
+ }
208
+ if (!yes) {
209
+ const confirm = await (0, _utils.promptConfirm)({
210
+ message: `Delete ${filename}`,
211
+ default: false
212
+ });
213
+ if (!confirm) {
214
+ return;
215
+ }
216
+ }
217
+ const filePath = this.dataPath("dump", database, filename);
218
+ await _makes.FS.rm(filePath);
219
+ const otherFiles = await _makes.FS.readdir(this.dataPath("dump", database));
220
+ if (otherFiles.length === 0) {
221
+ await _makes.FS.rm(this.dataPath("dump", database), {
222
+ force: true,
223
+ recursive: true
224
+ });
225
+ }
226
+ }
227
+ async restore(options, database, filename) {
228
+ if (!database) {
229
+ const dumps = await _makes.FS.readdir(this.dataPath("dump"));
230
+ if (dumps.length === 0) {
231
+ throw new Error("No dumps found");
232
+ }
233
+ database = await (0, _utils.promptSelect)({
234
+ message: "Database",
235
+ options: dumps
236
+ });
237
+ }
238
+ if (!database) {
239
+ throw new Error("Need database name");
240
+ }
241
+ const dirs = await _makes.FS.readdir(this.dataPath("dump", database));
242
+ if (!filename) {
243
+ filename = await (0, _utils.promptSelect)({
244
+ message: "File",
245
+ options: dirs
246
+ });
247
+ }
248
+ const path = this.dataPath("dump", database, filename);
249
+ const file = _makes.FS.createReadStream(path);
250
+ const stream = await _makes.Docker.exec("mariadb.workspace", ["mysql", "-uroot", "-ptoor", database], false);
251
+ file.on("data", data => {
252
+ stream.write(data);
253
+ });
254
+ file.on("end", () => {
255
+ stream.write("exit\n");
256
+ });
257
+ stream.on("error", data => {
258
+ process.stderr.write((0, _utils.demuxOutput)(data));
259
+ stream.write("exit\n");
260
+ file.close();
261
+ });
262
+ }
263
+ async dump(options, database) {
264
+ const date = (0, _format.default)(new Date(), "yyyy-MM-dd HH-mm");
265
+ const dirPath = this.dataPath("dump", database);
266
+ const filePath = this.dataPath("dump", database, `${date}.sql`);
267
+ if (!_makes.FS.existsSync(dirPath)) {
268
+ _makes.FS.mkdirSync(dirPath, {
269
+ recursive: true
270
+ });
271
+ }
272
+
273
+ // const filePath = Path.join(DATA_DIR, `${database} ${date}.sql`);
274
+ // console.log(filePath);
275
+
276
+ const file = _makes.FS.createWriteStream(filePath);
277
+ const stream = await _makes.Docker.exec("mariadb.workspace", ["mysqldump", "-uroot", "-ptoor", database]);
278
+ stream.pipe(file);
279
+ }
280
+ async import(options, database) {
281
+ const container = await _makes.Docker.getContainer("mariadb.workspace");
282
+ const exec = await container.exec({
283
+ AttachStdin: true,
284
+ AttachStdout: true,
285
+ AttachStderr: true,
286
+ Tty: process.stdin.isTTY,
287
+ Cmd: ["mysql", `-u${this.user}`, `-p${this.password}`, database]
288
+ });
289
+ const stream = await exec.start({
290
+ hijack: true,
291
+ stdin: true,
292
+ Tty: process.stdin.isTTY
293
+ });
294
+ await _makes.Docker.attachStream(stream);
295
+ }
296
+ }
297
+ exports.MariadbPlugin = MariadbPlugin;
@@ -0,0 +1,20 @@
1
+ import { Cli } from "@kearisp/cli";
2
+ import { Plugin } from "src/makes";
3
+ declare class MongodbPlugin extends Plugin {
4
+ container: string;
5
+ adminContainer: string;
6
+ constructor();
7
+ install(cli: Cli): void;
8
+ getDatabases(): Promise<string[]>;
9
+ getDatabasesDumps(): Promise<string[]>;
10
+ start(): Promise<void>;
11
+ startAdmin(): Promise<void>;
12
+ stop(): Promise<void>;
13
+ stopDB(): Promise<void>;
14
+ stopAdmin(): Promise<void>;
15
+ restart(): Promise<void>;
16
+ backup(database?: string): Promise<void>;
17
+ deleteBackup(database?: string, filename?: string, yes?: boolean): Promise<void>;
18
+ restore(database?: string, filename?: string): Promise<void>;
19
+ }
20
+ export { MongodbPlugin };
@@ -0,0 +1,271 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.MongodbPlugin = void 0;
7
+ var Path = _interopRequireWildcard(require("path"));
8
+ var dateFns = _interopRequireWildcard(require("date-fns"));
9
+ var _env = require("../env");
10
+ var _utils = require("../utils");
11
+ var _makes = require("../makes");
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
+ class MongodbPlugin extends _makes.Plugin {
15
+ container = "mongodb.workspace";
16
+ adminContainer = "dbadmin-mongodb.workspace";
17
+ constructor() {
18
+ super("mongodb");
19
+ this.dataDir = Path.join(_env.DATA_DIR, "db/mongodb");
20
+ }
21
+ install(cli) {
22
+ super.install(cli);
23
+ cli.command("mongodb:start").action(() => {
24
+ return this.start();
25
+ });
26
+ cli.command("mongodb:stop").action(() => {
27
+ return this.stop();
28
+ });
29
+ cli.command("mongodb:restart").action(() => {
30
+ return this.restart();
31
+ });
32
+ cli.command("mongodb:backup [database]").completion("database", () => {
33
+ return this.getDatabases();
34
+ }).action((options, database) => {
35
+ return this.backup(database);
36
+ });
37
+ cli.command("mongodb:restore [database] [filename]").completion("database", () => {
38
+ const dumpPath = this.dataPath("dump");
39
+ return _makes.FS.readdir(dumpPath);
40
+ }).completion("filename", (options, database) => {
41
+ if (!database) {
42
+ return [];
43
+ }
44
+ const dirPath = this.dataPath("dump", database);
45
+ if (!_makes.FS.existsSync(dirPath)) {
46
+ return [];
47
+ }
48
+ return _makes.FS.readdir(dirPath);
49
+ }).action((options, database, filename) => {
50
+ return this.restore(database, filename);
51
+ });
52
+ cli.command("mongodb:delete-backup [database] [filename]").option("yes", {
53
+ type: "boolean",
54
+ alias: "y"
55
+ }).completion("database", () => {
56
+ return this.getDatabasesDumps();
57
+ }).completion("filename", (options, database) => {
58
+ if (!database) {
59
+ return [];
60
+ }
61
+ const dumpPath = this.dataPath("dump", database);
62
+ if (!_makes.FS.existsSync(dumpPath)) {
63
+ return [];
64
+ }
65
+ return _makes.FS.readdirFiles(dumpPath);
66
+ }).action((options, database, filename) => {
67
+ return this.deleteBackup(database, filename, options.yes);
68
+ });
69
+ }
70
+ async getDatabases() {
71
+ const stream = await _makes.Docker.exec(this.container, ["mongosh", "--username", "root", "--password", "toor", "--quiet", "--eval", "db.getMongo().getDBNames().forEach(function(i){print(i)})"], false);
72
+ let res = "";
73
+ stream.on("data", data => {
74
+ res += (0, _utils.demuxOutput)(data).toString();
75
+ });
76
+ await new Promise((resolve, reject) => {
77
+ stream.on("end", resolve);
78
+ stream.on("error", reject);
79
+ });
80
+ return res.split(/\r?\n/).filter(database => {
81
+ return !!database;
82
+ });
83
+ }
84
+ async getDatabasesDumps() {
85
+ const dumpDir = this.dataPath("dump");
86
+ return _makes.FS.readdir(dumpDir);
87
+ }
88
+ async start() {
89
+ console.log("Mongidb starting...");
90
+ await _makes.Docker.pullImage("mongo:latest");
91
+ const container = await _makes.Docker.createContainer({
92
+ name: this.container,
93
+ restart: "always",
94
+ image: "mongo:latest",
95
+ volumes: [`${this.dataPath()}:/data/db`],
96
+ ports: ["27017:27017"],
97
+ env: {
98
+ MONGO_INITDB_ROOT_USERNAME: "root",
99
+ MONGO_INITDB_ROOT_PASSWORD: "toor",
100
+ MONGO_ROOT_USER: "root",
101
+ MONGO_ROOT_PASSWORD: "toor"
102
+ }
103
+ });
104
+ await container.start();
105
+ await this.startAdmin();
106
+ }
107
+ async startAdmin() {
108
+ console.log("Mongodb Admin starting...");
109
+ await _makes.Docker.pullImage("mongo-express:latest");
110
+ const container = await _makes.Docker.createContainer({
111
+ name: this.adminContainer,
112
+ restart: "always",
113
+ env: {
114
+ ME_CONFIG_MONGODB_SERVER: this.container,
115
+ ME_CONFIG_MONGODB_PORT: "27017",
116
+ MONGO_ROOT_USER: "root",
117
+ MONGO_ROOT_PASSWORD: "toor",
118
+ ME_CONFIG_MONGODB_ADMINUSERNAME: "root",
119
+ ME_CONFIG_MONGODB_ADMINPASSWORD: "toor",
120
+ ME_CONFIG_MONGODB_ENABLE_ADMIN: "true",
121
+ ME_CONFIG_MONGODB_AUTH_DATABASE: "admin",
122
+ ME_CONFIG_MONGODB_AUTH_USERNAME: "root",
123
+ ME_CONFIG_MONGODB_AUTH_PASSWORD: "toor",
124
+ VIRTUAL_HOST: this.adminContainer,
125
+ VIRTUAL_PORT: "8081"
126
+ },
127
+ image: "mongo-express:latest"
128
+ });
129
+ await container.start();
130
+ }
131
+ async stop() {
132
+ await this.stopDB();
133
+ await this.stopAdmin();
134
+ }
135
+ async stopDB() {
136
+ console.log("Mongodb stopping...");
137
+ const container = await _makes.Docker.getContainer(this.container);
138
+ if (container) {
139
+ try {
140
+ await container.stop();
141
+ await container.remove();
142
+ } catch (err) {
143
+ _makes.Logger.error(err.message);
144
+ }
145
+ }
146
+ }
147
+ async stopAdmin() {
148
+ console.log("Mongodb Admin stopping...");
149
+ const container = await _makes.Docker.getContainer(this.adminContainer);
150
+ if (container) {
151
+ await container.stop().catch(() => {
152
+ //
153
+ });
154
+ await container.remove().catch(() => {
155
+ //
156
+ });
157
+ }
158
+ }
159
+ async restart() {
160
+ await this.stop();
161
+ await this.start();
162
+ }
163
+ async backup(database) {
164
+ if (!database) {
165
+ database = await (0, _utils.promptSelect)({
166
+ message: "Database",
167
+ options: await this.getDatabases()
168
+ });
169
+ }
170
+ const date = dateFns.format(new Date(), "yyyy-MM-dd HH-mm");
171
+ const dirPath = this.dataPath("dump", database);
172
+ const filePath = this.dataPath("dump", database, `${date}.gz`);
173
+ if (!_makes.FS.existsSync(dirPath)) {
174
+ _makes.FS.mkdirSync(dirPath, {
175
+ recursive: true
176
+ });
177
+ }
178
+ const stream = await _makes.Docker.exec(this.container, ["mongodump", "--authenticationDatabase", "admin", "--host", `${this.container}:27017`, "--username", "root", "--password", "toor", "--db", database, "--archive", "--gzip"], false);
179
+ const file = _makes.FS.createWriteStream(filePath);
180
+ stream.on("data", data => {
181
+ file.write((0, _utils.demuxOutput)(data));
182
+ });
183
+ await new Promise((resolve, reject) => {
184
+ stream.on("end", resolve);
185
+ stream.on("error", reject);
186
+ });
187
+ }
188
+ async deleteBackup(database, filename, yes) {
189
+ if (!database) {
190
+ database = await (0, _utils.promptSelect)({
191
+ message: "Database",
192
+ options: await this.getDatabasesDumps()
193
+ });
194
+ }
195
+ if (!database) {
196
+ throw new Error("No database");
197
+ }
198
+ const dirPath = this.dataPath("dump", database);
199
+ if (!_makes.FS.existsSync(dirPath)) {
200
+ throw new Error(`Backups dir for database "${database}" not found`);
201
+ }
202
+ if (!filename) {
203
+ const files = await _makes.FS.readdirFiles(dirPath);
204
+ if (files.length === 0) {
205
+ throw new Error(`No backups for ${database}`);
206
+ }
207
+ filename = await (0, _utils.promptSelect)({
208
+ message: "File",
209
+ options: files
210
+ });
211
+ }
212
+ const filePath = this.dataPath("dump", database, filename);
213
+ if (!_makes.FS.existsSync(filePath)) {
214
+ throw new Error(`Backup "${filename}" not found`);
215
+ }
216
+ if (!yes) {
217
+ yes = await (0, _utils.promptConfirm)({
218
+ message: `Delete ${filename}?`,
219
+ default: false
220
+ });
221
+ }
222
+ if (!yes) {
223
+ return;
224
+ }
225
+ await _makes.FS.rm(filePath);
226
+ const otherFiles = await _makes.FS.readdir(dirPath);
227
+ if (otherFiles.length === 0) {
228
+ await _makes.FS.rm(dirPath, {
229
+ force: true,
230
+ recursive: true
231
+ });
232
+ }
233
+ }
234
+ async restore(database, filename) {
235
+ if (!database) {
236
+ const dumps = await _makes.FS.readdir(this.dataPath("dump"));
237
+ if (dumps.length === 0) {
238
+ throw new Error("No dumps found");
239
+ }
240
+ database = await (0, _utils.promptSelect)({
241
+ message: "Database",
242
+ options: dumps
243
+ });
244
+ }
245
+ if (!database) {
246
+ throw new Error("Need database name");
247
+ }
248
+ if (!filename) {
249
+ const filenames = await _makes.FS.readdir(this.dataPath("dump", database));
250
+ filename = await (0, _utils.promptSelect)({
251
+ message: "File",
252
+ options: filenames
253
+ });
254
+ }
255
+ const path = this.dataPath("dump", database, filename);
256
+ const file = _makes.FS.createReadStream(path);
257
+ const stream = await _makes.Docker.exec(this.container, ["mongorestore", "--authenticationDatabase", "admin", "--host", `${this.container}:27017`, "--username", "root", "--password", "toor", "--db", database, "--drop", "--gzip", "--archive"], false);
258
+ file.on("data", data => {
259
+ stream.write(data);
260
+ });
261
+ file.on("end", () => {
262
+ throw new Error("File end");
263
+ });
264
+ stream.on("error", err => {
265
+ file.close();
266
+ throw err;
267
+ });
268
+ console.log(path);
269
+ }
270
+ }
271
+ exports.MongodbPlugin = MongodbPlugin;
@@ -0,0 +1,37 @@
1
+ import { Cli } from "@kearisp/cli";
2
+ import { Plugin } from "src/makes";
3
+ import { Project } from "src/models";
4
+ import { AppEventsService, ProjectService } from "src/services";
5
+ type StartOptions = {
6
+ name?: string;
7
+ detach?: string;
8
+ };
9
+ type StopOptions = {
10
+ name?: string;
11
+ };
12
+ type LogsOptions = {
13
+ name?: string;
14
+ };
15
+ type AttachOptions = {
16
+ name?: string;
17
+ };
18
+ type ForwardingOptions = {
19
+ name?: string;
20
+ };
21
+ declare class NgrokPlugin extends Plugin {
22
+ protected appEventsService: AppEventsService;
23
+ protected projectService: ProjectService;
24
+ constructor(appEventsService: AppEventsService, projectService: ProjectService);
25
+ install(cli: Cli): void;
26
+ init(options: any): Promise<void>;
27
+ getForwarding(project: Project): Promise<string | undefined>;
28
+ onProjectStart(project: Project): Promise<void>;
29
+ onProjectStop(project: Project): Promise<void>;
30
+ start(options: StartOptions): Promise<void>;
31
+ stop(options: StopOptions): Promise<void>;
32
+ restart(options: StartOptions): Promise<void>;
33
+ logs(options: LogsOptions): Promise<void>;
34
+ attach(options: AttachOptions): Promise<void>;
35
+ forwarding(options: ForwardingOptions): Promise<string>;
36
+ }
37
+ export { NgrokPlugin };