2017-10-21 20:04:42 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author : Tlams
Langage : Python
Minimum version require : 3.4
"""
from pathlib import Path
from api . v1 . api import *
2018-02-09 12:18:52 +00:00
from core . libs . logs import *
2017-10-21 20:04:42 +00:00
from core . modules . mod_access import *
import configparser
import getpass
import os
import stat
import urllib3
2018-04-27 15:24:31 +00:00
import argparse
2017-10-21 20:04:42 +00:00
urllib3 . disable_warnings ( urllib3 . exceptions . InsecureRequestWarning )
if __name__ == " __main__ " :
2018-02-09 12:18:52 +00:00
2018-04-27 15:24:31 +00:00
""" Arg parse """
parser = argparse . ArgumentParser ( )
group = parser . add_mutually_exclusive_group ( )
2018-04-28 11:45:45 +00:00
#group.add_argument("-a", "--api", action="store_true", help="Start only")
2018-04-27 15:24:31 +00:00
args = parser . parse_args ( )
2017-10-21 20:04:42 +00:00
""" Read conf """
localconf = configparser . ConfigParser ( )
localconf . read ( ' private/conf/config ' )
2018-03-22 15:32:46 +00:00
generalconf = {
2018-04-27 15:24:31 +00:00
" logger " : { " logs_level " : localconf [ ' logger ' ] [ ' logs_level ' ] ,
2018-04-12 16:29:34 +00:00
" logs_dir " : localconf [ ' logger ' ] [ ' logs_dir ' ] , " bulk_write " : localconf [ ' logger ' ] [ ' bulk_write ' ] ,
" bulk_size " : localconf [ ' logger ' ] [ ' bulk_size ' ] } ,
2018-04-29 20:55:05 +00:00
" analyst " : { " walker " : localconf [ ' walker ' ] [ ' walker ' ] , " walker_lock " : localconf [ ' walker ' ] [ ' walker_lock ' ] ,
" walker_uid " : localconf [ ' walker ' ] [ ' uid ' ]
} ,
2018-04-12 16:29:34 +00:00
2018-03-22 15:32:46 +00:00
" mongodb " : { " ip " : localconf [ ' databases ' ] [ ' mongodb_ip ' ] , ' port ' : localconf [ ' databases ' ] [ ' mongodb_port ' ] } ,
2018-04-12 16:29:34 +00:00
2018-03-22 15:32:46 +00:00
" redis " : { " ip " : localconf [ ' databases ' ] [ ' redis_ip ' ] , ' port ' : localconf [ ' databases ' ] [ ' redis_port ' ] } ,
2018-04-12 16:29:34 +00:00
" deploy " : { ' concurrencydeploy ' : localconf [ ' deploy ' ] [ ' concurrencydeploy ' ] ,
' delayrounddeploy ' : localconf [ ' deploy ' ] [ ' delayrounddeploy ' ] }
2018-03-22 15:32:46 +00:00
}
""" Active logger """
2018-04-12 16:42:17 +00:00
logger = Logger ( generalconf [ " logger " ] )
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " Start logger " } )
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " >>>>>>> -- NEW STARTUP -- <<<<<<< " } )
2018-03-22 15:32:46 +00:00
2017-10-21 20:04:42 +00:00
CritConf = CryticalData ( )
""" Step One: test private key or create it """
key_pvt = Path ( localconf [ ' system ' ] [ ' key_pvt ' ] )
if not key_pvt . is_file ( ) :
print ( " No key found, auto-generation started ... " )
passhash = encodepassphrase ( getpass . getpass ( " Need a passphrase to start the generation: " ) )
print ( " This action can take some minutes, please wait. " )
gen = CritConf . generate_key ( localconf [ ' system ' ] [ ' key_pvt ' ] , localconf [ ' system ' ] [ ' key_pub ' ] , passhash )
if gen [ ' result ' ] == " OK " :
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " Key generated in {0} " . format ( localconf [ ' system ' ] [ ' key_pvt ' ] ) } )
2017-10-21 20:04:42 +00:00
print ( " Your new key has been generate ! "
" \n - Private Key: {0} "
" \n - Public Key: {1} "
2018-03-22 15:32:46 +00:00
. format ( localconf [ ' system ' ] [ ' key_pvt ' ] , localconf [ ' system ' ] [ ' key_pub ' ] ) )
2017-10-21 20:04:42 +00:00
print ( " Passphrase HASH: {0} " . format ( passhash ) )
2018-04-28 21:33:14 +00:00
print ( " You MUST save your passphrase in a security place ! " )
2018-02-19 18:09:51 +00:00
key_pvt = CritConf . read_private_key ( localconf [ ' system ' ] [ ' key_pvt ' ] , passhash )
2017-10-21 20:04:42 +00:00
else :
print ( gen [ ' Error ' ] )
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " ERROR " , " type " : " HYPERPROXMOX " , " value " : " Your key is not create due to an error: {0} " . format ( gen [ ' value ' ] ) } )
2017-10-21 20:04:42 +00:00
exit ( 1 )
""" Test valid right for your private Key """
if oct ( stat . S_IMODE ( os . stat ( localconf [ ' system ' ] [ ' key_pvt ' ] ) . st_mode ) ) != oct ( 0o600 ) :
print ( " Your private key has not the good right( {0} )... "
" This problem can be very critical for your security. " .
format ( oct ( stat . S_IMODE ( os . stat ( localconf [ ' system ' ] [ ' key_pvt ' ] ) . st_mode ) ) ) )
os . chmod ( localconf [ ' system ' ] [ ' key_pvt ' ] , 0o600 )
print ( " Auto correction... done ! " )
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " Setting chmod on your key.. " } )
2017-10-21 20:04:42 +00:00
""" Step two """
if ' passhash ' not in vars ( ) :
2018-04-28 21:33:14 +00:00
passhash = encodepassphrase ( getpass . getpass ( " This system need a passphrase to start: " ) )
2017-10-21 20:04:42 +00:00
key_pvt = CritConf . read_private_key ( localconf [ ' system ' ] [ ' key_pvt ' ] , passhash )
if key_pvt [ ' result ' ] != " OK " :
2018-04-25 16:26:26 +00:00
print ( " {0} : {1} "
" \n Please verify your passphrase " . format ( key_pvt [ ' type ' ] , key_pvt [ ' value ' ] ) )
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " WARNING " , " type " : " HYPERPROXMOX " , " value " : " Bad passphrase, try again. " } )
2017-10-21 20:04:42 +00:00
exit ( 1 )
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " Loading keys in memory " } )
2017-10-21 20:04:42 +00:00
key_pub = CritConf . read_public_key ( localconf [ ' system ' ] [ ' key_pub ' ] )
2018-03-22 15:32:46 +00:00
generalconf [ " keys " ] = { " key_pvt " : key_pvt [ " value " ] , " key_pub " : key_pub [ " value " ] }
2017-11-07 09:30:33 +00:00
2017-10-21 20:04:42 +00:00
# URL MAPPING
urls = \
(
2018-02-11 18:40:30 +00:00
# FRESH DATA
2017-10-21 20:04:42 +00:00
# MAPPING INSTANCES
' /api/v1/instance ' , ' Instance ' ,
' /api/v1/instance/new ' , ' Instance ' ,
' /api/v1/instance/([0-9]+) ' , ' Instance ' ,
2018-02-18 17:53:15 +00:00
' /api/v1/instance/id/([a-z0-9]+)/status/(start|stop|current|reset|shutdown) ' , ' Instance ' ,
2017-10-21 20:04:42 +00:00
# AUTH
2018-04-27 15:24:31 +00:00
# '/api/v1/login', 'Login'
2017-10-21 20:04:42 +00:00
2018-02-15 11:05:50 +00:00
# MANAGEMENT CLUSTER
2018-04-28 16:53:52 +00:00
' /api/v1/administration/cluster/([0-9a-zA-Z \ _ \ -]+) ' , ' Cluster ' ,
2018-02-11 18:40:30 +00:00
' /api/v1/administration/cluster/ ' , ' Cluster ' ,
2018-04-28 16:04:26 +00:00
' /api/v1/administration/cluster ' , ' Cluster ' ,
2018-02-04 22:10:18 +00:00
2018-04-27 15:24:31 +00:00
# PURGE SYSTEM
2018-04-28 11:45:45 +00:00
' /api/v1/administration/purge ' , ' Purge ' ,
2018-04-27 15:24:31 +00:00
2018-02-04 22:10:18 +00:00
# CACHE DATA (MONGO)
# date/cluster/node/vmid
2018-02-08 18:18:02 +00:00
# Disks mapping
2018-02-11 18:40:30 +00:00
' /api/v1/static/(disks)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9]+) ' , ' QueryCache_Infra ' ,
' /api/v1/static/(disks)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
' /api/v1/static/(disks)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
2018-02-08 18:18:02 +00:00
' /api/v1/static/(disks)/([0-9]+)/ ' , ' QueryCache_Infra ' ,
# Storages mapping
2018-02-11 18:40:30 +00:00
' /api/v1/static/(storages)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
' /api/v1/static/(storages)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
2018-02-08 18:18:02 +00:00
' /api/v1/static/(storages)/([0-9]+)/ ' , ' QueryCache_Infra ' ,
# Instances mapping
2018-02-13 16:42:12 +00:00
' /api/v1/static/(instances)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9]+) ' , ' QueryCache_Infra ' ,
' /api/v1/static/(instances)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
' /api/v1/static/(instances)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
2018-02-07 17:59:47 +00:00
' /api/v1/static/(instances)/([0-9]+)/ ' , ' QueryCache_Infra ' ,
2018-02-08 18:18:02 +00:00
# Nodes mapping
2018-02-13 16:42:12 +00:00
' /api/v1/static/(nodes)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/([0-9a-zA-Z \ _ \ -]+) ' , ' QueryCache_Infra ' ,
' /api/v1/static/(nodes)/([0-9]+)/([0-9a-zA-Z \ _ \ -]+)/ ' , ' QueryCache_Infra ' ,
2018-02-07 17:59:47 +00:00
' /api/v1/static/(nodes)/([0-9]+)/ ' , ' QueryCache_Infra ' ,
2018-02-08 18:18:02 +00:00
# cluster mapping
2018-02-13 16:42:12 +00:00
' /api/v1/static/(clusters)/([0-9]+)/(?:[0-9a-zA-Z \ _ \ -]+) ' , ' QueryCache_Infra ' ,
2018-02-07 17:59:47 +00:00
' /api/v1/static/(clusters)/([0-9]+)/ ' , ' QueryCache_Infra ' ,
2018-02-04 22:10:18 +00:00
# date
2018-02-09 14:28:11 +00:00
' /api/v1/static/dates/(all|last) ' , ' QueryDates ' ,
2018-02-07 17:59:47 +00:00
2018-02-04 22:10:18 +00:00
# mongoid
2018-02-09 12:18:52 +00:00
' /api/v1/static/(instances|nodes|clusters|storages|disks)/id/([a-z0-9]+) ' , ' General_Search ' ,
2018-02-04 22:10:18 +00:00
2017-10-21 20:04:42 +00:00
)
""" Init Core thread """
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " Init Core thread " } )
core = Core ( generalconf , logger )
2017-10-21 20:04:42 +00:00
""" Init API thread """
2018-04-27 15:24:31 +00:00
logger . write ( { " thread " : threading . get_ident ( ) , " result " : " INFO " , " type " : " HYPERPROXMOX " , " value " : " Init API thread " } )
api_th = ThreadAPI ( 1 , " ThreadAPI " , urls , core , generalconf , logger )
2018-02-19 18:09:51 +00:00
api_th . start ( )