代码优化

pull/214/head
dqjdda 2019-11-24 20:48:33 +08:00
parent 88477d18b2
commit 468a092d21
16 changed files with 55 additions and 395 deletions

View File

@ -1,46 +0,0 @@
package me.zhengjie.modules.mnt.domain;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import javax.persistence.*;
import java.io.Serializable;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Entity
@Data
@Table(name="mnt_server_account")
public class ServerAccount implements Serializable {
/**
*
*/
@Id
@Column(name = "id")
private String id;
/**
*
*/
@Column(name = "name")
private String name;
/**
*
*/
@Column(name = "account")
private String account;
/**
*
*/
@Column(name = "password")
private String password;
public void copy(ServerAccount source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}

View File

@ -3,8 +3,11 @@ package me.zhengjie.modules.mnt.domain;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author zhanghouying
@ -19,14 +22,21 @@ public class ServerDeploy implements Serializable {
* IP
*/
@Id
@Column(name = "id")
private String id;
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
*
*/
@Column(name = "account_id")
private String accountId;
private String name;
private String ip;
private Integer port;
private String account;
private String password;
@CreationTimestamp
private Timestamp createTime;
public void copy(ServerDeploy source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));

View File

@ -1,12 +0,0 @@
package me.zhengjie.modules.mnt.repository;
import me.zhengjie.modules.mnt.domain.ServerAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author zhanghouying
* @date 2019-08-24
*/
public interface ServerAccountRepository extends JpaRepository<ServerAccount, String>, JpaSpecificationExecutor {
}

View File

@ -10,9 +10,11 @@ import org.springframework.data.jpa.repository.Query;
* @author zhanghouying
* @date 2019-08-24
*/
public interface ServerDeployRepository extends JpaRepository<ServerDeploy, String>, JpaSpecificationExecutor {
public interface ServerDeployRepository extends JpaRepository<ServerDeploy, Long>, JpaSpecificationExecutor<ServerDeploy> {
@Modifying
@Query(value = "update mnt_server set account_id = null where account_id = ?1", nativeQuery = true)
void changeByAccount(String id);
ServerDeploy findByIp(String ip);
}

View File

@ -1,61 +0,0 @@
package me.zhengjie.modules.mnt.rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.mnt.domain.ServerAccount;
import me.zhengjie.modules.mnt.service.dto.ServerAccountQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Api(tags = "服务器账号管理")
@RestController
@RequestMapping("/api/serverAccount")
public class ServerAccountController {
@Autowired
private me.zhengjie.modules.mnt.service.ServerAccountService ServerAccountService;
@Log("查询ServerAccount")
@ApiOperation(value = "查询ServerAccount")
@GetMapping
@PreAuthorize("@el.check('serverAccount:list')")
public ResponseEntity getServerAccounts(ServerAccountQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(ServerAccountService.queryAll(criteria,pageable),HttpStatus.OK);
}
@Log("新增ServerAccount")
@ApiOperation(value = "新增ServerAccount")
@PostMapping
@PreAuthorize("@el.check('serverAccount:add')")
public ResponseEntity create(@Validated @RequestBody ServerAccount resources){
return new ResponseEntity(ServerAccountService.create(resources),HttpStatus.CREATED);
}
@Log("修改ServerAccount")
@ApiOperation(value = "修改ServerAccount")
@PutMapping
@PreAuthorize("@el.check('serverAccount:edit')")
public ResponseEntity update(@Validated @RequestBody ServerAccount resources){
ServerAccountService.update(resources);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@Log("删除ServerAccount")
@ApiOperation(value = "删除ServerAccount")
@DeleteMapping(value = "/{id}")
@PreAuthorize("@el.check('serverAccount:del')")
public ResponseEntity delete(@PathVariable String id){
ServerAccountService.delete(id);
return new ResponseEntity(HttpStatus.OK);
}
}

View File

@ -55,7 +55,7 @@ public class ServerDeployController {
@ApiOperation(value = "删除Server")
@DeleteMapping(value = "/{id:.+}")
@PreAuthorize("@el.check('serverDeploy:del')")
public ResponseEntity delete(@PathVariable String id){
public ResponseEntity delete(@PathVariable Long id){
serverDeployService.delete(id);
return new ResponseEntity(HttpStatus.OK);
}

View File

@ -1,54 +0,0 @@
package me.zhengjie.modules.mnt.service;
import me.zhengjie.modules.mnt.domain.ServerAccount;
import me.zhengjie.modules.mnt.service.dto.ServerAccountDTO;
import me.zhengjie.modules.mnt.service.dto.ServerAccountQueryCriteria;
import org.springframework.data.domain.Pageable;
/**
* @author zhanghouying
* @date 2019-08-24
*/
public interface ServerAccountService {
/**
* queryAll
* @param criteria
* @param pageable
* @return
*/
Object queryAll(ServerAccountQueryCriteria criteria, Pageable pageable);
/**
* queryAll
* @param criteria
* @return
*/
public Object queryAll(ServerAccountQueryCriteria criteria);
/**
* findById
* @param id
* @return
*/
ServerAccountDTO findById(String id);
/**
* create
* @param resources
* @return
*/
ServerAccountDTO create(ServerAccount resources);
/**
* update
* @param resources
*/
void update(ServerAccount resources);
/**
* delete
* @param id
*/
void delete(String id);
}

View File

@ -31,7 +31,7 @@ public interface ServerDeployService {
* @param id
* @return
*/
ServerDeployDTO findById(String id);
ServerDeployDTO findById(Long id);
/**
* create
@ -50,5 +50,7 @@ public interface ServerDeployService {
* delete
* @param id
*/
void delete(String id);
void delete(Long id);
ServerDeployDTO findByIp(String ip);
}

View File

@ -1,33 +0,0 @@
package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import java.io.Serializable;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Data
public class ServerAccountDTO implements Serializable {
/**
*
*/
private String id;
/**
*
*/
private String name;
/**
*
*/
private String account;
/**
*
*/
private String password;
}

View File

@ -1,25 +0,0 @@
package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import me.zhengjie.annotation.Query;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Data
public class ServerAccountQueryCriteria{
/**
*
*/
@Query(type = Query.Type.INNER_LIKE)
private String name;
/**
*
*/
@Query(type = Query.Type.INNER_LIKE)
private String account;
}

View File

@ -1,11 +1,8 @@
package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import me.zhengjie.modules.mnt.service.ServerAccountService;
import me.zhengjie.modules.mnt.service.ServerDeployService;
import me.zhengjie.utils.SpringContextHolder;
import java.io.Serializable;
import java.sql.Timestamp;
/**
@ -15,26 +12,17 @@ import java.io.Serializable;
@Data
public class ServerDeployDTO implements Serializable {
/**
* IP
*/
private String id;
private Long id;
/**
*
*/
private String accountId;
private String name;
/**
*
*/
private String accountName;
private String ip;
public String getAccountName() {
if(accountId != null){
ServerAccountService serverAccountService = SpringContextHolder.getBean(ServerAccountService.class);
return serverAccountService.findById(accountId).getName();
}
return accountName;
}
private Integer port;
private String account;
private String password;
private Timestamp createTime;
}

View File

@ -3,6 +3,9 @@ package me.zhengjie.modules.mnt.service.dto;
import lombok.Data;
import me.zhengjie.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
/**
* @author zhanghouying
* @date 2019-08-24
@ -13,7 +16,9 @@ public class ServerDeployQueryCriteria{
/**
*
*/
@Query(type = Query.Type.INNER_LIKE)
private String id;
@Query(blurry = "name,ip,account")
private String blurry;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
}

View File

@ -59,13 +59,9 @@ public class DeployServiceImpl implements DeployService {
@Autowired
private DeployHistoryService deployHistoryService;
@Autowired
private ServerAccountService serverAccountService;
@Autowired
private DatabaseService databaseService;
@Override
public Object queryAll(DeployQueryCriteria criteria, Pageable pageable) {
Page<Deploy> page = deployRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
@ -243,7 +239,6 @@ public class DeployServiceImpl implements DeployService {
}
}
@Override
public String serverStatus(Deploy resources) {
String ip = resources.getIp();
@ -264,7 +259,6 @@ public class DeployServiceImpl implements DeployService {
return "执行完毕";
}
private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDTO appDTO) {
StringBuffer sb = new StringBuffer();
sb.append("find ");
@ -301,11 +295,8 @@ public class DeployServiceImpl implements DeployService {
return "执行完毕";
}
;
/**
*
*
* @param resources
* @return
*/
@ -333,8 +324,6 @@ public class DeployServiceImpl implements DeployService {
return "执行完毕";
}
;
@Override
public String serverReduction(DeployHistory resources) {
String deployId = resources.getDeployId();
@ -385,34 +374,21 @@ public class DeployServiceImpl implements DeployService {
}
private ExecuteShellUtil getExecuteShellUtil(String ip) {
ServerDeployDTO serverDeployDTO = serverDeployService.findById(ip);
ServerDeployDTO serverDeployDTO = serverDeployService.findByIp(ip);
if (serverDeployDTO == null) {
sendMsg("IP对应服务器信息不存在" + ip, MsgType.ERROR);
throw new BadRequestException("IP对应服务器信息不存在" + ip);
}
String accountId = serverDeployDTO.getAccountId();
ServerAccountDTO serverAccountDTO = serverAccountService.findById(accountId);
if (serverAccountDTO == null) {
sendMsg("IP对账号信息不存在" + ip, MsgType.ERROR);
throw new BadRequestException("IP对账号信息不存在" + ip);
}
return new ExecuteShellUtil(ip, serverAccountDTO.getAccount(), serverAccountDTO.getPassword());
return new ExecuteShellUtil(ip, serverDeployDTO.getAccount(), serverDeployDTO.getPassword());
}
private ScpClientUtil getScpClientUtil(String ip) {
ServerDeployDTO serverDeployDTO = serverDeployService.findById(ip);
ServerDeployDTO serverDeployDTO = serverDeployService.findByIp(ip);
if (serverDeployDTO == null) {
sendMsg("IP对应服务器信息不存在" + ip, MsgType.ERROR);
throw new BadRequestException("IP对应服务器信息不存在" + ip);
}
String accountId = serverDeployDTO.getAccountId();
ServerAccountDTO serverAccountDTO = serverAccountService.findById(accountId);
if (serverAccountDTO == null) {
sendMsg("IP对账号信息不存在" + ip, MsgType.ERROR);
throw new BadRequestException("IP对账号信息不存在" + ip);
}
return ScpClientUtil.getInstance(ip, 22, serverAccountDTO.getAccount(), serverAccountDTO.getPassword());
return ScpClientUtil.getInstance(ip, serverDeployDTO.getPort(), serverDeployDTO.getAccount(), serverDeployDTO.getPassword());
}
public void sendResultMsg(boolean result, StringBuilder sb) {

View File

@ -1,82 +0,0 @@
package me.zhengjie.modules.mnt.service.impl;
import cn.hutool.core.util.IdUtil;
import me.zhengjie.modules.mnt.domain.ServerAccount;
import me.zhengjie.modules.mnt.repository.ServerAccountRepository;
import me.zhengjie.modules.mnt.repository.ServerDeployRepository;
import me.zhengjie.modules.mnt.service.ServerAccountService;
import me.zhengjie.modules.mnt.service.dto.ServerAccountDTO;
import me.zhengjie.modules.mnt.service.dto.ServerAccountQueryCriteria;
import me.zhengjie.modules.mnt.service.mapper.ServerAccountMapper;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.hibernate.mapping.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ServerAccountServiceImpl implements ServerAccountService {
@Autowired
private ServerAccountRepository serverAccountRepository;
@Autowired
private ServerDeployRepository serverDeployRepository;
@Autowired
private ServerAccountMapper serverAccountMapper;
@Override
public Object queryAll(ServerAccountQueryCriteria criteria, Pageable pageable){
Page<ServerAccount> page = serverAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
return PageUtil.toPage(page.map(serverAccountMapper::toDto));
}
@Override
public Object queryAll(ServerAccountQueryCriteria criteria){
return serverAccountMapper.toDto(serverAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
}
@Override
public ServerAccountDTO findById(String id) {
Optional<ServerAccount> ServerAccount = serverAccountRepository.findById(id);
ValidationUtil.isNull(ServerAccount,"ServerAccount","id",id);
return serverAccountMapper.toDto(ServerAccount.get());
}
@Override
@Transactional(rollbackFor = Exception.class)
public ServerAccountDTO create(ServerAccount resources) {
resources.setId(IdUtil.getSnowflake(0, 0).toString());
return serverAccountMapper.toDto(serverAccountRepository.save(resources));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ServerAccount resources) {
Optional<ServerAccount> optionalServerAccount = serverAccountRepository.findById(resources.getId());
ValidationUtil.isNull( optionalServerAccount,"ServerAccount","id",resources.getId());
ServerAccount ServerAccount = optionalServerAccount.get();
ServerAccount.copy(resources);
serverAccountRepository.save(ServerAccount);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(String id) {
serverAccountRepository.deleteById(id);
serverDeployRepository.changeByAccount(id);
}
}

View File

@ -46,12 +46,18 @@ public class ServerDeployServiceImpl implements ServerDeployService {
}
@Override
public ServerDeployDTO findById(String id) {
public ServerDeployDTO findById(Long id) {
Optional<ServerDeploy> server = serverDeployRepository.findById(id);
ValidationUtil.isNull(server,"ServerDeploy","id",id);
return serverDeployMapper.toDto(server.get());
}
@Override
public ServerDeployDTO findByIp(String ip) {
ServerDeploy deploy = serverDeployRepository.findByIp(ip);
return serverDeployMapper.toDto(deploy);
}
@Override
@Transactional(rollbackFor = Exception.class)
public ServerDeployDTO create(ServerDeploy resources) {
@ -70,7 +76,7 @@ public class ServerDeployServiceImpl implements ServerDeployService {
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(String id) {
public void delete(Long id) {
serverDeployRepository.deleteById(id);
}
}

View File

@ -1,16 +0,0 @@
package me.zhengjie.modules.mnt.service.mapper;
import me.zhengjie.base.BaseMapper;
import me.zhengjie.modules.mnt.domain.ServerAccount;
import me.zhengjie.modules.mnt.service.dto.ServerAccountDTO;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper(componentModel = "spring",uses = {},unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ServerAccountMapper extends BaseMapper<ServerAccountDTO, ServerAccount> {
}