install.js 7.7 KB

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