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.

125 lines
2.8 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_configuration_get"}, call("lxc_configuration_get"), nil)
  47. page.leaf = true
  48. page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
  49. page.leaf = true
  50. end
  51. function lxc_create(lxc_name, lxc_template)
  52. luci.http.prepare_content("text/plain")
  53. local uci = require("uci").cursor()
  54. local url = uci:get("lxc", "lxc", "url")
  55. if not pcall(dofile, "/etc/openwrt_release") then
  56. return luci.http.write("1")
  57. end
  58. local target = _G.DISTRIB_TARGET:match('([^/]+)')
  59. local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_template, "--release", "bb", "--arch", target } } )
  60. luci.http.write(data)
  61. end
  62. function lxc_action(lxc_action, lxc_name)
  63. luci.http.prepare_content("application/json")
  64. local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} )
  65. luci.http.write_json(ec and {} or data)
  66. end
  67. function lxc_configuration_get(lxc_name)
  68. luci.http.prepare_content("text/plain")
  69. local f = io.open("/lxc/" .. lxc_name .. "/config", "r")
  70. local content = f:read("*all")
  71. f:close()
  72. luci.http.write(content)
  73. end
  74. function lxc_configuration_set(lxc_name)
  75. luci.http.prepare_content("text/plain")
  76. local lxc_configuration = luci.http.formvalue("lxc_configuration")
  77. if lxc_configuration == nil then
  78. return luci.http.write("1")
  79. end
  80. local f, err = io.open("/lxc/" .. lxc_name .. "/config","w+")
  81. if not f then
  82. return luci.http.write("2")
  83. end
  84. f:write(lxc_configuration)
  85. f:close()
  86. luci.http.write("0")
  87. end