|
| 1 | +package com.k4m.dx.tcontrol.server; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.io.FileWriter; |
| 10 | +import java.net.Socket; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Iterator; |
| 14 | + |
| 15 | +import org.json.simple.JSONObject; |
| 16 | +import org.json.simple.parser.JSONParser; |
| 17 | +import org.json.simple.parser.ParseException; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import com.k4m.dx.tcontrol.socket.ProtocolID; |
| 22 | +import com.k4m.dx.tcontrol.socket.SocketCtl; |
| 23 | +import com.k4m.dx.tcontrol.socket.TranCodeType; |
| 24 | +import com.k4m.dx.tcontrol.socket.client.ClientProtocolID; |
| 25 | +import com.k4m.dx.tcontrol.util.CommonUtil; |
| 26 | +import com.k4m.dx.tcontrol.util.FileUtil; |
| 27 | + |
| 28 | +public class DxT055 extends SocketCtl{ |
| 29 | + private Logger errLogger = LoggerFactory.getLogger("errorToFile"); |
| 30 | + private Logger socketLogger = LoggerFactory.getLogger("socketLogger"); |
| 31 | + |
| 32 | + public DxT055(Socket socket, BufferedInputStream is, BufferedOutputStream os) { |
| 33 | + this.client = socket; |
| 34 | + this.is = is; |
| 35 | + this.os = os; |
| 36 | + } |
| 37 | + |
| 38 | + @SuppressWarnings("unchecked") |
| 39 | + public void execute(String strDxExCode, JSONObject jObj) throws Exception { |
| 40 | + socketLogger.info("DxT055.execute : " + strDxExCode); |
| 41 | + byte[] sendBuff = null; |
| 42 | + String strErrCode = ""; |
| 43 | + String strErrMsg = ""; |
| 44 | + String strSuccessCode = "0"; |
| 45 | + String filePath = ""; |
| 46 | + |
| 47 | + CommonUtil util = new CommonUtil(); |
| 48 | + JSONObject outputObj = new JSONObject(); |
| 49 | + |
| 50 | + try { |
| 51 | + String pghomePathCmd = "echo $PGHOME"; |
| 52 | + String pghomePath = util.getPidExec(pghomePathCmd); |
| 53 | + |
| 54 | + String backrestLogPathCmd = "echo $PGBLOG"; |
| 55 | + String backrestLogPath = util.getPidExec(backrestLogPathCmd); |
| 56 | + |
| 57 | + String hostUserCmd = "echo $HOSTUSER"; |
| 58 | + String hostUser = util.getPidExec(hostUserCmd); |
| 59 | + |
| 60 | + String configPath = pghomePath + "/etc/pgbackrest/default.conf"; |
| 61 | + filePath = pghomePath + "/etc/pgbackrest/config/" + String.valueOf(jObj.get(ProtocolID.WRK_NM) + ".conf"); |
| 62 | + |
| 63 | + BufferedReader br = new BufferedReader(new FileReader(new File(configPath))); |
| 64 | + BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath))); |
| 65 | + |
| 66 | + socketLogger.info("DxT055.execute Restore Type: " + String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT))); |
| 67 | + |
| 68 | + String fileContent; |
| 69 | + while((fileContent = br.readLine()) != null) { |
| 70 | + fileContent = fileContent.replaceAll("#log-path=", "log-path=" + backrestLogPath); |
| 71 | + fileContent = fileContent.replaceAll("#log-level-console=detail", "log-level-console=detail"); |
| 72 | + fileContent = fileContent.replaceAll("#log-level-file=detail", "log-level-file=detail"); |
| 73 | + fileContent = fileContent.replaceAll("#pg1-path=", "pg1-path=" + String.valueOf(jObj.get(ClientProtocolID.PGDATA))); |
| 74 | + fileContent = fileContent.replaceAll("#pg1-port=", "pg1-port=" + String.valueOf(jObj.get(ClientProtocolID.DBMS_PORT))); |
| 75 | + fileContent = fileContent.replaceAll("#pg1-user=", "pg1-user=" + String.valueOf(jObj.get(ClientProtocolID.SPR_USR_ID))); |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).equals("cloud")) { |
| 80 | + ArrayList<String> cloudKeyList = new ArrayList<String>(); |
| 81 | + ArrayList<String> cloudValueList = new ArrayList<String>(); |
| 82 | + |
| 83 | + if(jObj.get(ClientProtocolID.CLOUD_MAP) == null || jObj.get(ClientProtocolID.CLOUD_MAP).equals("")) { |
| 84 | + }else { |
| 85 | + try { |
| 86 | + JSONParser parser = new JSONParser(); |
| 87 | + JSONObject jsonObject = (JSONObject) parser.parse(String.valueOf(jObj.get(ClientProtocolID.CLOUD_MAP))); |
| 88 | + Iterator<String> cloudKeys = jsonObject.keySet().iterator(); |
| 89 | + |
| 90 | + while(cloudKeys.hasNext()){ |
| 91 | + String key = cloudKeys.next().toString(); |
| 92 | + cloudKeyList.add(key); |
| 93 | + cloudValueList.add(String.valueOf(jsonObject.get(key))); |
| 94 | + } |
| 95 | + |
| 96 | + } catch (ParseException e1) { |
| 97 | + errLogger.info("DxT055 {} : JSON Parser Error (Cloud Opt)"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if(cloudKeyList.size() != 0) { |
| 102 | + for(int i=0; i < cloudKeyList.size(); i++) { |
| 103 | + socketLogger.info("DxT055.execute : cloudKeyList " + cloudKeyList.get(i) ); |
| 104 | + if(cloudKeyList.get(i).equals("s3_bucket")) { |
| 105 | + fileContent = fileContent.replaceAll("#repo1-s3-bucket=", "repo1-s3-bucket=" + cloudValueList.get(i)); |
| 106 | + }else if(cloudKeyList.get(i).equals("s3_region")) { |
| 107 | + fileContent = fileContent.replaceAll("#repo1-s3-region=", "repo1-s3-region=" + cloudValueList.get(i)); |
| 108 | + }else if(cloudKeyList.get(i).equals("s3_key")) { |
| 109 | + fileContent = fileContent.replaceAll("#repo1-s3-key=", "repo1-s3-key=" + cloudValueList.get(i)); |
| 110 | + }else if(cloudKeyList.get(i).equals("s3_endpoint")) { |
| 111 | + fileContent = fileContent.replaceAll("#repo1-s3-endpoint=", "repo1-s3-endpoint=" + cloudValueList.get(i)); |
| 112 | + }else if(cloudKeyList.get(i).equals("s3_path")) { |
| 113 | + fileContent = fileContent.replaceAll("#repo1-path=", "repo1-path=" + cloudValueList.get(i)); |
| 114 | + }else if(cloudKeyList.get(i).equals("s3_key-secret")) { |
| 115 | + fileContent = fileContent.replaceAll("#repo1-s3-key-secret=", "repo1-s3-key-secret=" + cloudValueList.get(i)); |
| 116 | + }else if(cloudKeyList.get(i).equals("cloud_type")) { |
| 117 | + fileContent = fileContent.replaceAll("#repo1-type=", "repo1-type=s3"); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + }else if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).toLowerCase().equals("remote")){ |
| 122 | + fileContent = fileContent.replaceAll("#repo1-host=", "repo1-host=" + String.valueOf(jObj.get(ClientProtocolID.REMOTE_IP))); |
| 123 | + fileContent = fileContent.replaceAll("#repo1-host-port=", "repo1-host-port=" + String.valueOf(jObj.get(ClientProtocolID.REMOTE_PORT))); |
| 124 | + fileContent = fileContent.replaceAll("#repo1-host-user=", "repo1-host-user=" + String.valueOf(jObj.get(ClientProtocolID.REMOTE_USR))); |
| 125 | + }else if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).equals("localRemote")){ |
| 126 | + fileContent = fileContent.replaceAll("#repo1-host-user=", "repo1-host-user=" + String.valueOf(jObj.get(ClientProtocolID.HOSTUSER))); |
| 127 | + fileContent = fileContent.replaceAll("#repo1-host-port=", "repo1-host-port=" + String.valueOf(jObj.get(ClientProtocolID.SSH_PORT))); |
| 128 | + fileContent = fileContent.replaceAll("#repo1-host=", "repo1-host=" + String.valueOf(jObj.get(ClientProtocolID.DBMS_IP))); |
| 129 | + }else if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).equals("HAlocal")){ |
| 130 | + fileContent = fileContent.replaceAll("#repo1-host-user=", "repo1-host-user=" + hostUser); |
| 131 | + fileContent = fileContent.replaceAll("#repo1-host=", "repo1-host=" + String.valueOf(jObj.get(ClientProtocolID.DBMS_IP))); |
| 132 | + } |
| 133 | + |
| 134 | + fileContent = fileContent.replaceAll("#repo1-path=", "repo1-path=" + String.valueOf(jObj.get(ClientProtocolID.BCK_FILE_PTH))); |
| 135 | + |
| 136 | + bw.write(fileContent + "\r\n"); |
| 137 | + bw.flush(); |
| 138 | + } |
| 139 | + |
| 140 | + bw.close(); |
| 141 | + br.close(); |
| 142 | + |
| 143 | + boolean blnIsFile = FileUtil.isFile(filePath); |
| 144 | + |
| 145 | + if(blnIsFile) { |
| 146 | + outputObj.put(ProtocolID.DX_EX_CODE, strDxExCode); |
| 147 | + outputObj.put(ProtocolID.RESULT_CODE, strSuccessCode); |
| 148 | + outputObj.put(ProtocolID.ERR_CODE, strErrCode); |
| 149 | + outputObj.put(ProtocolID.ERR_MSG, strErrMsg); |
| 150 | + |
| 151 | + sendBuff = outputObj.toString().getBytes(); |
| 152 | + send(4, sendBuff); |
| 153 | + }else { |
| 154 | + errLogger.info("DxT055 {} : file no create"); |
| 155 | + } |
| 156 | + }catch (Exception e) { |
| 157 | + errLogger.info("DxT055 {} : " + e.toString()); |
| 158 | + |
| 159 | + outputObj.put(ProtocolID.DX_EX_CODE, TranCodeType.DxT055); |
| 160 | + outputObj.put(ProtocolID.RESULT_CODE, "1"); |
| 161 | + outputObj.put(ProtocolID.ERR_CODE, TranCodeType.DxT055); |
| 162 | + outputObj.put(ProtocolID.ERR_MSG, "DxT055 Error [" + e.toString() + "]"); |
| 163 | + HashMap hp = new HashMap(); |
| 164 | + outputObj.put(ProtocolID.RESULT_DATA, hp); |
| 165 | + |
| 166 | + sendBuff = outputObj.toString().getBytes(); |
| 167 | + send(4, sendBuff); |
| 168 | + } finally { |
| 169 | + outputObj = null; |
| 170 | + sendBuff = null; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments