system.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. const { currentLoad, mem, networkStats, fsSize, dockerContainerStats, dockerImages, networkInterfaces, dockerInfo } = require('systeminformation');
  2. var Docker = require('dockerode');
  3. var docker = new Docker({ socketPath: '/var/run/docker.sock' });
  4. const { dashCard } = require('../components/dashCard');
  5. const { Readable } = require('stream');
  6. const Containers = require('../database/ContainerModel');
  7. // export docker
  8. module.exports.docker = docker;
  9. let IPv4 = '';
  10. networkInterfaces().then(data => {
  11. IPv4 = data[0].ip4;
  12. });
  13. let hidden = '';
  14. module.exports.hiddenContainers = async function () {
  15. hidden = await Containers.findAll({ where: {visibility:false}});
  16. hidden = hidden.map(a => a.name);
  17. }
  18. module.exports.serverStats = async function () {
  19. const cpuUsage = await currentLoad();
  20. const ramUsage = await mem();
  21. const netUsage = await networkStats();
  22. const diskUsage = await fsSize();
  23. const info = {
  24. cpu: Math.round(cpuUsage.currentLoad),
  25. ram: Math.round((ramUsage.active / ramUsage.total) * 100),
  26. tx: netUsage[0].tx_bytes,
  27. rx: netUsage[0].rx_bytes,
  28. disk: diskUsage[0].use,
  29. };
  30. return info;
  31. }
  32. module.exports.containerList = async function () {
  33. let card_list = '';
  34. const data = await docker.listContainers({ all: true });
  35. for (const container of data) {
  36. if (!hidden.includes(container.Names[0].slice(1))) {
  37. let imageVersion = container.Image.split('/');
  38. let service = imageVersion[imageVersion.length - 1].split(':')[0];
  39. let containerId = docker.getContainer(container.Id);
  40. let containerInfo = await containerId.inspect();
  41. // Get ports //////////////////////////
  42. let ports_list = [];
  43. try {
  44. for (const [key, value] of Object.entries(containerInfo.HostConfig.PortBindings)) {
  45. let ports = {
  46. check : 'checked',
  47. external: value[0].HostPort,
  48. internal: key.split('/')[0],
  49. protocol: key.split('/')[1]
  50. }
  51. ports_list.push(ports);
  52. }
  53. } catch {
  54. // console.log('no ports')
  55. }
  56. for (let i = 0; i < 12; i++) {
  57. if (ports_list[i] == undefined) {
  58. let ports = {
  59. check : '',
  60. external: '',
  61. internal: '',
  62. protocol: ''
  63. }
  64. ports_list[i] = ports;
  65. }
  66. } /////////////////////////////////////
  67. // Get volumes ////////////////////////
  68. let volumes_list = [];
  69. try { for (const [key, value] of Object.entries(containerInfo.HostConfig.Binds)) {
  70. let volumes = {
  71. check : 'checked',
  72. bind: value.split(':')[0],
  73. container: value.split(':')[1],
  74. readwrite: value.split(':')[2]
  75. }
  76. volumes_list.push(volumes);
  77. }} catch {
  78. // console.log('no volumes')
  79. }
  80. for (let i = 0; i < 12; i++) {
  81. if (volumes_list[i] == undefined) {
  82. let volumes = {
  83. check : '',
  84. bind: '',
  85. container: '',
  86. readwrite: ''
  87. }
  88. volumes_list[i] = volumes;
  89. }
  90. } /////////////////////////////////////
  91. // Get environment variables.
  92. let environment_variables = [];
  93. try { for (const [key, value] of Object.entries(containerInfo.Config.Env)) {
  94. let env = {
  95. check : 'checked',
  96. name: value.split('=')[0],
  97. default: value.split('=')[1]
  98. }
  99. environment_variables.push(env);
  100. }} catch { console.log('no env') }
  101. for (let i = 0; i < 12; i++) {
  102. if (environment_variables[i] == undefined) {
  103. let env = {
  104. check : '',
  105. name: '',
  106. default: ''
  107. }
  108. environment_variables[i] = env;
  109. }
  110. }
  111. // Get labels.
  112. let labels = [];
  113. for (const [key, value] of Object.entries(containerInfo.Config.Labels)) {
  114. let label = {
  115. check : 'checked',
  116. name: key,
  117. value: value
  118. }
  119. labels.push(label);
  120. }
  121. for (let i = 0; i < 12; i++) {
  122. if (labels[i] == undefined) {
  123. let label = {
  124. check : '',
  125. name: '',
  126. value: ''
  127. }
  128. labels[i] = label;
  129. }
  130. }
  131. let container_info = {
  132. name: container.Names[0].slice(1),
  133. service: service,
  134. id: container.Id,
  135. state: container.State,
  136. image: container.Image,
  137. external_port: ports_list[0].external || 0,
  138. internal_port: ports_list[0].internal || 0,
  139. ports: ports_list,
  140. volumes: volumes_list,
  141. environment_variables: environment_variables,
  142. labels: labels,
  143. IPv4: IPv4,
  144. style: "Compact"
  145. }
  146. let dockerCard = dashCard(container_info);
  147. card_list += dockerCard;
  148. }
  149. }
  150. return card_list;
  151. }
  152. module.exports.containerStats = async function () {
  153. let container_stats = [];
  154. const data = await docker.listContainers({ all: true });
  155. for (const container of data) {
  156. if (!hidden.includes(container.Names[0].slice(1))) {
  157. const stats = await dockerContainerStats(container.Id);
  158. let container_stat = {
  159. name: container.Names[0].slice(1),
  160. cpu: Math.round(stats[0].cpuPercent),
  161. ram: Math.round(stats[0].memPercent)
  162. }
  163. //push stats to an array
  164. container_stats.push(container_stat);
  165. }
  166. }
  167. return container_stats;
  168. }
  169. module.exports.containerAction = async function (data) {
  170. let { user, role, action, container, state } = data;
  171. console.log(`${user} wants to: ${action} ${container}`);
  172. if (role == 'admin') {
  173. var containerName = docker.getContainer(container);
  174. if ((action == 'start') && (state == 'stopped')) {
  175. containerName.start();
  176. } else if ((action == 'start') && (state == 'paused')) {
  177. containerName.unpause();
  178. } else if ((action == 'stop') && (state != 'stopped')) {
  179. containerName.stop();
  180. } else if ((action == 'pause') && (state == 'running')) {
  181. containerName.pause();
  182. } else if ((action == 'pause') && (state == 'paused')) {
  183. containerName.unpause();
  184. } else if (action == 'restart') {
  185. containerName.restart();
  186. }
  187. } else {
  188. console.log('User is not an admin');
  189. }
  190. }
  191. module.exports.containerExec = async function (data) {
  192. let { container, command } = data;
  193. var containerName = docker.getContainer(container);
  194. var options = {
  195. Cmd: ['/bin/sh', '-c', command],
  196. AttachStdout: true,
  197. AttachStderr: true,
  198. Tty: true
  199. };
  200. containerName.exec(options, function (err, exec) {
  201. if (err) return;
  202. exec.start(function (err, stream) {
  203. if (err) return;
  204. containerName.modem.demuxStream(stream, process.stdout, process.stderr);
  205. exec.inspect(function (err, data) {
  206. if (err) return;
  207. });
  208. });
  209. });
  210. }
  211. module.exports.containerLogs = function (data) {
  212. return new Promise((resolve, reject) => {
  213. let logString = '';
  214. var options = {
  215. follow: false,
  216. stdout: true,
  217. stderr: false,
  218. timestamps: false
  219. };
  220. var containerName = docker.getContainer(data);
  221. containerName.logs(options, function (err, stream) {
  222. if (err) {
  223. reject(err);
  224. return;
  225. }
  226. const readableStream = Readable.from(stream);
  227. readableStream.on('data', function (chunk) {
  228. logString += chunk.toString('utf8');
  229. });
  230. readableStream.on('end', function () {
  231. resolve(logString);
  232. });
  233. });
  234. });
  235. };
  236. module.exports.dockerImages = async function () {
  237. // get the names, tags, status, created date, and size of all images
  238. const data1 = await dockerImages({ all: true });
  239. const data2 = await docker.listImages({ all: true });
  240. // for ( i = 0; i < data.length; i++) {
  241. // console.log(`Image ${i}:`)
  242. // console.log(`repoTags: ${data[i].repoTags}`)
  243. // }
  244. // console.log(data1);
  245. console.log(data2);
  246. }
  247. module.exports.dockerVolumes = async function () {
  248. let volume_list = '';
  249. const data = await docker.listVolumes();
  250. return data;
  251. // for (const volume of data.Volumes) {
  252. // let volume_info = {
  253. // name: volume.Name,
  254. // style: "Compact"
  255. // }
  256. // let dockerCard = dashCard(volume_info);
  257. // volume_list += dockerCard;
  258. // }
  259. // return volume_list;
  260. }
  261. module.exports.dockerNetworks = async function () {
  262. let network_list = '';
  263. const data = await docker.listNetworks();
  264. return data;
  265. // for (const network of data) {
  266. // let network_info = {
  267. // name: network.Name,
  268. // style: "Compact"
  269. // }
  270. // let dockerCard = dashCard(network_info);
  271. // network_list += dockerCard;
  272. // }
  273. // return network_list;
  274. }