Initial commit
This commit is contained in:
89
internal/updater/updater.go
Normal file
89
internal/updater/updater.go
Normal file
@ -0,0 +1,89 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitea.unprism.ru/KRBL/FemaInstaller/internal/ssh"
|
||||
"gitea.unprism.ru/KRBL/FemaInstaller/pkg/config"
|
||||
"gitea.unprism.ru/KRBL/FemaInstaller/pkg/fileutils"
|
||||
)
|
||||
|
||||
// Updater handles the software update process
|
||||
type Updater struct {
|
||||
Config *config.UpdaterConfig
|
||||
BinaryData []byte
|
||||
}
|
||||
|
||||
// NewUpdater creates a new updater with the provided configuration and binary data
|
||||
func NewUpdater(config *config.UpdaterConfig, binaryData []byte) *Updater {
|
||||
return &Updater{
|
||||
Config: config,
|
||||
BinaryData: binaryData,
|
||||
}
|
||||
}
|
||||
|
||||
// Update performs the software update process
|
||||
func (u *Updater) Update() error {
|
||||
// Save binary to temporary file
|
||||
tempFile := "update_binary"
|
||||
if err := os.WriteFile(tempFile, u.BinaryData, 0644); err != nil {
|
||||
return fmt.Errorf("не удалось сохранить временный файл: %w", err)
|
||||
}
|
||||
defer os.Remove(tempFile) // Clean up temporary file
|
||||
|
||||
// Create SSH client configuration
|
||||
clientConfig := ssh.NewClientConfig(
|
||||
u.Config.IP,
|
||||
u.Config.Port,
|
||||
u.Config.Login,
|
||||
u.Config.Password,
|
||||
)
|
||||
|
||||
// Connect to SSH server
|
||||
sshClient, err := ssh.CreateSSHClient(clientConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ошибка подключения SSH: %w", err)
|
||||
}
|
||||
defer sshClient.Close()
|
||||
|
||||
// Create SFTP client
|
||||
sftpClient, err := ssh.CreateSFTPClient(clientConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ошибка подключения SFTP: %w", err)
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// Upload binary
|
||||
remotePath := "/root/fema/build.new"
|
||||
if err = fileutils.UploadFile(sftpClient, tempFile, remotePath); err != nil {
|
||||
return fmt.Errorf("ошибка загрузки файла: %w", err)
|
||||
}
|
||||
|
||||
// Execute update commands
|
||||
commands := []string{
|
||||
"mv -f /root/fema/build.new /root/fema/build",
|
||||
"chmod +x /root/fema/build",
|
||||
"systemctl restart fema.service",
|
||||
}
|
||||
|
||||
for _, cmd := range commands {
|
||||
if err := ssh.ExecuteCommand(sshClient, cmd); err != nil {
|
||||
return fmt.Errorf("ошибка выполнения команды '%s': %w", cmd, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfigFilePath returns the path to the configuration file
|
||||
func GetConfigFilePath() string {
|
||||
// Get executable directory
|
||||
execDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
if err != nil {
|
||||
execDir = "."
|
||||
}
|
||||
|
||||
return filepath.Join(execDir, "updater_config.json")
|
||||
}
|
Reference in New Issue
Block a user