FORGE: Added including script
[myslice.git] / forge / script / serviceScript.py
1 #!/usr/bin/env python
2
3 import paramiko
4 from scp import SCPClient
5 import time
6
7 yumOpt = "sudo -S yum -y --nogpgcheck "
8
9 # Functions used for the php module (you need to install an http server)
10 def setPHP(ssh):
11         execute(yumOpt+" install php", ssh)
12         execute("sudo -S /sbin/service httpd restart", ssh)
13
14 def removePHP(ssh):
15         execute(yumOpt+" remove php", ssh)
16
17 # Functions used for the X redirection using ssh
18 def setXRedirect(ssh):
19         # Installing X11 system and an alternate ssh server
20         execute(yumOpt+" install xorg-x11-xauth", ssh)
21         #time.sleep(10)
22
23 def removeXRedirect(ssh):
24         # Remove X11 system and the alternate ssh server
25         execute(yumOpt+" remove xorg-x11-xauth", ssh)
26
27
28 # Functions used for an http server
29 def setHttpd(ssh, port, hostUrl):
30         port = int(port)
31         execute(yumOpt+" install -y httpd", ssh)
32         SCPClient(ssh.get_transport()).put("./configService/httpd.conf", "~")
33         SCPClient(ssh.get_transport()).put("./configService/index.html", "~")
34         execute("sudo -S mv ~/index.html /var/www/html/", ssh)
35         execute("sudo -S echo \"<html>You are on "+str(hostUrl)+"</html>\" >> /var/www/html/index.html", ssh)
36         execute("sudo -S dd if=/dev/zero of=/var/www/html/file1Mo bs=1M count=1", ssh)
37         execute("sudo -S chmod a+r /var/www/html/file1Mo", ssh)
38         execute("sudo -S mv ~/httpd.conf /etc/httpd/conf/", ssh, display = True)
39         execute("sudo -S chown root:root /etc/httpd/conf/httpd.conf", ssh, display = True)
40         execute("sudo -S sed -i -e \"s/Listen \\*:80/Listen \\*:"+str(port)+"/g\" /etc/httpd/conf/httpd.conf", ssh)
41         returnCode = -1
42         while(returnCode != 0):
43                 time.sleep(2)
44                 returnCode = execute("sudo -S /sbin/service httpd restart", ssh, display = True)
45                 print returnCode
46                 if returnCode != 0:
47                         execute("sudo -S sed -i -e \"s/Listen \\*:"+str(port)+"/Listen \\*:"+str(port+1)+"/g\" /etc/httpd/conf/httpd.conf", ssh)
48                         port += 1
49         return port
50
51 def removeHttpd(ssh):
52         #Remove the httpd service
53         execute(yumOpt+" remove httpd", ssh)
54         execute("sudo -S rm -rf /etc/httpd/*", ssh)
55
56
57 # Functions used for wireshark traffic analyzer (you have to install X redirection)
58 def setWireshark(ssh):
59         # Install wireshark
60         execute(yumOpt+" install wireshark wireshark-gnome", ssh)
61         execute(yumOpt+" install wireshark xorg-x11-fonts-Type1", ssh)
62         
63 def removeWireshark(ssh):
64         # Remove wireshark
65         execute(yumOpt+" remove wireshark wireshark-gnome", ssh)
66         execute(yumOpt+" remove wireshark xorg-x11-fonts-Type1", ssh)
67
68 # Functions used for the browser firefox (you have to install X redirection)
69 def setFirefox(ssh):
70         # Install firefox
71         execute(yumOpt+" install firefox", ssh)
72         execute("sudo -S sh -c \"dbus-uuidgen > /var/lib/dbus/machine-id\"", ssh)
73
74 def removeFirefox(ssh):
75         # Remove firefox
76         execute(yumOpt+" remove firefox", ssh)
77
78
79 # Function used to execute a command on a remote host using ssh
80 def execute(command, ssh, display=False, retour=False):
81         print "# "+command
82         stdin, stdout, stderr = ssh.exec_command(command)
83         stdin.close()
84         # Wait for the end of the command
85         while not stdout.channel.exit_status_ready():
86                 time.sleep(2)
87         err = stderr.read()
88         if err != None:
89                 splitted = err.splitlines()
90                 if len(splitted) > 0:
91                         print "\tError in execution"
92                         for line in splitted:
93                                 print "\t > " + line    
94         if display:
95                 for line in stdout.read().splitlines():
96                         print " > " + line
97         elif retour:
98                 return stdout.read()
99         return stdout.channel.recv_exit_status()