servis.lua 5.2 KB

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