From 64c769862be9c48a29433ad12446a553a797a7fe Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 16 May 2024 00:10:51 +0800 Subject: [PATCH] [Feature] (Uninstall) Uninstall Service --- .../src/main/resources/bin/env.sh | 6 +- .../controller/UninstallingController.java | 55 +++++++++++++++++++ .../server/service/ServiceService.java | 7 +++ .../service/impl/ServiceServiceImpl.java | 53 ++++++++++++++++-- 4 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/UninstallingController.java diff --git a/bigtop-manager-agent/src/main/resources/bin/env.sh b/bigtop-manager-agent/src/main/resources/bin/env.sh index 5533b6bc..fad602a2 100644 --- a/bigtop-manager-agent/src/main/resources/bin/env.sh +++ b/bigtop-manager-agent/src/main/resources/bin/env.sh @@ -16,6 +16,8 @@ # specific language governing permissions and limitations # under the License. # - - +DEFAULT_JAVA_HOME="$(which java | sed 's/\/bin\/java//g')" +if [[ -z $JAVA_HOME ]];then + export JAVA_HOME="${DEFAULT_JAVA_HOME}" +fi export JAVA_OPTS="" \ No newline at end of file diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/UninstallingController.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/UninstallingController.java new file mode 100644 index 00000000..bae7a07b --- /dev/null +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/UninstallingController.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.apache.bigtop.manager.server.model.vo.ServiceVO; +import org.apache.bigtop.manager.server.service.ServiceService; +import org.apache.bigtop.manager.server.utils.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@Tag(name = "Uninstalling Controller") +@RestController +@RequestMapping("uninstall/{clusterId}/service") +public class UninstallingController { + + @Resource + private ServiceService serviceService; + + /** + * before call this api, please firstly call api "/api/clusters/1/services/1" to confirm all will be deleted services + * @param clusterId cluster id + * @param serviceId service id + * @return deleted services list + */ + @Operation(summary = "uninstall", description = "uninstall a service") + @GetMapping("/{serviceId}") + public ResponseEntity> get(@PathVariable Long clusterId, @PathVariable Long serviceId) { + return ResponseEntity.success(serviceService.uninstall(clusterId, serviceId)); + } + + +} diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/ServiceService.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/ServiceService.java index 6f088f54..f933feb7 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/ServiceService.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/ServiceService.java @@ -37,4 +37,11 @@ public interface ServiceService { * @return service */ ServiceVO get(Long id); + + /** + * @param serviceId is the service id + * @param clusterId is the cluster id + * @return ServiceVO + */ + List uninstall(Long clusterId, Long serviceId); } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ServiceServiceImpl.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ServiceServiceImpl.java index 33b03720..41475943 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ServiceServiceImpl.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ServiceServiceImpl.java @@ -18,25 +18,33 @@ */ package org.apache.bigtop.manager.server.service.impl; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; import org.apache.bigtop.manager.common.constants.ComponentCategories; +import org.apache.bigtop.manager.common.enums.Command; import org.apache.bigtop.manager.common.enums.MaintainState; import org.apache.bigtop.manager.dao.entity.HostComponent; import org.apache.bigtop.manager.dao.entity.Service; import org.apache.bigtop.manager.dao.repository.HostComponentRepository; import org.apache.bigtop.manager.dao.repository.ServiceRepository; +import org.apache.bigtop.manager.server.enums.CommandLevel; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.mapper.CommandMapper; import org.apache.bigtop.manager.server.model.mapper.ServiceMapper; +import org.apache.bigtop.manager.server.model.req.CommandReq; +import org.apache.bigtop.manager.server.model.req.command.ComponentCommandReq; +import org.apache.bigtop.manager.server.model.vo.HostComponentVO; import org.apache.bigtop.manager.server.model.vo.ServiceVO; +import org.apache.bigtop.manager.server.service.CommandService; +import org.apache.bigtop.manager.server.service.HostComponentService; import org.apache.bigtop.manager.server.service.ServiceService; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import jakarta.annotation.Resource; - -import lombok.extern.slf4j.Slf4j; - @Slf4j @org.springframework.stereotype.Service public class ServiceServiceImpl implements ServiceService { @@ -47,6 +55,12 @@ public class ServiceServiceImpl implements ServiceService { @Resource private HostComponentRepository hostComponentRepository; + @Resource + HostComponentService hostComponentService; + + @Resource + CommandService commandService; + @Override public List list(Long clusterId) { List res = new ArrayList<>(); @@ -89,4 +103,35 @@ public class ServiceServiceImpl implements ServiceService { Service service = serviceRepository.findById(id).orElse(new Service()); return ServiceMapper.INSTANCE.fromEntity2VO(service); } + + @Override + public List uninstall(Long clusterId, Long serviceId) { + ServiceVO serviceVO = get(serviceId); + List requiredServices = serviceVO.getRequiredServices(); + List serviceVOS = new ArrayList<>(); + serviceVOS.add(serviceVO); + if (null != requiredServices && !requiredServices.isEmpty()) { + List services = serviceRepository.findByClusterIdAndServiceNameIn(clusterId, requiredServices); + List requiredServicesVO = ServiceMapper.INSTANCE.fromEntity2VO(services); + serviceVOS.addAll(requiredServicesVO); + } + CommandReq commandReq = new CommandReq(); + commandReq.setClusterId(clusterId); + commandReq.setCommand(Command.UNINSTALL); + commandReq.setCommandLevel(CommandLevel.COMPONENT); + List commandReqList = new ArrayList<>(); + serviceVOS.forEach(serviceVO1 -> { + List hostComponentVO = hostComponentService.listByService(clusterId, serviceVO1.getId()); + hostComponentVO.forEach(hostComponentVO1 -> { + ComponentCommandReq componentCommandReq = new ComponentCommandReq(); + componentCommandReq.setComponentName(hostComponentVO1.getComponentName()); + componentCommandReq.setHostnames(Collections.singletonList(hostComponentVO1.getHostname())); + commandReqList.add(componentCommandReq); + }); + }); + commandReq.setComponentCommands(commandReqList); + CommandDTO commandDTO = CommandMapper.INSTANCE.fromReq2DTO(commandReq); + commandService.command(commandDTO); + return serviceVOS; + } } -- Gitee