compose.js 8.6 KB

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