OpenSecurity/client/launch.py
changeset 14 c187aaceca32
parent 12 11dc05750aea
child 15 2e4cb1ebcbed
     1.1 --- a/OpenSecurity/client/launch.py	Fri Dec 06 10:47:26 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,287 +0,0 @@
     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 -
    1.43 -from PyQt4 import QtCore
    1.44 -from PyQt4 import QtGui
    1.45 -
    1.46 -# local
    1.47 -from about import About
    1.48 -from cygwin import Cygwin
    1.49 -from environment import Environment
    1.50 -import opensecurity_server
    1.51 -
    1.52 -
    1.53 -# ------------------------------------------------------------
    1.54 -# code
    1.55 -
    1.56 -
    1.57 -class Chooser(QtGui.QDialog, object):
    1.58 -    
    1.59 -    """Ask the user what to launch."""
    1.60 -    
    1.61 -    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    1.62 -    
    1.63 -        super(Chooser, self).__init__(parent, flags)
    1.64 -        self.setWindowTitle('OpenSecuirty Launch Application')
    1.65 -        self.setup_ui()
    1.66 -        
    1.67 -        # known vms and applications
    1.68 -        self._apps, self_vms = [], []
    1.69 -        
    1.70 -        # positionate ourself central
    1.71 -        screen = QtGui.QDesktopWidget().screenGeometry()
    1.72 -        self.resize(self.geometry().width() * 1.25, self.geometry().height())
    1.73 -        size = self.geometry()
    1.74 -        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    1.75 -        
    1.76 -        # refresh vm and command input
    1.77 -        self.refresh()
    1.78 -        
    1.79 -        
    1.80 -    def app_get(self):
    1.81 -        """The application of the user."""
    1.82 -        a = str(self._cbApplication.currentText())
    1.83 -        for app in self._apps:
    1.84 -            if a == app['name']:
    1.85 -                return app['command']
    1.86 -        return a
    1.87 -        
    1.88 -    app = property(app_get)
    1.89 -        
    1.90 -
    1.91 -    def clicked_about(self):
    1.92 -        """clicked the about button"""
    1.93 -        dlgAbout = About()
    1.94 -        dlgAbout.exec_()
    1.95 -
    1.96 -
    1.97 -    def clicked_cancel(self):
    1.98 -        """clicked the cancel button"""
    1.99 -        self.reject()
   1.100 -    
   1.101 -
   1.102 -    def clicked_ok(self):
   1.103 -        """clicked the ok button"""
   1.104 -        self.accept()
   1.105 -    
   1.106 -    
   1.107 -    def refresh(self):
   1.108 -        """load the known vms and commands and adjust input fields"""
   1.109 -        
   1.110 -        self._apps = opensecurity_server.query_apps()
   1.111 -        self._vms = opensecurity_server.query_vms()
   1.112 -        
   1.113 -        # add the VMs we know
   1.114 -        self._cbApplication.clear()
   1.115 -        for app in self._apps:
   1.116 -            self._cbApplication.addItem(app['name'])
   1.117 -        
   1.118 -        # add the commands we know
   1.119 -        self._cbVM.clear()
   1.120 -        for vm in self._vms:
   1.121 -            self._cbVM.addItem(vm['name'])
   1.122 -        
   1.123 -        
   1.124 -    def setup_ui(self):
   1.125 -        """Create the widgets."""
   1.126 -        
   1.127 -        lyMain = QtGui.QVBoxLayout(self)
   1.128 -        lyMain.setContentsMargins(8, 8, 8, 8)
   1.129 -        
   1.130 -        # content area: left pixmap, right text
   1.131 -        lyContent = QtGui.QHBoxLayout()
   1.132 -        lyMain.addLayout(lyContent)
   1.133 -        
   1.134 -        # pixmap
   1.135 -        lbPix = QtGui.QLabel()
   1.136 -        lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
   1.137 -        lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
   1.138 -        lyContent.addSpacing(16)
   1.139 -        
   1.140 -        # launch ...
   1.141 -        lyLaunch = QtGui.QGridLayout()
   1.142 -        lyContent.addLayout(lyLaunch)
   1.143 -        lbTitle = QtGui.QLabel('Specify details for application to launch.')
   1.144 -        lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
   1.145 -        
   1.146 -        lbVM = QtGui.QLabel('&VM-ID:')
   1.147 -        lyLaunch.addWidget(lbVM, 1, 0)
   1.148 -        self._cbVM = QtGui.QComboBox()
   1.149 -        self._cbVM.setEditable(True)
   1.150 -        self._cbVM.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
   1.151 -        lyLaunch.addWidget(self._cbVM, 1, 1)
   1.152 -        lbVM.setBuddy(self._cbVM)
   1.153 -        
   1.154 -        lbApplication = QtGui.QLabel('&Application:')
   1.155 -        lyLaunch.addWidget(lbApplication, 2, 0)
   1.156 -        self._cbApplication = QtGui.QComboBox()
   1.157 -        self._cbApplication.setEditable(True)
   1.158 -        self._cbApplication.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
   1.159 -        lyLaunch.addWidget(self._cbApplication, 2, 1)
   1.160 -        lbApplication.setBuddy(self._cbApplication)
   1.161 -        
   1.162 -        lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
   1.163 -        lyLaunch.setColumnStretch(1, 1)
   1.164 -        lyLaunch.setRowStretch(3, 1)
   1.165 -        
   1.166 -        lyMain.addStretch(1)
   1.167 -        
   1.168 -        # buttons
   1.169 -        lyButton = QtGui.QHBoxLayout()
   1.170 -        lyMain.addLayout(lyButton)
   1.171 -        
   1.172 -        lyButton.addStretch(1)
   1.173 -        btnOk = QtGui.QPushButton('&Ok', self)
   1.174 -        btnOk.setDefault(True)
   1.175 -        btnOk.setMinimumWidth(100)
   1.176 -        lyButton.addWidget(btnOk)
   1.177 -        btnCancel = QtGui.QPushButton('&Cancel', self)
   1.178 -        btnCancel.setMinimumWidth(100)
   1.179 -        lyButton.addWidget(btnCancel)
   1.180 -        btnAbout = QtGui.QPushButton('&About', self)
   1.181 -        btnAbout.setMinimumWidth(100)
   1.182 -        lyButton.addWidget(btnAbout)
   1.183 -        
   1.184 -        button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
   1.185 -        btnOk.setMinimumWidth(button_width)
   1.186 -        btnCancel.setMinimumWidth(button_width)
   1.187 -        btnAbout.setMinimumWidth(button_width)
   1.188 -        
   1.189 -        # reduce to the max
   1.190 -        self.resize(lyMain.minimumSize())
   1.191 -        
   1.192 -        # connectors
   1.193 -        btnOk.clicked.connect(self.clicked_ok)
   1.194 -        btnCancel.clicked.connect(self.clicked_cancel)
   1.195 -        btnAbout.clicked.connect(self.clicked_about)
   1.196 -
   1.197 -        
   1.198 -    def user_get(self):
   1.199 -        """The user of the vm of choice."""
   1.200 -        v = str(self._cbVM.currentText())
   1.201 -        for vm in self._vms:
   1.202 -            if v == vm['name']:
   1.203 -                return vm['user']
   1.204 -        return v
   1.205 -        
   1.206 -    user = property(user_get)
   1.207 -    
   1.208 -    
   1.209 -    def vm_get(self):
   1.210 -        """The vm of choice."""
   1.211 -        v = str(self._cbVM.currentText())
   1.212 -        for vm in self._vms:
   1.213 -            if v == vm['name']:
   1.214 -                return vm['ip']
   1.215 -        return v
   1.216 -        
   1.217 -    vm = property(vm_get)
   1.218 -        
   1.219 -        
   1.220 -def ask_user():
   1.221 -    """ask the user for VM and app to start"""
   1.222 -    
   1.223 -    # launch Qt
   1.224 -    app = QtGui.QApplication(sys.argv)
   1.225 -    
   1.226 -    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   1.227 -    image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
   1.228 -    for file in os.listdir(image_path):
   1.229 -        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   1.230 -            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
   1.231 -            
   1.232 -    # we should have now our application icon
   1.233 -    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
   1.234 -    
   1.235 -    # pop up the dialog
   1.236 -    dlg = Chooser()
   1.237 -    dlg.show()
   1.238 -    app.exec_()
   1.239 -    
   1.240 -    if dlg.result() == QtGui.QDialog.Accepted:
   1.241 -        return dlg.user, dlg.vm, dlg.app
   1.242 -
   1.243 -    return '', '', ''
   1.244 -    
   1.245 -
   1.246 -def main():
   1.247 -    """entry point"""
   1.248 -    
   1.249 -    # parse command line
   1.250 -    parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
   1.251 -    parser.add_argument('user', metavar='USER', help='USER on Virtual Machine', nargs='?', type=str, default='')
   1.252 -    parser.add_argument('ip', metavar='IP', help='IP of Virtual Machine', nargs='?', type=str, default='')
   1.253 -    parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
   1.254 -    args = parser.parse_args()
   1.255 -    
   1.256 -    # we must have at least all or none set
   1.257 -    set_user = args.user != ''
   1.258 -    set_ip = args.ip != ''
   1.259 -    set_command = args.command != ''
   1.260 -    set_ALL = set_user and set_ip and set_command
   1.261 -    set_NONE = (not set_user) and (not set_ip) and (not set_command)
   1.262 -    if (not set_ALL) and (not set_NONE):
   1.263 -        sys.stderr.write("Please specify user, ip and command or none.\n")
   1.264 -        sys.stderr.write("Type '--help' for help.\n")
   1.265 -        sys.exit(1)
   1.266 -        
   1.267 -    # check if we need to ask the user
   1.268 -    if set_NONE:
   1.269 -        args.user, args.ip, args.command = ask_user()
   1.270 -        
   1.271 -    # still no IP? --> no chance, over and out!
   1.272 -    if args.ip == '':
   1.273 -        sys.exit(0)
   1.274 -        
   1.275 -    # ensure we have our X11 running
   1.276 -    Cygwin.start_X11()
   1.277 -    
   1.278 -    # the SSH command
   1.279 -    user_at_guest = args.user + '@' + args.ip
   1.280 -    ssh = 'DISPLAY=:0 /usr/bin/ssh -Y ' + user_at_guest + ' ' + args.command
   1.281 -    print(ssh)
   1.282 -    
   1.283 -    # off we go!
   1.284 -    Cygwin()(['/bin/bash', '--login', '-i', '-c', ssh], None, None, None)
   1.285 -
   1.286 -    
   1.287 -# start
   1.288 -if __name__ == "__main__":
   1.289 -    main()
   1.290 -