ait/os/bin/opensecurityd/launch.py
author om
Tue, 12 Nov 2013 11:31:34 +0100
branchom
changeset 2 c9bf2537109a
permissions -rwxr-xr-x
added C/C++ and Python sources
om@2
     1
#!/bin/env python
om@2
     2
# -*- coding: utf-8 -*-
om@2
     3
om@2
     4
# ------------------------------------------------------------
om@2
     5
# opensecurity-launcher
om@2
     6
# 
om@2
     7
# launches an application inside a VM
om@2
     8
#
om@2
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@2
    10
#
om@2
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@2
    12
# AIT Austrian Institute of Technology GmbH
om@2
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@2
    14
# http://www.ait.ac.at
om@2
    15
#
om@2
    16
# This program is free software; you can redistribute it and/or
om@2
    17
# modify it under the terms of the GNU General Public License
om@2
    18
# as published by the Free Software Foundation version 2.
om@2
    19
# 
om@2
    20
# This program is distributed in the hope that it will be useful,
om@2
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@2
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@2
    23
# GNU General Public License for more details.
om@2
    24
# 
om@2
    25
# You should have received a copy of the GNU General Public License
om@2
    26
# along with this program; if not, write to the Free Software
om@2
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@2
    28
# Boston, MA  02110-1301, USA.
om@2
    29
# ------------------------------------------------------------
om@2
    30
om@2
    31
om@2
    32
# ------------------------------------------------------------
om@2
    33
# imports
om@2
    34
om@2
    35
import argparse
om@2
    36
import os
om@2
    37
import subprocess
om@2
    38
import sys
om@2
    39
import urllib
om@2
    40
om@2
    41
from PyQt4 import QtCore
om@2
    42
from PyQt4 import QtGui
om@2
    43
om@2
    44
# local
om@2
    45
from about import About
om@2
    46
from environment import Environment
om@2
    47
om@2
    48
om@2
    49
# ------------------------------------------------------------
om@2
    50
# code
om@2
    51
om@2
    52
om@2
    53
class Chooser(QtGui.QDialog):
om@2
    54
    
om@2
    55
    """Ask the user what to launch."""
om@2
    56
    
om@2
    57
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@2
    58
        
om@2
    59
        super(Chooser, self).__init__(parent, flags)
om@2
    60
        self.setWindowTitle('OpenSecuirty Launch Application')
om@2
    61
        self.setup_ui()
om@2
    62
        
om@2
    63
        # positionate ourself central
om@2
    64
        screen = QtGui.QDesktopWidget().screenGeometry()
om@2
    65
        self.resize(self.geometry().width() * 1.25, self.geometry().height())
om@2
    66
        size = self.geometry()
om@2
    67
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
om@2
    68
        
om@2
    69
om@2
    70
    def clicked_about(self):
om@2
    71
        """clicked the about button"""
om@2
    72
        dlgAbout = About()
om@2
    73
        dlgAbout.exec_()
om@2
    74
    
om@2
    75
om@2
    76
    def clicked_cancel(self):
om@2
    77
        """clicked the cancel button"""
om@2
    78
        self.reject()
om@2
    79
    
om@2
    80
om@2
    81
    def clicked_ok(self):
om@2
    82
        """clicked the ok button"""
om@2
    83
        self.accept()
om@2
    84
    
om@2
    85
om@2
    86
    def setup_ui(self):
om@2
    87
        
om@2
    88
        """Create the widgets."""
om@2
    89
        
om@2
    90
        lyMain = QtGui.QVBoxLayout(self)
om@2
    91
        lyMain.setContentsMargins(8, 8, 8, 8)
om@2
    92
        
om@2
    93
        # content area: left pixmap, right text
om@2
    94
        lyContent = QtGui.QHBoxLayout()
om@2
    95
        lyMain.addLayout(lyContent)
om@2
    96
        
om@2
    97
        # pixmap
om@2
    98
        lbPix = QtGui.QLabel()
om@2
    99
        lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
om@2
   100
        lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
om@2
   101
        lyContent.addSpacing(16)
om@2
   102
        
om@2
   103
        # launch ...
om@2
   104
        lyLaunch = QtGui.QGridLayout()
om@2
   105
        lyContent.addLayout(lyLaunch)
om@2
   106
        lbTitle = QtGui.QLabel('Specify details for application to launch.')
om@2
   107
        lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
om@2
   108
        
om@2
   109
        lbVM = QtGui.QLabel('&VM-ID:')
om@2
   110
        lyLaunch.addWidget(lbVM, 1, 0)
om@2
   111
        self.edVM = QtGui.QLineEdit()
om@2
   112
        lyLaunch.addWidget(self.edVM, 1, 1)
om@2
   113
        lbVM.setBuddy(self.edVM)
om@2
   114
        
om@2
   115
        # TODO: HARD CODED!
om@2
   116
        self.edVM.setText('Debian 7')
om@2
   117
        
om@2
   118
        lbApplication = QtGui.QLabel('&Application:')
om@2
   119
        lyLaunch.addWidget(lbApplication, 2, 0)
om@2
   120
        self.cbApplication = QtGui.QComboBox()
om@2
   121
        self.cbApplication.setEditable(True)
om@2
   122
        lyLaunch.addWidget(self.cbApplication, 2, 1)
om@2
   123
        lbApplication.setBuddy(self.cbApplication)
om@2
   124
        
om@2
   125
        # TODO: HARD CODED!
om@2
   126
        self.cbApplication.addItem('iceweasel')
om@2
   127
        self.cbApplication.addItem('vlc')
om@2
   128
        self.cbApplication.addItem('xfce4-terminal')
om@2
   129
        
om@2
   130
        lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
om@2
   131
        lyLaunch.setColumnStretch(1, 1)
om@2
   132
        lyLaunch.setRowStretch(3, 1)
om@2
   133
        
om@2
   134
        lyMain.addStretch(1)
om@2
   135
        
om@2
   136
        # buttons
om@2
   137
        lyButton = QtGui.QHBoxLayout()
om@2
   138
        lyMain.addLayout(lyButton)
om@2
   139
        
om@2
   140
        lyButton.addStretch(1)
om@2
   141
        btnOk = QtGui.QPushButton('&Ok', self)
om@2
   142
        btnOk.setDefault(True)
om@2
   143
        btnOk.setMinimumWidth(100)
om@2
   144
        lyButton.addWidget(btnOk)
om@2
   145
        btnCancel = QtGui.QPushButton('&Cancel', self)
om@2
   146
        btnCancel.setMinimumWidth(100)
om@2
   147
        lyButton.addWidget(btnCancel)
om@2
   148
        btnAbout = QtGui.QPushButton('&About', self)
om@2
   149
        btnAbout.setMinimumWidth(100)
om@2
   150
        lyButton.addWidget(btnAbout)
om@2
   151
        
om@2
   152
        button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
om@2
   153
        btnOk.setMinimumWidth(button_width)
om@2
   154
        btnCancel.setMinimumWidth(button_width)
om@2
   155
        btnAbout.setMinimumWidth(button_width)
om@2
   156
        
om@2
   157
        # reduce to the max
om@2
   158
        self.resize(lyMain.minimumSize())
om@2
   159
        
om@2
   160
        # connectors
om@2
   161
        btnOk.clicked.connect(self.clicked_ok)
om@2
   162
        btnCancel.clicked.connect(self.clicked_cancel)
om@2
   163
        btnAbout.clicked.connect(self.clicked_about)
om@2
   164
om@2
   165
om@2
   166
def main():
om@2
   167
    
om@2
   168
    # parse command line
om@2
   169
    app = QtGui.QApplication(sys.argv)
om@2
   170
    
om@2
   171
    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
om@2
   172
    data_path = Environment("opensecurity").image_path
om@2
   173
    for file in os.listdir(data_path):
om@2
   174
        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
om@2
   175
            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(data_path, file)))
om@2
   176
            
om@2
   177
    # we should have now our application icon
om@2
   178
    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
om@2
   179
    
om@2
   180
    dlg = Chooser()
om@2
   181
om@2
   182
    # pop up the dialog
om@2
   183
    dlg.show()
om@2
   184
    app.exec_()
om@2
   185
    
om@2
   186
    if dlg.result() == QtGui.QDialog.Accepted:
om@2
   187
        # encode an proper GET request to the opensecurity daemon
om@2
   188
        get_vm = urllib.quote(str(dlg.edVM.text()))
om@2
   189
        get_app = urllib.quote(str(dlg.cbApplication.currentText()))
om@2
   190
        osd_request = 'http://127.0.0.1:8080/application?vm={0}&app={1}'.format(get_vm, get_app)
om@2
   191
        urllib.urlopen(osd_request)
om@2
   192
        res = 0
om@2
   193
    else:
om@2
   194
        res = 1
om@2
   195
        
om@2
   196
    sys.exit(res)
om@2
   197
    
om@2
   198
# start
om@2
   199
if __name__ == "__main__":
om@2
   200
    main()
om@2
   201