Added Deployment Files
[eliot.git] / blueprints / common / eliot-ui / be / src / eliotk8sclient / src / main / java / com / eliot / eliotbe / eliotk8sclient / service / AppService.java
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.eliot.eliotbe.eliotk8sclient.service;
18
19 import com.eliot.eliotbe.eliotk8sclient.deploy.GenericDeployFactory;
20 import com.google.gson.Gson;
21 import io.kubernetes.client.ApiException;
22 import io.kubernetes.client.apis.CoreV1Api;
23 import io.kubernetes.client.models.V1PodList;
24 import org.apache.tomcat.util.http.fileupload.FileUtils;
25 import org.apache.tomcat.util.http.fileupload.IOUtils;
26 import com.eliot.eliotbe.eliotk8sclient.AppException;
27 import com.eliot.eliotbe.eliotk8sclient.common.AppConstants;
28 import com.eliot.eliotbe.eliotk8sclient.deploy.GenericDeploy;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Component;
35 import org.springframework.web.multipart.MultipartFile;
36 import org.apache.http.ParseException;
37
38 import java.io.File;
39 //import org.apache.http.ParseException;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.nio.file.Path;
44 import java.security.SecureRandom;
45 import java.time.LocalDateTime;
46 import java.util.List;
47 import java.util.Map;
48
49 @Component
50 public class AppService {
51     private static final Logger logger = LoggerFactory.getLogger(AppService.class);
52
53     private static final  Gson gson = new Gson();
54
55 /*    @Value("${DEPLOY_TYPE}")
56     private String deploymentType;*/
57
58     //@Value("${PACKAGE_BASE_PATH}")
59     private String packageBasePath = "/home/root1/eliot/deploy";
60
61     public String instantiateApplication(String deploymentPathWithFile) {
62 /*        AppLcmUtils.setDefaultApiClient(k8sConfig);
63         String packageDir = getPackageDirName(AppConstants.MAX_PACKAGE_DIR_SIZE);
64         String packagePath = extractZipFile(file,packageDir);
65         if (packagePath.isEmpty()) {
66             logger.error("extract zip file fail");
67             return AppConstants.EMPTY_STRING;
68         }*/
69         try {
70             logger.info("Deployment Begin...");
71             GenericDeploy deploy = GenericDeployFactory.create("kubernetes");
72             return deploy.deploy(deploymentPathWithFile);
73         } catch (AppException | ParseException e) {
74             throw new AppException(e.getMessage());
75         }
76 /*        finally {
77             if (!packagePath.isEmpty()) {
78                 File appPackage = new File(packagePath);
79                 if (appPackage.exists()) {
80                     try {
81                         FileUtils.cleanDirectory(appPackage);
82                         FileUtils.deleteDirectory(appPackage);
83                     } catch (IOException e) {
84                         logger.error("failed to delete application package : {}",e.getMessage());
85                     }
86                 }
87             }
88         }*/
89     }
90     /**
91      * Returns storage base path.
92      *
93      * @return base path
94      */
95     public String getPackageBasePath() {
96         File file = new File(packageBasePath);
97         if (file.exists()) {
98             logger.info("Inside File Exists");
99             return packageBasePath + '/';
100         }
101         boolean isMk = file.mkdir();
102         if (!isMk) {
103             logger.info("Directory Create");
104             return AppConstants.EMPTY_STRING;
105         }
106         return packageBasePath + '/';
107     }
108
109
110     /**
111      * Generate application package directory name.
112      *
113      * @param size directory name size
114      * @return directory name
115      */
116     private String getPackageDirName(int size) {
117         final String allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
118
119         SecureRandom sr = new SecureRandom();
120         StringBuilder sb = new StringBuilder(size);
121         for (int i = 0; i < size; i++) {
122             sb.append(allowed.charAt(sr.nextInt(allowed.length())));
123         }
124         return sb.toString();
125     }
126
127
128
129 }