package org.edgegallery.example_app.service;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
import java.util.List;
+import java.util.StringTokenizer;
+
+import org.apache.commons.lang.StringUtils;
import org.edgegallery.example_app.model.EALTEdgeBackup;
import org.springframework.beans.factory.annotation.Autowired;
import org.edgegallery.example_app.util.ShellCommand;
import org.springframework.stereotype.Service;
+import org.edgegallery.example_app.common.*;
@Service
public class backupService {
}
public List<EALTEdgeBackup> getBackupTables() {
+ String ip = System.getenv("HOSTIP");
+ String command = "sshpass ssh root@" + ip + " velero get backups";
- EALTEdgeBackup backup = new EALTEdgeBackup();
- String command = "velero get backups";
-
- String output = ShellCommands.executeCommand(command);
-
- //System.out.println(output);
List<EALTEdgeBackup> backupsList = new ArrayList<EALTEdgeBackup>();
-
- String list = ShellCommands.parseResult(output);
-
- //TODO: after parse the result, need to fill info in backup node in list
- backup.setName("backup1");
-
- backupsList.add(backup);
+ backupsList = ShellCommands.executeBackupCommand(command);
return backupsList;
}
package org.edgegallery.example_app.service;
-import java.util.ArrayList;
import java.util.List;
import org.edgegallery.example_app.model.EALTEdgeBackup;
import org.edgegallery.example_app.model.EALTEdgeBackupRestore;
@Autowired
private restoreService RestoreService;
+
/**
* get back/restore tables.
* @return
*/
public ResponseEntity<EALTEdgeBackupRestore> getBackupRestoreDetails() {
- EALTEdgeBackupRestore eALTEdgeBackupRestore = new EALTEdgeBackupRestore();
+ EALTEdgeBackupRestore ealtEdgeBackupRestore = new EALTEdgeBackupRestore();
- List<EALTEdgeBackup> backupsList = BackupService.getBackupTables();
+ List<EALTEdgeBackup> backupsList = BackupService.getBackupTables();
List<EALTEdgeRestore> restoresList = RestoreService.getRestoreTables();
- eALTEdgeBackupRestore.setBackupsData(backupsList);
- eALTEdgeBackupRestore.setRestoresData(restoresList);
- return ResponseEntity.ok(eALTEdgeBackupRestore);
+ ealtEdgeBackupRestore.setBackupsData(backupsList);
+ ealtEdgeBackupRestore.setRestoresData(restoresList);
+ return ResponseEntity.ok(ealtEdgeBackupRestore);
}
/**
package org.edgegallery.example_app.service;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
import java.util.List;
+import java.util.StringTokenizer;
+
+import org.apache.commons.lang.StringUtils;
+import org.edgegallery.example_app.common.Constants;
import org.edgegallery.example_app.model.EALTEdgeBackup;
import org.edgegallery.example_app.model.EALTEdgeRestore;
import org.edgegallery.example_app.util.ShellCommand;
System.out.println(output);
return "success";
}
-
+
+ /**
+ * get restore table and parse
+ * @return
+ */
public List<EALTEdgeRestore> getRestoreTables() {
- EALTEdgeRestore restoreDetails = new EALTEdgeRestore();
- String command = "velero get restores";
-
- String output = shellCommand.executeCommand(command);
+ String ip = System.getenv("HOSTIP");
+ String command = "sshpass ssh root@" + ip + " velero get restores";
- //System.out.println(output);
List<EALTEdgeRestore> restoresList = new ArrayList<EALTEdgeRestore>();
+ restoresList = shellCommand.executeRestoreCommand(command);
- String list = shellCommand.parseResult(output);
-
- //TODO: after parse the result, need to fill info in backup node in list
- restoreDetails.setName("restore1");
-
- restoresList.add(restoreDetails);
return restoresList;
}
}
package org.edgegallery.example_app.util;
import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
import java.util.List;
+import java.util.StringTokenizer;
+
+import org.apache.commons.lang.StringUtils;
+import org.edgegallery.example_app.model.EALTEdgeBackup;
+import org.edgegallery.example_app.model.EALTEdgeRestore;
import org.springframework.stereotype.Service;
+import com.jcraft.jsch.*;
+
@Service
public class ShellCommand {
- public String executeCommand(String command) {
+ public String executeCommand(String command) {
- StringBuffer output = new StringBuffer();
+ StringBuffer output = new StringBuffer();
- Process p;
- try {
+ Process p;
+ try {
+
+ p = Runtime.getRuntime().exec(command);
+ p.waitFor();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
+
+ String line = "";
+ while ((line = reader.readLine())!= null) {
+ output.append(line + "\n");
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ return output.toString();
+
+ }
+
+ public List<EALTEdgeBackup> executeBackupCommand(String command) {
+
+ EALTEdgeBackup backup = new EALTEdgeBackup();
+ List<EALTEdgeBackup> backupsList = new ArrayList<EALTEdgeBackup>();
+
+ try {
+ Process p;
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
String line = "";
while ((line = reader.readLine())!= null) {
- output.append(line + "\n");
+ if(line.startsWith("NAME")) {
+ continue;
+ }
+ else {
+ backup = parseBackupResult(line);
+ backupsList.add(backup);
+ }
}
} catch (Exception e) {
e.printStackTrace();
}
+ return backupsList;
+ }
+
+ public static EALTEdgeBackup parseBackupResult(String newstr){
- return output.toString();
+ EALTEdgeBackup backup = new EALTEdgeBackup();
+ List<String> al = new ArrayList<String>();
+
+ StringTokenizer st = new StringTokenizer(newstr, " ");
+ StringBuffer sb = new StringBuffer();
+
+ while(st.hasMoreElements()) {
+ sb.append(st.nextElement()).append(" ");
+ }
+
+ String newstrwithProperSpacing = sb.toString();
+ String str[] = newstrwithProperSpacing.split(" ");
+
+ str[4] = str[4] + str[5] + str[6] + str[7];
+
+ al = Arrays.asList(str);
+
+ for(int i = 0; i < al.size(); i++) {
+ if( i == 0 ) {
+ backup.setName(al.get(i));
+ }
+ if( i == 1) {
+ backup.setStatus(al.get(i));
+ }
+ if( i == 2) {
+ backup.setErrors(al.get(i));
+ }
+ if( i == 3){
+ backup.setWarnings(al.get(i));
+ }
+ if( i == 4) {
+ backup.setCreated(al.get(i));
+ }
+ }
+ return backup;
}
- //parse velero cmd and get details
- public String parseResult(String msg){
- List<String> itemsList = new ArrayList<String>();
+ public List<EALTEdgeRestore> executeRestoreCommand(String command) {
- /*
- if (msg == null || msg.equals(""))
- return itemsList;
+ EALTEdgeRestore restore = new EALTEdgeRestore();
+ List<EALTEdgeRestore> restoresList = new ArrayList<EALTEdgeRestore>();
+
+ try {
+ Process p;
+ p = Runtime.getRuntime().exec(command);
+ p.waitFor();
+ BufferedReader reader =
+ new BufferedReader(new InputStreamReader(p.getInputStream()));
- matcher = pattern.matcher(msg);
- while (matcher.find()) {
- ipList.add(matcher.group(0));
+ String line = "";
+ while ((line = reader.readLine())!= null) {
+ if(line.startsWith("NAME")) {
+ continue;
+ }
+ else {
+ restore = parseRestoreResult(line);
+ restoresList.add(restore);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
}
- return ipList;
-*/
- return "success";
+ return restoresList;
+ }
+
+ public static EALTEdgeRestore parseRestoreResult(String newstr){
+
+ EALTEdgeRestore restore = new EALTEdgeRestore();
+
+ StringTokenizer st = new StringTokenizer(newstr, " ");
+ StringBuffer sb = new StringBuffer();
+
+ while(st.hasMoreElements()) {
+ sb.append(st.nextElement()).append(" ");
+ }
+
+ String newstrwithProperSpacing = sb.toString();
+ String str[] = newstrwithProperSpacing.split(" ");
+
+ List<String> ll = new LinkedList<String>(Arrays.asList(str));
+
+ for(int i = 0; i < ll.size(); i++) {
+ if( i == 0 ) {
+ restore.setName(ll.get(i));
+ }
+ if( i == 1) {
+ restore.setBackup(ll.get(i));
+ }
+ if( i == 2) {
+ restore.setStatus(ll.get(i));
+ }
+ }
+
+ return restore;
}
}
\ No newline at end of file