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.

170 lines
5.5KB

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