servis.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env slua
  2. -- Milis Linux 2.3 Servis Yönetim Betiği
  3. -- milisarge 2023/09
  4. package.path = "/usr/milis/mps/lua/?.lua" .. ";".. package.path
  5. package.path = "/etc/init/?.lua" .. ";".. package.path
  6. local serpent = require('serpent')
  7. local c = require ("config")
  8. local config_file="/etc/init/config.lua"
  9. l5=require("l5")
  10. if os.getenv("init_color") == nil then
  11. l5.setenv("init_color","1")
  12. end
  13. function help()
  14. if arg[0] == "/usr/milis/bin/servis" then
  15. print ("Milis Linux 2.3 Servis Yönetim Betiği")
  16. print ("-------------------------------------")
  17. local script="servis"
  18. print(script,"ekle","servis_adı","| servis sistemine ekleme")
  19. print(script,"sil","servis_adı","| servis sisteminden silme")
  20. print(script,"aktif","servis_adı","| servis otomatik başlatma")
  21. print(script,"pasif","servis_adı","| servis otomatik başlatmama")
  22. print(script,"kos","servis_adı","| servis başlatma")
  23. print(script,"dur","servis_adı","| servis durdurma")
  24. print(script,"yuk","servis_adı","| servis yeniden yükleme")
  25. print(script,"bil","servis_adı","| servis bilgisi")
  26. print(script,"liste",' ',"| aktif servis listesi")
  27. print(script,"eliste",' ',"| ekli servis listesi")
  28. print(script,"dliste",' ',"| depo servis listesi")
  29. else
  30. print ("Milis Linux 2.3 Service Management")
  31. print ("-------------------------------------")
  32. local script="service"
  33. print(script,"add","service","| add service")
  34. print(script,"del","service","| remove service")
  35. print(script,"active","service","| autostart service")
  36. print(script,"pasive","service","| disable autostart service")
  37. print(script,"start","service","| start service")
  38. print(script,"stop","service","| stop service")
  39. print(script,"reload","service","| reload service")
  40. print(script,"status","service","| service status")
  41. print(script,"list",' ',"| autostart service list")
  42. print(script,"alist",' ',"| added service list")
  43. print(script,"rlist",' ',"| service repo list")
  44. os.exit()
  45. end
  46. end
  47. function not_ready()
  48. print("hazır değil!")
  49. os.exit()
  50. end
  51. function service_not_found(srv)
  52. if srv == nil then
  53. print("servis parametresi eksik!")
  54. else
  55. print(srv.." servisi bulunamadı!")
  56. end
  57. os.exit()
  58. end
  59. function auth()
  60. if shell("id -u") ~= "0" then
  61. print ("Yetkili çalıştırın!")
  62. os.exit()
  63. end
  64. end
  65. function shell(command)
  66. local handle=io.popen(command)
  67. local result=handle:read('*all')
  68. handle:close()
  69. -- komut çıktısı sonu yeni satır karakterin silinmesi - en sondaki \n
  70. if result:sub(-1) == "\n" then
  71. result=result:sub(1,-2)
  72. end
  73. return result
  74. end
  75. function content(file)
  76. local f = assert(io.open(file, "rb"))
  77. local content = f:read("*all")
  78. f:close()
  79. return content
  80. end
  81. function update_tasks()
  82. local data=serpent.block(c)
  83. print(serpent.block(c.tasks))
  84. --not_ready()
  85. io.output(io.open(config_file, "w"))
  86. io.write("local config=")
  87. io.write(data)
  88. io.write("\n")
  89. io.write("return config")
  90. io.write("\n")
  91. io.close()
  92. end
  93. function list(srv)
  94. for _,s in ipairs(c.tasks) do
  95. print(s)
  96. end
  97. end
  98. function elist(srv)
  99. for s,_ in pairs(service_list("/etc/init")) do
  100. print(s)
  101. end
  102. end
  103. function dlist(srv)
  104. for s,_ in pairs(service_list("/usr/milis/ayarlar/init")) do
  105. print(s)
  106. end
  107. end
  108. function service_list(path)
  109. local services = {}
  110. for fn in shell("ls "..path.."/*.lua"):gmatch('[^\n]+') do
  111. if content(fn):match("local task={") then
  112. local name = fn:match("[^/]*.lua$")
  113. services[name:sub(0, #name - 4)]=true
  114. end
  115. end
  116. return services
  117. end
  118. function add(srv)
  119. if not srv then service_not_found(srv) end
  120. os.execute("cp -fv /usr/milis/ayarlar/init/"..srv..".lua /etc/init/")
  121. end
  122. function delete(srv)
  123. if not srv then service_not_found(srv) end
  124. local cmd="rm -v /etc/init/"..srv..".lua"
  125. --print(cmd)
  126. os.execute(cmd)
  127. end
  128. function run(srv)
  129. if service_list("/etc/init")[srv] == nil then service_not_found(srv) end
  130. local cmd="/etc/init/init.lua start "..srv
  131. --print(cmd)
  132. os.execute(cmd)
  133. end
  134. function stop(srv)
  135. if service_list("/etc/init")[srv] == nil then service_not_found(srv) end
  136. os.execute("/etc/init/init.lua stop "..srv)
  137. end
  138. function reload(srv)
  139. if service_list("/etc/init")[srv] == nil then service_not_found(srv) end
  140. os.execute("/etc/init/init.lua reload "..srv)
  141. end
  142. function activate(srv)
  143. if not srv then service_not_found(srv) end
  144. local add=true
  145. for _,t in ipairs(c.tasks) do
  146. if t == srv then
  147. add=false
  148. end
  149. end
  150. if add then table.insert(c.tasks,srv) end
  151. update_tasks()
  152. end
  153. function deactivate(srv)
  154. if not srv then service_not_found(srv) end
  155. local ntasks={}
  156. for _,t in ipairs(c.tasks) do
  157. if t ~= srv then
  158. table.insert(ntasks,t)
  159. end
  160. end
  161. c.tasks=ntasks
  162. update_tasks()
  163. end
  164. function info(srv)
  165. if service_list("/etc/init")[srv] == nil then service_not_found(srv) end
  166. local cmd="/etc/init/init.lua status "..srv
  167. --print(cmd)
  168. os.execute(cmd)
  169. end
  170. command=arg[1]
  171. service=arg[2]
  172. auth()
  173. if command == nil then help() end
  174. commands={
  175. liste=list,
  176. dliste=dlist,
  177. eliste=elist,
  178. ekle=add,
  179. sil=delete,
  180. kos=run,
  181. dur=stop,
  182. yuk=reload,
  183. aktif=activate,
  184. pasif=deactivate,
  185. bil=info,
  186. -- english
  187. list=list,rlist=dlist,alist=elist,add=add,del=delete,reload=reload,
  188. start=run,stop=stop,active=activate,pasive=deactivate,status=info,
  189. }
  190. if commands[command] then commands[command](service)
  191. else help()
  192. end
  193. --notlar
  194. --verbose çıktıları için os.execute()
  195. -- içerik okunacak shell çıktıları için shell()
  196. -- dosya içerik için content()