service 5.1 KB

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