backuprestore tables 55/4155/4
authorSrinivasan <srinivasan.s.n@huawei.com>
Thu, 4 Feb 2021 19:17:28 +0000 (00:47 +0530)
committerSrinivasan <srinivasan.s.n@huawei.com>
Fri, 5 Feb 2021 12:23:45 +0000 (17:53 +0530)
Signed-off-by: Srinivasan <srinivasan.s.n@huawei.com>
Change-Id: Ib187661853872403a6237a5f2e53cb1c57b770f3

example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/controller/backupController.java
example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/model/EALTEdgeBackup.java
example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/model/EALTEdgeRestore.java
example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/service/backupService.java
example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/service/backupServiceHandler.java
example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/service/restoreService.java
example-apps/ROBO/backup_BE/src/main/java/org/edgegallery/example_app/util/ShellCommand.java

index bba8503..7d7a263 100644 (file)
@@ -37,7 +37,7 @@ public class backupController {
     @Autowired
     private backupServiceHandler BackupServiceHandler;
 
-    @GetMapping(value = "/backups-restores", produces = MediaType.APPLICATION_JSON)
+    @GetMapping(value = "/backup-restore", produces = MediaType.APPLICATION_JSON)
     @ApiOperation(value = "get backup and restore tables.", response = EALTEdgeBackupRestore.class,
             responseContainer = "List")
     @ApiResponses(value = {
index 11a399c..6ec2dc0 100644 (file)
@@ -17,8 +17,4 @@ public class EALTEdgeBackup {
     private String errors;
     private String warnings;
     private String created;
-    private String expires;
-    private String storage;
-    private String location;
-    private String selector;
 }
index 178f73b..fa321e8 100644 (file)
@@ -13,10 +13,4 @@ public class EALTEdgeRestore {
     private String name;
     private String backup;
     private String status;
-    private String started;
-    private String completed;
-    private String errors;
-    private String warnings;
-    private String created;
-    private String selector;
 }
index 60aebf7..10906d3 100644 (file)
@@ -1,11 +1,17 @@
 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 {
@@ -23,21 +29,11 @@ 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;
     }
index 2c4642d..5e01c02 100644 (file)
@@ -1,6 +1,5 @@
 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;
@@ -17,20 +16,21 @@ public class backupServiceHandler {
 
     @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);
     }
 
     /**
index f340e96..cd8881a 100644 (file)
@@ -1,7 +1,13 @@
 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;
@@ -22,22 +28,18 @@ public class restoreService {
         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;
     }
 }
index cc53aa5..0f392e0 100644 (file)
@@ -1,20 +1,55 @@
 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 =
@@ -22,31 +57,117 @@ public class ShellCommand {
 
             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