app_actions.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const { writeFileSync, mkdirSync, readFileSync } = require("fs");
  2. const { exec, execSync } = require("child_process");
  3. const { dashCard } = require('../components/dashCard');
  4. const yaml = require('js-yaml');
  5. exports.Install = async function (req, res) {
  6. if (req.session.role == "admin") {
  7. let { service_name, name, image, command_check, command, net_mode, restart_policy } = req.body;
  8. let { port0, port1, port2, port3, port4, port5 } = req.body;
  9. let { volume0, volume1, volume2, volume3, volume4, volume5 } = req.body;
  10. let { env0, env1, env2, env3, env4, env5, env6, env7, env8, env9, env10, env11 } = req.body;
  11. let { label0, label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11 } = req.body;
  12. if (image.startsWith('https://')){
  13. mkdirSync(`./appdata/${name}`, { recursive: true });
  14. execSync(`curl -o ./appdata/${name}/${name}_stack.yml -L ${image}`);
  15. console.log(`Downloaded stackfile: ${image}`);
  16. let stackfile = yaml.load(readFileSync(`./appdata/${name}/${name}_stack.yml`, 'utf8'));
  17. let services = Object.keys(stackfile.services);
  18. for ( let i = 0; i < services.length; i++ ) {
  19. try {
  20. console.log(stackfile.services[Object.keys(stackfile.services)[i]].environment);
  21. } catch { console.log('no env') }
  22. }
  23. } else {
  24. let installCard = dashCard(req.body.name, req.body.service_name, '', 'installing', req.body.image, 0, 0);
  25. req.app.locals.install = installCard;
  26. let compose_file = `version: '3'`;
  27. compose_file += `\nservices:`
  28. compose_file += `\n ${service_name}:`
  29. compose_file += `\n container_name: ${name}`;
  30. compose_file += `\n image: ${image}`;
  31. // Command
  32. if (command_check == 'on') {
  33. compose_file += `\n command: ${command}`
  34. }
  35. // Network mode
  36. if (net_mode == 'host') {
  37. compose_file += `\n network_mode: 'host'`
  38. }
  39. else if (net_mode != 'host' && net_mode != 'docker') {
  40. compose_file += `\n network_mode: '${net_mode}'`
  41. }
  42. // Restart policy
  43. if (restart_policy != '') {
  44. compose_file += `\n restart: ${restart_policy}`
  45. }
  46. // Ports
  47. if ((port0 == 'on' || port1 == 'on' || port2 == 'on' || port3 == 'on' || port4 == 'on' || port5 == 'on') && (net_mode != 'host')) {
  48. compose_file += `\n ports:`
  49. for (let i = 0; i < 6; i++) {
  50. if (req.body[`port${i}`] == 'on') {
  51. compose_file += `\n - ${req.body[`port_${i}_external`]}:${req.body[`port_${i}_internal`]}/${req.body[`port_${i}_protocol`]}`
  52. }
  53. }
  54. }
  55. // Volumes
  56. if (volume0 == 'on' || volume1 == 'on' || volume2 == 'on' || volume3 == 'on' || volume4 == 'on' || volume5 == 'on') {
  57. compose_file += `\n volumes:`
  58. for (let i = 0; i < 6; i++) {
  59. if (req.body[`volume${i}`] == 'on') {
  60. compose_file += `\n - ${req.body[`volume_${i}_bind`]}:${req.body[`volume_${i}_container`]}:${req.body[`volume_${i}_readwrite`]}`
  61. }
  62. }
  63. }
  64. // Environment variables
  65. if (env0 == 'on' || env1 == 'on' || env2 == 'on' || env3 == 'on' || env4 == 'on' || env5 == 'on' || env6 == 'on' || env7 == 'on' || env8 == 'on' || env9 == 'on' || env10 == 'on' || env11 == 'on') {
  66. compose_file += `\n environment:`
  67. }
  68. for (let i = 0; i < 12; i++) {
  69. if (req.body[`env${i}`] == 'on') {
  70. compose_file += `\n - ${req.body[`env_${i}_name`]}=${req.body[`env_${i}_default`]}`
  71. }
  72. }
  73. // Add labels
  74. if (label0 == 'on' || label1 == 'on' || label2 == 'on' || label3 == 'on' || label4 == 'on' || label5 == 'on' || label6 == 'on' || label7 == 'on' || label8 == 'on' || label9 == 'on' || label10 == 'on' || label11 == 'on') {
  75. compose_file += `\n labels:`
  76. }
  77. for (let i = 0; i < 12; i++) {
  78. if (req.body[`label${i}`] == 'on') {
  79. compose_file += `\n - ${req.body[`label_${i}_name`]}=${req.body[`label_${i}_value`]}`
  80. }
  81. }
  82. // Add privileged mode
  83. if (req.body.privileged == 'on') {
  84. compose_file += `\n privileged: true`
  85. }
  86. // Add hardware acceleration to the docker-compose file if one of the environment variables has the label DRINODE
  87. if (env0 == 'on' || env1 == 'on' || env2 == 'on' || env3 == 'on' || env4 == 'on' || env5 == 'on' || env6 == 'on' || env7 == 'on' || env8 == 'on' || env9 == 'on' || env10 == 'on' || env11 == 'on') {
  88. for (let i = 0; i < 12; i++) {
  89. if (req.body[`env${i}`] == 'on') {
  90. if (req.body[`env_${i}_name`] == 'DRINODE') {
  91. compose_file += `\n deploy:`
  92. compose_file += `\n resources:`
  93. compose_file += `\n reservations:`
  94. compose_file += `\n devices:`
  95. compose_file += `\n - driver: nvidia`
  96. compose_file += `\n count: 1`
  97. compose_file += `\n capabilities: [gpu]`
  98. }
  99. }
  100. }
  101. }
  102. try {
  103. mkdirSync(`./appdata/${name}`, { recursive: true });
  104. writeFileSync(`./appdata/${name}/docker-compose.yml`, compose_file, function (err) { console.log(err) });
  105. exec(`docker compose -f ./appdata/${name}/docker-compose.yml up -d`, (error, stdout, stderr) => {
  106. if (error) { console.error(`error: ${error.message}`); return; }
  107. if (stderr) { console.error(`stderr: ${stderr}`); return; }
  108. console.log(`stdout:\n${stdout}`);
  109. });
  110. } catch { console.log('error creating directory or compose file') }
  111. }
  112. // Redirect to the home page
  113. res.redirect("/");
  114. } else {
  115. // Redirect to the login page
  116. res.redirect("/login");
  117. }
  118. }
  119. exports.Uninstall = async function (req, res) {
  120. if (req.session.role == "admin") {
  121. if (req.body.confirm == 'Yes') {
  122. exec(`docker compose -f ./appdata/${req.body.service_name}/docker-compose.yml down`, (error, stdout, stderr) => {
  123. if (error) { console.error(`error: ${error.message}`); return; }
  124. if (stderr) { console.error(`stderr: ${stderr}`); return; }
  125. console.log(`stdout:\n${stdout}`);
  126. });
  127. }
  128. // Redirect to the home page
  129. res.redirect("/");
  130. } else {
  131. // Redirect to the login page
  132. res.redirect("/login");
  133. }
  134. }