2 * Copyright 2020 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.eliot.eliotbe.eliotk8sclient.service;
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;
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;
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;
50 public class AppService {
51 private static final Logger logger = LoggerFactory.getLogger(AppService.class);
53 private static final Gson gson = new Gson();
55 /* @Value("${DEPLOY_TYPE}")
56 private String deploymentType;*/
58 //@Value("${PACKAGE_BASE_PATH}")
59 private String packageBasePath = "/home/root1/eliot/deploy";
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;
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());
77 if (!packagePath.isEmpty()) {
78 File appPackage = new File(packagePath);
79 if (appPackage.exists()) {
81 FileUtils.cleanDirectory(appPackage);
82 FileUtils.deleteDirectory(appPackage);
83 } catch (IOException e) {
84 logger.error("failed to delete application package : {}",e.getMessage());
91 * Returns storage base path.
95 public String getPackageBasePath() {
96 File file = new File(packageBasePath);
98 logger.info("Inside File Exists");
99 return packageBasePath + '/';
101 boolean isMk = file.mkdir();
103 logger.info("Directory Create");
104 return AppConstants.EMPTY_STRING;
106 return packageBasePath + '/';
111 * Generate application package directory name.
113 * @param size directory name size
114 * @return directory name
116 private String getPackageDirName(int size) {
117 final String allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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())));
124 return sb.toString();