#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from models.Formatter import Formatter
from models.Lun import Lun
from models.PhysicalVolume import PhysicalVolume
from models.VolumeGroup import VolumeGroup
from models.LogicalVolume import LogicalVolume
from models.FileSystem import FileSystem
from models.Hana import Hana
from models.Hwcct import Hwcct
from string import Template
import optparse, os, re, subprocess

lu = Lun()
pv = PhysicalVolume()
vg = VolumeGroup()
lv = LogicalVolume()
fs = FileSystem()
sw = Hana()
hw = Hwcct()

def about():
	print "HOPE - HANA on POWER Enhanced"
	print "Version:      0"
	print "Modification: 11"
	print "Fix:          3"

def show(resource):
	resource.show()

def create(resource):
	resource.create()

def create_from_config_file(resource):
	resource.create_from_config_file()

def remove(resource):
	resource.remove()

def install_hana():
	sw.check_and_install_unrar()
	sw.extract_hana_software()
	sw.install()
	hw.extract()

def uninstall_hana():
	sw.remove()
	
def run_hwcct_eval_os():
	test_eval_os  = ['/opt/hope/templates/template_hwcct_eval_os.json' , '/opt/hope/config/eval_os.json']
	hw.generate_files(test_eval_os)
	hw.run_test(test_eval_os)

def run_hwcct_fs_short():
	test_fs_short = ['/opt/hope/templates/template_hwcct_fs_short.json', '/opt/hope/config/fs_short.json']
	hw.generate_files(test_fs_short)
	hw.run_test(test_fs_short)

def run_hwcct_fs_long():
	test_fs_long  = ['/opt/hope/templates/template_hwcct_fs_long.json' , '/opt/hope/config/fs_long.json']
	hw.generate_files(test_fs_long)
	hw.run_test(test_fs_long)

def menu():

	option = True
	
	while option:
		print '(1)  \033[32mList\033[0m   Multipath Storage Volumes'
		print '(2)  \033[32mList\033[0m   Physical Volumes'
		print '(3)  \033[32mList\033[0m   Volume Groups'
		print '(4)  \033[32mList\033[0m   Logical Volumes'
		print '(5)  \033[32mList\033[0m   File Systems'
		print '(6)  \033[32mList\033[0m   ALL Resources' + '\n'
		print '(7)  \033[33mCreate\033[0m Multipath Storage Volumes'
		print '(8)  \033[33mCreate\033[0m Physical Volumes'
		print '(9)  \033[33mCreate\033[0m Volume Groups'
		print '(10) \033[33mCreate\033[0m Logical Volumes'
		print '(11) \033[33mCreate\033[0m File Systems'
		print '(12) \033[33mCreate\033[0m ALL Resources' + '\n'
		print '(13) \033[31mREMOVE\033[0m Multipath Storage Volumes'
		print '(14) \033[31mREMOVE\033[0m Physical Volumes'
		print '(15) \033[31mREMOVE\033[0m Volume Groups'
		print '(16) \033[31mREMOVE\033[0m Logical Volumes'
		print '(17) \033[31mREMOVE\033[0m File Systems'
		print '(18) \033[31mREMOVE\033[0m ALL Resources' + '\n'
		print '(Q,q,E,e,X,x) Quit/Exit' + '\n'
		print 'Choose your Option:',
		option = raw_input()

		if (option == '1'):
			lu = Lun()
			show(lu)
		elif (option == '2'):
			pv = PhysicalVolume()
			show(pv)
		elif (option == '3'):
			vg = VolumeGroup()
			show(vg)
		elif (option == '4'):
			lv = LogicalVolume()
			show(lv)
		elif (option == '5'):
			fs = FileSystem()
			show(fs)
		elif (option == '6'):
			lu = Lun()
			pv = PhysicalVolume()
			vg = VolumeGroup()
			lv = LogicalVolume()
			fs = FileSystem()
			show(lu)
			show(pv)
			show(vg)
			show(lv)
			show(fs)
		elif (option == '7'):
			lu = Lun()
			create(lu)
		elif (option == '8'):
			pv = PhysicalVolume()
			create(pv)
		elif (option == '9'):
			vg = VolumeGroup()
			create(vg)
		elif (option == '10'):
			lv = LogicalVolume()
			create(lv)
		elif (option == '11'):
			fs = FileSystem()
			create(fs)
		elif (option == '12'):
			lu = Lun()
			pv = PhysicalVolume()
			vg = VolumeGroup()
			lv = LogicalVolume()
			fs = FileSystem()
			create(lu)
			create(pv)
			create(vg)
			create(lv)
			create(fs)
		elif (option == '13'):
			lu = Lun()
			remove(lu)
		elif (option == '14'):
			pv = PhysicalVolume()
			remove(pv)
		elif (option == '15'):
			vg = VolumeGroup()
			remove(vg)
		elif (option == '16'):
			lv = LogicalVolume()
			remove(lv)
		elif (option == '17'):
			fs = FileSystem()
			remove(fs)
		elif (option == '18'):
			lu = Lun()
			pv = PhysicalVolume()
			vg = VolumeGroup()
			lv = LogicalVolume()
			fs = FileSystem()
			remove(fs)
			remove(lv)
			remove(vg)
			remove(pv)
			remove(lu)
		elif (option in ['q' , 'Q' , 'e' , 'E', 'X', 'x']):
			break
		else:
			print 'Invalid Option!'

parser = optparse.OptionParser()

parser.add_option('-c', '--create',         '--cr',  action='store_true', dest='create',         help='Creates Resources.'                    )
parser.add_option('-l', '--list',           '--ls',  action='store_true', dest='list',           help='List Resources.'                       )
parser.add_option('-r', '--remove',         '--rm',  action='store_true', dest='remove',         help='Remove Resources.'                     )
parser.add_option('-v', '--version',                 action='store_true', dest='version',        help='Display Hope Version.'                 )
parser.add_option('-m', '--menu',                    action='store_true', dest='menu',           help='Shows an Interactive Menu.'            )
parser.add_option('-S', '--sanvolume',      '--sv',  action='store_true', dest='sanvolume',      help='Creates /etc/multipath.conf File.'     )
parser.add_option('-P', '--physicalvolume', '--pv',  action='store_true', dest='physicalvolume', help='Creates Physical Volumes.'             )
parser.add_option('-V', '--volumegroup',    '--vg',  action='store_true', dest='volumegroup',    help='Creates Volume Groups.'                )
parser.add_option('-L', '--logicalvolume',  '--lv',  action='store_true', dest='logicalvolume',  help='Creates Logical Volumes.'              )
parser.add_option('-F', '--filesystem',     '--fs',  action='store_true', dest='filesystem',     help='Creates File Systems.'                 )
parser.add_option('-C', '--configfile',     '--cfg', action='store_true', dest='configfile',     help='Creates Resources from Config File.'   )
parser.add_option('-i', '--install',                 action='store_true', dest='install',        help='Install SAP HANA Software.'            )
parser.add_option('-u', '--uninstall',               action='store_true', dest='uninstall',      help='Uninstall SAP HANA Software.'          )
parser.add_option('-e', '--evalos',                  action='store_true', dest='evalos',         help='Runs HWCCT EvalOs.'                    )
parser.add_option('-k', '--kpi_short',               action='store_true', dest='kpi_short',      help='Runs HWCCT for I/O KPIs in Short Mode.')
parser.add_option('-K', '--kpi_long',                action='store_true', dest='kpi_long',       help='Runs HWCCT for I/O KPIs in Long Mode.' )
parser.add_option('-a', '--all',                     action='store_true', dest='all',            help='Creates/List/Remove All Resources.'    )

(opts, args) = parser.parse_args()

if opts.version:
	about()

if opts.menu:
	menu()

if opts.list and opts.sanvolume:
	show(lu)

if opts.list and opts.physicalvolume:
	show(pv)

if opts.list and opts.volumegroup:
	show(vg)

if opts.list and opts.logicalvolume:
	show(lv)

if opts.list and opts.filesystem:
	show(fs)

if opts.list and opts.all:
	show(lu)
	show(pv)
	show(vg)
	show(lv)
	show(fs)

if opts.configfile and opts.sanvolume:
	create_from_config_file(lu)

if opts.configfile and opts.physicalvolume:
	create_from_config_file(pv)

if opts.configfile and opts.volumegroup:
	create_from_config_file(vg)

if opts.configfile and opts.logicalvolume:
	create_from_config_file(lv)

if opts.configfile and opts.filesystem:
	create_from_config_file(fs)

if opts.configfile and opts.install:
	install_hana()

if opts.configfile and opts.uninstall:
	uninstall_hana()

if opts.configfile and opts.evalos:
	run_hwcct_eval_os()

if opts.configfile and opts.kpi_short:
	run_hwcct_fs_short()

if opts.configfile and opts.kpi_long:
	run_hwcct_fs_long()

if opts.configfile and opts.all:
	create_from_config_file(lu)
	create_from_config_file(pv)
	create_from_config_file(vg)
	create_from_config_file(lv)
	create_from_config_file(fs)
	install_hana()
	run_hwcct_eval_os()
	run_hwcct_fs_short()
	run_hwcct_fs_long()

if opts.create and opts.sanvolume:
	create(lu)

if opts.create and opts.physicalvolume:
	create(pv)

if opts.create and opts.volumegroup:
	create(vg)

if opts.create and opts.logicalvolume:
	create(lv)

if opts.create and opts.filesystem:
	create(fs)

if opts.remove and opts.sanvolume:
	remove(lu)

if opts.remove and opts.physicalvolume:
	remove(pv)

if opts.remove and opts.volumegroup:
	remove(vg)

if opts.remove and opts.logicalvolume:
	remove(lv)

if opts.remove and opts.filesystem:
	remove(fs)

if opts.remove and opts.all:
	remove(fs)
	remove(lv)
	remove(vg)
	remove(pv)
	remove(lu)
