рефактор
parent
a03a6abdfc
commit
93e04a7e56
|
@ -3,10 +3,12 @@ package ru.ldeloff.servermonitorbot;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import ru.ldeloff.servermonitorbot.config.SshConfig;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import ru.ldeloff.servermonitorbot.config.SshServerList;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(SshConfig.class)
|
||||
@EnableScheduling
|
||||
@EnableConfigurationProperties(SshServerList.class)
|
||||
public class ServerMonitorBotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
package ru.ldeloff.servermonitorbot.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -13,6 +12,8 @@ import java.util.List;
|
|||
@ConfigurationProperties(prefix = "ssh")
|
||||
@Getter
|
||||
@Setter
|
||||
public class SshConfig {
|
||||
public class SshServerList {
|
||||
|
||||
private List<SshServer> servers;
|
||||
|
||||
}
|
|
@ -2,16 +2,37 @@ package ru.ldeloff.servermonitorbot.config;
|
|||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.RoleService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "bot")
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserConfig {
|
||||
private List<InitUserDto> users;
|
||||
|
||||
private List<UserProperty> users;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class UserProperty {
|
||||
private Long telegramId;
|
||||
private String role;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users.stream().map(user -> new User()
|
||||
.setTelegramId(user.getTelegramId())
|
||||
.setRole(roleService.findRoleByName(user.getRole()))
|
||||
).toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
public class AddCommands {
|
||||
public static void fillCommandTable() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,34 +1,24 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.config.UserConfig;
|
||||
import ru.ldeloff.servermonitorbot.mapper.UserMapper;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
|
||||
import java.util.List;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.UserService;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AddUsers implements ApplicationRunner {
|
||||
final List<InitUserDto> initUsers;
|
||||
final UserService userService;
|
||||
final UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
public AddUsers(UserConfig userConfig, UserService userService, UserMapper userMapper) {
|
||||
this.initUsers = userConfig.getUsers();
|
||||
this.userService = userService;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
final UserConfig userConfig;
|
||||
final UserService userService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
initUsers.forEach(initUserDto -> {
|
||||
User user = userService.saveOrUpdateUser(userMapper.dtoToUser(initUserDto));
|
||||
});
|
||||
public void run(ApplicationArguments args) {
|
||||
userConfig.getUsers().forEach(userService::saveOrUpdateUser);
|
||||
log.info("Загружены и обновлены пользователи");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SshConnect implements ApplicationRunner {
|
||||
final SshRepository sshRepository;
|
||||
|
||||
public SshConnect(SshRepository sshRepository) {
|
||||
this.sshRepository = sshRepository;
|
||||
}
|
||||
final SshService sshService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
sshRepository.connectToAllServer();
|
||||
public void run(ApplicationArguments args) {
|
||||
sshService.updateConnection();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
package ru.ldeloff.servermonitorbot.integration.in;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -10,13 +10,11 @@ import org.telegram.telegrambots.meta.TelegramBotsApi;
|
|||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.TelegramBotKeyboard;
|
||||
|
||||
import static ru.ldeloff.servermonitorbot.init.AddCommands.fillCommandTable;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
|
@ -35,7 +33,6 @@ public class StartBot implements ApplicationRunner {
|
|||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
try {
|
||||
fillCommandTable();
|
||||
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
|
||||
botsApi.registerBot(telegramBot);
|
||||
userService.getAllUsers().forEach(this::SendInitMessage);
|
|
@ -1,24 +1,26 @@
|
|||
package ru.ldeloff.servermonitorbot.controller;
|
||||
package ru.ldeloff.servermonitorbot.integration.in;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu;
|
||||
import ru.ldeloff.servermonitorbot.integration.in.model.TelegramBot;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.mainmenu.service.SwitchToMainMenu;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotController extends TelegramLongPollingBot {
|
||||
|
||||
private final Map<String, CommandTemplate> commands;
|
||||
final TelegramBot telegramBot;
|
||||
final SshRepository sshRepository;
|
||||
final SwitchToMainMenu switchToMainMenu;
|
||||
private final TelegramBot telegramBot;
|
||||
private final SwitchToMainMenu switchToMainMenu;
|
||||
private final SshService sshService;
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(Update update) {
|
||||
boolean result = false;
|
||||
|
@ -37,17 +39,20 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
switchToMainMenu.execute(update, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotUsername() {
|
||||
return telegramBot.getBotUsername();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotToken() {
|
||||
return telegramBot.getBotToken();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosing() {
|
||||
super.onClosing();
|
||||
sshRepository.disconnectSessions();
|
||||
sshService.disconnectAllConnections();
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
package ru.ldeloff.servermonitorbot.integration.in.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
|
@ -0,0 +1,12 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.repository;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
|
||||
public interface SshRepository {
|
||||
|
||||
void connect(SshServer sshServer) throws JSchException;
|
||||
|
||||
void disconnect(SshServer sshServer);
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.repository;
|
||||
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class SshRepositoryImpl implements SshRepository {
|
||||
|
||||
@Value("${ssh.timeout:5000}")
|
||||
private int TIMEOUT;
|
||||
|
||||
@Override
|
||||
public void connect(SshServer sshServer) throws JSchException {
|
||||
Session session = new JSch().getSession(sshServer.getUser(),
|
||||
sshServer.getHost(),
|
||||
sshServer.getPort());
|
||||
session.setPassword(sshServer.getPassword());
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
session.setTimeout(TIMEOUT);
|
||||
|
||||
session.connect();
|
||||
|
||||
sshServer.setSession(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(SshServer sshServer) {
|
||||
sshServer.getSession().disconnect();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.service;
|
||||
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SshService {
|
||||
|
||||
void updateConnection();
|
||||
|
||||
void disconnectAllConnections();
|
||||
|
||||
SendMessage getStatusSessions(SendMessage update);
|
||||
|
||||
List<SshServer> getAllSshServers();
|
||||
|
||||
List<SshServer> getHealthSshServers();
|
||||
|
||||
SshCommand execute(SshCommand sshCommand);
|
||||
|
||||
List<SshCommand> execute(List<SshCommand> sshCommands);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.service.ssh;
|
||||
package ru.ldeloff.servermonitorbot.integration.out.service;
|
||||
|
||||
import com.jcraft.jsch.ChannelExec;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
|
@ -7,27 +7,83 @@ import lombok.RequiredArgsConstructor;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.config.SshServerList;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.repository.SshRepository;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SshServiceImpl implements SshService {
|
||||
final SshRepository sshRepository;
|
||||
|
||||
private final SshServerList sshServerList;
|
||||
private final SshRepository sshRepository;
|
||||
|
||||
@Override
|
||||
public void updateConnection() {
|
||||
sshServerList.getServers().forEach(sshServer -> {
|
||||
try {
|
||||
sshRepository.connect(sshServer);
|
||||
log.info("Успешно подключён к {}", sshServer.getHost());
|
||||
} catch (JSchException e) {
|
||||
log.warn("Не удалось соединиться с {}: {}", sshServer.getHost(), e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnectAllConnections() {
|
||||
sshServerList.getServers().forEach(sshRepository::disconnect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage getStatusSessions(SendMessage message) {
|
||||
message.setText(sshRepository.getStatusSessions());
|
||||
StringBuilder text = new StringBuilder("Статус соединения (может выполняться долго): \n");
|
||||
sshServerList.getServers().forEach(server -> {
|
||||
text.append(server.getName())
|
||||
.append(": ")
|
||||
.append(checkStatusServer(server))
|
||||
.append("\n");
|
||||
});
|
||||
message.setText(text.toString());
|
||||
return message;
|
||||
}
|
||||
|
||||
private String checkStatusServer(SshServer server) {
|
||||
if (Objects.isNull(server.getSession())) {
|
||||
return "нет соединения";
|
||||
}
|
||||
|
||||
if (server.getSession().isConnected()) {
|
||||
return "OK";
|
||||
} else {
|
||||
return "потеряно соединение";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean checkHealth(SshServer server) {
|
||||
if (Objects.isNull(server.getSession())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return server.getSession().isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshServer> getSshServers() {
|
||||
return sshRepository.getSshServers();
|
||||
public List<SshServer> getAllSshServers() {
|
||||
return sshServerList.getServers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshServer> getHealthSshServers() {
|
||||
return sshServerList.getServers().stream().filter(this::checkHealth).toList();
|
||||
}
|
||||
|
||||
@Override
|
|
@ -1,30 +1,29 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
package ru.ldeloff.servermonitorbot.internal.cpu.temp.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class CpuTempCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public CpuTempCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
public class CpuTemp extends CommandTemplate {
|
||||
|
||||
public CpuTemp(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
setName("CPU.temp");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
Optional<SshServer> server = sshService.getAllSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
|
@ -36,7 +35,7 @@ public class CpuTempCommand extends CommandTemplate {
|
|||
Double.parseDouble(result.getResponse().replaceAll("\n", ""))/1000))
|
||||
+ "°C";
|
||||
} else {
|
||||
log.error("Ошибка при выполнении команды 'CPUtemp'. Искомый сервер (" + serverName + ") не найден");
|
||||
log.error("Ошибка при выполнении команды 'CPUtemp'. Искомый сервер ({}) не найден", serverName);
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.UserEntity;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.RoleService;
|
||||
|
||||
@Component
|
||||
public class UserMapper {
|
||||
final RoleService roleService;
|
||||
|
||||
public UserMapper(RoleService roleService) {
|
||||
this.roleService = roleService;
|
||||
}
|
||||
|
||||
public UserEntity toEntity(User user) {
|
||||
return new UserEntity()
|
||||
.setTelegramId(user.getTelegramId())
|
||||
.setRole(user.getRole());
|
||||
}
|
||||
|
||||
public User toModel(UserEntity userEntity) {
|
||||
return new User()
|
||||
.setTelegramId(userEntity.getTelegramId())
|
||||
.setRole(userEntity.getRole());
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import com.jcraft.jsch.Session;
|
||||
import jakarta.validation.constraints.NotNull;
|
|
@ -0,0 +1,12 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class User {
|
||||
private Long telegramId;
|
||||
private Role role;
|
||||
private String login;
|
||||
}
|
|
@ -1,12 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Accessors(chain = true)
|
||||
public class UserEntity {
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
@ -18,7 +20,4 @@ public class User {
|
|||
@ManyToOne
|
||||
@JoinColumn(name = "role_id")
|
||||
private Role role;
|
||||
|
||||
@Transient
|
||||
private String login;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
package ru.ldeloff.servermonitorbot.repository;
|
||||
package ru.ldeloff.servermonitorbot.internal.data.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
|
||||
import java.util.Optional;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.repository;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.UserEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||
|
||||
UserEntity getByTelegramId(long id);
|
||||
|
||||
@NotNull List<UserEntity> findAll();
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.service;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
|
||||
public interface RoleService {
|
||||
Role findRoleByName(String name);
|
||||
|
||||
Role findRoleById(Long id);
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package ru.ldeloff.servermonitorbot.service.role;
|
||||
package ru.ldeloff.servermonitorbot.internal.data.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.repository.RoleRepository;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.repository.RoleRepository;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
@ -11,6 +11,7 @@ import java.util.Optional;
|
|||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RoleServiceImpl implements RoleService {
|
||||
|
||||
private final RoleRepository roleRepository;
|
||||
|
||||
@Override
|
|
@ -0,0 +1,19 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.service;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.UserEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void saveOrUpdateUser(User user);
|
||||
|
||||
UserEntity getEntityByTelegramId(Long id);
|
||||
|
||||
User getByTelegramId(Long id);
|
||||
|
||||
List<UserEntity> getAllEntity();
|
||||
|
||||
List<User> getAllUsers();
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.mapper.UserMapper;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.UserEntity;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.repository.UserRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
final UserRepository userRepository;
|
||||
final UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public void saveOrUpdateUser(User user) {
|
||||
UserEntity userEntity = getEntityByTelegramId(user.getTelegramId());
|
||||
if (userEntity != null) {
|
||||
userEntity.setRole(user.getRole());
|
||||
} else {
|
||||
userEntity = userMapper.toEntity(user);
|
||||
}
|
||||
userRepository.save(userEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserEntity getEntityByTelegramId(Long id) {
|
||||
return userRepository.getByTelegramId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByTelegramId(Long id) {
|
||||
return userMapper.toModel(userRepository.getByTelegramId(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserEntity> getAllEntity() {return userRepository.findAll(); }
|
||||
|
||||
@Override
|
||||
public List<User> getAllUsers() {return userRepository.findAll().stream().map(userMapper::toModel).toList(); }
|
||||
}
|
|
@ -1,28 +1,28 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
package ru.ldeloff.servermonitorbot.internal.hdd.mdadm.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class MdadmStatusCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public MdadmStatusCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
public class MdadmStatus extends CommandTemplate {
|
||||
|
||||
public MdadmStatus(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
setName("MDADM");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
Optional<SshServer> server = sshService.getAllSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
|
@ -40,10 +40,11 @@ public class MdadmStatusCommand extends CommandTemplate {
|
|||
});
|
||||
return server.get().getName() + ": \n" + result;
|
||||
} else {
|
||||
log.error("Ошибка при выполнении команды 'mdadm'. Искомый сервер (" + serverName + ") не найден");
|
||||
log.error("Ошибка при выполнении команды 'mdadm'. Искомый сервер ({}) не найден", serverName);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> parseRaidNames(SshCommand result) {
|
||||
List<String> names = new ArrayList<>();
|
||||
Arrays.stream(result.getResponse().split("\n"))
|
||||
|
@ -53,6 +54,7 @@ public class MdadmStatusCommand extends CommandTemplate {
|
|||
.forEach(names::add);
|
||||
return names;
|
||||
}
|
||||
|
||||
private List<String> parseHddState(SshCommand result) {
|
||||
List<String> names = new ArrayList<>();
|
||||
names.add("Number Name State");
|
|
@ -1,29 +1,28 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
package ru.ldeloff.servermonitorbot.internal.hdd.temp.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class HddTempCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public HddTempCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
public class HddTemp extends CommandTemplate {
|
||||
public HddTemp(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
setName("HDD.temp");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
Optional<SshServer> server = sshService.getAllSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
|
@ -0,0 +1,34 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.mainmenu.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.TelegramBotKeyboard;
|
||||
|
||||
@Service
|
||||
public class FirstUseCommand extends CommandTemplate {
|
||||
|
||||
private final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public FirstUseCommand(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("/start");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
message.setText("Добро пожаловать " + user.getLogin() + "!");
|
||||
return telegramBotKeyboard.uiForm(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.mainmenu.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.TelegramBotKeyboard;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SwitchToMainMenu extends CommandTemplate {
|
||||
|
||||
private final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public SwitchToMainMenu(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("Not found");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText("Неверная команда. Попробуем сначала.");
|
||||
return telegramBotKeyboard.uiForm(answer);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.statuschecker.scheduler;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CheckSessionStatusScheduler {
|
||||
|
||||
private final SshService sshService;
|
||||
|
||||
@Scheduled(fixedRateString = "${ssh.checkConnectionDelay}")
|
||||
public void updateSessionStatusCheck() {
|
||||
sshService.updateConnection();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.statuschecker.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class CheckSessionStatus extends CommandTemplate {
|
||||
|
||||
public CheckSessionStatus(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
setName("Статус");
|
||||
}
|
||||
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
return super.sshService.getStatusSessions(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
package ru.ldeloff.servermonitorbot.internal.template;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
@ -7,14 +7,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.integration.in.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -23,10 +21,10 @@ import java.util.Objects;
|
|||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public abstract class CommandTemplate {
|
||||
private final UserService userService;
|
||||
private final RoleService roleService;
|
||||
|
||||
private final UpdateUtil updateUtil;
|
||||
private final ServerListButtons serverListButtons;
|
||||
private final SshService sshService;
|
||||
public final SshService sshService;
|
||||
@Getter
|
||||
@Setter
|
||||
private long expectedRole = 1L;
|
||||
|
@ -39,13 +37,13 @@ public abstract class CommandTemplate {
|
|||
return false;
|
||||
}
|
||||
|
||||
User user = getUser(update);
|
||||
String message = getMessage(update);
|
||||
User user = updateUtil.getUser(update);
|
||||
String message = updateUtil.getMessage(update);
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText(message);
|
||||
|
||||
if (!isAuth(update)) {
|
||||
if (!updateUtil.roleChecker(update, expectedRole)) {
|
||||
logNotAuth(user, message);
|
||||
sendMessage(actionForNotAuth(user, answer), bot);
|
||||
return true;
|
||||
|
@ -72,7 +70,7 @@ public abstract class CommandTemplate {
|
|||
if (update.getMessage().hasText()) {
|
||||
String messageText = update.getMessage().getText();
|
||||
if (Objects.equals(messageText, name)) {
|
||||
log.debug("Команда " + messageText + " будет выполнена");
|
||||
log.debug("Команда {} будет выполнена", messageText);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +78,7 @@ public abstract class CommandTemplate {
|
|||
String [] tags = update.getCallbackQuery().getData().split(":");
|
||||
if (tags.length > 1) {
|
||||
if (Objects.equals(tags[0], name)) {
|
||||
log.debug("Команда " + tags[0] + " будет выполнена");
|
||||
log.debug("Команда {} будет выполнена", tags[0]);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
|
@ -106,19 +104,17 @@ public abstract class CommandTemplate {
|
|||
}
|
||||
return "error";
|
||||
}
|
||||
private boolean isAuth(Update update) {
|
||||
User user = getUser(update);
|
||||
return user.getRole().getId() <= expectedRole;
|
||||
}
|
||||
|
||||
|
||||
// Методы при верных условиях
|
||||
void logSuccess(User user, String message) {
|
||||
log.info("Получена команда '" + message + "' от " + user.getLogin()
|
||||
+ " (" + user.getTelegramId() + "). OK");
|
||||
log.info("Получена команда '{}' от {} ({}). OK", message, user.getLogin(), user.getTelegramId());
|
||||
}
|
||||
SendMessage executeAggregate(User user, SendMessage message) {
|
||||
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
return serverListButtons.uiForm(message);
|
||||
}
|
||||
|
||||
SendMessage executeCommand(User user, SendMessage message) {
|
||||
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||
.map(String::trim)
|
||||
|
@ -132,7 +128,7 @@ public abstract class CommandTemplate {
|
|||
return message;
|
||||
}
|
||||
private String executeCommandAllHost() {
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
List<SshServer> servers = sshService.getHealthSshServers();
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> response.append(executeCommandSpecificHost(server.getName())).append("\n"));
|
||||
return response.toString();
|
||||
|
@ -151,35 +147,6 @@ public abstract class CommandTemplate {
|
|||
}
|
||||
|
||||
// Общее
|
||||
// TODO: вынести в UpdateUtils
|
||||
User getUser(Update update) {
|
||||
long id = -1L;
|
||||
String login = null;
|
||||
if (update.hasMessage()) {
|
||||
id = update.getMessage().getChat().getId();
|
||||
login = update.getMessage().getChat().getUserName();
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
id = update.getCallbackQuery().getMessage().getChat().getId();
|
||||
login = update.getCallbackQuery().getMessage().getChat().getUserName();
|
||||
}
|
||||
User user = userService.getByTelegramId(id);
|
||||
if (user == null) {
|
||||
user = new User();
|
||||
user.setTelegramId(id);
|
||||
user.setRole(roleService.findRoleById(3L));
|
||||
}
|
||||
user.setLogin(login);
|
||||
return user;
|
||||
}
|
||||
String getMessage(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
return update.getMessage().getText();
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
return update.getCallbackQuery().getData();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// TODO: вынести
|
||||
public static void sendMessage(SendMessage message, TelegramBotController bot) {
|
||||
try {
|
|
@ -0,0 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.template.model;
|
||||
|
||||
public enum CommandType {
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.template.util;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.UserService;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UpdateUtil {
|
||||
|
||||
private final UserService userService;
|
||||
private final RoleService roleService;
|
||||
|
||||
public User getUser(Update update) {
|
||||
long id = -1L;
|
||||
String login = null;
|
||||
if (update.hasMessage()) {
|
||||
id = update.getMessage().getChat().getId();
|
||||
login = update.getMessage().getChat().getUserName();
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
id = update.getCallbackQuery().getMessage().getChat().getId();
|
||||
login = update.getCallbackQuery().getMessage().getChat().getUserName();
|
||||
}
|
||||
User user = userService.getByTelegramId(id);
|
||||
if (user == null) {
|
||||
user = new User();
|
||||
user.setTelegramId(id);
|
||||
user.setRole(roleService.findRoleById(3L));
|
||||
}
|
||||
user.setLogin(login);
|
||||
return user;
|
||||
}
|
||||
|
||||
public Role getRole(Update update) {
|
||||
return getUser(update).getRole();
|
||||
}
|
||||
|
||||
public String getMessage(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
return update.getMessage().getText();
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
return update.getCallbackQuery().getData();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public boolean roleChecker(Update update, long expectedRole) {
|
||||
User user = getUser(update);
|
||||
return user.getRole().getId() <= expectedRole;
|
||||
}
|
||||
}
|
|
@ -1,14 +1,11 @@
|
|||
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||
package ru.ldeloff.servermonitorbot.internal.ui;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.utils.RoleChecker;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.UiFormer;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -25,7 +22,7 @@ public class ServerListButtons implements UiFormer {
|
|||
List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
|
||||
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
|
||||
|
||||
sshService.getSshServers().forEach(sshServer -> {
|
||||
sshService.getAllSshServers().forEach(sshServer -> {
|
||||
if (Objects.nonNull(sshServer.getSession())) {
|
||||
if (sshServer.getSession().isConnected()) {
|
||||
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
|
@ -1,16 +1,18 @@
|
|||
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||
package ru.ldeloff.servermonitorbot.internal.ui;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
|
||||
import ru.ldeloff.servermonitorbot.model.CommandList;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.utils.CommandUtil;
|
||||
import ru.ldeloff.servermonitorbot.utils.RoleChecker;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.util.CommandUtil;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -20,23 +22,28 @@ import java.util.Map;
|
|||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotKeyboard implements UiFormer {
|
||||
private final RoleChecker roleChecker;
|
||||
|
||||
private final UserService userService;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public SendMessage uiForm(SendMessage message) {
|
||||
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
|
||||
replyKeyboardMarkup.setResizeKeyboard(true);
|
||||
replyKeyboardMarkup.setOneTimeKeyboard(false);
|
||||
|
||||
ArrayList<KeyboardRow> keyboardRows = formKeyboard(
|
||||
roleChecker.getRole(message).getId());
|
||||
String chatId = message.getChatId();
|
||||
User user = userService.getByTelegramId(Long.parseLong(chatId));
|
||||
Role role = user.getRole();
|
||||
|
||||
ArrayList<KeyboardRow> keyboardRows = formKeyboard(role.getId());
|
||||
replyKeyboardMarkup.setKeyboard(keyboardRows);
|
||||
message.setReplyMarkup(replyKeyboardMarkup);
|
||||
return message;
|
||||
}
|
||||
|
||||
private ArrayList<KeyboardRow> formKeyboard(long role) {
|
||||
HashMap<String, Long> commands = CommandUtil.getCommands();
|
||||
Map<String, CommandTemplate> commands = applicationContext.getBeansOfType(CommandTemplate.class);
|
||||
if (role < 3) {
|
||||
commands.remove("/start");
|
||||
commands.remove("Not found");
|
||||
|
@ -46,16 +53,16 @@ public class TelegramBotKeyboard implements UiFormer {
|
|||
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
|
||||
KeyboardRow keyboardRow = new KeyboardRow();
|
||||
|
||||
for (Map.Entry<String, Long> entry : commands.entrySet()) {
|
||||
if (role <= entry.getValue()) {
|
||||
keyboardRow.add(new KeyboardButton(entry.getKey()));
|
||||
for (Map.Entry<String, CommandTemplate> entry : commands.entrySet()) {
|
||||
if (role <= entry.getValue().getExpectedRole()) {
|
||||
keyboardRow.add(new KeyboardButton(entry.getValue().getName()));
|
||||
}
|
||||
if (keyboardRow.size()>=buttonOnLine) {
|
||||
keyboardRows.add(keyboardRow);
|
||||
keyboardRow = new KeyboardRow();
|
||||
}
|
||||
}
|
||||
if (keyboardRow.size() > 0) {
|
||||
if (!keyboardRow.isEmpty()) {
|
||||
keyboardRows.add(keyboardRow);
|
||||
}
|
||||
return keyboardRows;
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||
package ru.ldeloff.servermonitorbot.internal.ui;
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.ui.util;
|
||||
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandUtil {
|
||||
private final Map<String, CommandTemplate> commandsMap;
|
||||
|
||||
public HashMap<String, Long> getCommands() {
|
||||
HashMap<String, Long> commands = new HashMap<>();
|
||||
for (Map.Entry<String, CommandTemplate> entry : commandsMap.entrySet()) {
|
||||
commands.put(entry.getValue().getName(), entry.getValue().getExpectedRole());
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.uname.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.internal.template.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.internal.ui.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class Uname extends CommandTemplate {
|
||||
|
||||
public Uname(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
setName("uname");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getAllSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
if (server.isPresent()) {
|
||||
SshCommand result = sshService.execute(new SshCommand("uname -a", 100, server.get()));
|
||||
return server.get().getName() + ": " +
|
||||
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||
} else {
|
||||
log.error("Ошибка при выполнении команды 'uname'. Искомый сервер ({}) не найден", serverName);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
|
||||
@Component
|
||||
public class UserMapper {
|
||||
final RoleService roleService;
|
||||
|
||||
public UserMapper(RoleService roleService) {
|
||||
this.roleService = roleService;
|
||||
}
|
||||
|
||||
public User dtoToUser(InitUserDto initUserDto) {
|
||||
User user = new User();
|
||||
user.setTelegramId(initUserDto.getTelegramId());
|
||||
user.setRole(roleService.findRoleByName(initUserDto.getRole()));
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.utils.CommandUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CommandList {
|
||||
private final Map<String, CommandTemplate> services;
|
||||
@Getter
|
||||
private HashMap<String, Long> commands;
|
||||
@PostConstruct
|
||||
private void setCommands() {
|
||||
commands = new HashMap<>();
|
||||
for (Map.Entry<String, CommandTemplate> entry : services.entrySet()) {
|
||||
commands.put(entry.getValue().getName(), entry.getValue().getExpectedRole());
|
||||
}
|
||||
CommandUtil.setCommands(commands);
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
public class InitUserDto {
|
||||
@NotNull
|
||||
private long telegramId;
|
||||
@NotNull
|
||||
private String role;
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.repository;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SshRepository {
|
||||
void connectToAllServer();
|
||||
Session connectToServer(SshServer sshServer) throws JSchException;
|
||||
void disconnectSessions();
|
||||
String getStatusSessions();
|
||||
List<SshServer> getSshServers();
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.repository;
|
||||
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.config.SshConfig;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Repository
|
||||
public class SshRepositoryImpl implements SshRepository {
|
||||
|
||||
private final List<SshServer> sshServers;
|
||||
|
||||
@Value("${ssh.timeout:5000}")
|
||||
private int TIMEOUT;
|
||||
|
||||
@Autowired
|
||||
public SshRepositoryImpl(SshConfig sshConfig) {
|
||||
this.sshServers = sshConfig.getServers();
|
||||
}
|
||||
|
||||
private Set<Session> sessions = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public void connectToAllServer() {
|
||||
sshServers.forEach(sshServer -> {
|
||||
try {
|
||||
Session session = connectToServer(sshServer);
|
||||
sshServer.setSession(session);
|
||||
log.info("Успешно подключён к " + sshServer.getHost());
|
||||
} catch (JSchException e) {
|
||||
log.warn("Не удалось соединиться с " + sshServer.getHost() + ": " + e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Session connectToServer(SshServer sshServer) throws JSchException {
|
||||
Session session = new JSch().getSession(sshServer.getUser(),
|
||||
sshServer.getHost(),
|
||||
sshServer.getPort());
|
||||
session.setPassword(sshServer.getPassword());
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
session.setTimeout(TIMEOUT);
|
||||
session.connect();
|
||||
sessions.add(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnectSessions() {
|
||||
if (!sessions.isEmpty()) {
|
||||
sessions.forEach(Session::disconnect);
|
||||
sessions.forEach(sessions::remove);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusSessions() {
|
||||
StringBuilder text = new StringBuilder("Статус соединения (может выполняться долго): \n");
|
||||
sshServers.forEach(server -> {
|
||||
text.append(server.getName())
|
||||
.append(": ")
|
||||
.append(checkStatusServer(server))
|
||||
.append("\n");
|
||||
});
|
||||
return String.valueOf(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshServer> getSshServers() {
|
||||
return sshServers;
|
||||
}
|
||||
|
||||
private String checkStatusServer(SshServer server) {
|
||||
if (Objects.isNull(server.getSession())) {
|
||||
try {
|
||||
connectToServer(server);
|
||||
return "OK";
|
||||
} catch (JSchException e) {
|
||||
log.warn("Не удалось соединиться с " + server.getHost() + ": " + e.getMessage());
|
||||
return "нет соединения";
|
||||
}
|
||||
}
|
||||
|
||||
if (server.getSession().isConnected()) {
|
||||
return "OK";
|
||||
} else {
|
||||
try {
|
||||
connectToServer(server);
|
||||
return "OK";
|
||||
} catch (JSchException e) {
|
||||
log.warn("Не удалось соединиться с " + server.getHost() + ": " + e.getMessage());
|
||||
return "потеряно соединение";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.repository;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
User getByTelegramId(long id);
|
||||
@NotNull List<User> findAll();
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
|
||||
@Service
|
||||
public class FirstUseCommand extends CommandTemplate {
|
||||
final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public FirstUseCommand(UserService userService,
|
||||
RoleService roleService,
|
||||
SshService sshService,
|
||||
ServerListButtons serverListButtons,
|
||||
TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("/start");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
message.setText("Добро пожаловать " + user.getLogin() + "!");
|
||||
return telegramBotKeyboard.uiForm(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class GetStatusSessions extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public GetStatusSessions(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("Статус");
|
||||
}
|
||||
@Override
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
return sshService.getStatusSessions(message);
|
||||
}
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SwitchToMainMenu extends CommandTemplate {
|
||||
final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public SwitchToMainMenu(UserService userService,
|
||||
RoleService roleService,
|
||||
SshService sshService,
|
||||
ServerListButtons serverListButtons,
|
||||
TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("Not found");
|
||||
}
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
void logSuccess(User user, String message) {
|
||||
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||
+ " (" + user.getTelegramId() + "). OK");
|
||||
}
|
||||
@Override
|
||||
SendMessage executeAggregate(User user, SendMessage message) {
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText("Неверная команда. Попробуем сначала.");
|
||||
return telegramBotKeyboard.uiForm(answer);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UnameCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public UnameCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("uname");
|
||||
}
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
if (server.isPresent()) {
|
||||
SshCommand result = sshService.execute(new SshCommand("uname -a", 100, server.get()));
|
||||
return server.get().getName() + ": " +
|
||||
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||
} else {
|
||||
log.error("Ошибка при выполнении команды 'uname'. Искомый сервер (" + serverName + ") не найден");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.role;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
|
||||
public interface RoleService {
|
||||
Role findRoleByName(String name);
|
||||
|
||||
Role findRoleById(Long id);
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.ssh;
|
||||
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SshService {
|
||||
SendMessage getStatusSessions(SendMessage update);
|
||||
|
||||
List<SshServer> getSshServers();
|
||||
SshCommand execute(SshCommand sshCommand);
|
||||
List<SshCommand> execute(List<SshCommand> sshCommands);
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.user;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
User saveOrUpdateUser(User user);
|
||||
|
||||
User getByTelegramId(Long id);
|
||||
|
||||
List<User> getAllUsers();
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.user;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.repository.UserRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public User saveOrUpdateUser(User user) {
|
||||
User existUser = userRepository.getByTelegramId(user.getTelegramId());
|
||||
if (existUser != null) {
|
||||
user.setId(existUser.getId());
|
||||
}
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByTelegramId(Long id) {
|
||||
return userRepository.getByTelegramId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAllUsers() {return userRepository.findAll(); }
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.utils;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@UtilityClass
|
||||
public class CommandUtil {
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private HashMap<String, Long> commands;
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.utils;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
public class RoleChecker {
|
||||
final UserService userService;
|
||||
public Role getRole(SendMessage sendMessage) {
|
||||
String chatId = sendMessage.getChatId();
|
||||
User user = userService.getByTelegramId(Long.parseLong(chatId));
|
||||
return user.getRole();
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.utils;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SshServerUtils {
|
||||
public static List<SshServer> filterGoodServers(List<SshServer> sshServers) {
|
||||
List<SshServer> goodSshServers = sshServers.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(server -> Objects.nonNull(server.getSession()))
|
||||
.filter(server -> server.getSession().isConnected())
|
||||
.toList();
|
||||
return goodSshServers;
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ bot:
|
|||
- telegramId: 123456789
|
||||
role: admin
|
||||
ssh:
|
||||
checkConnectionDelay: 60000
|
||||
timeout: 5000
|
||||
servers:
|
||||
-
|
||||
|
|
Loading…
Reference in New Issue