app_actions.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 container_info = {
  25. name: name,
  26. service: service_name,
  27. state: 'installing',
  28. image: image,
  29. restart_policy: restart_policy
  30. }
  31. let installCard = dashCard(container_info);
  32. req.app.locals.install = installCard;
  33. let compose_file = `version: '3'`;
  34. compose_file += `\nservices:`
  35. compose_file += `\n ${service_name}:`
  36. compose_file += `\n container_name: ${name}`;
  37. compose_file += `\n image: ${image}`;
  38. // Command
  39. if (command_check == 'on') {
  40. compose_file += `\n command: ${command}`
  41. }
  42. // Network mode
  43. if (net_mode == 'host') {
  44. compose_file += `\n network_mode: 'host'`
  45. }
  46. else if (net_mode != 'host' && net_mode != 'docker') {
  47. compose_file += `\n network_mode: '${net_mode}'`
  48. }
  49. // Restart policy
  50. if (restart_policy != '') {
  51. compose_file += `\n restart: ${restart_policy}`
  52. }
  53. // Ports
  54. if ((port0 == 'on' || port1 == 'on' || port2 == 'on' || port3 == 'on' || port4 == 'on' || port5 == 'on') && (net_mode != 'host')) {
  55. compose_file += `\n ports:`
  56. for (let i = 0; i < 6; i++) {
  57. if (req.body[`port${i}`] == 'on') {
  58. compose_file += `\n - ${req.body[`port_${i}_external`]}:${req.body[`port_${i}_internal`]}/${req.body[`port_${i}_protocol`]}`
  59. }
  60. }
  61. }
  62. // Volumes
  63. if (volume0 == 'on' || volume1 == 'on' || volume2 == 'on' || volume3 == 'on' || volume4 == 'on' || volume5 == 'on') {
  64. compose_file += `\n volumes:`
  65. for (let i = 0; i < 6; i++) {
  66. if (req.body[`volume${i}`] == 'on') {
  67. compose_file += `\n - ${req.body[`volume_${i}_bind`]}:${req.body[`volume_${i}_container`]}:${req.body[`volume_${i}_readwrite`]}`
  68. }
  69. }
  70. }
  71. // Environment variables
  72. 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') {
  73. compose_file += `\n environment:`
  74. }
  75. for (let i = 0; i < 12; i++) {
  76. if (req.body[`env${i}`] == 'on') {
  77. compose_file += `\n - ${req.body[`env_${i}_name`]}=${req.body[`env_${i}_default`]}`
  78. }
  79. }
  80. // Add labels
  81. 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') {
  82. compose_file += `\n labels:`
  83. }
  84. for (let i = 0; i < 12; i++) {
  85. if (req.body[`label${i}`] == 'on') {
  86. compose_file += `\n - ${req.body[`label_${i}_name`]}=${req.body[`label_${i}_value`]}`
  87. }
  88. }
  89. // Add privileged mode
  90. if (req.body.privileged == 'on') {
  91. compose_file += `\n privileged: true`
  92. }
  93. // Add hardware acceleration to the docker-compose file if one of the environment variables has the label DRINODE
  94. 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') {
  95. for (let i = 0; i < 12; i++) {
  96. if (req.body[`env${i}`] == 'on') {
  97. if (req.body[`env_${i}_name`] == 'DRINODE') {
  98. compose_file += `\n deploy:`
  99. compose_file += `\n resources:`
  100. compose_file += `\n reservations:`
  101. compose_file += `\n devices:`
  102. compose_file += `\n - driver: nvidia`
  103. compose_file += `\n count: 1`
  104. compose_file += `\n capabilities: [gpu]`
  105. }
  106. }
  107. }
  108. }
  109. try {
  110. mkdirSync(`./appdata/${name}`, { recursive: true });
  111. writeFileSync(`./appdata/${name}/docker-compose.yml`, compose_file, function (err) { console.log(err) });
  112. exec(`docker compose -f ./appdata/${name}/docker-compose.yml up -d`, (error, stdout, stderr) => {
  113. if (error) { console.error(`error: ${error.message}`); return; }
  114. if (stderr) { console.error(`stderr: ${stderr}`); return; }
  115. console.log(`stdout:\n${stdout}`);
  116. });
  117. } catch { console.log('error creating directory or compose file') }
  118. }
  119. // Redirect to the home page
  120. res.redirect("/");
  121. } else {
  122. // Redirect to the login page
  123. res.redirect("/login");
  124. }
  125. }
  126. exports.Uninstall = async function (req, res) {
  127. if (req.session.role == "admin") {
  128. if (req.body.confirm == 'Yes') {
  129. exec(`docker compose -f ./appdata/${req.body.service_name}/docker-compose.yml down`, (error, stdout, stderr) => {
  130. if (error) { console.error(`error: ${error.message}`); return; }
  131. if (stderr) { console.error(`stderr: ${stderr}`); return; }
  132. console.log(`stdout:\n${stdout}`);
  133. });
  134. }
  135. // Redirect to the home page
  136. res.redirect("/");
  137. } else {
  138. // Redirect to the login page
  139. res.redirect("/login");
  140. }
  141. }