overview.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
  4. ]]--
  5. require "luci.util"
  6. local docker = require "luci.model.docker"
  7. local uci = require "luci.model.uci"
  8. function byte_format(byte)
  9. local suff = {"B", "KB", "MB", "GB", "TB"}
  10. for i=1, 5 do
  11. if byte > 1024 and i < 5 then
  12. byte = byte / 1024
  13. else
  14. return string.format("%.2f %s", byte, suff[i])
  15. end
  16. end
  17. end
  18. local map_dockerman = Map("dockerman", translate("Docker"), translate("DockerMan is a Simple Docker manager client for LuCI, If you have any issue please visit:") .. " ".. [[<a href="https://github.com/lisaac/luci-app-dockerman" target="_blank">]] ..translate("Github") .. [[</a>]])
  19. local docker_info_table = {}
  20. -- docker_info_table['0OperatingSystem'] = {_key=translate("Operating System"),_value='-'}
  21. -- docker_info_table['1Architecture'] = {_key=translate("Architecture"),_value='-'}
  22. -- docker_info_table['2KernelVersion'] = {_key=translate("Kernel Version"),_value='-'}
  23. docker_info_table['3ServerVersion'] = {_key=translate("Docker Version"),_value='-'}
  24. docker_info_table['4ApiVersion'] = {_key=translate("Api Version"),_value='-'}
  25. docker_info_table['5NCPU'] = {_key=translate("CPUs"),_value='-'}
  26. docker_info_table['6MemTotal'] = {_key=translate("Total Memory"),_value='-'}
  27. docker_info_table['7DockerRootDir'] = {_key=translate("Docker Root Dir"),_value='-'}
  28. docker_info_table['8IndexServerAddress'] = {_key=translate("Index Server Address"),_value='-'}
  29. docker_info_table['9RegistryMirrors'] = {_key=translate("Registry Mirrors"),_value='-'}
  30. local s = map_dockerman:section(Table, docker_info_table)
  31. s:option(DummyValue, "_key", translate("Info"))
  32. s:option(DummyValue, "_value")
  33. s = map_dockerman:section(SimpleSection)
  34. s.containers_running = '-'
  35. s.images_used = '-'
  36. s.containers_total = '-'
  37. s.images_total = '-'
  38. s.networks_total = '-'
  39. s.volumes_total = '-'
  40. local containers_list
  41. -- local socket = luci.model.uci.cursor():get("dockerman", "local", "socket_path")
  42. if (require "luci.model.docker").new():_ping().code == 200 then
  43. local dk = docker.new()
  44. containers_list = dk.containers:list({query = {all=true}}).body
  45. local images_list = dk.images:list().body
  46. local vol = dk.volumes:list()
  47. local volumes_list = vol and vol.body and vol.body.Volumes or {}
  48. local networks_list = dk.networks:list().body or {}
  49. local docker_info = dk:info()
  50. -- docker_info_table['0OperatingSystem']._value = docker_info.body.OperatingSystem
  51. -- docker_info_table['1Architecture']._value = docker_info.body.Architecture
  52. -- docker_info_table['2KernelVersion']._value = docker_info.body.KernelVersion
  53. docker_info_table['3ServerVersion']._value = docker_info.body.ServerVersion
  54. docker_info_table['4ApiVersion']._value = docker_info.headers["Api-Version"]
  55. docker_info_table['5NCPU']._value = tostring(docker_info.body.NCPU)
  56. docker_info_table['6MemTotal']._value = byte_format(docker_info.body.MemTotal)
  57. if docker_info.body.DockerRootDir then
  58. local statvfs = nixio.fs.statvfs(docker_info.body.DockerRootDir)
  59. local size = statvfs and (statvfs.bavail * statvfs.bsize) or 0
  60. docker_info_table['7DockerRootDir']._value = docker_info.body.DockerRootDir .. " (" .. tostring(byte_format(size)) .. " " .. translate("Available") .. ")"
  61. end
  62. docker_info_table['8IndexServerAddress']._value = docker_info.body.IndexServerAddress
  63. for i, v in ipairs(docker_info.body.RegistryConfig.Mirrors) do
  64. docker_info_table['9RegistryMirrors']._value = docker_info_table['9RegistryMirrors']._value == "-" and v or (docker_info_table['9RegistryMirrors']._value .. ", " .. v)
  65. end
  66. s.images_used = 0
  67. for i, v in ipairs(images_list) do
  68. for ci,cv in ipairs(containers_list) do
  69. if v.Id == cv.ImageID then
  70. s.images_used = s.images_used + 1
  71. break
  72. end
  73. end
  74. end
  75. s.containers_running = tostring(docker_info.body.ContainersRunning)
  76. s.images_used = tostring(s.images_used)
  77. s.containers_total = tostring(docker_info.body.Containers)
  78. s.images_total = tostring(#images_list)
  79. s.networks_total = tostring(#networks_list)
  80. s.volumes_total = tostring(#volumes_list)
  81. end
  82. s.template = "dockerman/overview"
  83. local section_dockerman = map_dockerman:section(NamedSection, "local", "section", translate("Setting"))
  84. section_dockerman:tab("daemon", translate("Docker Daemon"))
  85. section_dockerman:tab("ac", translate("Access Control"))
  86. section_dockerman:tab("dockerman", translate("DockerMan"))
  87. local socket_path = section_dockerman:taboption("dockerman", Value, "socket_path", translate("Docker Socket Path"))
  88. socket_path.default = "/var/run/docker.sock"
  89. socket_path.placeholder = "/var/run/docker.sock"
  90. socket_path.rmempty = false
  91. local remote_endpoint = section_dockerman:taboption("dockerman", Flag, "remote_endpoint", translate("Remote Endpoint"), translate("Dockerman connect to remote endpoint"))
  92. remote_endpoint.rmempty = false
  93. remote_endpoint.enabled = "true"
  94. remote_endpoint.disabled = "false"
  95. local remote_host = section_dockerman:taboption("dockerman", Value, "remote_host", translate("Remote Host"))
  96. remote_host.placeholder = "10.1.1.2"
  97. -- remote_host:depends("remote_endpoint", "true")
  98. local remote_port = section_dockerman:taboption("dockerman", Value, "remote_port", translate("Remote Port"))
  99. remote_port.placeholder = "2375"
  100. remote_port.default = "2375"
  101. -- remote_port:depends("remote_endpoint", "true")
  102. -- local status_path = section_dockerman:taboption("dockerman", Value, "status_path", translate("Action Status Tempfile Path"), translate("Where you want to save the docker status file"))
  103. -- local debug = section_dockerman:taboption("dockerman", Flag, "debug", translate("Enable Debug"), translate("For debug, It shows all docker API actions of luci-app-dockerman in Debug Tempfile Path"))
  104. -- debug.enabled="true"
  105. -- debug.disabled="false"
  106. -- local debug_path = section_dockerman:taboption("dockerman", Value, "debug_path", translate("Debug Tempfile Path"), translate("Where you want to save the debug tempfile"))
  107. if nixio.fs.access("/usr/bin/dockerd") then
  108. local allowed_interface = section_dockerman:taboption("ac", DynamicList, "ac_allowed_interface", translate("Allowed access interfaces"), translate("Which interface(s) can access containers under the bridge network, fill-in Interface Name"))
  109. local interfaces = luci.sys and luci.sys.net and luci.sys.net.devices() or {}
  110. for i, v in ipairs(interfaces) do
  111. allowed_interface:value(v, v)
  112. end
  113. local allowed_container = section_dockerman:taboption("ac", DynamicList, "ac_allowed_container", translate("Containers allowed to be accessed"), translate("Which container(s) under bridge network can be accessed, even from interfaces that are not allowed, fill-in Container Id or Name"))
  114. -- allowed_container.placeholder = "container name_or_id"
  115. if containers_list then
  116. for i, v in ipairs(containers_list) do
  117. if v.State == "running" and v.NetworkSettings and v.NetworkSettings.Networks and v.NetworkSettings.Networks.bridge and v.NetworkSettings.Networks.bridge.IPAddress then
  118. allowed_container:value(v.Id:sub(1,12), v.Names[1]:sub(2) .. " | " .. v.NetworkSettings.Networks.bridge.IPAddress)
  119. end
  120. end
  121. end
  122. local dockerd_enable = section_dockerman:taboption("daemon", Flag, "daemon_ea", translate("Enable"))
  123. dockerd_enable.enabled = "true"
  124. dockerd_enable.rmempty = true
  125. local data_root = section_dockerman:taboption("daemon", Value, "daemon_data_root", translate("Docker Root Dir"))
  126. data_root.placeholder = "/opt/docker/"
  127. local registry_mirrors = section_dockerman:taboption("daemon", DynamicList, "daemon_registry_mirrors", translate("Registry Mirrors"))
  128. registry_mirrors:value("https://hub-mirror.c.163.com", "https://hub-mirror.c.163.com")
  129. local log_level = section_dockerman:taboption("daemon", ListValue, "daemon_log_level", translate("Log Level"), translate('Set the logging level'))
  130. log_level:value("debug", "debug")
  131. log_level:value("info", "info")
  132. log_level:value("warn", "warn")
  133. log_level:value("error", "error")
  134. log_level:value("fatal", "fatal")
  135. local hosts = section_dockerman:taboption("daemon", DynamicList, "daemon_hosts", translate("Server Host"), translate('Daemon unix socket (unix:///var/run/docker.sock) or TCP Remote Hosts (tcp://0.0.0.0:2375), default: unix:///var/run/docker.sock'))
  136. hosts:value("unix:///var/run/docker.sock", "unix:///var/run/docker.sock")
  137. hosts:value("tcp://0.0.0.0:2375", "tcp://0.0.0.0:2375")
  138. hosts.rmempty = true
  139. end
  140. return map_dockerman