$ ( function ( ) {
$ ( "select" ) . selectmenu ( {
width : 180
} ) ;
var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ ;
var server _creating = $ ( "#server_creating" ) . dialog ( {
autoOpen : false ,
width : 574 ,
modal : true ,
title : "Creating a new HA cluster" ,
buttons : {
Close : function ( ) {
$ ( this ) . dialog ( "close" ) ;
cleanProvisioningProccess ( '#server_creating ul li' , '#created-mess' , '#creating-error' , '#creating-warning' , '#creating-progress' , '#creating-master' , '#creating-slave' , '#haproxy_installing_div' , '#nginx_installing_div' ) ;
$ ( '#wait_mess' ) . show ( ) ;
}
}
} ) ;
var address _add = $ ( "#address_creating" ) . dialog ( {
autoOpen : false ,
width : 574 ,
modal : true ,
title : "Adding a new VRRP address" ,
buttons : {
Close : function ( ) {
$ ( this ) . dialog ( "close" ) ;
cleanProvisioningProccess ( '#address_creating ul li' , '#created-mess-add' , '#creating-error' , '#creating-warning-add' , '#creating-progress-add' , '#creating-master-add' , '#creating-slave-add' , '' , '' ) ;
$ ( '#wait_mess-add' ) . show ( ) ;
}
}
} ) ;
$ ( '#add-vrrp' ) . click ( function ( ) {
var kp = 0 ;
if ( $ ( '#kp' ) . is ( ':checked' ) ) {
kp = '1' ;
} else {
kp = '0' ;
}
$ ( "#ajax" ) . html ( '' )
if ( $ ( "#master-add" ) . val ( ) == "" || $ ( "#slave-add" ) . val ( ) == "" || $ ( "#interface-add" ) . val ( ) == "" ||
$ ( "#vrrp-ip-add" ) . val ( ) == "" ) {
toastr . warning ( 'Please fill in all fields' )
} else if ( ! $ ( "#vrrp-ip-add" ) . val ( ) . match ( ipformat ) ) {
toastr . warning ( 'Please enter IP in "VRRP IP" field' )
} else {
$ ( "#wait-mess-add" ) . html ( wait _mess ) ;
address _add . dialog ( 'open' ) ;
add _master _addr ( kp ) ;
$ . getScript ( "/inc/fontawesome.min.js" ) ;
add _slave _addr ( kp ) ;
}
} ) ;
$ ( '#create' ) . click ( function ( ) {
var hap = 0 ;
var nginx = 0 ;
var syn _flood = 0 ;
if ( $ ( '#hap' ) . is ( ':checked' ) ) {
hap = '1' ;
}
if ( $ ( '#nginx' ) . is ( ':checked' ) ) {
nginx = '1' ;
}
if ( $ ( '#syn_flood' ) . is ( ':checked' ) ) {
syn _flood = '1' ;
}
$ ( "#ajax" ) . html ( '' )
if ( $ ( "#master" ) . val ( ) == "" || $ ( "#slave" ) . val ( ) == "" || $ ( "#interface" ) . val ( ) == "" ||
$ ( "#vrrp-ip" ) . val ( ) == "" ) {
toastr . warning ( 'Please fill in all fields' ) ;
} else if ( ! $ ( "#vrrp-ip" ) . val ( ) . match ( ipformat ) ) {
toastr . warning ( 'Please enter IP in "VRRP IP" field' ) ;
} else if ( $ ( "#master" ) . val ( ) == $ ( "#slave" ) . val ( ) ) {
toastr . warning ( 'Master and slave must be diff servers' ) ;
} else {
$ ( "#wait-mess" ) . html ( wait _mess ) ;
server _creating . dialog ( 'open' ) ;
create _master _keepalived ( hap , nginx , syn _flood ) ;
$ . getScript ( "/inc/fontawesome.min.js" ) ;
create _slave _keepalived ( hap , nginx , syn _flood ) ;
if ( hap == '1' ) {
$ ( '#haproxy_installing_div' ) . show ( ) ;
}
if ( nginx == '1' ) {
$ ( '#nginx_installing_div' ) . show ( ) ;
}
}
} ) ;
$ ( '#hap' ) . click ( function ( ) {
if ( $ ( '#hap' ) . is ( ':checked' ) ) {
$ ( '#haproxy_docker_td' ) . show ( ) ;
$ ( '#haproxy_docker_td_header' ) . show ( ) ;
} else {
$ ( '#haproxy_docker_td' ) . hide ( ) ;
$ ( '#haproxy_docker_td_header' ) . hide ( ) ;
}
} ) ;
$ ( '#nginx' ) . click ( function ( ) {
if ( $ ( '#nginx' ) . is ( ':checked' ) ) {
$ ( '#nginx_docker_td' ) . show ( ) ;
$ ( '#nginx_docker_td_header' ) . show ( ) ;
} else {
$ ( '#nginx_docker_td' ) . hide ( ) ;
$ ( '#nginx_docker_td_header' ) . hide ( ) ;
}
} ) ;
} ) ;
function add _master _addr ( kp ) {
$ . ajax ( {
url : "options.py" ,
data : {
masteradd : $ ( '#master-add' ) . val ( ) ,
slaveadd : $ ( '#slave-add' ) . val ( ) ,
interfaceadd : $ ( "#interface-add" ) . val ( ) ,
vrrpipadd : $ ( '#vrrp-ip-add' ) . val ( ) ,
kp : kp ,
token : $ ( '#token' ) . val ( )
} ,
type : "POST" ,
success : function ( data ) {
data = data . replace ( /\s+/g , ' ' ) ;
if ( data . indexOf ( 'error:' ) != '-1' || data . indexOf ( 'UNREACHABLE' ) != '-1' ) {
showProvisioningError ( data , '#creating-master-add' , '#wait-mess-add' , '#creating-error-add' ) ;
} else if ( data == '' ) {
showProvisioningWarning ( '#creating-master-add' , 'master Keepalived' , '#creating-warning-add' , '#wait_mess-add' ) ;
} else if ( data . indexOf ( 'success' ) != '-1' ) {
showProvisioningProccess ( '<p>' + data + '</p>' , '#creating-master-add' , '50' , '#creating-progress-add' , '#created-mess-add' , '#wait-mess-add' ) ;
}
}
} ) ;
}
function add _slave _addr ( kp ) {
$ . ajax ( {
url : "options.py" ,
data : {
masteradd _slave : $ ( '#master-add' ) . val ( ) ,
slaveadd : $ ( '#slave-add' ) . val ( ) ,
interfaceadd : $ ( "#interface-add" ) . val ( ) ,
vrrpipadd : $ ( '#vrrp-ip-add' ) . val ( ) ,
kp : kp ,
token : $ ( '#token' ) . val ( )
} ,
type : "POST" ,
success : function ( data ) {
data = data . replace ( /\s+/g , ' ' ) ;
if ( data . indexOf ( 'error:' ) != '-1' || data . indexOf ( 'UNREACHABLE' ) != '-1' ) {
showProvisioningError ( data , '#creating-slave-add' , '#wait-mess-add' , '#creating-error-add' ) ;
} else if ( data == '' ) {
showProvisioningWarning ( '#creating-slave-add' , 'master Keepalived' , '#creating-warning-add' , '#wait_mess-add' ) ;
} else if ( data . indexOf ( 'success' ) != '-1' ) {
showProvisioningProccess ( '<p>' + data + '</p>' , '#creating-slave-add' , '100' , '#creating-progress-add' , '#created-mess-add' , '#wait-mess-add' ) ;
}
}
} ) ;
}
function create _master _keepalived ( hap , nginx , syn _flood ) {
if ( hap == '0' && nginx == '0' ) {
var progress _value = '50' ;
} else if ( hap == '1' || nginx == '0' ) {
var progress _value = '43' ;
} else if ( hap == '1' && nginx == '1' ) {
var progress _value = '50' ;
}
var virt _server = 0 ;
var haproxy _docker = 0 ;
var nginx _docker = 0 ;
if ( $ ( '#virt_server' ) . is ( ':checked' ) ) {
virt _server = '1' ;
}
if ( $ ( '#hap_docker' ) . is ( ':checked' ) ) {
haproxy _docker = '1' ;
}
if ( $ ( '#nginx_docker' ) . is ( ':checked' ) ) {
nginx _docker = '1' ;
}
$ . ajax ( {
url : "options.py" ,
data : {
master : $ ( '#master' ) . val ( ) ,
slave : $ ( '#slave' ) . val ( ) ,
interface : $ ( "#interface" ) . val ( ) ,
vrrpip : $ ( '#vrrp-ip' ) . val ( ) ,
hap : hap ,
nginx : nginx ,
syn _flood : syn _flood ,
virt _server : virt _server ,
token : $ ( '#token' ) . val ( )
} ,
type : "POST" ,
success : function ( data ) {
data = data . replace ( /\s+/g , ' ' ) ;
if ( data . indexOf ( 'error:' ) != '-1' || data . indexOf ( 'FAILED' ) != '-1' || data . indexOf ( 'UNREACHABLE' ) != '-1' ) {
showProvisioningError ( data , '#creating-master' , '#wait-mess' , '#creating-error' ) ;
} else if ( data == '' ) {
showProvisioningWarning ( step _id , 'master Keepalived' , '#creating-warning' , '#wait_mess' ) ;
} else if ( data . indexOf ( 'success' ) != '-1' ) {
showProvisioningProccess ( '<p>' + data + '</p>' , '#creating-master' , progress _value , '#creating-progress' , '#created-mess' , '#wait-mess' ) ;
if ( hap === '1' ) {
create _keep _alived _hap ( nginx , 'master' , haproxy _docker ) ;
}
if ( hap == '0' && nginx == '1' ) {
create _keep _alived _nginx ( 'master' , nginx _docker ) ;
}
} else {
toastr . clear ( ) ;
toastr . info ( data ) ;
}
}
} ) ;
}
function create _slave _keepalived ( hap , nginx , syn _flood ) {
if ( hap == '0' && nginx == '0' ) {
var progress _value = '100' ;
} else if ( hap == '1' || nginx == '0' ) {
var progress _value = '67' ;
} else if ( hap == '1' && nginx == '1' ) {
var progress _value = '50' ;
}
$ . ajax ( {
url : "options.py" ,
data : {
master _slave : $ ( '#master' ) . val ( ) ,
slave : $ ( '#slave' ) . val ( ) ,
interface : $ ( "#interface" ) . val ( ) ,
vrrpip : $ ( '#vrrp-ip' ) . val ( ) ,
hap : hap ,
nginx : nginx ,
syn _flood : syn _flood ,
token : $ ( '#token' ) . val ( )
} ,
type : "POST" ,
success : function ( data ) {
data = data . replace ( /\s+/g , ' ' ) ;
if ( data . indexOf ( 'error:' ) != '-1' || data . indexOf ( 'FAILED' ) != '-1' || data . indexOf ( 'UNREACHABLE' ) != '-1' ) {
showProvisioningError ( data , '#creating-slave' , '#wait-mess' , '#creating-error' ) ;
} else if ( data == '' ) {
showProvisioningWarning ( step _id , 'slave Keepalived' , '#creating-warning' , '#wait_mess' ) ;
} else if ( data . indexOf ( 'success' ) != '-1' ) {
showProvisioningProccess ( '<p>' + data + '</p>' , '#creating-slave' , progress _value , '#creating-progress' , '#created-mess' , '#wait-mess' ) ;
} else {
toastr . clear ( ) ;
toastr . info ( data ) ;
}
if ( hap === '1' ) {
create _keep _alived _hap ( nginx , 'slave' , docker ) ;
}
if ( hap == '0' && nginx == '1' ) {
create _keep _alived _nginx ( 'slave' , docker ) ;
}
}
} ) ;
}
function create _keep _alived _hap ( nginx , server , docker ) {
if ( nginx == '0' ) {
var progress _value = '100' ;
} else if ( nginx == '1' ) {
var progress _value = '75' ;
}
if ( server === 'master' ) {
var step _id = '#creating-haproxy-master' ;
var install _step = 'master Haproxy' ;
} else {
var step _id = '#creating-haproxy-slave' ;
var install _step = 'slave Haproxy' ;
}
$ ( step _id ) . addClass ( 'proccessing' ) ;
$ . ajax ( {
url : "options.py" ,
data : {
master _slave _hap : $ ( '#master' ) . val ( ) ,
slave : $ ( '#slave' ) . val ( ) ,
server : server ,
docker : docker ,
token : $ ( '#token' ) . val ( )
} ,
type : "POST" ,
success : function ( data ) {
data = data . replace ( /\s+/g , ' ' ) ;
if ( data . indexOf ( 'error:' ) != '-1' || data . indexOf ( 'FAILED' ) != '-1' || data . indexOf ( 'UNREACHABLE' ) != '-1' ) {
showProvisioningError ( data , step _id , '#wait-mess' , '#creating-error' ) ;
} else if ( data == '' ) {
showProvisioningWarning ( step _id , install _step , '#creating-warning' , '#wait_mess' ) ;
} else if ( data . indexOf ( 'success' ) != '-1' ) {
showProvisioningProccess ( '<br>' + data , step _id , progress _value , '#creating-progress' , '#created-mess' , '#wait-mess' ) ;
} else {
toastr . clear ( ) ;
toastr . info ( data ) ;
}
if ( nginx == '1' ) {
create _keep _alived _nginx ( server , docker )
}
}
} ) ;
}
function create _keep _alived _nginx ( server , docker ) {
if ( server === 'master' ) {
var step _id = '#creating-nginx-master' ;
var install _step = 'master Nginx' ;
} else {
var step _id = '#creating-nginx-slave' ;
var install _step = 'slave Nginx' ;
}
$ ( step _id ) . addClass ( 'proccessing' ) ;
$ . ajax ( {
url : "options.py" ,
data : {
master _slave _nginx : $ ( '#master' ) . val ( ) ,
slave : $ ( '#slave' ) . val ( ) ,
server : server ,
docker : docker ,
token : $ ( '#token' ) . val ( )
} ,
type : "POST" ,
success : function ( data ) {
data = data . replace ( /\s+/g , ' ' ) ;
if ( data . indexOf ( 'error:' ) != '-1' || data . indexOf ( 'FAILED' ) != '-1' || data . indexOf ( 'UNREACHABLE' ) != '-1' ) {
showProvisioningError ( data , step _id , '#wait-mess' , '#creating-error' ) ;
} else if ( data == '' ) {
showProvisioningWarning ( step _id , install _step , '#creating-warning' , '#wait_mess' ) ;
} else if ( data . indexOf ( 'success' ) != '-1' ) {
showProvisioningProccess ( '<br>' + data , step _id , '100' , '#creating-progress' , '#created-mess' , '#wait-mess' ) ;
} else {
toastr . clear ( ) ;
toastr . info ( data ) ;
}
}
} ) ;
}
function showProvisioningError ( data , step _id , wait _mess , error _id ) {
$ ( wait _mess ) . hide ( ) ;
$ ( error _id ) . append ( data ) ;
$ ( error _id ) . show ( ) ;
$ ( step _id ) . removeClass ( 'proccessing' ) ;
$ ( step _id ) . addClass ( 'processing_error' ) ;
$ . getScript ( "/inc/fontawesome.min.js" ) ;
}
function showProvisioningWarning ( step _id , install _step , warning _id , wait _id ) {
$ ( warning _id ) . append ( '<p>Something went wrong with installation on ' + install _step + ', check logs</p>' ) ;
$ ( warning _id ) . show ( ) ;
$ ( step _id ) . removeClass ( 'proccessing' ) ;
$ ( step _id ) . addClass ( 'processing_warning' ) ;
$ ( wait _id ) . hide ( ) ;
$ . getScript ( "/inc/fontawesome.min.js" ) ;
}
function cleanProvisioningProccess ( div _id , success _div , error _id , warning _id , progres _id , keepalived _m _div , keepalived _s _div , haproxy _div , nginx _div ) {
$ ( success _div ) . empty ( ) ;
$ ( success _div ) . hide ( ) ;
$ ( error _id ) . empty ( ) ;
$ ( error _id ) . hide ( ) ;
$ ( warning _id ) . empty ( ) ;
$ ( warning _id ) . hide ( ) ;
$ ( progres _id ) . css ( 'width' , '0%' ) ;
$ ( haproxy _div ) . hide ( ) ;
$ ( nginx _div ) . hide ( ) ;
$ ( div _id ) . each ( function ( ) {
$ ( this ) . removeClass ( 'proccessing_done' ) ;
$ ( this ) . removeClass ( 'processing_error' ) ;
$ ( this ) . removeClass ( 'processing_warning' ) ;
$ ( this ) . removeClass ( 'proccessing' ) ;
} ) ;
$ ( keepalived _m _div ) . addClass ( 'proccessing' ) ;
$ ( keepalived _s _div ) . addClass ( 'proccessing' ) ;
$ . getScript ( "/inc/fontawesome.min.js" ) ;
}
function showProvisioningProccess ( data , step _id , progress _value , progress _id , created _id , waid _id ) {
$ ( step _id ) . addClass ( 'proccessing_done' ) ;
$ ( step _id ) . removeClass ( 'proccessing' ) ;
$ ( created _id ) . show ( ) ;
$ ( created _id ) . append ( data ) ;
$ ( progress _id ) . css ( 'width' , progress _value + '%' ) ;
if ( progress _value === '100' )
$ ( waid _id ) . hide ( ) ;
$ . getScript ( "/inc/fontawesome.min.js" ) ;
}