убрал базу данных
parent
93e04a7e56
commit
d23910af9e
48
pom.xml
48
pom.xml
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<version>3.4.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>ru.ldeloff</groupId>
|
||||
|
@ -15,10 +15,6 @@
|
|||
<description>ServerMonitorBot</description>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<flyway.version>9.16.0</flyway.version>
|
||||
<db.url>${database.url}</db.url>
|
||||
<db.user>${database.username}</db.user>
|
||||
<db.password>${database.password}</db.password>
|
||||
<start-class>ru.ldeloff.servermonitorbot.ServerMonitorBotApplication</start-class>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
@ -26,7 +22,6 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
@ -45,26 +40,13 @@
|
|||
<dependency>
|
||||
<groupId>org.telegram</groupId>
|
||||
<artifactId>telegrambots</artifactId>
|
||||
<version>6.8.0</version>
|
||||
<version>6.9.7.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
<artifactId>jsch</artifactId>
|
||||
<version>0.1.55</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
<version>${flyway.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -80,32 +62,6 @@
|
|||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-maven-plugin</artifactId>
|
||||
<version>${flyway.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>migrate</id>
|
||||
<phase>deploy</phase>
|
||||
<goals>
|
||||
<goal>migrate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<url>${database.url}</url>
|
||||
<user>${database.username}</user>
|
||||
<password>${database.password}</password>
|
||||
<locations>
|
||||
<location>classpath:db/migration</location>
|
||||
</locations>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
|
|
|
@ -4,11 +4,11 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import ru.ldeloff.servermonitorbot.config.SshServerList;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServers;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@EnableConfigurationProperties(SshServerList.class)
|
||||
@EnableConfigurationProperties(SshServers.class)
|
||||
public class ServerMonitorBotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.template;
|
||||
package ru.ldeloff.servermonitorbot.commands;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
@ -6,13 +6,13 @@ import lombok.Setter;
|
|||
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.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 ru.ldeloff.servermonitorbot.model.user.Role;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.view.ServerListButtons;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -25,14 +25,15 @@ public abstract class CommandTemplate {
|
|||
private final UpdateUtil updateUtil;
|
||||
private final ServerListButtons serverListButtons;
|
||||
public final SshService sshService;
|
||||
private final MessagingService messagingService;
|
||||
@Getter
|
||||
@Setter
|
||||
private long expectedRole = 1L;
|
||||
private List<Role> expectedRole = List.of(Role.ADMIN);
|
||||
@Getter
|
||||
@Setter
|
||||
private String name;
|
||||
|
||||
public boolean execute(Update update, TelegramBotController bot) {
|
||||
public boolean execute(Update update) {
|
||||
if (!isExecute(update)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -45,7 +46,7 @@ public abstract class CommandTemplate {
|
|||
|
||||
if (!updateUtil.roleChecker(update, expectedRole)) {
|
||||
logNotAuth(user, message);
|
||||
sendMessage(actionForNotAuth(user, answer), bot);
|
||||
messagingService.send(actionForNotAuth(user, answer));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -53,11 +54,11 @@ public abstract class CommandTemplate {
|
|||
switch (type) {
|
||||
case "aggregate":
|
||||
logSuccess(user, message);
|
||||
sendMessage(executeAggregate(user, answer), bot);
|
||||
messagingService.send(executeAggregate(user, answer));
|
||||
return true;
|
||||
case "command":
|
||||
logSuccess(user, message);
|
||||
sendMessage(executeCommand(user, answer), bot);
|
||||
messagingService.send(executeCommand(user, answer));
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
|
@ -145,15 +146,4 @@ public abstract class CommandTemplate {
|
|||
+ ") недостаточно прав для выполнения этой команды");
|
||||
return message;
|
||||
}
|
||||
|
||||
// Общее
|
||||
// TODO: вынести
|
||||
public static void sendMessage(SendMessage message, TelegramBotController bot) {
|
||||
try {
|
||||
bot.execute(message);
|
||||
} catch (TelegramApiException e) {
|
||||
e.printStackTrace();
|
||||
log.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.cpu.temp.service;
|
||||
package ru.ldeloff.servermonitorbot.commands.cpu.temp.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 ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.view.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
@ -16,8 +17,8 @@ import java.util.Optional;
|
|||
@Slf4j
|
||||
public class CpuTemp extends CommandTemplate {
|
||||
|
||||
public CpuTemp(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
public CpuTemp(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
setName("CPU.temp");
|
||||
}
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.hdd.mdadm.service;
|
||||
package ru.ldeloff.servermonitorbot.commands.hdd.mdadm.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 ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.view.ServerListButtons;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -15,8 +16,8 @@ import java.util.*;
|
|||
@Slf4j
|
||||
public class MdadmStatus extends CommandTemplate {
|
||||
|
||||
public MdadmStatus(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
public MdadmStatus(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
setName("MDADM");
|
||||
}
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.hdd.temp.service;
|
||||
package ru.ldeloff.servermonitorbot.commands.hdd.temp.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 ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.view.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
@ -15,8 +16,8 @@ import java.util.Optional;
|
|||
@Service
|
||||
@Slf4j
|
||||
public class HddTemp extends CommandTemplate {
|
||||
public HddTemp(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
public HddTemp(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
setName("HDD.temp");
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.statuschecker.scheduler;
|
||||
package ru.ldeloff.servermonitorbot.commands.statuschecker.scheduler;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
|
@ -0,0 +1,28 @@
|
|||
package ru.ldeloff.servermonitorbot.commands.statuschecker.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.view.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class CheckSessionStatus extends CommandTemplate {
|
||||
|
||||
public CheckSessionStatus(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
setName("Статус");
|
||||
}
|
||||
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
return super.sshService.getStatusSessions(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.uname.service;
|
||||
package ru.ldeloff.servermonitorbot.commands.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 ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.view.ServerListButtons;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
@ -16,8 +17,8 @@ import java.util.Optional;
|
|||
@Slf4j
|
||||
public class Uname extends CommandTemplate {
|
||||
|
||||
public Uname(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
public Uname(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
setName("uname");
|
||||
}
|
||||
|
|
@ -5,8 +5,8 @@ import lombok.Setter;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.User;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.RoleService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.in;
|
||||
package ru.ldeloff.servermonitorbot.controller;
|
||||
|
||||
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.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 ru.ldeloff.servermonitorbot.model.TelegramBot;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.view.SwitchToMainMenu;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -17,15 +17,17 @@ import java.util.Map;
|
|||
public class TelegramBotController extends TelegramLongPollingBot {
|
||||
|
||||
private final Map<String, CommandTemplate> commands;
|
||||
|
||||
private final TelegramBot telegramBot;
|
||||
private final SwitchToMainMenu switchToMainMenu;
|
||||
private final SshService sshService;
|
||||
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(Update update) {
|
||||
boolean result = false;
|
||||
for (Map.Entry<String, CommandTemplate> entry : commands.entrySet()) {
|
||||
result = entry.getValue().execute(update, this);
|
||||
result = entry.getValue().execute(update);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
|
@ -36,7 +38,7 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
message.setText("Not found");
|
||||
}
|
||||
update.setMessage(message);
|
||||
switchToMainMenu.execute(update, this);
|
||||
switchToMainMenu.execute(update);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,13 +4,15 @@ import lombok.RequiredArgsConstructor;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.config.UserConfig;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.service.UserService;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Order(2)
|
||||
public class AddUsers implements ApplicationRunner {
|
||||
|
||||
final UserConfig userConfig;
|
||||
|
|
|
@ -3,12 +3,13 @@ package ru.ldeloff.servermonitorbot.init;
|
|||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Order(3)
|
||||
public class SshConnect implements ApplicationRunner {
|
||||
|
||||
final SshService sshService;
|
||||
|
|
|
@ -1,31 +1,35 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.in;
|
||||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
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.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 ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.view.TelegramBotKeyboard;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@NoArgsConstructor
|
||||
@Order(1)
|
||||
public class StartBot implements ApplicationRunner {
|
||||
private TelegramBotController telegramBot;
|
||||
private TelegramBotKeyboard telegramBotKeyboard;
|
||||
private UserService userService;
|
||||
private MessagingService messagingService;
|
||||
|
||||
@Autowired
|
||||
public StartBot(TelegramBotController telegramBot, TelegramBotKeyboard telegramBotKeyboard, UserService userService) {
|
||||
public StartBot(TelegramBotController telegramBot, TelegramBotKeyboard telegramBotKeyboard, UserService userService, MessagingService messagingService) {
|
||||
this.telegramBot = telegramBot;
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
this.userService = userService;
|
||||
|
@ -35,16 +39,16 @@ public class StartBot implements ApplicationRunner {
|
|||
try {
|
||||
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
|
||||
botsApi.registerBot(telegramBot);
|
||||
userService.getAllUsers().forEach(this::SendInitMessage);
|
||||
userService.getAllUsers().forEach(this::sendInitMessage);
|
||||
log.info("Бот запущен");
|
||||
} catch (TelegramApiException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
private void SendInitMessage(User user) {
|
||||
private void sendInitMessage(User user) {
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText("Бот перезапущен");
|
||||
CommandTemplate.sendMessage(telegramBotKeyboard.uiForm(answer), telegramBot);
|
||||
messagingService.send(telegramBotKeyboard.uiForm(answer));
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
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,17 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "roles")
|
||||
public class Role {
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "users")
|
||||
@Accessors(chain = true)
|
||||
public class UserEntity {
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "telegram_id")
|
||||
private Long telegramId;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "role_id")
|
||||
private Role role;
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface RoleRepository extends JpaRepository<Role, Long> {
|
||||
Optional<Role> findRoleByName(String name);
|
||||
Optional<Role> findRoleById(Long id);
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
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();
|
||||
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
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,29 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.repository.RoleRepository;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RoleServiceImpl implements RoleService {
|
||||
|
||||
private final RoleRepository roleRepository;
|
||||
|
||||
@Override
|
||||
public Role findRoleByName(String name) {
|
||||
Optional<Role> role = roleRepository.findRoleByName(name.toUpperCase(Locale.ROOT));
|
||||
//noinspection OptionalGetWithoutIsPresent
|
||||
return role.orElseGet(() -> roleRepository.findRoleByName("ANONYMOUS").get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role findRoleById(Long id) {
|
||||
Optional<Role> role = roleRepository.findRoleById(id);
|
||||
return role.orElseGet(() -> roleRepository.findRoleByName("ANONYMOUS").get());
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
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();
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
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,27 +0,0 @@
|
|||
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 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.template.model;
|
||||
|
||||
public enum CommandType {
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
package ru.ldeloff.servermonitorbot.model;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
|
||||
@Getter
|
||||
@Setter
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.in.model;
|
||||
package ru.ldeloff.servermonitorbot.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
|
@ -0,0 +1,7 @@
|
|||
package ru.ldeloff.servermonitorbot.model.server;
|
||||
|
||||
public enum ServerStatus {
|
||||
ONLINE,
|
||||
OFFLINE,
|
||||
CONNECTION_LOST
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
package ru.ldeloff.servermonitorbot.model.server;
|
||||
|
||||
import com.jcraft.jsch.Session;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
@ -21,4 +21,5 @@ public class SshServer {
|
|||
@NotNull
|
||||
private String password;
|
||||
private Session session;
|
||||
private ServerStatus status;
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
package ru.ldeloff.servermonitorbot.config;
|
||||
package ru.ldeloff.servermonitorbot.model.server;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -12,8 +11,6 @@ import java.util.List;
|
|||
@ConfigurationProperties(prefix = "ssh")
|
||||
@Getter
|
||||
@Setter
|
||||
public class SshServerList {
|
||||
|
||||
public class SshServers {
|
||||
private List<SshServer> servers;
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package ru.ldeloff.servermonitorbot.model.user;
|
||||
|
||||
public enum Role {
|
||||
ADMIN,
|
||||
USER,
|
||||
ANONYMOUS
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.data.model;
|
||||
package ru.ldeloff.servermonitorbot.model.user;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
|
@ -0,0 +1,13 @@
|
|||
package ru.ldeloff.servermonitorbot.model.user;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Component
|
||||
public class Users {
|
||||
private final List<User> users = new ArrayList<>();
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.repository;
|
||||
package ru.ldeloff.servermonitorbot.repository;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import ru.ldeloff.servermonitorbot.internal.data.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
|
||||
public interface SshRepository {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.repository;
|
||||
package ru.ldeloff.servermonitorbot.repository;
|
||||
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
|
@ -6,7 +6,7 @@ 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;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
|
@ -0,0 +1,9 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
|
||||
public interface MessagingService {
|
||||
void sendMessageToAdmins(String message);
|
||||
|
||||
void send(SendMessage message);
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MessagingServiceImpl implements MessagingService {
|
||||
|
||||
private final UserService userService;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void sendMessageToAdmins(String message) {
|
||||
userService.getAllAdmins().forEach(user -> {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
sendMessage.setChatId(user.getTelegramId());
|
||||
sendMessage.setText(message);
|
||||
send(sendMessage);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(SendMessage message) {
|
||||
try {
|
||||
applicationContext.getBean(TelegramBotController.class).execute(message);
|
||||
} catch (TelegramApiException e) {
|
||||
log.warn("Ошибка при отправке сообщения", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.model.user.Role;
|
||||
|
||||
public interface RoleService {
|
||||
Role findRoleByName(String name);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.user.Role;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RoleServiceImpl implements RoleService {
|
||||
@Override
|
||||
public Role findRoleByName(String name) {
|
||||
return Role.valueOf(name);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.service;
|
||||
package ru.ldeloff.servermonitorbot.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 ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.integration.out.service;
|
||||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import com.jcraft.jsch.ChannelExec;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
|
@ -7,10 +7,11 @@ 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.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 ru.ldeloff.servermonitorbot.model.server.ServerStatus;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServers;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.server.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
@ -22,16 +23,24 @@ import java.util.Objects;
|
|||
@RequiredArgsConstructor
|
||||
public class SshServiceImpl implements SshService {
|
||||
|
||||
private final SshServerList sshServerList;
|
||||
private final SshServers sshServers;
|
||||
private final SshRepository sshRepository;
|
||||
private final MessagingService messagingService;
|
||||
|
||||
@Override
|
||||
public void updateConnection() {
|
||||
sshServerList.getServers().forEach(sshServer -> {
|
||||
sshServers.getServers().forEach(sshServer -> {
|
||||
try {
|
||||
sshRepository.connect(sshServer);
|
||||
updateServerStatus(sshServer, ServerStatus.ONLINE);
|
||||
log.info("Успешно подключён к {}", sshServer.getHost());
|
||||
} catch (JSchException e) {
|
||||
if (sshServer.getStatus() == null) {
|
||||
updateServerStatus(sshServer, ServerStatus.OFFLINE);
|
||||
}
|
||||
if (sshServer.getStatus() == ServerStatus.ONLINE) {
|
||||
updateServerStatus(sshServer, ServerStatus.CONNECTION_LOST);
|
||||
}
|
||||
log.warn("Не удалось соединиться с {}: {}", sshServer.getHost(), e.getMessage());
|
||||
}
|
||||
});
|
||||
|
@ -39,18 +48,16 @@ public class SshServiceImpl implements SshService {
|
|||
|
||||
@Override
|
||||
public void disconnectAllConnections() {
|
||||
sshServerList.getServers().forEach(sshRepository::disconnect);
|
||||
sshServers.getServers().forEach(sshRepository::disconnect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage getStatusSessions(SendMessage message) {
|
||||
StringBuilder text = new StringBuilder("Статус соединения (может выполняться долго): \n");
|
||||
sshServerList.getServers().forEach(server -> {
|
||||
text.append(server.getName())
|
||||
.append(": ")
|
||||
.append(checkStatusServer(server))
|
||||
.append("\n");
|
||||
});
|
||||
sshServers.getServers().forEach(server -> text.append(server.getName())
|
||||
.append(": ")
|
||||
.append(checkStatusServer(server))
|
||||
.append("\n"));
|
||||
message.setText(text.toString());
|
||||
return message;
|
||||
}
|
||||
|
@ -78,12 +85,12 @@ public class SshServiceImpl implements SshService {
|
|||
|
||||
@Override
|
||||
public List<SshServer> getAllSshServers() {
|
||||
return sshServerList.getServers();
|
||||
return sshServers.getServers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshServer> getHealthSshServers() {
|
||||
return sshServerList.getServers().stream().filter(this::checkHealth).toList();
|
||||
return sshServers.getServers().stream().filter(this::checkHealth).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -127,4 +134,13 @@ public class SshServiceImpl implements SshService {
|
|||
}
|
||||
return sshCommands;
|
||||
}
|
||||
|
||||
public void updateServerStatus(SshServer server, ServerStatus newStatus) {
|
||||
ServerStatus oldStatus = server.getStatus();
|
||||
if (oldStatus != newStatus) {
|
||||
server.setStatus(newStatus);
|
||||
messagingService.sendMessageToAdmins("Server status changed for " + server.getName() +
|
||||
": " + oldStatus + " -> " + newStatus);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void saveOrUpdateUser(User user);
|
||||
|
||||
User getByTelegramId(Long id);
|
||||
|
||||
List<User> getAllUsers();
|
||||
|
||||
List<User> getAllAdmins();
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.model.user.Role;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.model.user.Users;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final Users users;
|
||||
|
||||
@Override
|
||||
public void saveOrUpdateUser(User user) {
|
||||
if (getByTelegramId(user.getTelegramId()) == null) {
|
||||
users.getUsers().add(user);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByTelegramId(Long telegramId) {
|
||||
return users.getUsers().stream()
|
||||
.filter(user -> user.getTelegramId().equals(telegramId))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAllUsers() {return users.getUsers(); }
|
||||
|
||||
@Override
|
||||
public List<User> getAllAdmins() {
|
||||
return users.getUsers().stream().filter(user -> user.getRole().equals(Role.ADMIN)).toList();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.template.util;
|
||||
package ru.ldeloff.servermonitorbot.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;
|
||||
import ru.ldeloff.servermonitorbot.model.user.Role;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UpdateUtil {
|
||||
|
||||
private final UserService userService;
|
||||
private final RoleService roleService;
|
||||
|
||||
public User getUser(Update update) {
|
||||
long id = -1L;
|
||||
|
@ -22,23 +22,19 @@ public class UpdateUtil {
|
|||
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();
|
||||
id = update.getCallbackQuery().getMessage().getChatId();
|
||||
login = update.getCallbackQuery().getFrom().getUserName();
|
||||
}
|
||||
User user = userService.getByTelegramId(id);
|
||||
if (user == null) {
|
||||
user = new User();
|
||||
user.setTelegramId(id);
|
||||
user.setRole(roleService.findRoleById(3L));
|
||||
user.setRole(Role.ANONYMOUS);
|
||||
}
|
||||
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();
|
||||
|
@ -48,8 +44,8 @@ public class UpdateUtil {
|
|||
return "";
|
||||
}
|
||||
|
||||
public boolean roleChecker(Update update, long expectedRole) {
|
||||
public boolean roleChecker(Update update, List<Role> expectedRoles) {
|
||||
User user = getUser(update);
|
||||
return user.getRole().getId() <= expectedRole;
|
||||
return expectedRoles.contains(user.getRole());
|
||||
}
|
||||
}
|
|
@ -1,21 +1,20 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.mainmenu.service;
|
||||
package ru.ldeloff.servermonitorbot.view;
|
||||
|
||||
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;
|
||||
import ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
|
||||
@Service
|
||||
public class FirstUseCommand extends CommandTemplate {
|
||||
|
||||
private final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public FirstUseCommand(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
public FirstUseCommand(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, TelegramBotKeyboard telegramBotKeyboard, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("/start");
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.ui;
|
||||
package ru.ldeloff.servermonitorbot.view;
|
||||
|
||||
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.integration.out.service.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,14 +1,13 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.mainmenu.service;
|
||||
package ru.ldeloff.servermonitorbot.view;
|
||||
|
||||
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;
|
||||
import ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.util.UpdateUtil;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
|
@ -16,8 +15,8 @@ public class SwitchToMainMenu extends CommandTemplate {
|
|||
|
||||
private final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public SwitchToMainMenu(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(updateUtil, serverListButtons, sshService);
|
||||
public SwitchToMainMenu(UpdateUtil updateUtil, ServerListButtons serverListButtons, SshService sshService, TelegramBotKeyboard telegramBotKeyboard, MessagingService messagingService) {
|
||||
super(updateUtil, serverListButtons, sshService, messagingService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("Not found");
|
||||
}
|
|
@ -1,24 +1,25 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.ui;
|
||||
package ru.ldeloff.servermonitorbot.view;
|
||||
|
||||
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.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 ru.ldeloff.servermonitorbot.model.user.Role;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.commands.CommandTemplate;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static ru.ldeloff.servermonitorbot.model.user.Role.ADMIN;
|
||||
import static ru.ldeloff.servermonitorbot.model.user.Role.USER;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotKeyboard implements UiFormer {
|
||||
|
@ -34,17 +35,17 @@ public class TelegramBotKeyboard implements UiFormer {
|
|||
|
||||
String chatId = message.getChatId();
|
||||
User user = userService.getByTelegramId(Long.parseLong(chatId));
|
||||
Role role = user.getRole();
|
||||
|
||||
ArrayList<KeyboardRow> keyboardRows = formKeyboard(role.getId());
|
||||
ArrayList<KeyboardRow> keyboardRows = formKeyboard(user);
|
||||
replyKeyboardMarkup.setKeyboard(keyboardRows);
|
||||
message.setReplyMarkup(replyKeyboardMarkup);
|
||||
return message;
|
||||
}
|
||||
|
||||
private ArrayList<KeyboardRow> formKeyboard(long role) {
|
||||
private ArrayList<KeyboardRow> formKeyboard(User user) {
|
||||
Role role = user.getRole();
|
||||
Map<String, CommandTemplate> commands = applicationContext.getBeansOfType(CommandTemplate.class);
|
||||
if (role < 3) {
|
||||
if (List.of(ADMIN, USER).contains(role)) {
|
||||
commands.remove("/start");
|
||||
commands.remove("Not found");
|
||||
}
|
||||
|
@ -54,7 +55,7 @@ public class TelegramBotKeyboard implements UiFormer {
|
|||
KeyboardRow keyboardRow = new KeyboardRow();
|
||||
|
||||
for (Map.Entry<String, CommandTemplate> entry : commands.entrySet()) {
|
||||
if (role <= entry.getValue().getExpectedRole()) {
|
||||
if (entry.getValue().getExpectedRole().contains(role)) {
|
||||
keyboardRow.add(new KeyboardButton(entry.getValue().getName()));
|
||||
}
|
||||
if (keyboardRow.size()>=buttonOnLine) {
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.internal.ui;
|
||||
package ru.ldeloff.servermonitorbot.view;
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
--changeset L_DelOff:create_table_users rollbackSplitStatements:true
|
||||
--comment: Создание таблицы пользователей
|
||||
CREATE TABLE users
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
login VARCHAR(128),
|
||||
id_role integer
|
||||
);
|
||||
|
||||
COMMENT ON TABLE users IS 'Пользователи';
|
||||
COMMENT ON COLUMN users.id IS 'Идентификатор пользователя';
|
||||
COMMENT ON COLUMN users.login IS 'Логин пользователя';
|
||||
COMMENT ON COLUMN users.id_role IS 'Ссылка на роль пользователя';
|
||||
|
||||
--rollback DROP TABLE users;
|
|
@ -1,13 +0,0 @@
|
|||
--changeset L_DelOff:create_table_roles rollbackSplitStatements:true
|
||||
--comment: Создание таблицы ролей
|
||||
CREATE TABLE roles
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
role VARCHAR(128)
|
||||
);
|
||||
|
||||
COMMENT ON TABLE roles IS 'Пользователи';
|
||||
COMMENT ON COLUMN roles.id IS 'Идентификатор пользователя';
|
||||
COMMENT ON COLUMN roles.role IS 'Логин пользователя';
|
||||
|
||||
--rollback DROP TABLE roles;
|
|
@ -1,2 +0,0 @@
|
|||
ALTER TABLE users
|
||||
ADD COLUMN role_id INT NOT NULL REFERENCES roles(id);
|
|
@ -1,2 +0,0 @@
|
|||
INSERT INTO roles (id, role) VALUES (1, 'ADMIN');
|
||||
INSERT INTO roles (id, role) VALUES (2, 'USER');
|
|
@ -1 +0,0 @@
|
|||
INSERT INTO roles (id, role) VALUES (3, 'ANONYMOUS');
|
|
@ -1,9 +0,0 @@
|
|||
ALTER TABLE users
|
||||
DROP COLUMN login;
|
||||
|
||||
ALTER TABLE users
|
||||
ADD COLUMN telegram_id INTEGER UNIQUE;
|
||||
COMMENT ON COLUMN users.telegram_id IS 'Telegram ID пользователя';
|
||||
|
||||
ALTER TABLE users
|
||||
DROP COLUMN id_role;
|
|
@ -1,6 +0,0 @@
|
|||
ALTER TABLE roles
|
||||
DROP COLUMN role;
|
||||
|
||||
ALTER TABLE roles
|
||||
ADD COLUMN name VARCHAR(128);
|
||||
COMMENT ON COLUMN roles.name IS 'Имя роли';
|
|
@ -1,4 +0,0 @@
|
|||
TRUNCATE TABLE roles CASCADE;
|
||||
INSERT INTO roles (id, name) VALUES (1, 'ADMIN');
|
||||
INSERT INTO roles (id, name) VALUES (2, 'USER');
|
||||
INSERT INTO roles (id, name) VALUES (3, 'ANONYMOUS');
|
Loading…
Reference in New Issue