Last active
July 25, 2017 05:24
-
-
Save curioswati-zz/61cb80a58574ba48e2a7 to your computer and use it in GitHub Desktop.
Installer script for virtual box in ubuntu 14.04, 14.10, 12.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import subprocess | |
import optparse | |
import platform | |
install = [] | |
uninstall = ["sudo apt-get purge virtualbox*"] | |
PLATFORM = platform.system() | |
DIST_VERSION = platform.dist()[1] | |
ARCHITECTURE = platform.architecture()[0] | |
#-------------------------------------Running commands------------------------------------------------------------ | |
def run_commands(cmds): | |
for cmd in cmds: | |
try: | |
subprocess.call(cmd, shell=True) | |
except Exception as e: | |
print e | |
#--------------------------------Function to control option parsing in Python------------------------------------ | |
def controller(): | |
global VERBOSE, install, uninstall | |
#Create instance of OptionParser Module, included in Standard Library | |
p = optparse.OptionParser(description='For installing Virtual Box', | |
prog='install-virtual-box', | |
version='install-virtual-box 0.1', | |
usage= '%prog [option]') | |
p.add_option('--install','-i', action="store_true", help='installs virtual box') | |
p.add_option('--uninstall', '-u', action="store_true", help='uninstalls virtual box') | |
p.add_option('--Version', '-V', help='specifying version') | |
p.add_option('--verbose', '-v', | |
action = 'store_true', | |
help='prints verbosely', | |
default=False) | |
#Option Handling passes correct parameter to runBash | |
options, arguments = p.parse_args() | |
if options.verbose: | |
VERBOSE=True | |
if options.install: | |
#----------------------------------------setting commands--------------------------------------------- | |
#change to /tmp | |
install.append("cd /tmp") | |
#download the application | |
if PLATFORM == "Linux": | |
#Adding apt-key | |
if DIST_VERSION == "14.10": | |
install.append('echo "deb http://download.virtualbox.org/virtualbox/debian utopic contrib" | sudo tee /etc/apt/sources.list > /dev/null') | |
elif DIST_VERSION == "14.04": | |
install.append('echo "deb http://download.virtualbox.org/virtualbox/debian trusty contrib" | sudo tee /etc/apt/sources.list > /dev/null') | |
elif DIST_VERSION == "12.04": | |
install.append('echo "deb http://download.virtualbox.org/virtualbox/debian precise contrib" | sudo tee /etc/apt/sources.list > /dev/null') | |
install.append("wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -") | |
install.append("sudo apt-get update") | |
##For 4.3 | |
if not options.Version: | |
install.append("sudo apt-get install virtualbox-4.3") | |
#Or which specified | |
else: | |
install.append("sudo apt-get install virtualbox-"+options.Version) | |
#Istalling dkms | |
install.append("sudo apt-get install dkms") | |
else: | |
print "Wrong operating system detected." | |
value = run_commands(install) | |
elif options.uninstall: | |
value = run_commands(uninstall) | |
else: | |
p.print_help() | |
#Runs all the functions | |
def main(): | |
controller() | |
#This idiom means the below code only runs when executed from command line | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment