system.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. const { currentLoad, mem, networkStats, fsSize, dockerContainerStats } = require('systeminformation');
  2. var Docker = require('dockerode');
  3. var docker = new Docker({ socketPath: '/var/run/docker.sock' });
  4. const { dashCard } = require('../components/dashCard');
  5. // export docker
  6. module.exports.docker = docker;
  7. module.exports.serverStats = async function () {
  8. const cpuUsage = await currentLoad();
  9. const ramUsage = await mem();
  10. const netUsage = await networkStats();
  11. const diskUsage = await fsSize();
  12. const info = {
  13. cpu: Math.round(cpuUsage.currentLoad),
  14. ram: Math.round((ramUsage.active / ramUsage.total) * 100),
  15. tx: netUsage[0].tx_bytes,
  16. rx: netUsage[0].rx_bytes,
  17. disk: diskUsage[0].use,
  18. };
  19. return info;
  20. }
  21. module.exports.containerList = async function () {
  22. let card_list = '';
  23. const data = await docker.listContainers({ all: true });
  24. for (const container of data) {
  25. if ((container.Names[0].slice(1) != 'DweebUI') && (container.Names[0].slice(1) != 'DweebCache')) {
  26. let imageVersion = container.Image.split('/');
  27. let service = imageVersion[imageVersion.length - 1].split(':')[0];
  28. let containerId = docker.getContainer(container.Id);
  29. let containerInfo = await containerId.inspect();
  30. // Get ports //////////////////////////
  31. let ports_list = [];
  32. try {
  33. for (const [key, value] of Object.entries(containerInfo.HostConfig.PortBindings)) {
  34. let ports = {
  35. check : 'checked',
  36. external: value[0].HostPort,
  37. internal: key.split('/')[0],
  38. protocol: key.split('/')[1]
  39. }
  40. ports_list.push(ports);
  41. }
  42. } catch { console.log('no ports') }
  43. for (let i = 0; i < 12; i++) {
  44. if (ports_list[i] == undefined) {
  45. let ports = {
  46. check : '',
  47. external: '',
  48. internal: '',
  49. protocol: ''
  50. }
  51. ports_list[i] = ports;
  52. }
  53. } /////////////////////////////////////
  54. // Get volumes ////////////////////////
  55. let volumes_list = [];
  56. try { for (const [key, value] of Object.entries(containerInfo.HostConfig.Binds)) {
  57. let volumes = {
  58. check : 'checked',
  59. bind: value.split(':')[0],
  60. container: value.split(':')[1],
  61. readwrite: value.split(':')[2]
  62. }
  63. volumes_list.push(volumes);
  64. }} catch { console.log('no volumes') }
  65. for (let i = 0; i < 12; i++) {
  66. if (volumes_list[i] == undefined) {
  67. let volumes = {
  68. check : '',
  69. bind: '',
  70. container: '',
  71. readwrite: ''
  72. }
  73. volumes_list[i] = volumes;
  74. }
  75. } /////////////////////////////////////
  76. // Get environment variables.
  77. let environment_variables = [];
  78. try { for (const [key, value] of Object.entries(containerInfo.Config.Env)) {
  79. let env = {
  80. check : 'checked',
  81. name: value.split('=')[0],
  82. default: value.split('=')[1]
  83. }
  84. environment_variables.push(env);
  85. }} catch { console.log('no env') }
  86. for (let i = 0; i < 12; i++) {
  87. if (environment_variables[i] == undefined) {
  88. let env = {
  89. check : '',
  90. name: '',
  91. default: ''
  92. }
  93. environment_variables[i] = env;
  94. }
  95. }
  96. // Get labels.
  97. let labels = [];
  98. for (const [key, value] of Object.entries(containerInfo.Config.Labels)) {
  99. let label = {
  100. check : 'checked',
  101. name: key,
  102. value: value
  103. }
  104. labels.push(label);
  105. }
  106. for (let i = 0; i < 12; i++) {
  107. if (labels[i] == undefined) {
  108. let label = {
  109. check : '',
  110. name: '',
  111. value: ''
  112. }
  113. labels[i] = label;
  114. }
  115. }
  116. let container_info = {
  117. name: container.Names[0].slice(1),
  118. service: service,
  119. id: container.Id,
  120. state: container.State,
  121. image: container.Image,
  122. external_port: ports_list[0].external || 0,
  123. internal_port: ports_list[0].internal || 0,
  124. ports: ports_list,
  125. volumes: volumes_list,
  126. environment_variables: environment_variables,
  127. labels: labels,
  128. }
  129. let dockerCard = dashCard(container_info);
  130. card_list += dockerCard;
  131. }
  132. }
  133. return card_list;
  134. }
  135. module.exports.containerStats = async function () {
  136. let container_stats = [];
  137. const data = await docker.listContainers({ all: true });
  138. for (const container of data) {
  139. if ((container.Names[0].slice(1) != 'DweebUI') && (container.Names[0].slice(1) != 'DweebCache')) {
  140. const stats = await dockerContainerStats(container.Id);
  141. let container_stat = {
  142. name: container.Names[0].slice(1),
  143. cpu: Math.round(stats[0].cpuPercent),
  144. ram: Math.round(stats[0].memPercent)
  145. }
  146. //push stats to an array
  147. container_stats.push(container_stat);
  148. }
  149. }
  150. return container_stats;
  151. }
  152. module.exports.containerAction = async function (data) {
  153. let { user, role, action, container, state } = data;
  154. console.log(`${user} wants to: ${action} ${container}`);
  155. if (role == 'admin') {
  156. var containerName = docker.getContainer(container);
  157. if ((action == 'start') && (state == 'stopped')) {
  158. containerName.start();
  159. } else if ((action == 'start') && (state == 'paused')) {
  160. containerName.unpause();
  161. } else if ((action == 'stop') && (state != 'stopped')) {
  162. containerName.stop();
  163. } else if ((action == 'pause') && (state == 'running')) {
  164. containerName.pause();
  165. } else if ((action == 'pause') && (state == 'paused')) {
  166. containerName.unpause();
  167. } else if (action == 'restart') {
  168. containerName.restart();
  169. }
  170. } else {
  171. console.log('User is not an admin');
  172. }
  173. }
  174. module.exports.containerExec = async function (data) {
  175. let { container, command } = data;
  176. var containerName = docker.getContainer(container);
  177. var options = {
  178. Cmd: ['/bin/sh', '-c', command],
  179. AttachStdout: true,
  180. AttachStderr: true,
  181. Tty: true
  182. };
  183. containerName.exec(options, function (err, exec) {
  184. if (err) return;
  185. exec.start(function (err, stream) {
  186. if (err) return;
  187. containerName.modem.demuxStream(stream, process.stdout, process.stderr);
  188. exec.inspect(function (err, data) {
  189. if (err) return;
  190. });
  191. });
  192. });
  193. }