Mercurial > prosody-hg
comparison util/prosodyctl.lua @ 1087:5e9475bec571
prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 02 May 2009 17:03:19 +0100 |
| parents | |
| children | da7ff11a03ee |
comparison
equal
deleted
inserted
replaced
| 1086:0b895e1ac713 | 1087:5e9475bec571 |
|---|---|
| 1 | |
| 2 local config = require "core.configmanager"; | |
| 3 local encodings = require "util.encodings"; | |
| 4 local stringprep = encodings.stringprep; | |
| 5 local usermanager = require "core.usermanager"; | |
| 6 local signal = require "util.signal"; | |
| 7 | |
| 8 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep; | |
| 9 | |
| 10 local io, os = io, os; | |
| 11 local tostring, tonumber = tostring, tonumber; | |
| 12 module "prosodyctl" | |
| 13 | |
| 14 function adduser(params) | |
| 15 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password; | |
| 16 if not user then | |
| 17 return false, "invalid-username"; | |
| 18 elseif not host then | |
| 19 return false, "invalid-hostname"; | |
| 20 end | |
| 21 | |
| 22 usermanager.create_user(user, password, host); | |
| 23 return true; | |
| 24 end | |
| 25 | |
| 26 function user_exists(params) | |
| 27 return usermanager.user_exists(params.user, params.host); | |
| 28 end | |
| 29 | |
| 30 function passwd(params) | |
| 31 if not _M.user_exists(params) then | |
| 32 return false, "no-such-user"; | |
| 33 end | |
| 34 | |
| 35 return _M.adduser(params); | |
| 36 end | |
| 37 | |
| 38 function deluser(params) | |
| 39 if not _M.user_exists(params) then | |
| 40 return false, "no-such-user"; | |
| 41 end | |
| 42 params.password = nil; | |
| 43 | |
| 44 return _M.adduser(params); | |
| 45 end | |
| 46 | |
| 47 function getpid() | |
| 48 local pidfile = config.get("*", "core", "pidfile"); | |
| 49 if not pidfile then | |
| 50 return false, "no-pidfile"; | |
| 51 end | |
| 52 | |
| 53 local file, err = io.open(pidfile); | |
| 54 if not file then | |
| 55 return false, "pidfile-read-failed", ret; | |
| 56 end | |
| 57 | |
| 58 local pid = tonumber(file:read("*a")); | |
| 59 file:close(); | |
| 60 | |
| 61 if not pid then | |
| 62 return false, "invalid-pid"; | |
| 63 end | |
| 64 | |
| 65 return true, pid; | |
| 66 end | |
| 67 | |
| 68 function isrunning() | |
| 69 local ok, pid, err = _M.getpid(); | |
| 70 if not ok then | |
| 71 if pid == "pidfile-read-failed" then | |
| 72 -- Report as not running, since we can't open the pidfile | |
| 73 -- (it probably doesn't exist) | |
| 74 return true, false; | |
| 75 end | |
| 76 return ok, pid; | |
| 77 end | |
| 78 return true, signal.kill(pid, 0) == 0; | |
| 79 end | |
| 80 | |
| 81 function start() | |
| 82 local ok, ret = _M.isrunning(); | |
| 83 if not ok then | |
| 84 return ok, ret; | |
| 85 end | |
| 86 if ret then | |
| 87 return false, "already-running"; | |
| 88 end | |
| 89 if not CFG_SOURCEDIR then | |
| 90 os.execute("./prosody"); | |
| 91 elseif CFG_SOURCEDIR:match("^/usr/local") then | |
| 92 os.execute("/usr/local/bin/prosody"); | |
| 93 else | |
| 94 os.execute("prosody"); | |
| 95 end | |
| 96 return true; | |
| 97 end | |
| 98 | |
| 99 function stop() | |
| 100 local ok, ret = _M.isrunning(); | |
| 101 if not ok then | |
| 102 return ok, ret; | |
| 103 end | |
| 104 if not ret then | |
| 105 return false, "not-running"; | |
| 106 end | |
| 107 | |
| 108 local ok, pid = _M.getpid() | |
| 109 if not ok then return false, pid; end | |
| 110 | |
| 111 signal.kill(pid, signal.SIGTERM); | |
| 112 return true; | |
| 113 end |
