~benste/junk/l4atool

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python
# -*- coding: utf-8 -*-

""" Author - benste (benste.blogspot.com)
Version 0.1

Feautres:
 - Check Status of System Services
 - Display Proxy and Hostapd status
 - execute service commands to enabled / disable the service
 - use init student script with an amount of students to recreate users from skel

Bugs:
 - add ping based check of internet connection

Usage
 - copy .py and .glade into /opt/l4atool/
 - create a starter in /usr/local/sbin/ which
   * cd into /opt/l4atool
   * gksu /opt/l4atool

"""

from gi.repository import Gtk
from os import system as shell
from os.path import abspath, split
from time import sleep
	
"""
Class defining the application and it's functions 
"""
class L4AAdminTool:
    
    """
      Preparing the GTK interface imported from Glade
      Assigning the Handlers (Signals / Events)
      Assign the window variable without Builder
      
      Also the two switches need to be initialized
      unfortuneatly there is no setter for for the object - just executing .activate() will change it again - init in glade is OFF
    """
    def __init__(self):
        self.init=True
        builder = Gtk.Builder()
        builder.add_from_file(split(abspath(__file__))[0]+"/l4atool.glade") 
        
        handlers = { # we need to use
            "onDeleteWindow": Gtk.main_quit,
            "on_sw_internet_button_release_event": self.on_internet_activate,
            "on_sw_wireless_button_release_event": self.on_wireless_activate,
            "on_students_clicked_cb": self.on_students_clicked,
        }
        
        builder.connect_signals(handlers)
        self.window = builder.get_object("window")
        self.scale_students = builder.get_object("scale_students")
        self.box1 = builder.get_object("box1")
        
        self.sw_wireless = builder.get_object("sw_wireless")
        if self.service_is_running("hostapd"):
            self.sw_wireless.activate()
        self.sw_internet = builder.get_object("sw_internet")
        if self.service_is_running("squid3"):
            self.sw_internet.activate()
        self.init=False    
            
    """
      Check if Squid is running, dis/en-able it based on the change made by the user
      5 seconds delay because squid takes some time to shutdown as a service
    """    
    def service_is_running(self,servicename="No Service"):
         return shell("pidof "+servicename) != 256
    
    def on_internet_activate(self, *args):
      if not(self.init):
        self.box1.hide()
        if self.service_is_running("squid3"):
          shell("service squid3 stop")
          shell("zenity --info --text='Internet is now switched OFF'")
        else:
          shell("service squid3 restart")
          sleep(2)
          if self.service_is_running("squid3"):
            #TODO - check connection with PING
            shell("zenity --info --text='Your internet is now on if your PC is connected to the internet.'")   
          else:
            shell("zenity --warning --text='Ops - something went wrong! - Contact your administrator\nSquid is not running after\n\nservice squid3 restart'") 
            self.sw_internet.activate() #disable after unsuccessful           
        self.box1.show()      
    """
      Check if hostapd is running and change the status based on the user input
      5 seconds delay because hostapd takes some time to shutdown as a service
    """
    def on_wireless_activate(self, *args):
      if not(self.init):
        self.box1.hide()
        if self.service_is_running("hostapd"):
          shell("service hostapd stop")
          shell("zenity --info --text='Internet is now switched OFF'")
        else:
          shell("service hostapd restart")
          sleep(2)
          if self.service_is_running("hostapd"):
            shell("zenity --info --text='You created a Wireless network for your classroom\n\n SSID:L4A\nPassword:OpenSourceRocks\nServerIP is 192.168.2.250'") 
          else:
            shell("zenity --warning --text='Ops - something went wrong! - Contact your administrator\nhostapd is not running after\n\nservice hostapd restart'") 
            self.sw_wireless.activate() #disable after unsuccessful
        self.box1.show()
    """
      initiate the User regeneration process based on the number of users specified next to it.
      - check if one of the tsusers is still logged in
      - confirm action
    """
    def on_students_clicked(self, button, *args):
      if not(self.init):    
        self.box1.hide()
        if not(shell("finger | grep tsuser")): 
            dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK, "Please make sure that all students are logged off")
            dialog.run()
            dialog.destroy()
        else:
            dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Initialization of Student Accounts")
            dialog.format_secondary_text(
                "Regenerating the users will erase every users settings and data - only files stored by the teacher or stored in /home/4all or /home/documents will remain!\n\n Are you sure you want to continue?")
            response = dialog.run()
            dialog.destroy()
            if response == Gtk.ResponseType.YES:
                dialog2 = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
                    Gtk.ButtonsType.OK, str(int(self.scale_students.get_value())) + " New users are now created this might take some time\n Please click OK and wait for the success message")
                dialog2.run()
                dialog2.destroy()
                shell("init_students "+str(int(self.scale_students.get_value())))
                
                dialog2 = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
                    Gtk.ButtonsType.OK, "Done creating "+str(int(self.scale_students.get_value()))+"new users - You can now use them")
                dialog2.run()
                dialog2.destroy()
                shell("init_students "+str(int(self.scale_students.get_value())))
            elif response == Gtk.ResponseType.NO:
                print "QUESTION dialog closed by clicking NO button"
        self.box1.hide()

"""
  Start the whole application defined before
"""
if __name__ == "__main__":
    app = L4AAdminTool()
    app.window.connect("delete-event", Gtk.main_quit)
    app.window.show()
    Gtk.main()