ait/os/bin/opensecurityd/launch.py
branchom
changeset 2 c9bf2537109a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ait/os/bin/opensecurityd/launch.py	Tue Nov 12 11:31:34 2013 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +#!/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# ------------------------------------------------------------
     1.8 +# opensecurity-launcher
     1.9 +# 
    1.10 +# launches an application inside a VM
    1.11 +#
    1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 +#
    1.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    1.15 +# AIT Austrian Institute of Technology GmbH
    1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 +# http://www.ait.ac.at
    1.18 +#
    1.19 +# This program is free software; you can redistribute it and/or
    1.20 +# modify it under the terms of the GNU General Public License
    1.21 +# as published by the Free Software Foundation version 2.
    1.22 +# 
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +# 
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program; if not, write to the Free Software
    1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 +# Boston, MA  02110-1301, USA.
    1.32 +# ------------------------------------------------------------
    1.33 +
    1.34 +
    1.35 +# ------------------------------------------------------------
    1.36 +# imports
    1.37 +
    1.38 +import argparse
    1.39 +import os
    1.40 +import subprocess
    1.41 +import sys
    1.42 +import urllib
    1.43 +
    1.44 +from PyQt4 import QtCore
    1.45 +from PyQt4 import QtGui
    1.46 +
    1.47 +# local
    1.48 +from about import About
    1.49 +from environment import Environment
    1.50 +
    1.51 +
    1.52 +# ------------------------------------------------------------
    1.53 +# code
    1.54 +
    1.55 +
    1.56 +class Chooser(QtGui.QDialog):
    1.57 +    
    1.58 +    """Ask the user what to launch."""
    1.59 +    
    1.60 +    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    1.61 +        
    1.62 +        super(Chooser, self).__init__(parent, flags)
    1.63 +        self.setWindowTitle('OpenSecuirty Launch Application')
    1.64 +        self.setup_ui()
    1.65 +        
    1.66 +        # positionate ourself central
    1.67 +        screen = QtGui.QDesktopWidget().screenGeometry()
    1.68 +        self.resize(self.geometry().width() * 1.25, self.geometry().height())
    1.69 +        size = self.geometry()
    1.70 +        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    1.71 +        
    1.72 +
    1.73 +    def clicked_about(self):
    1.74 +        """clicked the about button"""
    1.75 +        dlgAbout = About()
    1.76 +        dlgAbout.exec_()
    1.77 +    
    1.78 +
    1.79 +    def clicked_cancel(self):
    1.80 +        """clicked the cancel button"""
    1.81 +        self.reject()
    1.82 +    
    1.83 +
    1.84 +    def clicked_ok(self):
    1.85 +        """clicked the ok button"""
    1.86 +        self.accept()
    1.87 +    
    1.88 +
    1.89 +    def setup_ui(self):
    1.90 +        
    1.91 +        """Create the widgets."""
    1.92 +        
    1.93 +        lyMain = QtGui.QVBoxLayout(self)
    1.94 +        lyMain.setContentsMargins(8, 8, 8, 8)
    1.95 +        
    1.96 +        # content area: left pixmap, right text
    1.97 +        lyContent = QtGui.QHBoxLayout()
    1.98 +        lyMain.addLayout(lyContent)
    1.99 +        
   1.100 +        # pixmap
   1.101 +        lbPix = QtGui.QLabel()
   1.102 +        lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
   1.103 +        lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
   1.104 +        lyContent.addSpacing(16)
   1.105 +        
   1.106 +        # launch ...
   1.107 +        lyLaunch = QtGui.QGridLayout()
   1.108 +        lyContent.addLayout(lyLaunch)
   1.109 +        lbTitle = QtGui.QLabel('Specify details for application to launch.')
   1.110 +        lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
   1.111 +        
   1.112 +        lbVM = QtGui.QLabel('&VM-ID:')
   1.113 +        lyLaunch.addWidget(lbVM, 1, 0)
   1.114 +        self.edVM = QtGui.QLineEdit()
   1.115 +        lyLaunch.addWidget(self.edVM, 1, 1)
   1.116 +        lbVM.setBuddy(self.edVM)
   1.117 +        
   1.118 +        # TODO: HARD CODED!
   1.119 +        self.edVM.setText('Debian 7')
   1.120 +        
   1.121 +        lbApplication = QtGui.QLabel('&Application:')
   1.122 +        lyLaunch.addWidget(lbApplication, 2, 0)
   1.123 +        self.cbApplication = QtGui.QComboBox()
   1.124 +        self.cbApplication.setEditable(True)
   1.125 +        lyLaunch.addWidget(self.cbApplication, 2, 1)
   1.126 +        lbApplication.setBuddy(self.cbApplication)
   1.127 +        
   1.128 +        # TODO: HARD CODED!
   1.129 +        self.cbApplication.addItem('iceweasel')
   1.130 +        self.cbApplication.addItem('vlc')
   1.131 +        self.cbApplication.addItem('xfce4-terminal')
   1.132 +        
   1.133 +        lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
   1.134 +        lyLaunch.setColumnStretch(1, 1)
   1.135 +        lyLaunch.setRowStretch(3, 1)
   1.136 +        
   1.137 +        lyMain.addStretch(1)
   1.138 +        
   1.139 +        # buttons
   1.140 +        lyButton = QtGui.QHBoxLayout()
   1.141 +        lyMain.addLayout(lyButton)
   1.142 +        
   1.143 +        lyButton.addStretch(1)
   1.144 +        btnOk = QtGui.QPushButton('&Ok', self)
   1.145 +        btnOk.setDefault(True)
   1.146 +        btnOk.setMinimumWidth(100)
   1.147 +        lyButton.addWidget(btnOk)
   1.148 +        btnCancel = QtGui.QPushButton('&Cancel', self)
   1.149 +        btnCancel.setMinimumWidth(100)
   1.150 +        lyButton.addWidget(btnCancel)
   1.151 +        btnAbout = QtGui.QPushButton('&About', self)
   1.152 +        btnAbout.setMinimumWidth(100)
   1.153 +        lyButton.addWidget(btnAbout)
   1.154 +        
   1.155 +        button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
   1.156 +        btnOk.setMinimumWidth(button_width)
   1.157 +        btnCancel.setMinimumWidth(button_width)
   1.158 +        btnAbout.setMinimumWidth(button_width)
   1.159 +        
   1.160 +        # reduce to the max
   1.161 +        self.resize(lyMain.minimumSize())
   1.162 +        
   1.163 +        # connectors
   1.164 +        btnOk.clicked.connect(self.clicked_ok)
   1.165 +        btnCancel.clicked.connect(self.clicked_cancel)
   1.166 +        btnAbout.clicked.connect(self.clicked_about)
   1.167 +
   1.168 +
   1.169 +def main():
   1.170 +    
   1.171 +    # parse command line
   1.172 +    app = QtGui.QApplication(sys.argv)
   1.173 +    
   1.174 +    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   1.175 +    data_path = Environment("opensecurity").image_path
   1.176 +    for file in os.listdir(data_path):
   1.177 +        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   1.178 +            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(data_path, file)))
   1.179 +            
   1.180 +    # we should have now our application icon
   1.181 +    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
   1.182 +    
   1.183 +    dlg = Chooser()
   1.184 +
   1.185 +    # pop up the dialog
   1.186 +    dlg.show()
   1.187 +    app.exec_()
   1.188 +    
   1.189 +    if dlg.result() == QtGui.QDialog.Accepted:
   1.190 +        # encode an proper GET request to the opensecurity daemon
   1.191 +        get_vm = urllib.quote(str(dlg.edVM.text()))
   1.192 +        get_app = urllib.quote(str(dlg.cbApplication.currentText()))
   1.193 +        osd_request = 'http://127.0.0.1:8080/application?vm={0}&app={1}'.format(get_vm, get_app)
   1.194 +        urllib.urlopen(osd_request)
   1.195 +        res = 0
   1.196 +    else:
   1.197 +        res = 1
   1.198 +        
   1.199 +    sys.exit(res)
   1.200 +    
   1.201 +# start
   1.202 +if __name__ == "__main__":
   1.203 +    main()
   1.204 +