install.js 9.1 KB

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