~benste/junk/l4atool

1 by Benedict.Stein+ubuntu at gmail
first used release
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
""" Author - benste (benste.blogspot.com)
5
Version 0.1
6
7
Feautres:
8
 - Check Status of System Services
9
 - Display Proxy and Hostapd status
10
 - execute service commands to enabled / disable the service
3 by Benedict.Stein+ubuntu at gmail
changed version info and install
11
 - use init student script with an amount of students to recreate users from skel
1 by Benedict.Stein+ubuntu at gmail
first used release
12
13
Bugs:
14
 - add ping based check of internet connection
3 by Benedict.Stein+ubuntu at gmail
changed version info and install
15
16
Usage
17
 - copy .py and .glade into /opt/l4atool/
18
 - create a starter in /usr/local/sbin/ which
19
   * cd into /opt/l4atool
20
   * gksu /opt/l4atool
1 by Benedict.Stein+ubuntu at gmail
first used release
21
22
"""
23
24
from gi.repository import Gtk
25
from os import system as shell
2 by Benedict.Stein+ubuntu at gmail
added abspath for glade file
26
from os.path import abspath, split
1 by Benedict.Stein+ubuntu at gmail
first used release
27
from time import sleep
28
	
29
"""
30
Class defining the application and it's functions 
31
"""
32
class L4AAdminTool:
33
    
34
    """
35
      Preparing the GTK interface imported from Glade
36
      Assigning the Handlers (Signals / Events)
37
      Assign the window variable without Builder
38
      
39
      Also the two switches need to be initialized
40
      unfortuneatly there is no setter for for the object - just executing .activate() will change it again - init in glade is OFF
41
    """
42
    def __init__(self):
43
        self.init=True
44
        builder = Gtk.Builder()
2 by Benedict.Stein+ubuntu at gmail
added abspath for glade file
45
        builder.add_from_file(split(abspath(__file__))[0]+"/l4atool.glade") 
1 by Benedict.Stein+ubuntu at gmail
first used release
46
        
47
        handlers = { # we need to use
48
            "onDeleteWindow": Gtk.main_quit,
49
            "on_sw_internet_button_release_event": self.on_internet_activate,
50
            "on_sw_wireless_button_release_event": self.on_wireless_activate,
51
            "on_students_clicked_cb": self.on_students_clicked,
52
        }
53
        
54
        builder.connect_signals(handlers)
55
        self.window = builder.get_object("window")
56
        self.scale_students = builder.get_object("scale_students")
4 by Benedict.Stein+ubuntu at gmail
bugfix
57
        self.box1 = builder.get_object("box1")
1 by Benedict.Stein+ubuntu at gmail
first used release
58
        
4 by Benedict.Stein+ubuntu at gmail
bugfix
59
        self.sw_wireless = builder.get_object("sw_wireless")
1 by Benedict.Stein+ubuntu at gmail
first used release
60
        if self.service_is_running("hostapd"):
61
            self.sw_wireless.activate()
62
        self.sw_internet = builder.get_object("sw_internet")
63
        if self.service_is_running("squid3"):
64
            self.sw_internet.activate()
65
        self.init=False    
66
            
67
    """
68
      Check if Squid is running, dis/en-able it based on the change made by the user
69
      5 seconds delay because squid takes some time to shutdown as a service
70
    """    
71
    def service_is_running(self,servicename="No Service"):
72
         return shell("pidof "+servicename) != 256
73
    
74
    def on_internet_activate(self, *args):
75
      if not(self.init):
4 by Benedict.Stein+ubuntu at gmail
bugfix
76
        self.box1.hide()
1 by Benedict.Stein+ubuntu at gmail
first used release
77
        if self.service_is_running("squid3"):
78
          shell("service squid3 stop")
79
          shell("zenity --info --text='Internet is now switched OFF'")
80
        else:
81
          shell("service squid3 restart")
82
          sleep(2)
83
          if self.service_is_running("squid3"):
84
            #TODO - check connection with PING
85
            shell("zenity --info --text='Your internet is now on if your PC is connected to the internet.'")   
86
          else:
87
            shell("zenity --warning --text='Ops - something went wrong! - Contact your administrator\nSquid is not running after\n\nservice squid3 restart'") 
88
            self.sw_internet.activate() #disable after unsuccessful           
4 by Benedict.Stein+ubuntu at gmail
bugfix
89
        self.box1.show()      
1 by Benedict.Stein+ubuntu at gmail
first used release
90
    """
91
      Check if hostapd is running and change the status based on the user input
92
      5 seconds delay because hostapd takes some time to shutdown as a service
93
    """
94
    def on_wireless_activate(self, *args):
95
      if not(self.init):
4 by Benedict.Stein+ubuntu at gmail
bugfix
96
        self.box1.hide()
1 by Benedict.Stein+ubuntu at gmail
first used release
97
        if self.service_is_running("hostapd"):
98
          shell("service hostapd stop")
99
          shell("zenity --info --text='Internet is now switched OFF'")
100
        else:
101
          shell("service hostapd restart")
102
          sleep(2)
103
          if self.service_is_running("hostapd"):
104
            shell("zenity --info --text='You created a Wireless network for your classroom\n\n SSID:L4A\nPassword:OpenSourceRocks\nServerIP is 192.168.2.250'") 
105
          else:
106
            shell("zenity --warning --text='Ops - something went wrong! - Contact your administrator\nhostapd is not running after\n\nservice hostapd restart'") 
107
            self.sw_wireless.activate() #disable after unsuccessful
4 by Benedict.Stein+ubuntu at gmail
bugfix
108
        self.box1.show()
1 by Benedict.Stein+ubuntu at gmail
first used release
109
    """
110
      initiate the User regeneration process based on the number of users specified next to it.
111
      - check if one of the tsusers is still logged in
112
      - confirm action
113
    """
114
    def on_students_clicked(self, button, *args):
115
      if not(self.init):    
4 by Benedict.Stein+ubuntu at gmail
bugfix
116
        self.box1.hide()
117
        if not(shell("finger | grep tsuser")): 
1 by Benedict.Stein+ubuntu at gmail
first used release
118
            dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
119
            Gtk.ButtonsType.OK, "Please make sure that all students are logged off")
120
            dialog.run()
121
            dialog.destroy()
122
        else:
123
            dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Initialization of Student Accounts")
124
            dialog.format_secondary_text(
125
                "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?")
126
            response = dialog.run()
127
            dialog.destroy()
128
            if response == Gtk.ResponseType.YES:
129
                dialog2 = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
4 by Benedict.Stein+ubuntu at gmail
bugfix
130
                    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")
131
                dialog2.run()
132
                dialog2.destroy()
133
                shell("init_students "+str(int(self.scale_students.get_value())))
134
                
135
                dialog2 = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
136
                    Gtk.ButtonsType.OK, "Done creating "+str(int(self.scale_students.get_value()))+"new users - You can now use them")
1 by Benedict.Stein+ubuntu at gmail
first used release
137
                dialog2.run()
138
                dialog2.destroy()
139
                shell("init_students "+str(int(self.scale_students.get_value())))
140
            elif response == Gtk.ResponseType.NO:
141
                print "QUESTION dialog closed by clicking NO button"
4 by Benedict.Stein+ubuntu at gmail
bugfix
142
        self.box1.hide()
1 by Benedict.Stein+ubuntu at gmail
first used release
143
144
"""
145
  Start the whole application defined before
146
"""
147
if __name__ == "__main__":
148
    app = L4AAdminTool()
149
    app.window.connect("delete-event", Gtk.main_quit)
150
    app.window.show()
151
    Gtk.main()