Compare commits
1 Commits
feature/ta
...
feature/ta
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ade9f67e5 |
11
Dockerfile
11
Dockerfile
@@ -1,11 +0,0 @@
|
||||
# Используем официальный образ OpenJDK 17 как базовый образ
|
||||
FROM openjdk:17
|
||||
|
||||
# Установка рабочей директории внутри контейнера
|
||||
WORKDIR /app
|
||||
|
||||
# Копируем JAR-файл приложения из локальной директории внутрь контейнера
|
||||
COPY ServerMonitorBot-0.0.1-SNAPSHOT.jar app.jar
|
||||
|
||||
# Команда для запуска Spring Boot приложения при старте контейнера
|
||||
CMD ["java", "-jar", "app.jar"]
|
||||
@@ -1,71 +0,0 @@
|
||||
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'
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
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'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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:
|
||||
49
pom.xml
49
pom.xml
@@ -15,11 +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>
|
||||
<dependency>
|
||||
@@ -45,7 +40,7 @@
|
||||
<dependency>
|
||||
<groupId>org.telegram</groupId>
|
||||
<artifactId>telegrambots</artifactId>
|
||||
<version>6.8.0</version>
|
||||
<version>6.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
@@ -57,13 +52,12 @@
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
<version>${flyway.version}</version>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -81,39 +75,6 @@
|
||||
</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>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -14,8 +14,6 @@ import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateComma
|
||||
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;
|
||||
|
||||
@@ -35,8 +33,6 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
||||
final CpuTempCommand cpuTempCommand;
|
||||
final HddTempAggregateCommand hddTempAggregateCommand;
|
||||
final HddTempCommand hddTempCommand;
|
||||
final MdadmStatusAggregateCommand mdadmStatusAggregateCommand;
|
||||
final MdadmStatusCommand mdadmStatusCommand;
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(Update update) {
|
||||
@@ -59,9 +55,6 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
||||
case "HDD.temp" -> {
|
||||
hddTempAggregateCommand.execute(update, this);
|
||||
}
|
||||
case "MDADM" -> {
|
||||
mdadmStatusAggregateCommand.execute(update,this);
|
||||
}
|
||||
default -> {
|
||||
switchToMainMenu.execute(update, this);
|
||||
}
|
||||
@@ -80,9 +73,6 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
||||
case "HDD.temp" -> {
|
||||
hddTempCommand.execute(update, this);
|
||||
}
|
||||
case "MDADM" -> {
|
||||
mdadmStatusCommand.execute(update,this);
|
||||
}
|
||||
default -> {
|
||||
switchToMainMenu.execute(update, this);
|
||||
}
|
||||
|
||||
@@ -61,4 +61,4 @@ public class HddTempCommand extends CommandTemplate {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
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<>();
|
||||
names.add("Number Name State");
|
||||
|
||||
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(elements.get(0) + ", "
|
||||
+ elements.get(6) + ", "
|
||||
+ elements.get(4) + " "
|
||||
+ elements.get(5));
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ public class TelegramBotKeyboard implements UiFormer {
|
||||
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
|
||||
@@ -26,17 +25,13 @@ public class TelegramBotKeyboard implements UiFormer {
|
||||
|
||||
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
|
||||
|
||||
KeyboardRow keyboardRow1 = new KeyboardRow();
|
||||
keyboardRows.add(keyboardRow1);
|
||||
KeyboardRow keyboardRow = new KeyboardRow();
|
||||
keyboardRows.add(keyboardRow);
|
||||
|
||||
keyboardRow1.add(new KeyboardButton(STATUS));
|
||||
keyboardRow1.add(new KeyboardButton(UNAME));
|
||||
keyboardRow1.add(new KeyboardButton(CPU_TEMP));
|
||||
keyboardRow1.add(new KeyboardButton(HDD_TEMP));
|
||||
|
||||
KeyboardRow keyboardRow2 = new KeyboardRow();
|
||||
keyboardRows.add(keyboardRow2);
|
||||
keyboardRow2.add(new KeyboardButton(MDADM));
|
||||
keyboardRow.add(new KeyboardButton(STATUS));
|
||||
keyboardRow.add(new KeyboardButton(UNAME));
|
||||
keyboardRow.add(new KeyboardButton(CPU_TEMP));
|
||||
keyboardRow.add(new KeyboardButton(HDD_TEMP));
|
||||
|
||||
replyKeyboardMarkup.setKeyboard(keyboardRows);
|
||||
message.setReplyMarkup(replyKeyboardMarkup);
|
||||
|
||||
@@ -2,14 +2,20 @@ spring:
|
||||
application:
|
||||
name: ServerMonitorBot
|
||||
datasource:
|
||||
url: jdbc:postgresql://db:5432/servermonitorbot
|
||||
url: jdbc:postgresql://localhost:5432/servermonitorbot
|
||||
username: servermonitorbot
|
||||
password: servermonitorbot
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: none
|
||||
flyway:
|
||||
locations: classpath:db/migration
|
||||
liquibase:
|
||||
enabled: true
|
||||
change-log: classpath:db/scripts/changelog-master.xml
|
||||
url: jdbc:postgresql://localhost:5432/servermonitorbot
|
||||
user: servermonitorbot
|
||||
password: servermonitorbot
|
||||
liquibase-schema: "liquibase"
|
||||
default-schema: "servermonitorbot"
|
||||
bot:
|
||||
name: "ServerMonitorBot"
|
||||
token: "token"
|
||||
@@ -24,12 +30,6 @@ ssh:
|
||||
port: 22
|
||||
user: "user"
|
||||
password: "pass"
|
||||
-
|
||||
name: "Server Name 2"
|
||||
host: "url 2"
|
||||
port: 22
|
||||
user: "user 2"
|
||||
password: "pass 2"
|
||||
|
||||
|
||||
|
||||
|
||||
10
src/main/resources/db/scripts/changelog-master.xml
Normal file
10
src/main/resources/db/scripts/changelog-master.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||
|
||||
<include file="release_0_0_1/changelog.xml" relativeToChangelogFile="true"/>
|
||||
|
||||
|
||||
</databaseChangeLog>
|
||||
16
src/main/resources/db/scripts/release_0_0_1/changelog.xml
Normal file
16
src/main/resources/db/scripts/release_0_0_1/changelog.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||
|
||||
<include file="scripts/01_create_table_users.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/02_create_table_roles.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/03_add_FK_users_to_roles.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/04_add_roles.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/05_add_anonymous_role.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/06_refactor_users_table.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/07_refactor_roles_table.sql" relativeToChangelogFile="true"/>
|
||||
<include file="scripts/08_add_roles.sql" relativeToChangelogFile="true"/>
|
||||
|
||||
</databaseChangeLog>
|
||||
@@ -1,3 +1,5 @@
|
||||
--liquibase formatted sql
|
||||
|
||||
--changeset L_DelOff:create_table_users rollbackSplitStatements:true
|
||||
--comment: Создание таблицы пользователей
|
||||
CREATE TABLE users
|
||||
@@ -1,3 +1,5 @@
|
||||
--liquibase formatted sql
|
||||
|
||||
--changeset L_DelOff:create_table_roles rollbackSplitStatements:true
|
||||
--comment: Создание таблицы ролей
|
||||
CREATE TABLE roles
|
||||
Reference in New Issue
Block a user