Milis Linux Servis Yönetimi için Arayüz Uygulaması
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.

166 lines
5.3KB

  1. #!/usr/bin/python3
  2. import gi, os, subprocess
  3. from gi.repository import Gtk, GdkPixbuf
  4. class MService(Gtk.Window):
  5. def __init__(self):
  6. Gtk.Window.__init__(self)
  7. self.set_title("MService")
  8. self.set_default_size(360,500)
  9. box = Gtk.VBox()
  10. self.add(box)
  11. self.services_black_list = ["halt","initbase","random","rc","reboot","sendsignals"]
  12. self.icon_dir = "./icons"
  13. self.set_icon(GdkPixbuf.Pixbuf.new_from_file("{}/simge.svg".format(self.icon_dir)))
  14. self.search_widget = Gtk.SearchEntry()
  15. self.search_widget.connect("search-changed",self.search_change)
  16. box.pack_start(self.search_widget,False,False,5)
  17. self.services_store = Gtk.ListStore(GdkPixbuf.Pixbuf(),str)
  18. self.services_tree = Gtk.TreeView(model=self.services_store)
  19. self.services_tree.set_activate_on_single_click(True)
  20. self.services_tree.connect("button-release-event",self.click_tree)
  21. tree_icon = Gtk.CellRendererPixbuf()
  22. tree_icon.set_fixed_size(32,32)
  23. icon = Gtk.TreeViewColumn("Durum",tree_icon,gicon = 0)
  24. self.services_tree.append_column(icon)
  25. tree_text = Gtk.CellRendererText()
  26. text = Gtk.TreeViewColumn("Servis Adı",tree_text, text = 1)
  27. self.services_tree.append_column(text)
  28. scroll = Gtk.ScrolledWindow()
  29. scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
  30. scroll.add(self.services_tree)
  31. box.pack_start(scroll,True, True, 5)
  32. self.get_services()
  33. self.append_tree(self.services)
  34. def click_tree(self,widget,event):
  35. selected = self.services_tree.get_selection()
  36. tree_model, tree_iter = selected.get_selected()
  37. select = tree_model[tree_iter][1]
  38. status = self.services[select]
  39. if event.button == 1:
  40. if status:
  41. text = "{} servisi durdurulacak onaylıyor musunuz?".format(select)
  42. else:
  43. text = "{} servisi başlatılacak onaylıyor musunuz?".format(select)
  44. question = Gtk.MessageDialog(self,0,Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL,"Onay")
  45. question.set_title("Onay")
  46. question.format_secondary_text(text)
  47. get = question.run()
  48. if get == Gtk.ResponseType.OK:
  49. if status:
  50. get = subprocess.Popen(["service","stop",select], stdout = subprocess.PIPE)
  51. else:
  52. get = subprocess.Popen(["service","start",select], stdout = subprocess.PIPE)
  53. get.wait()
  54. self.get_services()
  55. self.search_widget.set_text("")
  56. self.append_tree(self.services)
  57. question.destroy()
  58. elif event.button == 3:
  59. menu = Gtk.Menu()
  60. enable = Gtk.MenuItem("Aktif")
  61. enable.connect("activate",self.enable_service,select)
  62. menu.add(enable)
  63. disable = Gtk.MenuItem("Pasif")
  64. disable.connect("activate",self.disable_service,select)
  65. menu.add(disable)
  66. menu.show_all()
  67. menu.popup_at_pointer()
  68. print(select)
  69. def subprocess_parse(self,get):
  70. get = get.communicate()[0]
  71. get = get.decode("utf-8","strict")
  72. return get
  73. def enable_service(self,widget,service):
  74. get = subprocess.Popen(["service","enable",service], stdout = subprocess.PIPE)
  75. get.wait()
  76. get = self.subprocess_parse(get)
  77. info = Gtk.MessageDialog(self,0,Gtk.MessageType.INFO, Gtk.ButtonsType.OK,"{} servisi aktif hale getirildi.".format(service))
  78. info.set_title("{} servisi aktif hale getirildi.".format(service))
  79. info.format_secondary_text(get)
  80. info.run()
  81. info.destroy()
  82. def disable_service(self,widget,service):
  83. get = subprocess.Popen(["service","disable",service], stdout = subprocess.PIPE)
  84. get.wait()
  85. get = self.subprocess_parse(get)
  86. info = Gtk.MessageDialog(self,0,Gtk.MessageType.INFO, Gtk.ButtonsType.OK,"{} servisi pasif hale getirildi.".format(service))
  87. info.set_title("{} servisi pasif hale getirildi.".format(service))
  88. info.format_secondary_text(get)
  89. info.run()
  90. info.destroy()
  91. def get_services(self):
  92. self.services = {}
  93. service_dir = "/etc/init.d"
  94. if os.path.exists(service_dir):
  95. services = os.listdir(service_dir)
  96. for service in services:
  97. if service not in self.services_black_list:
  98. self.services[service] = self.get_service_status(service)
  99. else:
  100. dialog = Gtk.MessageDialog(None,0, Gtk.MessageType.ERROR,
  101. Gtk.ButtonsType.OK, "Hata")
  102. dialog.format_secondary_text("{} dizini bulunamadı. Servisler listelenemeyecek.".format(service_dir))
  103. dialog.run()
  104. self.destroy()
  105. def get_service_status(self,service):
  106. get = subprocess.Popen(["service","status",service], stdout = subprocess.PIPE)
  107. get.wait()
  108. get = self.subprocess_parse(get)
  109. get = get.split("\n")[0]
  110. get = get.split(" ")
  111. if "running" in get:
  112. return True
  113. else:
  114. return False
  115. def search_change(self,search_widget):
  116. search_text = search_widget.get_text()
  117. s = {}
  118. for service in self.services.keys():
  119. if search_text in service:
  120. s[service] = self.services[service]
  121. self.append_tree(s)
  122. def append_tree(self,services):
  123. self.services_store.clear()
  124. keys_list = list(services.keys())
  125. keys_list.sort()
  126. for service in keys_list:
  127. service_state = services[service]
  128. if service_state:
  129. icon = GdkPixbuf.Pixbuf.new_from_file("{}/2.svg".format(self.icon_dir))
  130. else:
  131. icon = GdkPixbuf.Pixbuf.new_from_file("{}/1.svg".format(self.icon_dir))
  132. self.services_store.append([icon,service])
  133. if __name__ == "__main__":
  134. if os.getuid() != 0:
  135. dialog = Gtk.MessageDialog(None,0, Gtk.MessageType.ERROR,
  136. Gtk.ButtonsType.OK, "Hata")
  137. dialog.format_secondary_text("MService çalışmak için root yetkinlerine ihtiyaç duyar.")
  138. dialog.run()
  139. else:
  140. pen = MService()
  141. pen.connect("destroy", Gtk.main_quit)
  142. pen.show_all()
  143. Gtk.main()