You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.0 KiB

  1. --[[
  2. LuCI LXC module
  3. Copyright (C) 2014, Cisco Systems, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Author: Petar Koretic <petar.koretic@sartura.hr>
  9. ]]--
  10. module("luci.controller.lxc", package.seeall)
  11. require "ubus"
  12. local conn = ubus.connect()
  13. if not conn then
  14. error("Failed to connect to ubus")
  15. end
  16. function fork_exec(command)
  17. local pid = nixio.fork()
  18. if pid > 0 then
  19. return
  20. elseif pid == 0 then
  21. -- change to root dir
  22. nixio.chdir("/")
  23. -- patch stdin, out, err to /dev/null
  24. local null = nixio.open("/dev/null", "w+")
  25. if null then
  26. nixio.dup(null, nixio.stderr)
  27. nixio.dup(null, nixio.stdout)
  28. nixio.dup(null, nixio.stdin)
  29. if null:fileno() > 2 then
  30. null:close()
  31. end
  32. end
  33. -- replace with target command
  34. nixio.exec("/bin/sh", "-c", command)
  35. end
  36. end
  37. function index()
  38. page = node("admin", "services", "lxc")
  39. page.target = cbi("lxc")
  40. page.title = _("LXC Containers")
  41. page.order = 70
  42. page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
  43. page.leaf = true
  44. page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
  45. page.leaf = true
  46. page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil)
  47. page.leaf = true
  48. page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
  49. page.leaf = true
  50. page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
  51. page.leaf = true
  52. end
  53. function lxc_get_downloadable()
  54. luci.http.prepare_content("application/json")
  55. local f = io.popen('uname -m', 'r')
  56. local target = f:read('*a')
  57. f:close()
  58. target = target:gsub("^%s*(.-)%s*$", "%1")
  59. local templates = {}
  60. local f = io.popen('lxc-create -n just_want_to_list_available_lxc_templates -t download -- --list', 'r')
  61. for line in f:lines() do
  62. local dist,version = line:match("^(%S+)%s+(%S+)%s+" .. target .. "%s+default%s+%S+$")
  63. if dist~=nil and version~=nil then templates[#templates + 1] = dist .. ":" .. version end
  64. end
  65. f:close()
  66. luci.http.write_json(templates)
  67. end
  68. function lxc_create(lxc_name, lxc_template)
  69. luci.http.prepare_content("text/plain")
  70. local uci = require("uci").cursor()
  71. local url = uci:get("lxc", "lxc", "url")
  72. if not pcall(dofile, "/etc/openwrt_release") then
  73. return luci.http.write("1")
  74. end
  75. local f = io.popen('uname -m', 'r')
  76. local target = f:read('*a')
  77. f:close()
  78. target = target:gsub("^%s*(.-)%s*$", "%1")
  79. local lxc_dist = lxc_template:gsub("(.*):(.*)", '%1')
  80. local lxc_release = lxc_template:gsub("(.*):(.*)", '%2')
  81. local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_dist, "--release", lxc_release, "--arch", target } } )
  82. luci.http.write(data)
  83. end
  84. function lxc_action(lxc_action, lxc_name)
  85. luci.http.prepare_content("application/json")
  86. local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} )
  87. luci.http.write_json(ec and {} or data)
  88. end
  89. function lxc_get_config_path()
  90. local f = io.open("/etc/lxc/lxc.conf", "r")
  91. local content = f:read("*all")
  92. f:close()
  93. local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
  94. if ret then
  95. return ret .. "/"
  96. else
  97. return "/srv/lxc/"
  98. end
  99. end
  100. function lxc_configuration_get(lxc_name)
  101. luci.http.prepare_content("text/plain")
  102. local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
  103. local content = f:read("*all")
  104. f:close()
  105. luci.http.write(content)
  106. end
  107. function lxc_configuration_set(lxc_name)
  108. luci.http.prepare_content("text/plain")
  109. local lxc_configuration = luci.http.formvalue("lxc_configuration")
  110. if lxc_configuration == nil then
  111. return luci.http.write("1")
  112. end
  113. local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
  114. if not f then
  115. return luci.http.write("2")
  116. end
  117. f:write(lxc_configuration)
  118. f:close()
  119. luci.http.write("0")
  120. end