install.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { writeFileSync, mkdirSync, readFileSync } from "fs";
  2. import yaml from 'js-yaml';
  3. import { execSync } from "child_process";
  4. import { docker } from "../app.js";
  5. import DockerodeCompose from "dockerode-compose";
  6. export const Install = async (req, res) => {
  7. console.log('Install:')
  8. console.log(req.body);
  9. res.redirect('/');
  10. }
  11. // module.exports.install = async function (data) {
  12. // console.log(`[Start of install function]`);
  13. // let { service_name, name, image, command_check, command, net_mode, restart_policy } = data;
  14. // let { port0, port1, port2, port3, port4, port5 } = data;
  15. // let { volume0, volume1, volume2, volume3, volume4, volume5 } = data;
  16. // let { env0, env1, env2, env3, env4, env5, env6, env7, env8, env9, env10, env11 } = data;
  17. // let { label0, label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11 } = data;
  18. // let docker_volumes = [];
  19. // if (image.startsWith('https://')){
  20. // mkdirSync(`./appdata/${name}`, { recursive: true });
  21. // execSync(`curl -o ./appdata/${name}/${name}_stack.yml -L ${image}`);
  22. // console.log(`Downloaded stackfile: ${image}`);
  23. // let stackfile = yaml.load(readFileSync(`./appdata/${name}/${name}_stack.yml`, 'utf8'));
  24. // let services = Object.keys(stackfile.services);
  25. // for ( let i = 0; i < services.length; i++ ) {
  26. // try {
  27. // console.log(stackfile.services[Object.keys(stackfile.services)[i]].environment);
  28. // } catch { console.log('no env') }
  29. // }
  30. // } else {
  31. // let compose_file = `version: '3'`;
  32. // compose_file += `\nservices:`
  33. // compose_file += `\n ${service_name}:`
  34. // compose_file += `\n container_name: ${name}`;
  35. // compose_file += `\n image: ${image}`;
  36. // // Command
  37. // if (command_check == 'on') {
  38. // compose_file += `\n command: ${command}`
  39. // }
  40. // // Network mode
  41. // if (net_mode == 'host') {
  42. // compose_file += `\n network_mode: 'host'`
  43. // }
  44. // else if (net_mode != 'host' && net_mode != 'docker') {
  45. // compose_file += `\n network_mode: '${net_mode}'`
  46. // }
  47. // // Restart policy
  48. // if (restart_policy != '') {
  49. // compose_file += `\n restart: ${restart_policy}`
  50. // }
  51. // // Ports
  52. // if ((port0 == 'on' || port1 == 'on' || port2 == 'on' || port3 == 'on' || port4 == 'on' || port5 == 'on') && (net_mode != 'host')) {
  53. // compose_file += `\n ports:`
  54. // for (let i = 0; i < 6; i++) {
  55. // if (data[`port${i}`] == 'on') {
  56. // compose_file += `\n - ${data[`port_${i}_external`]}:${data[`port_${i}_internal`]}/${data[`port_${i}_protocol`]}`
  57. // }
  58. // }
  59. // }
  60. // // Volumes
  61. // if (volume0 == 'on' || volume1 == 'on' || volume2 == 'on' || volume3 == 'on' || volume4 == 'on' || volume5 == 'on') {
  62. // compose_file += `\n volumes:`
  63. // for (let i = 0; i < 6; i++) {
  64. // // if volume is on and neither bind or container is empty, it's a bind mount (ex /mnt/user/appdata/config:/config )
  65. // if ((data[`volume${i}`] == 'on') && (data[`volume_${i}_bind`] != '') && (data[`volume_${i}_container`] != '')) {
  66. // compose_file += `\n - ${data[`volume_${i}_bind`]}:${data[`volume_${i}_container`]}:${data[`volume_${i}_readwrite`]}`
  67. // }
  68. // // if bind is empty create a docker volume (ex container_name_config:/config) convert any '/' in container name to '_'
  69. // else if ((data[`volume${i}`] == 'on') && (data[`volume_${i}_bind`] == '') && (data[`volume_${i}_container`] != '')) {
  70. // let volume_name = data[`volume_${i}_container`].replace(/\//g, '_');
  71. // compose_file += `\n - ${name}_${volume_name}:${data[`volume_${i}_container`]}:${data[`volume_${i}_readwrite`]}`
  72. // docker_volumes.push(`${name}_${volume_name}`);
  73. // }
  74. // }
  75. // }
  76. // // Environment variables
  77. // 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') {
  78. // compose_file += `\n environment:`
  79. // }
  80. // for (let i = 0; i < 12; i++) {
  81. // if (data[`env${i}`] == 'on') {
  82. // compose_file += `\n - ${data[`env_${i}_name`]}=${data[`env_${i}_default`]}`
  83. // }
  84. // }
  85. // // Add labels
  86. // 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') {
  87. // compose_file += `\n labels:`
  88. // }
  89. // for (let i = 0; i < 12; i++) {
  90. // if (data[`label${i}`] == 'on') {
  91. // compose_file += `\n - ${data[`label_${i}_name`]}=${data[`label_${i}_value`]}`
  92. // }
  93. // }
  94. // // Add privileged mode
  95. // if (data.privileged == 'on') {
  96. // compose_file += `\n privileged: true`
  97. // }
  98. // // Add hardware acceleration to the docker-compose file if one of the environment variables has the label DRINODE
  99. // 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') {
  100. // for (let i = 0; i < 12; i++) {
  101. // if (data[`env${i}`] == 'on') {
  102. // if (data[`env_${i}_name`] == 'DRINODE') {
  103. // compose_file += `\n deploy:`
  104. // compose_file += `\n resources:`
  105. // compose_file += `\n reservations:`
  106. // compose_file += `\n devices:`
  107. // compose_file += `\n - driver: nvidia`
  108. // compose_file += `\n count: 1`
  109. // compose_file += `\n capabilities: [gpu]`
  110. // }
  111. // }
  112. // }
  113. // }
  114. // // add any docker volumes to the docker-compose file
  115. // if ( docker_volumes.length > 0 ) {
  116. // compose_file += `\n`
  117. // compose_file += `\nvolumes:`
  118. // // check docker_volumes for duplicates and remove them completely
  119. // docker_volumes = docker_volumes.filter((item, index) => docker_volumes.indexOf(item) === index)
  120. // for (let i = 0; i < docker_volumes.length; i++) {
  121. // if ( docker_volumes[i] != '') {
  122. // compose_file += `\n ${docker_volumes[i]}:`
  123. // }
  124. // }
  125. // }
  126. // try {
  127. // mkdirSync(`./appdata/${name}`, { recursive: true });
  128. // writeFileSync(`./appdata/${name}/docker-compose.yml`, compose_file, function (err) { console.log(err) });
  129. // } catch { console.log('error creating directory or compose file') }
  130. // try {
  131. // var compose = new DockerodeCompose(docker, `./appdata/${name}/docker-compose.yml`, `${name}`);
  132. // (async () => {
  133. // await compose.pull();
  134. // await compose.up();
  135. // })();
  136. // } catch { console.log('error running compose file')}
  137. // }
  138. // }
  139. // module.exports.uninstall = async function (data) {
  140. // if (data.confirm == 'Yes') {
  141. // console.log(`Uninstalling ${data.service_name}: ${data}`);
  142. // var containerName = docker.getContainer(`${data.service_name}`);
  143. // try {
  144. // await containerName.stop();
  145. // console.log(`Stopped ${data.service_name} container`);
  146. // } catch {
  147. // console.log(`Error stopping ${data.service_name} container`);
  148. // }
  149. // try {
  150. // await containerName.remove();
  151. // console.log(`Removed ${data.service_name} container`);
  152. // } catch {
  153. // console.log(`Error removing ${data.service_name} container`);
  154. // }
  155. // }
  156. // }