Brekeke Contact Center Suite Wiki

Sample Script of Switching Brekeke CCS

The Sample Script for switching Brekeke CCS it can be set at Brekeke PBX >[SYSTEM]->[Redundancy]->[Mirroring]->[Script].

 

/***Need to change according to the environment*******************************/
// URL of CIM and CA
var CIM_URL = "http://192.168.0.1:8080/cim";
var CA_URL = "http://192.168.0.1:8080/ca";
// If the property "webif.v1p.system.restart.password" is configured on the Advanced Option field on CIM and CA,
// set password.
// If not, no password setting is needed.
var CIM_PD = "";
var CA_PD = "";
// Peer CIM and CA URL
var OPPOSITE_CIM_URL = "http://192.168.0.2:8080/cim";
var OPPOSITE_CA_URL = "http://192.168.0.2:8080/ca";
// If the property "webif.v1p.system.restart.password" is configured on the Advanced Option field on Peer CIM and CA,
// set password.
// If not, no password setting is needed.
var OPPOSITE_CIM_PD = "";
var OPPOSITE_CA_PD = "";
// Whether CA's scheduled report is used (false: no, true: yes)
var CA_SCHEDULED_REPORT = false;
/***************************************************************************/
var OPERATION_RESTART = "/webif/v1p/system/restart";
var OPERATION_STOP = "/webif/v1p/system/stop";
var OPERATION_STATUS = "/webif/v1p/system/status";
var RETRY_GET_STATUS = 5;
var RETRY_RESTART = 5;
var RETRY_STOP = 5;
var CMD_LSYNCD_START = "sudo systemctl start lsyncd";
var CMD_LSYNCD_STOP = "sudo systemctl stop lsyncd";
var is_exec_restart = false;
var is_exec_stop = false;
var is_exec_oppsite_stop = false;
var is_exec_report_sync_start = false;
var is_exec_report_sync_stop = false;
var URL = Java.type("java.net.URL");
var BufferedReader = Java.type("java.io.BufferedReader");
var BufferedWriter = Java.type("java.io.BufferedWriter");
var InputStreamReader = Java.type("java.io.InputStreamReader");
var OutputStreamWriter = Java.type("java.io.OutputStreamWriter");
var Thread = Java.type("java.lang.Thread");
var SimpleDateFormat = Java.type("java.text.SimpleDateFormat");
var Date = Java.type("java.util.Date");
var File = Java.type("java.io.File");
var FileReader = Java.type("java.io.FileReader");
var Boolean = Java.type("java.lang.Boolean");
var ProcessBuilder = Java.type("java.lang.ProcessBuilder");
// Log output function (with timestamp)
var f_writeLogWithDate = function(msg) {
  var df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
  log.filewriteln(msg + "\ttimestamp=" + df.format(new Date()));
}
// REST API execution function
var f_execOperation = function(url, ope, password) {
  var url = new URL(url + ope);
  var uc = null;
  var out = null;
  var ins = null;
  var ret = false;
  var ret_data = "";
  try {
    uc = url.openConnection();      // Get connection
    uc.setRequestMethod("POST");    // Method
    uc.setRequestProperty("Content-Type", "application/json");  // Content-Type
    uc.setDoOutput(true);       // Allow request body submission
    // Write request body
    out = new BufferedWriter(new OutputStreamWriter(uc.getOutputStream()));
    var content = "";
    if(password) {
      content = '{"password":"' + password + '"}';
    }
    out.write(content);
    out.flush();
    out.close();
    // Read response body
    var i = null;
    try {
      i = uc.getInputStream();
    } catch(ie) {
      i = uc.getErrorStream();
    }
    if(i == null) {
      f_writeLogWithDate("No response.");
    } else {
      ins = new BufferedReader(new InputStreamReader(i));
      var s;
      var response_text = "";
      while((s = ins.readLine()) !== null) {
        response_text += s;
        response_text += "\r\n";
      }
      var response_code = uc.getResponseCode();
      ins.close();
//      f_writeLogWithDate("response code=" + response_code);
//      f_writeLogWithDate("response body=" + response_text);
      f_writeLogWithDate("response code=" + response_code + ", response body="+ response_text);
      // Successful completion if the response code is 200
      if(response_code == 200) {
        ret = true;
      }
      ret_data = response_text;
    }
  } catch(e) {
    f_writeLogWithDate("Couldn't connect. detail=" + e.message);
  } finally {
    if(uc) {
      try {
        uc.disconnect();
      } catch(e){}
    }
  }
  return { result:ret, data:ret_data };
}
// Standby mode acquisition function
var f_getStandbyMode = function() {
  var standby_mode = null;
  var ret = null;
  var retry_cnt = 0;
  f_writeLogWithDate("Get cim standbymode.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(CIM_URL, OPERATION_STATUS, CIM_PD);
    if(ret["result"]) {
      var jsonObject = JSON.parse(ret["data"]);
      standby_mode = jsonObject.standby;
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_GET_STATUS);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed get cim standbymode.");
  }
  f_writeLogWithDate("standby mode=" + standby_mode);
  return standby_mode;
}
// CCS restart function
var f_restartCCS = function(cim_url, cim_password, ca_url, ca_password) {
  // Restart CIM
  var retry_cnt = 0;
  var ret = null;
  f_writeLogWithDate("Start restarting cim.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(cim_url, OPERATION_RESTART, cim_password);
    if(ret["result"]) {
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_RESTART);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed restarting cim.");
  }
  f_writeLogWithDate("End restarting cim.");
  // Restart CA
  retry_cnt = 0;
  ret = null;
  f_writeLogWithDate("Start restarting ca.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(ca_url, OPERATION_RESTART, ca_password);
    if(ret["result"]) {
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_RESTART);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed restarting ca.");
  }
  f_writeLogWithDate("End restarting ca.");
}
// CCS Stop Function
var f_stopCCS = function(cim_url, cim_password, ca_url, ca_password) {
  // Stop CA
  var retry_cnt = 0;
  var ret = null;
  f_writeLogWithDate("Start stopping ca.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(ca_url, OPERATION_STOP, ca_password);
    if(ret["result"]) {
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_STOP);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed stopping ca.");
  }
  f_writeLogWithDate("End stopping ca.");
  // Stop CIM
  retry_cnt = 0;
  ret = null;
  f_writeLogWithDate("Start stopping cim.");
  do {
    f_writeLogWithDate("execute #" + (retry_cnt+1));
    ret = f_execOperation(cim_url, OPERATION_STOP, cim_password);
    if(ret["result"]) {
      break;
    }
    retry_cnt++;
    Thread.sleep(1000);
  } while(retry_cnt < RETRY_STOP);
  if(!ret["result"]) {
    f_writeLogWithDate("Failed stopping cim.");
  }
  f_writeLogWithDate("End stopping cim.");
}
// Command execution function
var f_execCommand = function(cmd) {
  var br = null;
  var exec_cmd = cmd.split(" ");
  var pb = new ProcessBuilder(exec_cmd);
  pb.redirectErrorStream(true);   // Merge the contents of stderr into stdout
  try {
    // Start Process
    var process = pb.start();
    br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while(true) {
      var line = br.readLine();
      if(line == null) {
        break;
      }
      log.filewriteln(line);
    }
    // Waiting for process termination
    var ret = process.waitFor();
    f_writeLogWithDate("result = " + ret);
  } finally {
    if(br != null) {
      br.close();
    }
  }
}
if(event == 0) {
  if(phase == 1) {
    // When started as primary
    f_writeLogWithDate("Start as primary.");
    var standby_mode = f_getStandbyMode();
    if(standby_mode != null) {
      // If in standby mode, restart to go to active mode
      if(standby_mode) {
        is_exec_restart = true;
      }
    }
    // Start file synchronization when using scheduled reports
    if(CA_SCHEDULED_REPORT) {
      is_exec_report_sync_start = true;
    }
  }
  else if(phase == 2) {
    // When started as secondary
    f_writeLogWithDate("Start as secondary.");
    var standby_mode = f_getStandbyMode();
    if(standby_mode != null) {
      // If in active mode, stop to put it in standby mode
      if(!standby_mode) {
        is_exec_stop = true;
      }
    }
    // Stop file synchronization when using scheduled reports
    if(CA_SCHEDULED_REPORT) {
      is_exec_report_sync_stop = true;
    }
  }
  else if(phase == 3) {
    // When started as standalone
    f_writeLogWithDate("Start as standalone.");
    var standby_mode = f_getStandbyMode();
    if(standby_mode != null) {
      // If in standby mode, restart to go to active mode
      if(standby_mode) {
        is_exec_restart = true;
      }
    }
    // Start file synchronization when using scheduled reports
    if(CA_SCHEDULED_REPORT) {
      is_exec_report_sync_start = true;
    }
  }
  else {
  }
}
if(event == 1) {
  if((phase == 3) && (phaseold == 2)) {
    // When transitioning from secondary to standalone
    f_writeLogWithDate("Switch to standalone from secondary.");
    is_exec_restart = true;
    is_exec_oppsite_stop = true;
    // Start file synchronization when using scheduled reports
    if(CA_SCHEDULED_REPORT) {
      is_exec_report_sync_start = true;
    }
  }
  else if((phase == 2) && (phaseold == 1)) {
    // When transitioning from primary to secondary
    f_writeLogWithDate("Switch to secondary from primary.");
    is_exec_stop = true;
    // Stop file synchronization when using scheduled reports
    if(CA_SCHEDULED_REPORT) {
      is_exec_report_sync_stop = true;
    }
  }
  else if((phase == 1) && (phaseold == 2)) {
    // When transitioning from secondary to primary
    f_writeLogWithDate("Switch to primary from secondary.");
    is_exec_restart = true;
    is_exec_oppsite_stop = true;
    // Start file synchronization when using scheduled reports
    if(CA_SCHEDULED_REPORT) {
      is_exec_report_sync_start = true;
    }
  }
  else {
  }
}
if(is_exec_oppsite_stop) {
  // Opposite CCS stop
  f_writeLogWithDate("Start stopping oppsite ccs.");
  f_stopCCS(OPPOSITE_CIM_URL, OPPOSITE_CIM_PD, OPPOSITE_CA_URL, OPPOSITE_CA_PD);
  f_writeLogWithDate("End stopping oppsite ccs.");
}
if(is_exec_restart) {
  // Restart CCS
  f_writeLogWithDate("Start restarting own ccs.");
  f_restartCCS(CIM_URL, CIM_PD, CA_URL, CA_PD);
  f_writeLogWithDate("End restarting own ccs.");
}
if(is_exec_stop) {
  // Stop CCS
  f_writeLogWithDate("Start stopping own ccs.");
  f_stopCCS(CIM_URL, CIM_PD, CA_URL, CA_PD);
  f_writeLogWithDate("End stopping own ccs.");
}
if(CA_SCHEDULED_REPORT) {
  if(is_exec_report_sync_start) {
    // Start lsyncd for file sync
    f_writeLogWithDate("Start file sync.");
    f_execCommand(CMD_LSYNCD_START);
  }
  if(is_exec_report_sync_stop) {
    // stop lsyncd for file sync
    f_writeLogWithDate("Stop file sync.");
    f_execCommand(CMD_LSYNCD_STOP);
  }
}
Yes No
Suggest Edit