Compare commits
111 Commits
feature/ad
...
b842114bb3
| Author | SHA1 | Date | |
|---|---|---|---|
| b842114bb3 | |||
| 187e20ad9d | |||
| 6545e01f9b | |||
| f2a74b9f84 | |||
| 15d6894013 | |||
| 5bd78701e9 | |||
| 74e5e2b017 | |||
| 30772ec123 | |||
| c5d7689b97 | |||
| 5b0decff19 | |||
| 2bca814b3f | |||
| 24839dc3fe | |||
| 2b8a5f326c | |||
| 180e1e6be4 | |||
| 1a1fca9325 | |||
| e416a84bf7 | |||
| dcd357c6e0 | |||
| 43958b0d7c | |||
| c8ff9725e5 | |||
| bac9f84092 | |||
| 38a989e223 | |||
| 2b782867c9 | |||
| 3a0866dfa4 | |||
| 386dceed36 | |||
| 5d4370d932 | |||
| 23effc5fe6 | |||
| e259bb0262 | |||
| 9849a46a40 | |||
| 757850aae6 | |||
| 431e777988 | |||
| 8b8833cd2b | |||
| 754ae67714 | |||
| 7d929254dd | |||
| d822059e4e | |||
| 0fecaa501b | |||
| 034387af16 | |||
| 07b27cb6de | |||
| 28ec8dcd64 | |||
| de303fcd8e | |||
| 35a8d6a8c7 | |||
| bf070c29a1 | |||
| 395da1739b | |||
| 34744f93ec | |||
| a20b0c4963 | |||
| 94485ab9d2 | |||
| 204dbc5f73 | |||
| 98bc9633be | |||
| 8bdc56bd8a | |||
| 051e201525 | |||
| b68520ffa4 | |||
| 74078b120c | |||
| 94285d88b7 | |||
| 008b173b70 | |||
| 0ba76e16b2 | |||
| bdb19c81ca | |||
| d56b5a5617 | |||
| 182f850a3b | |||
| 11c0b4f369 | |||
| a6151fb01e | |||
| 3c53ea17a5 | |||
| 3b505398e4 | |||
| ead94bea2f | |||
| ecfd0a9ae5 | |||
| 7a0c2fde66 | |||
| 56a74a8b49 | |||
| 0863650ddc | |||
| 02a4b5081f | |||
| 9ef9ad18cf | |||
| fd39b88296 | |||
| 74f02e7827 | |||
| dc07591612 | |||
| 0f9721f562 | |||
| 742d246cdb | |||
| 0ac12deed3 | |||
| 9a4652b8f2 | |||
| edd29ea029 | |||
| 7838e60a82 | |||
| 02a56ee434 | |||
| d60187cc07 | |||
| 6359c59af9 | |||
| c84e935f05 | |||
| f9ad7fddbd | |||
| f011205a22 | |||
| 5920067b02 | |||
| 9e6cba52f6 | |||
| 638dc42a46 | |||
| fd4248d722 | |||
| b71b4db66e | |||
| 9ee1a4de3c | |||
| c42781c5ee | |||
| daf9e9cb06 | |||
| d0be1f0a62 | |||
| e75c496ea8 | |||
| 32147b0e52 | |||
| 99d06bb3bc | |||
| ad7ee9a96c | |||
| e1ea86b36b | |||
| 148c511b9e | |||
| f08d88355a | |||
| 4974796f0a | |||
| 047502916a | |||
| c174f36848 | |||
| 619996b913 | |||
| 0cfdd37702 | |||
| e6fac12e15 | |||
| b6147e9e91 | |||
| fcd4929e0c | |||
| 7b18162d30 | |||
| 26d9f83917 | |||
| 6d56771999 | |||
| 91efa22e12 |
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Используем официальный образ OpenJDK 17 как базовый образ
|
||||||
|
FROM openjdk:17
|
||||||
|
|
||||||
|
# Установка рабочей директории внутри контейнера
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Копируем JAR-файл приложения из локальной директории внутрь контейнера
|
||||||
|
COPY ServerMonitorBot-0.0.1-SNAPSHOT.jar app.jar
|
||||||
|
|
||||||
|
# Команда для запуска Spring Boot приложения при старте контейнера
|
||||||
|
CMD ["java", "-jar", "app.jar"]
|
||||||
71
Jenkinsfile_CD
Normal file
71
Jenkinsfile_CD
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
pipeline {
|
||||||
|
agent {
|
||||||
|
docker {
|
||||||
|
image 'maven:3.9.5-eclipse-temurin-17'
|
||||||
|
args '-v /root/.m2:/root/.m2'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
environment {
|
||||||
|
SSH_HOST = credentials('ROCK_PI_5_SSH_HOST')
|
||||||
|
SSH_PORT = credentials('ROCK_PI_5_SSH_PORT')
|
||||||
|
SSH_USER = credentials('ROCK_PI_5_SSH_USER')
|
||||||
|
SSH_PASS = credentials('ROCK_PI_5_SSH_PASS')
|
||||||
|
PATH_TO_DEV_FOLDER = credentials('SERVERMONITORBOT_PATH_TO_DEV_FOLDER')
|
||||||
|
IMAGE_NAME = 'servermonitorbot'
|
||||||
|
IMAGE_VERSION = 'latest'
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Init container') {
|
||||||
|
steps {
|
||||||
|
sh 'apt-get update && apt-get install -y sshpass openssh-client'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Build') {
|
||||||
|
steps {
|
||||||
|
withCredentials([file(credentialsId: 'SERVERMONITORBOT_APPLICATION_YAML', variable: 'application_yaml')]) {
|
||||||
|
sh "cp -f \$application_yaml src/main/resources/application.yml"
|
||||||
|
}
|
||||||
|
sh 'mvn -B -DskipTests -X clean package spring-boot:repackage'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Stop container') {
|
||||||
|
steps {
|
||||||
|
sh """
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'cd ${PATH_TO_DEV_FOLDER} && docker compose down'
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Remove old image') {
|
||||||
|
steps {
|
||||||
|
sh """
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'docker image rm -f ${IMAGE_NAME}-app:${IMAGE_VERSION} || true'
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Deploy') {
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
sh """
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'mkdir -p ${PATH_TO_DEV_FOLDER}'
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'rm -f ${PATH_TO_DEV_FOLDER}/{*.*}'
|
||||||
|
sshpass -p ${SSH_PASS} scp -o StrictHostKeyChecking=no -P ${SSH_PORT} target/*.jar ${SSH_USER}@${SSH_HOST}:${PATH_TO_DEV_FOLDER}/
|
||||||
|
sshpass -p ${SSH_PASS} scp -o StrictHostKeyChecking=no -P ${SSH_PORT} Dockerfile ${SSH_USER}@${SSH_HOST}:${PATH_TO_DEV_FOLDER}/
|
||||||
|
sshpass -p ${SSH_PASS} scp -o StrictHostKeyChecking=no -P ${SSH_PORT} docker-compose.yml ${SSH_USER}@${SSH_HOST}:${PATH_TO_DEV_FOLDER}/
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'cd ${PATH_TO_DEV_FOLDER} && docker build --no-cache -t ${IMAGE_NAME}:${IMAGE_VERSION} .'
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'cd ${PATH_TO_DEV_FOLDER} && docker compose up -d'
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Remove useless images') {
|
||||||
|
steps {
|
||||||
|
sh """
|
||||||
|
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'docker image prune --all --force'
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
15
Jenkinsfile_CI
Normal file
15
Jenkinsfile_CI
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
pipeline {
|
||||||
|
agent {
|
||||||
|
docker {
|
||||||
|
image 'maven:3.9.5-eclipse-temurin-17'
|
||||||
|
args '-v /root/.m2:/root/.m2'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stages {
|
||||||
|
stage('Build') {
|
||||||
|
steps {
|
||||||
|
sh 'mvn -B -DskipTests -X clean package spring-boot:repackage'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
docker-compose.yml
Normal file
21
docker-compose.yml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
|
|
||||||
|
db:
|
||||||
|
image: postgres:latest
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: servermonitorbot
|
||||||
|
POSTGRES_PASSWORD: servermonitorbot
|
||||||
|
POSTGRES_DB: servermonitorbot
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
55
pom.xml
55
pom.xml
@@ -15,6 +15,11 @@
|
|||||||
<description>ServerMonitorBot</description>
|
<description>ServerMonitorBot</description>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<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>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -40,15 +45,26 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>telegrambots</artifactId>
|
<artifactId>telegrambots</artifactId>
|
||||||
<version>6.7.0</version>
|
<version>6.8.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.jcraft</groupId>
|
<groupId>com.jcraft</groupId>
|
||||||
<artifactId>jsch</artifactId>
|
<artifactId>jsch</artifactId>
|
||||||
<version>0.1.55</version>
|
<version>0.1.55</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -65,6 +81,39 @@
|
|||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</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>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package ru.ldeloff.servermonitorbot.config;
|
package ru.ldeloff.servermonitorbot.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
@@ -8,14 +11,8 @@ import java.util.List;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "ssh")
|
@ConfigurationProperties(prefix = "ssh")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
public class SshConfig {
|
public class SshConfig {
|
||||||
private List<SshServer> servers;
|
private List<SshServer> servers;
|
||||||
|
|
||||||
public List<SshServer> getServers() {
|
|
||||||
return servers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setServers(List<SshServer> servers) {
|
|
||||||
this.servers = servers;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "bot")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserConfig {
|
||||||
|
private List<InitUserDto> users;
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.controller;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||||
|
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.FirstUseCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusAggregateCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TelegramBotController extends TelegramLongPollingBot {
|
||||||
|
final TelegramBot telegramBot;
|
||||||
|
final SshRepository sshRepository;
|
||||||
|
|
||||||
|
final FirstUseCommand firstUseCommand;
|
||||||
|
final GetStatusSessions getStatusSessions;
|
||||||
|
final UnameAggregateCommand unameAggregateCommand;
|
||||||
|
final SwitchToMainMenu switchToMainMenu;
|
||||||
|
final UnameCommand unameCommand;
|
||||||
|
final CpuTempAggregateCommand cpuTempAggregateCommand;
|
||||||
|
final CpuTempCommand cpuTempCommand;
|
||||||
|
final HddTempAggregateCommand hddTempAggregateCommand;
|
||||||
|
final HddTempCommand hddTempCommand;
|
||||||
|
final MdadmStatusAggregateCommand mdadmStatusAggregateCommand;
|
||||||
|
final MdadmStatusCommand mdadmStatusCommand;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUpdateReceived(Update update) {
|
||||||
|
if (update.hasMessage()) {
|
||||||
|
if (update.getMessage().hasText()) {
|
||||||
|
String messageText = update.getMessage().getText();
|
||||||
|
switch (messageText) {
|
||||||
|
case "/start" -> {
|
||||||
|
firstUseCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "Статус" -> {
|
||||||
|
getStatusSessions.execute(update, this);
|
||||||
|
}
|
||||||
|
case "uname" -> {
|
||||||
|
unameAggregateCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "CPU.temp" -> {
|
||||||
|
cpuTempAggregateCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "HDD.temp" -> {
|
||||||
|
hddTempAggregateCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "MDADM" -> {
|
||||||
|
mdadmStatusAggregateCommand.execute(update,this);
|
||||||
|
}
|
||||||
|
default -> {
|
||||||
|
switchToMainMenu.execute(update, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (update.hasCallbackQuery()) {
|
||||||
|
String [] tags = update.getCallbackQuery().getData().split(":");
|
||||||
|
if (tags.length > 1) {
|
||||||
|
switch (tags[0]) {
|
||||||
|
case "uname" -> {
|
||||||
|
unameCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "CPU.temp" -> {
|
||||||
|
cpuTempCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "HDD.temp" -> {
|
||||||
|
hddTempCommand.execute(update, this);
|
||||||
|
}
|
||||||
|
case "MDADM" -> {
|
||||||
|
mdadmStatusCommand.execute(update,this);
|
||||||
|
}
|
||||||
|
default -> {
|
||||||
|
switchToMainMenu.execute(update, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/main/java/ru/ldeloff/servermonitorbot/init/AddUsers.java
Normal file
34
src/main/java/ru/ldeloff/servermonitorbot/init/AddUsers.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.init;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
initUsers.forEach(initUserDto -> {
|
||||||
|
User user = userService.saveOrUpdateUser(userMapper.dtoToUser(initUserDto));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,18 +3,18 @@ package ru.ldeloff.servermonitorbot.init;
|
|||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class SshConnect implements ApplicationRunner {
|
public class SshConnect implements ApplicationRunner {
|
||||||
final SshService sshService;
|
final SshRepository sshRepository;
|
||||||
|
|
||||||
public SshConnect(SshService sshService) {
|
public SshConnect(SshRepository sshRepository) {
|
||||||
this.sshService = sshService;
|
this.sshRepository = sshRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
sshService.ConnectToAllServer();
|
sshRepository.connectToAllServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,16 +9,16 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||||
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class StartBot implements ApplicationRunner {
|
public class StartBot implements ApplicationRunner {
|
||||||
private TelegramBot telegramBot;
|
private TelegramBotController telegramBot;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public StartBot(TelegramBot telegramBot) {
|
public StartBot(TelegramBotController telegramBot) {
|
||||||
this.telegramBot = telegramBot;
|
this.telegramBot = telegramBot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/ru/ldeloff/servermonitorbot/model/Command.java
Normal file
25
src/main/java/ru/ldeloff/servermonitorbot/model/Command.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.model;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Min;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class Command {
|
||||||
|
@NotNull
|
||||||
|
private String command;
|
||||||
|
@NotNull
|
||||||
|
@Min(100)
|
||||||
|
private int timeout;
|
||||||
|
@NotNull
|
||||||
|
private SshServer sshServer;
|
||||||
|
private String response;
|
||||||
|
|
||||||
|
public Command(String command, int timeout, SshServer sshServer) {
|
||||||
|
this.command = command;
|
||||||
|
this.timeout = timeout;
|
||||||
|
this.sshServer = sshServer;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/java/ru/ldeloff/servermonitorbot/model/Role.java
Normal file
17
src/main/java/ru/ldeloff/servermonitorbot/model/Role.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.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,5 +1,7 @@
|
|||||||
package ru.ldeloff.servermonitorbot.model;
|
package ru.ldeloff.servermonitorbot.model;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.Session;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -8,8 +10,15 @@ import org.springframework.stereotype.Component;
|
|||||||
@Getter
|
@Getter
|
||||||
@Component
|
@Component
|
||||||
public class SshServer {
|
public class SshServer {
|
||||||
|
@NotNull
|
||||||
|
private String name;
|
||||||
|
@NotNull
|
||||||
private String host;
|
private String host;
|
||||||
|
@NotNull
|
||||||
private int port;
|
private int port;
|
||||||
|
@NotNull
|
||||||
private String user;
|
private String user;
|
||||||
|
@NotNull
|
||||||
private String password;
|
private String password;
|
||||||
|
private Session session;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +1,17 @@
|
|||||||
package ru.ldeloff.servermonitorbot.model;
|
package ru.ldeloff.servermonitorbot.model;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
@Component
|
@Component
|
||||||
@NoArgsConstructor
|
public class TelegramBot{
|
||||||
@AllArgsConstructor
|
private final String botUsername;
|
||||||
public class TelegramBot extends TelegramLongPollingBot {
|
private final String botToken;
|
||||||
@Value("${bot.name}")
|
|
||||||
private String botUsername;
|
|
||||||
|
|
||||||
@Value("${bot.token}")
|
public TelegramBot(@Value("${bot.name}") String botUsername, @Value("${bot.token}") String botToken) {
|
||||||
private String botToken;
|
this.botUsername = botUsername;
|
||||||
|
this.botToken = botToken;
|
||||||
@Override
|
|
||||||
public void onUpdateReceived(Update update) {
|
|
||||||
// We check if the update has a message and the message has text
|
|
||||||
if (update.hasMessage() && update.getMessage().hasText()) {
|
|
||||||
SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields
|
|
||||||
message.setChatId(update.getMessage().getChatId().toString());
|
|
||||||
message.setText(update.getMessage().getText());
|
|
||||||
|
|
||||||
try {
|
|
||||||
execute(message); // Call method to send the message
|
|
||||||
} catch (TelegramApiException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getBotUsername() {
|
|
||||||
return botUsername;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getBotToken() {
|
|
||||||
return botToken;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/main/java/ru/ldeloff/servermonitorbot/model/User.java
Normal file
24
src/main/java/ru/ldeloff/servermonitorbot/model/User.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Table(name = "users")
|
||||||
|
public class User {
|
||||||
|
@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;
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
private String login;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Role;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface RoleRepository extends JpaRepository<Role, Long> {
|
||||||
|
Optional<Role> findRoleByName(String name);
|
||||||
|
Optional<Role> findRoleById(Long id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
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.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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Repository
|
||||||
|
public class SshRepositoryImpl implements SshRepository {
|
||||||
|
|
||||||
|
private final List<SshServer> sshServers;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public SshRepositoryImpl(SshConfig sshConfig) {
|
||||||
|
this.sshServers = sshConfig.getServers();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Session> sessions = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connectToAllServer() {
|
||||||
|
sshServers.forEach(sshServer -> {
|
||||||
|
try {
|
||||||
|
Session session = connectToServer(sshServer);
|
||||||
|
sshServer.setSession(session);
|
||||||
|
log.info("Успешно подключён к " + sshServer.getHost());
|
||||||
|
sessions.add(session);
|
||||||
|
} 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.connect();
|
||||||
|
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())) {
|
||||||
|
return "нет соединения";
|
||||||
|
} else {
|
||||||
|
return server.getSession().isConnected() ? "OK" : "отключён";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
|
User getByTelegramId(long id);
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package ru.ldeloff.servermonitorbot.service;
|
|
||||||
|
|
||||||
import com.jcraft.jsch.JSchException;
|
|
||||||
import com.jcraft.jsch.Session;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
|
||||||
|
|
||||||
|
|
||||||
public interface SshService {
|
|
||||||
void ConnectToAllServer();
|
|
||||||
Session ConnectToServer(SshServer sshServer) throws JSchException;
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
package ru.ldeloff.servermonitorbot.service;
|
|
||||||
|
|
||||||
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.stereotype.Service;
|
|
||||||
import ru.ldeloff.servermonitorbot.config.SshConfig;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
public class SshServiceImpl implements SshService{
|
|
||||||
|
|
||||||
private final List<SshServer> sshServers;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public SshServiceImpl(SshConfig sshConfig) {
|
|
||||||
this.sshServers = sshConfig.getServers();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Session> sessions = new ArrayList<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void ConnectToAllServer() {
|
|
||||||
sshServers.forEach(sshServer -> {
|
|
||||||
try {
|
|
||||||
Session session = ConnectToServer(sshServer);
|
|
||||||
log.info("Успешно подключён к " + sshServer.getHost());
|
|
||||||
sessions.add(session);
|
|
||||||
} 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.connect();
|
|
||||||
return session;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
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.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public abstract class CommandTemplate {
|
||||||
|
final UserService userService;
|
||||||
|
final RoleService roleService;
|
||||||
|
long expectedRole = 2L;
|
||||||
|
public abstract SendMessage actionForAuth(User user, SendMessage message);
|
||||||
|
|
||||||
|
public SendMessage actionForNotAuth(User user, SendMessage message) {
|
||||||
|
message.setText("У пользователя " + user.getLogin() + " (" + user.getTelegramId()
|
||||||
|
+ ") недостаточно прав для выполнения этой команды");
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
public void execute(Update update, TelegramBotController bot) {
|
||||||
|
User user = getUser(update);
|
||||||
|
String message = getMessage(update);
|
||||||
|
SendMessage answer = new SendMessage();
|
||||||
|
answer.setChatId(user.getTelegramId());
|
||||||
|
answer.setText(message);
|
||||||
|
if (user.getRole().getId() <= expectedRole) {
|
||||||
|
logSuccess(user, message);
|
||||||
|
sendMessage(actionForAuth(user, answer), bot);
|
||||||
|
} else {
|
||||||
|
logNotAuth(user, message);
|
||||||
|
sendMessage(actionForNotAuth(user, answer), bot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected void logSuccess(User user, String message) {
|
||||||
|
log.info("Получена команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). OK");
|
||||||
|
}
|
||||||
|
protected void logNotAuth(User user, String message) {
|
||||||
|
log.warn("Получена команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). Нет прав");
|
||||||
|
}
|
||||||
|
private 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getMessage(Update update) {
|
||||||
|
if (update.hasMessage()) {
|
||||||
|
return update.getMessage().getText();
|
||||||
|
} else if (update.hasCallbackQuery()) {
|
||||||
|
return update.getCallbackQuery().getData();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(SendMessage message, TelegramBotController bot) {
|
||||||
|
try {
|
||||||
|
bot.execute(message);
|
||||||
|
} catch (TelegramApiException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
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.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FirstUseCommand extends CommandTemplate {
|
||||||
|
final TelegramBotKeyboard telegramBotKeyboard;
|
||||||
|
|
||||||
|
public FirstUseCommand(UserService userService, RoleService roleService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
message.setText("Добро пожаловать " + user.getLogin() + "!");
|
||||||
|
return telegramBotKeyboard.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GetStatusSessions extends CommandTemplate {
|
||||||
|
final SshService sshService;
|
||||||
|
|
||||||
|
public GetStatusSessions(UserService userService, RoleService roleService, SshService sshService) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.sshService = sshService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return sshService.getStatusSessions(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
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 ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class SwitchToMainMenu extends CommandTemplate {
|
||||||
|
final TelegramBotKeyboard telegramBotKeyboard;
|
||||||
|
|
||||||
|
public SwitchToMainMenu(UserService userService, RoleService roleService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
message.setText("Неверная команда. Попробуем сначала.");
|
||||||
|
return telegramBotKeyboard.uiForm(message);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void logSuccess(User user, String message) {
|
||||||
|
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). OK");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void logNotAuth(User user, String message) {
|
||||||
|
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). Нет прав");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.cputemp;
|
||||||
|
|
||||||
|
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.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.uname.ServerListButtons;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CpuTempAggregateCommand extends CommandTemplate {
|
||||||
|
final ServerListButtons serverListButtons;
|
||||||
|
|
||||||
|
public CpuTempAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.serverListButtons = serverListButtons;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return serverListButtons.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.cputemp;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
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 java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
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) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.sshService = sshService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||||
|
.map(String::trim)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
if (tags[1].equals("all")) {
|
||||||
|
message.setText(hddTempAllHost());
|
||||||
|
} else {
|
||||||
|
message.setText(Objects.requireNonNull(hddTempSpecificHost(tags[1])));
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hddTempAllHost() {
|
||||||
|
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
servers.forEach(server -> response.append(hddTempSpecificHost(server.getName())).append("\n"));
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hddTempSpecificHost(String serverName) {
|
||||||
|
Optional<SshServer> server = sshService.getSshServers()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getName().equals(serverName))
|
||||||
|
.findFirst();
|
||||||
|
if (server.isPresent()) {
|
||||||
|
Command result = sshService.execute(new Command("cat /sys/class/thermal/thermal_zone0/temp", 100, server.get()));
|
||||||
|
return server.get().getName() + ": \n" +
|
||||||
|
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" :
|
||||||
|
String.format("%.1f",
|
||||||
|
Double.parseDouble(result.getResponse().replaceAll("\n", ""))/1000))
|
||||||
|
+ "°C";
|
||||||
|
} else {
|
||||||
|
log.error("Ошибка при выполнении команды 'CPUtemp'. Искомый сервер (" + serverName + ") не найден");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.hddtemp;
|
||||||
|
|
||||||
|
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.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.uname.ServerListButtons;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class HddTempAggregateCommand extends CommandTemplate {
|
||||||
|
final ServerListButtons serverListButtons;
|
||||||
|
|
||||||
|
public HddTempAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.serverListButtons = serverListButtons;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return serverListButtons.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.hddtemp;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
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 java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
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) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.sshService = sshService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||||
|
.map(String::trim)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
if (tags[1].equals("all")) {
|
||||||
|
message.setText(hddTempAllHost());
|
||||||
|
} else {
|
||||||
|
message.setText(Objects.requireNonNull(hddTempSpecificHost(tags[1])));
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hddTempAllHost() {
|
||||||
|
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
servers.forEach(server -> response.append(hddTempSpecificHost(server.getName())).append("\n"));
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hddTempSpecificHost(String serverName) {
|
||||||
|
Optional<SshServer> server = sshService.getSshServers()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getName().equals(serverName))
|
||||||
|
.findFirst();
|
||||||
|
if (server.isPresent()) {
|
||||||
|
Command result = sshService.execute(new Command("hddtemp /dev/sd*", 500, server.get()));
|
||||||
|
return server.get().getName() + ": \n" +
|
||||||
|
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||||
|
} else {
|
||||||
|
log.error("Ошибка при выполнении команды 'hddtemp'. Искомый сервер (" + serverName + ") не найден");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.mdadm;
|
||||||
|
|
||||||
|
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.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.uname.ServerListButtons;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MdadmStatusAggregateCommand extends CommandTemplate {
|
||||||
|
|
||||||
|
final ServerListButtons serverListButtons;
|
||||||
|
|
||||||
|
public MdadmStatusAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.serverListButtons = serverListButtons;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return serverListButtons.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.mdadm;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
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 java.util.*;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class MdadmStatusCommand extends CommandTemplate {
|
||||||
|
|
||||||
|
final SshService sshService;
|
||||||
|
|
||||||
|
public MdadmStatusCommand(UserService userService, RoleService roleService, SshService sshService) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.sshService = sshService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||||
|
.map(String::trim)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
if (tags[1].equals("all")) {
|
||||||
|
message.setText(mdadmStatusAllHost());
|
||||||
|
} else {
|
||||||
|
message.setText(Objects.requireNonNull(mdadmStatusSpecificHost(tags[1])));
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String mdadmStatusAllHost() {
|
||||||
|
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
servers.forEach(server -> response.append(mdadmStatusSpecificHost(server.getName())).append("\n"));
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String mdadmStatusSpecificHost(String serverName) {
|
||||||
|
Optional<SshServer> server = sshService.getSshServers()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getName().equals(serverName))
|
||||||
|
.findFirst();
|
||||||
|
if (server.isPresent()) {
|
||||||
|
// узнаю скок рейдов
|
||||||
|
Command commandRaid = sshService.execute(new Command("mdadm --detail --scan", 500, server.get()));
|
||||||
|
List<String> raidNames = parseRaidNames(commandRaid);
|
||||||
|
// Для каждого рейда надо выяснить статус
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
raidNames.forEach(raid -> {
|
||||||
|
result.append(raid).append(":\n");
|
||||||
|
Command commandHdd = sshService.execute(new Command("mdadm --detail " + raid, 500, server.get()));
|
||||||
|
List<String> hdds = parseHddState(commandHdd);
|
||||||
|
hdds.forEach(hdd -> result.append(hdd).append("\n"));
|
||||||
|
});
|
||||||
|
return server.get().getName() + ": \n" + result;
|
||||||
|
} else {
|
||||||
|
log.error("Ошибка при выполнении команды 'mdadm'. Искомый сервер (" + serverName + ") не найден");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private List<String> parseRaidNames(Command result) {
|
||||||
|
List<String> names = new ArrayList<>();
|
||||||
|
Arrays.stream(result.getResponse().split("\n"))
|
||||||
|
.map(line -> line.split(" "))
|
||||||
|
.filter(lineArray -> lineArray.length >= 2)
|
||||||
|
.map(lineArray -> lineArray[1])
|
||||||
|
.forEach(names::add);
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> parseHddState(Command result) {
|
||||||
|
List<String> names = new ArrayList<>();
|
||||||
|
|
||||||
|
String[] lines = result.getResponse().split("\n");
|
||||||
|
|
||||||
|
boolean foundHeader = false;
|
||||||
|
|
||||||
|
for (String line : lines) {
|
||||||
|
if (!foundHeader) {
|
||||||
|
if ( line.contains("Number") &&
|
||||||
|
line.contains("Major") &&
|
||||||
|
line.contains("Minor") &&
|
||||||
|
line.contains("RaidDevice") ) {
|
||||||
|
foundHeader = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
List<String> elements = Arrays.stream(line.split(" "))
|
||||||
|
.filter(x -> !Objects.equals(x, "")).toList();
|
||||||
|
names.add("Number Name State");
|
||||||
|
names.add(elements.get(0) + ", "
|
||||||
|
+ elements.get(6) + ", "
|
||||||
|
+ elements.get(4) + " "
|
||||||
|
+ elements.get(5));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.uname;
|
||||||
|
|
||||||
|
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.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.uname.ServerListButtons;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UnameAggregateCommand extends CommandTemplate {
|
||||||
|
final ServerListButtons serverListButtons;
|
||||||
|
|
||||||
|
public UnameAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.serverListButtons = serverListButtons;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return serverListButtons.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.command.uname;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
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 java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
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) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.sshService = sshService;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||||
|
.map(String::trim)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
if (tags[1].equals("all")) {
|
||||||
|
message.setText(unameAllHost());
|
||||||
|
} else {
|
||||||
|
message.setText(Objects.requireNonNull(unameSpecificHost(tags[1])));
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String unameAllHost() {
|
||||||
|
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
servers.forEach(server -> response.append(unameSpecificHost(server.getName())).append("\n"));
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String unameSpecificHost(String serverName) {
|
||||||
|
Optional<SshServer> server = sshService.getSshServers()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getName().equals(serverName))
|
||||||
|
.findFirst();
|
||||||
|
if (server.isPresent()) {
|
||||||
|
Command result = sshService.execute(new Command("uname -a", 100, server.get()));
|
||||||
|
return server.get().getName() + ": " +
|
||||||
|
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||||
|
} else {
|
||||||
|
log.error("Ошибка при выполнении команды 'uname'. Искомый сервер (" + serverName + ") не найден");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.role;
|
||||||
|
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Role;
|
||||||
|
|
||||||
|
public interface RoleService {
|
||||||
|
Role findRoleByName(String name);
|
||||||
|
|
||||||
|
Role findRoleById(Long id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.role;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Role;
|
||||||
|
import ru.ldeloff.servermonitorbot.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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.ssh;
|
||||||
|
|
||||||
|
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface SshService {
|
||||||
|
SendMessage getStatusSessions(SendMessage update);
|
||||||
|
|
||||||
|
List<SshServer> getSshServers();
|
||||||
|
Command execute(Command command);
|
||||||
|
List<Command> execute(List<Command> commands);
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.ssh;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.ChannelExec;
|
||||||
|
import com.jcraft.jsch.JSchException;
|
||||||
|
import com.jcraft.jsch.Session;
|
||||||
|
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.Command;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SshServiceImpl implements SshService {
|
||||||
|
final SshRepository sshRepository;
|
||||||
|
@Override
|
||||||
|
public SendMessage getStatusSessions(SendMessage message) {
|
||||||
|
message.setText(sshRepository.getStatusSessions());
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public List<SshServer> getSshServers() {
|
||||||
|
return sshRepository.getSshServers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Command execute(Command command) {
|
||||||
|
List<Command> commands = new ArrayList<>();
|
||||||
|
commands.add(command);
|
||||||
|
return execute(commands).get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Command> execute(List<Command> commands) {
|
||||||
|
Session session = commands.get(0).getSshServer().getSession(); // подразумевается, что в листе с командами сервер один и тот же
|
||||||
|
ChannelExec channel = null;
|
||||||
|
try {
|
||||||
|
channel = (ChannelExec) session.openChannel("exec");
|
||||||
|
ChannelExec finalChannel = channel;
|
||||||
|
|
||||||
|
commands.forEach(command -> {
|
||||||
|
try {
|
||||||
|
finalChannel.setCommand(command.getCommand());
|
||||||
|
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
|
||||||
|
finalChannel.setOutputStream(responseStream);
|
||||||
|
finalChannel.connect();
|
||||||
|
while (finalChannel.isConnected()) {
|
||||||
|
Thread.sleep(command.getTimeout());
|
||||||
|
}
|
||||||
|
String responseString = new String(responseStream.toByteArray());
|
||||||
|
command.setResponse(responseString);
|
||||||
|
} catch (JSchException | InterruptedException e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (JSchException e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
if (channel != null) {
|
||||||
|
channel.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.service.user;
|
||||||
|
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
User saveOrUpdateUser(User user);
|
||||||
|
|
||||||
|
User getByTelegramId(Long id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||||
|
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TelegramBotKeyboard implements UiFormer {
|
||||||
|
private final String STATUS = "Статус";
|
||||||
|
private final String UNAME = "uname";
|
||||||
|
private final String CPU_TEMP = "CPU.temp";
|
||||||
|
private final String HDD_TEMP = "HDD.temp";
|
||||||
|
private final String MDADM = "MDADM";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage uiForm(SendMessage message) {
|
||||||
|
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
|
||||||
|
replyKeyboardMarkup.setResizeKeyboard(true);
|
||||||
|
replyKeyboardMarkup.setOneTimeKeyboard(false);
|
||||||
|
|
||||||
|
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
|
||||||
|
|
||||||
|
KeyboardRow keyboardRow = new KeyboardRow();
|
||||||
|
keyboardRows.add(keyboardRow);
|
||||||
|
|
||||||
|
keyboardRow.add(new KeyboardButton(STATUS));
|
||||||
|
keyboardRow.add(new KeyboardButton(UNAME));
|
||||||
|
keyboardRow.add(new KeyboardButton(CPU_TEMP));
|
||||||
|
keyboardRow.add(new KeyboardButton(HDD_TEMP));
|
||||||
|
keyboardRow.add(new KeyboardButton(MDADM));
|
||||||
|
|
||||||
|
replyKeyboardMarkup.setKeyboard(keyboardRows);
|
||||||
|
message.setReplyMarkup(replyKeyboardMarkup);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||||
|
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
|
||||||
|
public interface UiFormer {
|
||||||
|
SendMessage uiForm(SendMessage message);
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package ru.ldeloff.servermonitorbot.utils.ui.uname;
|
||||||
|
|
||||||
|
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.service.ssh.SshService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.UiFormer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class ServerListButtons implements UiFormer {
|
||||||
|
|
||||||
|
private final SshService sshService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage uiForm(SendMessage message) {
|
||||||
|
List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
|
||||||
|
|
||||||
|
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
|
||||||
|
|
||||||
|
sshService.getSshServers().forEach(sshServer -> {
|
||||||
|
if (Objects.nonNull(sshServer.getSession())) {
|
||||||
|
if (sshServer.getSession().isConnected()) {
|
||||||
|
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
||||||
|
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||||
|
inlineKeyboardButton.setText(sshServer.getName());
|
||||||
|
inlineKeyboardButton.setCallbackData(message.getText() + ": " + sshServer.getName());
|
||||||
|
keyboardRow.add(inlineKeyboardButton);
|
||||||
|
keyboard.add(keyboardRow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (keyboard.size() > 1) {
|
||||||
|
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
||||||
|
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||||
|
inlineKeyboardButton.setText("Все сервера");
|
||||||
|
inlineKeyboardButton.setCallbackData(message.getText() + ": all");
|
||||||
|
keyboardRow.add(inlineKeyboardButton);
|
||||||
|
keyboard.add(keyboardRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
inlineKeyboardMarkup.setKeyboard(keyboard);
|
||||||
|
message.setReplyMarkup(inlineKeyboardMarkup);
|
||||||
|
message.setText("Выполнить " + message.getText() + " для");
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,35 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: ServerMonitorBot
|
||||||
|
datasource:
|
||||||
|
url: jdbc:postgresql://db:5432/servermonitorbot
|
||||||
|
username: servermonitorbot
|
||||||
|
password: servermonitorbot
|
||||||
|
jpa:
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: none
|
||||||
|
flyway:
|
||||||
|
locations: classpath:db/migration
|
||||||
bot:
|
bot:
|
||||||
name: "ServerMonitorBot"
|
name: "ServerMonitorBot"
|
||||||
token: "token"
|
token: "token"
|
||||||
|
users:
|
||||||
|
- telegramId: 123456789
|
||||||
|
role: admin
|
||||||
ssh:
|
ssh:
|
||||||
servers:
|
servers:
|
||||||
-
|
-
|
||||||
|
name: "Server Name"
|
||||||
host: "url"
|
host: "url"
|
||||||
port: 22
|
port: 22
|
||||||
user: "user"
|
user: "user"
|
||||||
password: "pass"
|
password: "pass"
|
||||||
|
-
|
||||||
|
name: "Server Name 2"
|
||||||
|
host: "url 2"
|
||||||
|
port: 22
|
||||||
|
user: "user 2"
|
||||||
|
password: "pass 2"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
15
src/main/resources/db/migration/V001__create_table_users.sql
Normal file
15
src/main/resources/db/migration/V001__create_table_users.sql
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
--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;
|
||||||
13
src/main/resources/db/migration/V002__create_table_roles.sql
Normal file
13
src/main/resources/db/migration/V002__create_table_roles.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
--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;
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE users
|
||||||
|
ADD COLUMN role_id INT NOT NULL REFERENCES roles(id);
|
||||||
2
src/main/resources/db/migration/V004__add_roles.sql
Normal file
2
src/main/resources/db/migration/V004__add_roles.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
INSERT INTO roles (id, role) VALUES (1, 'ADMIN');
|
||||||
|
INSERT INTO roles (id, role) VALUES (2, 'USER');
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
INSERT INTO roles (id, role) VALUES (3, 'ANONYMOUS');
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
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;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
ALTER TABLE roles
|
||||||
|
DROP COLUMN role;
|
||||||
|
|
||||||
|
ALTER TABLE roles
|
||||||
|
ADD COLUMN name VARCHAR(128);
|
||||||
|
COMMENT ON COLUMN roles.name IS 'Имя роли';
|
||||||
4
src/main/resources/db/migration/V008__add_roles.sql
Normal file
4
src/main/resources/db/migration/V008__add_roles.sql
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
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');
|
||||||
Reference in New Issue
Block a user