14 Commits

Author SHA1 Message Date
62a473000a fix
Some checks are pending
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master Build queued...
2023-11-22 20:48:51 +03:00
187e20ad9d fix
Some checks are pending
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master Build queued...
2023-11-22 20:42:47 +03:00
f2a74b9f84 добавил mdadm
All checks were successful
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master This commit looks good
2023-11-22 20:08:23 +03:00
15d6894013 уточнил пример ямлика 2023-11-22 19:11:03 +03:00
5bd78701e9 hotfix
All checks were successful
Hedgehog_server_CD/ServerMonitorBot/pipeline/head This commit looks good
2023-11-22 18:58:53 +03:00
74e5e2b017 hotfix
Some checks failed
Hedgehog_server_CD/ServerMonitorBot/pipeline/head There was a failure building this commit
2023-11-22 18:55:42 +03:00
30772ec123 Merge pull request 'fix' (#33) from feature/task-30-docker into master
All checks were successful
Hedgehog_server_CD/ServerMonitorBot/pipeline/head This commit looks good
Reviewed-on: #33
2023-11-22 18:51:11 +03:00
c5d7689b97 Удаление старых образов перед деплоем
Some checks are pending
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master Build queued...
2023-11-22 18:50:36 +03:00
5b0decff19 Merge pull request 'Удаление старых образов перед деплоем' (#32) from feature/task-30-docker into master
Some checks failed
Hedgehog_server_CD/ServerMonitorBot/pipeline/head There was a failure building this commit
Reviewed-on: #32
2023-11-22 18:48:16 +03:00
2bca814b3f Удаление старых образов перед деплоем
All checks were successful
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master This commit looks good
2023-11-22 18:45:22 +03:00
24839dc3fe Merge pull request 'feature/task-30-docker' (#31) from feature/task-30-docker into master
All checks were successful
Hedgehog_server_CD/ServerMonitorBot/pipeline/head This commit looks good
Reviewed-on: #31
2023-11-22 11:36:39 +03:00
2b8a5f326c убрал лишние шаги
All checks were successful
Hedgehog_server_CI/ServerMonitorBot/pipeline/head This commit looks good
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master This commit looks good
2023-11-22 11:32:44 +03:00
180e1e6be4 fix
Some checks failed
Hedgehog_server_CI/ServerMonitorBot/pipeline/head There was a failure building this commit
Hedgehog_server_CD/ServerMonitorBot/pipeline/head This commit looks good
2023-11-21 23:21:59 +03:00
1a1fca9325 fix 2023-11-21 23:21:34 +03:00
8 changed files with 182 additions and 32 deletions

View File

@@ -24,15 +24,26 @@ pipeline {
} }
stage('Build') { stage('Build') {
steps { steps {
sh "rm -f src/main/resources/application.yml"
withCredentials([file(credentialsId: 'SERVERMONITORBOT_APPLICATION_YAML', variable: 'application_yaml')]) { withCredentials([file(credentialsId: 'SERVERMONITORBOT_APPLICATION_YAML', variable: 'application_yaml')]) {
sh "cp -f \$application_yaml src/main/resources/application.yml" sh "cp -f \$application_yaml src/main/resources/application.yml"
sh "ls -lh"
sh "ls -lh src/main/resources"
} }
sh 'mvn -B -DskipTests -X clean package spring-boot:repackage' 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') { stage('Deploy') {
steps { steps {
script { script {
@@ -42,17 +53,16 @@ pipeline {
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} 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} 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} 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 builder prune --force'
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 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' 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('Prune images') { stage('Remove useless images') {
steps { steps {
sh """ sh """
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'docker image prune -f' sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'docker image prune --all --force'
""" """
} }
} }

View File

@@ -5,30 +5,11 @@ pipeline {
args '-v /root/.m2:/root/.m2' 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')
}
stages { stages {
stage('Init container') {
steps {
sh 'apt-get update && apt-get install -y sshpass openssh-client'
}
}
stage('Build') { stage('Build') {
steps { steps {
sh 'mvn -B -DskipTests -X clean package spring-boot:repackage' sh 'mvn -B -DskipTests -X clean package spring-boot:repackage'
} }
} }
stage('Prune images') {
steps {
sh """
sshpass -p ${SSH_PASS} ssh -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} -p ${SSH_PORT} 'docker image prune -f'
"""
}
}
} }
} }

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version> <version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>ru.ldeloff</groupId> <groupId>ru.ldeloff</groupId>

View File

@@ -14,6 +14,8 @@ import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateComma
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand; import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand;
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand; import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand; 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.UnameAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand; import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand;
@@ -33,6 +35,8 @@ public class TelegramBotController extends TelegramLongPollingBot {
final CpuTempCommand cpuTempCommand; final CpuTempCommand cpuTempCommand;
final HddTempAggregateCommand hddTempAggregateCommand; final HddTempAggregateCommand hddTempAggregateCommand;
final HddTempCommand hddTempCommand; final HddTempCommand hddTempCommand;
final MdadmStatusAggregateCommand mdadmStatusAggregateCommand;
final MdadmStatusCommand mdadmStatusCommand;
@Override @Override
public void onUpdateReceived(Update update) { public void onUpdateReceived(Update update) {
@@ -55,6 +59,9 @@ public class TelegramBotController extends TelegramLongPollingBot {
case "HDD.temp" -> { case "HDD.temp" -> {
hddTempAggregateCommand.execute(update, this); hddTempAggregateCommand.execute(update, this);
} }
case "MDADM" -> {
mdadmStatusAggregateCommand.execute(update,this);
}
default -> { default -> {
switchToMainMenu.execute(update, this); switchToMainMenu.execute(update, this);
} }
@@ -73,6 +80,9 @@ public class TelegramBotController extends TelegramLongPollingBot {
case "HDD.temp" -> { case "HDD.temp" -> {
hddTempCommand.execute(update, this); hddTempCommand.execute(update, this);
} }
case "MDADM" -> {
mdadmStatusCommand.execute(update,this);
}
default -> { default -> {
switchToMainMenu.execute(update, this); switchToMainMenu.execute(update, this);
} }

View File

@@ -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);
}
}

View File

@@ -0,0 +1,113 @@
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;
}
}

View File

@@ -15,6 +15,7 @@ public class TelegramBotKeyboard implements UiFormer {
private final String UNAME = "uname"; private final String UNAME = "uname";
private final String CPU_TEMP = "CPU.temp"; private final String CPU_TEMP = "CPU.temp";
private final String HDD_TEMP = "HDD.temp"; private final String HDD_TEMP = "HDD.temp";
private final String MDADM = "MDADM";
@Override @Override
@@ -25,13 +26,17 @@ public class TelegramBotKeyboard implements UiFormer {
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>(); ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
KeyboardRow keyboardRow = new KeyboardRow(); KeyboardRow keyboardRow1 = new KeyboardRow();
keyboardRows.add(keyboardRow); keyboardRows.add(keyboardRow1);
keyboardRow.add(new KeyboardButton(STATUS)); keyboardRow1.add(new KeyboardButton(STATUS));
keyboardRow.add(new KeyboardButton(UNAME)); keyboardRow1.add(new KeyboardButton(UNAME));
keyboardRow.add(new KeyboardButton(CPU_TEMP)); keyboardRow1.add(new KeyboardButton(CPU_TEMP));
keyboardRow.add(new KeyboardButton(HDD_TEMP)); keyboardRow1.add(new KeyboardButton(HDD_TEMP));
KeyboardRow keyboardRow2 = new KeyboardRow();
keyboardRows.add(keyboardRow2);
keyboardRow2.add(new KeyboardButton(MDADM));
replyKeyboardMarkup.setKeyboard(keyboardRows); replyKeyboardMarkup.setKeyboard(keyboardRows);
message.setReplyMarkup(replyKeyboardMarkup); message.setReplyMarkup(replyKeyboardMarkup);

View File

@@ -24,6 +24,12 @@ ssh:
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"