2023-02-02 15:01:37 +08:00
package hook
import (
2023-07-04 16:16:12 +08:00
"encoding/base64"
2023-02-02 15:01:37 +08:00
"github.com/1Panel-dev/1Panel/backend/app/repo"
2023-08-23 17:16:18 +08:00
"github.com/1Panel-dev/1Panel/backend/constant"
2023-02-02 15:01:37 +08:00
"github.com/1Panel-dev/1Panel/backend/global"
2023-05-13 12:20:29 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
2023-02-02 15:01:37 +08:00
)
func Init ( ) {
settingRepo := repo . NewISettingRepo ( )
portSetting , err := settingRepo . Get ( settingRepo . WithByKey ( "ServerPort" ) )
if err != nil {
global . LOG . Errorf ( "load service port from setting failed, err: %v" , err )
}
global . CONF . System . Port = portSetting . Value
2023-10-25 10:41:26 +08:00
ipv6Setting , err := settingRepo . Get ( settingRepo . WithByKey ( "Ipv6" ) )
if err != nil {
global . LOG . Errorf ( "load ipv6 status from setting failed, err: %v" , err )
}
global . CONF . System . Ipv6 = ipv6Setting . Value
bindAddressSetting , err := settingRepo . Get ( settingRepo . WithByKey ( "BindAddress" ) )
if err != nil {
global . LOG . Errorf ( "load bind address from setting failed, err: %v" , err )
}
global . CONF . System . BindAddress = bindAddressSetting . Value
2023-04-25 14:34:16 +08:00
sslSetting , err := settingRepo . Get ( settingRepo . WithByKey ( "SSL" ) )
if err != nil {
global . LOG . Errorf ( "load service ssl from setting failed, err: %v" , err )
}
global . CONF . System . SSL = sslSetting . Value
2023-02-02 15:01:37 +08:00
2023-07-04 16:16:12 +08:00
OneDriveID , err := settingRepo . Get ( settingRepo . WithByKey ( "OneDriveID" ) )
if err != nil {
global . LOG . Errorf ( "load onedrive info from setting failed, err: %v" , err )
}
idItem , _ := base64 . StdEncoding . DecodeString ( OneDriveID . Value )
global . CONF . System . OneDriveID = string ( idItem )
OneDriveSc , err := settingRepo . Get ( settingRepo . WithByKey ( "OneDriveSc" ) )
if err != nil {
global . LOG . Errorf ( "load onedrive info from setting failed, err: %v" , err )
}
scItem , _ := base64 . StdEncoding . DecodeString ( OneDriveSc . Value )
global . CONF . System . OneDriveSc = string ( scItem )
2023-02-02 15:01:37 +08:00
if _ , err := settingRepo . Get ( settingRepo . WithByKey ( "SystemStatus" ) ) ; err != nil {
_ = settingRepo . Create ( "SystemStatus" , "Free" )
}
if err := settingRepo . Update ( "SystemStatus" , "Free" ) ; err != nil {
2023-05-13 12:20:29 +08:00
global . LOG . Fatalf ( "init service before start failed, err: %v" , err )
}
if global . CONF . System . ChangeUserInfo {
if err := settingRepo . Update ( "UserName" , common . RandStrAndNum ( 10 ) ) ; err != nil {
global . LOG . Fatalf ( "init username before start failed, err: %v" , err )
}
pass , _ := encrypt . StringEncrypt ( common . RandStrAndNum ( 10 ) )
if err := settingRepo . Update ( "Password" , pass ) ; err != nil {
global . LOG . Fatalf ( "init password before start failed, err: %v" , err )
}
if err := settingRepo . Update ( "SecurityEntrance" , common . RandStrAndNum ( 10 ) ) ; err != nil {
global . LOG . Fatalf ( "init entrance before start failed, err: %v" , err )
}
2023-05-30 15:30:57 +08:00
sudo := cmd . SudoHandleCmd ( )
_ , _ = cmd . Execf ( "%s sed -i '/CHANGE_USER_INFO=true/d' /usr/local/bin/1pctl" , sudo )
2023-02-02 15:01:37 +08:00
}
2023-08-23 17:16:18 +08:00
handleSnapStatus ( )
}
func handleSnapStatus ( ) {
snapRepo := repo . NewISnapshotRepo ( )
snaps , _ := snapRepo . GetList ( )
for _ , snap := range snaps {
if snap . Status == "OnSaveData" {
_ = snapRepo . Update ( snap . ID , map [ string ] interface { } { "status" : constant . StatusSuccess } )
}
if snap . Status == constant . StatusWaiting {
_ = snapRepo . Update ( snap . ID , map [ string ] interface { } { "status" : constant . StatusFailed , "message" : "the task was interrupted due to the restart of the 1panel service" } )
}
}
status , _ := snapRepo . GetStatusList ( )
for _ , statu := range status {
2023-08-29 10:50:15 +08:00
updates := make ( map [ string ] interface { } )
2023-08-23 17:16:18 +08:00
if statu . Panel == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "panel" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . PanelInfo == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "panel_info" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . DaemonJson == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "daemon_json" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . AppData == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "app_data" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . PanelData == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "panel_data" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . BackupData == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "backup_data" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . Compress == constant . StatusRunning {
2023-08-29 10:50:15 +08:00
updates [ "compress" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
if statu . Upload == constant . StatusUploading {
2023-08-29 10:50:15 +08:00
updates [ "upload" ] = constant . StatusFailed
2023-08-23 17:16:18 +08:00
}
2023-08-29 10:50:15 +08:00
if len ( updates ) != 0 {
_ = snapRepo . UpdateStatus ( statu . ID , updates )
2023-08-23 17:16:18 +08:00
}
}
2023-02-02 15:01:37 +08:00
}