Documentation
¶
Index ¶
- Constants
- Variables
- func IsServerDoesNotExistError(err error) bool
- func IsTooFrequentCrashError(err error) bool
- type APIResponse
- type Configuration
- type ConfigurationMeta
- type ConsoleThrottle
- type CrashHandler
- type EggConfiguration
- type InstallationProcess
- func (ip *InstallationProcess) AfterExecute(containerId string) error
- func (ip *InstallationProcess) BeforeExecute() error
- func (ip *InstallationProcess) Execute() (string, error)
- func (ip *InstallationProcess) GetLogPath() string
- func (ip *InstallationProcess) RemoveContainer() error
- func (ip *InstallationProcess) Run() error
- func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) error
- type Manager
- func (m *Manager) Add(s *Server)
- func (m *Manager) All() []*Server
- func (m *Manager) Client() remote.Client
- func (m *Manager) Filter(filter func(match *Server) bool) []*Server
- func (m *Manager) Find(filter func(match *Server) bool) *Server
- func (m *Manager) Get(uuid string) (*Server, bool)
- func (m *Manager) InitServer(data remote.ServerConfigurationResponse) (*Server, error)
- func (m *Manager) Keys() []string
- func (m *Manager) Len() int
- func (m *Manager) PersistStates() error
- func (m *Manager) Put(s []*Server)
- func (m *Manager) ReadStates() (map[string]string, error)
- func (m *Manager) Remove(filter func(match *Server) bool)
- type Mount
- type PowerAction
- type RequestActivity
- type ResourceUsage
- type Server
- func (s *Server) Backup(b backup.BackupInterface) error
- func (s *Server) CleanupForDestroy()
- func (s *Server) Config() *Configuration
- func (s *Server) Context() context.Context
- func (s *Server) CreateEnvironment() error
- func (s *Server) CtxCancel()
- func (s *Server) DestroyAllSinks()
- func (s *Server) DiskSpace() int64
- func (s *Server) EnsureDataDirectoryExists() error
- func (s *Server) Events() *events.Bus
- func (s *Server) ExecutingPowerAction() bool
- func (s *Server) Filesystem() *filesystem.Filesystem
- func (s *Server) GetEnvironmentVariables() []string
- func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error
- func (s *Server) ID() string
- func (s *Server) Id() string
- func (s *Server) Install() error
- func (s *Server) IsInProtectedState() bool
- func (s *Server) IsInstalling() bool
- func (s *Server) IsRestoring() bool
- func (s *Server) IsRunning() bool
- func (s *Server) IsSuspended() bool
- func (s *Server) IsTransferring() bool
- func (s *Server) Log() *log.Entry
- func (s *Server) MemoryLimit() int64
- func (s *Server) Mounts() []environment.Mount
- func (s *Server) NewRequestActivity(user string, ip string) RequestActivity
- func (s *Server) OnStateChange()
- func (s *Server) Proc() ResourceUsage
- func (s *Server) ProcessConfiguration() *remote.ProcessConfiguration
- func (s *Server) PublishConsoleOutputFromDaemon(data string)
- func (s *Server) ReadLogfile(len int) ([]string, error)
- func (s *Server) Reinstall() error
- func (s *Server) RestoreBackup(b backup.BackupInterface, reader io.ReadCloser) (err error)
- func (s *Server) SaveActivity(a RequestActivity, event models.Event, metadata models.ActivityMeta)
- func (s *Server) SetInstalling(state bool)
- func (s *Server) SetRestoring(state bool)
- func (s *Server) SetTransferring(state bool)
- func (s *Server) Sftp() *system.ContextBag
- func (s *Server) Sink(name system.SinkName) *system.SinkPool
- func (s *Server) StartEventListeners()
- func (s *Server) Sync() error
- func (s *Server) SyncInstallState(successful, reinstall bool) error
- func (s *Server) SyncWithConfiguration(cfg remote.ServerConfigurationResponse) error
- func (s *Server) SyncWithEnvironment()
- func (s *Server) Throttler() *ConsoleThrottle
- func (s *Server) ToAPIResponse() APIResponse
- func (s *Server) UpdateConfigurationFiles()
- func (s *Server) Websockets() *WebsocketBag
- type WebsocketBag
Constants ¶
const ( ActivityConsoleCommand = models.Event("server:console.command") ActivitySftpWrite = models.Event("server:sftp.write") ActivitySftpCreate = models.Event("server:sftp.create") ActivitySftpCreateDirectory = models.Event("server:sftp.create-directory") ActivitySftpRename = models.Event("server:sftp.rename") ActivitySftpDelete = models.Event("server:sftp.delete") ActivityFileUploaded = models.Event("server:file.uploaded") )
const ( DaemonMessageEvent = "daemon message" InstallOutputEvent = "install output" InstallStartedEvent = "install started" InstallCompletedEvent = "install completed" ConsoleOutputEvent = "console output" StatusEvent = "status" StatsEvent = "stats" BackupRestoreCompletedEvent = "backup restore completed" BackupCompletedEvent = "backup completed" TransferLogsEvent = "transfer logs" TransferStatusEvent = "transfer status" DeletedEvent = "deleted" )
定義伺服器所有可能的輸出事件。
const ( PowerActionStart = "start" PowerActionStop = "stop" PowerActionRestart = "restart" PowerActionTerminate = "kill" )
可對指定伺服器執行的電源動作。此機制會存取給定的伺服器環境, 並以能防止競態條件發生的方式執行這些動作。例如,連續傳送兩次 "start" 動作,第二個動作要等到第一個動作完成後才會被處理。
此機制使用一個限制為一個工作者的 workerpool,讓所有動作 都以同步方式執行。
const ActivityPowerPrefix = "server:power."
Variables ¶
Functions ¶
func IsTooFrequentCrashError ¶
Types ¶
type APIResponse ¶
type APIResponse struct {
State string `json:"state"`
IsSuspended bool `json:"is_suspended"`
Utilization ResourceUsage `json:"utilization"`
Configuration Configuration `json:"configuration"`
}
APIResponse 是請求 Wings 上單一伺服器實例詳細資訊時所回傳的型別。 其中包含面板所需的資訊,用於顯示資源使用率與此系統上的目前狀態。
type Configuration ¶
type Configuration struct {
// 伺服器的唯一識別碼,用於向面板 API 參照該伺服器(以及內部使用)。
// 此識別碼也會用於命名 Docker 容器以及日誌輸出中。
Uuid string `json:"uuid"`
Meta ConfigurationMeta `json:"meta"`
// 伺服器是否處於暫停狀態。暫停中的伺服器無法啟動或修改,
// 除非管理員使用者在特定情境下操作。
Suspended bool `json:"suspended"`
// 啟動此伺服器實例時應使用的指令。
Invocation string `json:"invocation"`
// 預設為 false,但若在面板上安裝或重新安裝伺服器時選取此選項,
// 該伺服器處理程序將會跳過特定的安裝腳本。
SkipEggScripts bool `json:"skip_egg_scripts"`
// 應傳遞給執行中伺服器處理程序的環境變數陣列。
EnvVars environment.Variables `json:"environment"`
// Labels 是一份應套用至執行中伺服器處理程序的容器標籤對應表。
Labels map[string]string `json:"labels"`
Allocations environment.Allocations `json:"allocations"`
Build environment.Limits `json:"build"`
CrashDetectionEnabled bool `json:"crash_detection_enabled"`
Mounts []Mount `json:"mounts"`
Egg EggConfiguration `json:"egg,omitempty"`
Container struct {
// 定義此伺服器將使用的 Docker 映像檔
Image string `json:"image,omitempty"`
} `json:"container,omitempty"`
// contains filtered or unexported fields
}
func (*Configuration) GetUuid ¶
func (c *Configuration) GetUuid() string
func (*Configuration) SetSuspended ¶
func (c *Configuration) SetSuspended(s bool)
type ConfigurationMeta ¶
type ConsoleThrottle ¶
type ConsoleThrottle struct {
// contains filtered or unexported fields
}
func (*ConsoleThrottle) Allow ¶
func (ct *ConsoleThrottle) Allow() bool
Allow 檢查主控台是否被允許處理更多輸出資料,或是目前已透過此連線 傳送過多資料。若輸出量過多,會觸發 strike 回呼函式, 但僅會在此程序目前階段尚未觸發過的情況下才會執行。
若允許輸出,節流器上的鎖定會被釋放,下次觸發時 strike 函式會再次被執行。
type CrashHandler ¶
type CrashHandler struct {
// contains filtered or unexported fields
}
func (*CrashHandler) LastCrashTime ¶
func (cd *CrashHandler) LastCrashTime() time.Time
LastCrashTime 回傳此伺服器實例上一次當機的時間。
func (*CrashHandler) SetLastCrash ¶
func (cd *CrashHandler) SetLastCrash(t time.Time)
SetLastCrash 設定伺服器最近一次的當機時間。
type EggConfiguration ¶
type InstallationProcess ¶
type InstallationProcess struct {
Server *Server
Script *remote.InstallationScript
// contains filtered or unexported fields
}
func NewInstallationProcess ¶
func NewInstallationProcess(s *Server, script *remote.InstallationScript) (*InstallationProcess, error)
NewInstallationProcess 回傳一個新的安裝流程結構,該結構將用於 建立容器,以及執行伺服器其他安裝相關指令。
func (*InstallationProcess) AfterExecute ¶
func (ip *InstallationProcess) AfterExecute(containerId string) error
AfterExecute 在安裝流程執行完畢後進行清理。此步驟會擷取此流程的 日誌並儲存至伺服器組態目錄,接著銷毀相關聯的安裝容器。
func (*InstallationProcess) BeforeExecute ¶
func (ip *InstallationProcess) BeforeExecute() error
BeforeExecute 在容器執行前先執行。此步驟會拉取所需的 docker 容器映像檔,並將安裝腳本寫入磁碟。此流程以非同步方式執行, 若其中任一步驟失敗,都會回傳該錯誤。
func (*InstallationProcess) Execute ¶
func (ip *InstallationProcess) Execute() (string, error)
Execute 在一個特別建立的 docker 容器中執行安裝流程。
func (*InstallationProcess) GetLogPath ¶
func (ip *InstallationProcess) GetLogPath() string
GetLogPath 回傳安裝流程的日誌檔路徑。
func (*InstallationProcess) RemoveContainer ¶
func (ip *InstallationProcess) RemoveContainer() error
RemoveContainer 移除伺服器的安裝容器。
func (*InstallationProcess) Run ¶
func (ip *InstallationProcess) Run() error
Run 執行安裝流程,此動作是在背景執行緒中完成的。 這會設定所需的環境,接著啟動安裝容器。容器完成安裝後, 結果會被儲存至伺服器組態目錄中的安裝日誌檔案。
func (*InstallationProcess) StreamOutput ¶
func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) error
StreamOutput 將安裝流程的輸出串流至伺服器組態目錄中的日誌檔案, 同時也串流至 websocket 監聽端,讓管理員可以在面板中檢視此流程。
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func NewEmptyManager ¶
NewEmptyManager 回傳一個新的空管理員集合,實際上並不會從磁碟 載入任何伺服器。這讓呼叫端可以依需求自行將伺服器設定進集合中。
func NewManager ¶
NewManager 回傳一個新的伺服器管理員實例。此函式會啟動所有目前 存在於檔案系統上的伺服器,並將它們設定進管理員中。
func (*Manager) InitServer ¶
func (m *Manager) InitServer(data remote.ServerConfigurationResponse) (*Server, error)
InitServer 使用一個資料位元組陣列來初始化伺服器。此資料會透過 YAML marshaler 轉換至給定的結構中。此函式也會為伺服器設定給定的環境。
func (*Manager) PersistStates ¶
PersistStates 將每個伺服器目前的環境狀態寫入磁碟。這通常會依 根執行器指令中定義的特定間隔被呼叫,避免大量伺服器同時切換狀態 時造成磁碟 I/O 過載。此檔案稍微不同步也沒關係, 它只是用來讓意外的系統重新開機後的復原過程稍微簡單一點。
func (*Manager) ReadStates ¶
ReadStates 回傳各伺服器的狀態。
type Mount ¶
type Mount environment.Mount
為避免在處理掛載點時產生混淆,請假設 server.Mount 尚未經過妥善清理 且尚未設定路徑。environment.Mount 應該只在路徑經過檢查且有效時才回傳。
type PowerAction ¶
type PowerAction string
func (PowerAction) IsStart ¶
func (pa PowerAction) IsStart() bool
type RequestActivity ¶
type RequestActivity struct {
// contains filtered or unexported fields
}
RequestActivity 是 LoggedEvent 的包裝器,能夠追蹤額外的請求相關中繼資料, 包括與後續所有事件相關聯的特定使用者與 IP 位址。內部的紀錄事件結構 可以透過呼叫 RequestEvent.Event() 取得。
func (RequestActivity) Event ¶
func (ra RequestActivity) Event(event models.Event, metadata models.ActivityMeta) *models.Activity
Event 從 RequestEvent 實例中回傳底層的紀錄事件,並在其上設定 特定的事件與中繼資料。
func (RequestActivity) SetUser ¶
func (ra RequestActivity) SetUser(u string) RequestActivity
SetUser 複製 RequestActivity 結構,並在複製後的實例上設定新的使用者值 再回傳。
type ResourceUsage ¶
type ResourceUsage struct {
// 將目前的環境統計資料嵌入此伺服器專屬的資源使用量結構中。
environment.Stats
// 目前的伺服器狀態。
State *system.AtomicString `json:"state"`
// 伺服器目前使用的磁碟空間。此數值不保證隨時都是準確的,
// 它是在每次呼叫 server.Proc() 時「手動」設定的。目前這只是個
// 有點取巧的解法,用來避免到處傳遞事件。
Disk int64 `json:"disk_bytes"`
// contains filtered or unexported fields
}
ResourceUsage 定義給定伺服器實例目前的資源使用量。若伺服器處於離線狀態, 理所當然地記憶體與 CPU 使用量應為 0。不過磁碟使用量永遠都會被回傳, 因為收集該資料並不需要伺服器處於執行中狀態。
func (*ResourceUsage) Reset ¶
func (ru *ResourceUsage) Reset()
Reset 將使用量數值重設為零,在伺服器停止時使用, 確保我們不會錯誤地保留任何數值。
func (*ResourceUsage) UpdateStats ¶
func (ru *ResourceUsage) UpdateStats(stats environment.Stats)
UpdateStats 更新此伺服器資源使用量目前的統計資料。
type Server ¶
type Server struct {
// 內部使用的互斥鎖,用於阻擋需要循序執行的動作,
// 例如將組態寫入磁碟。
sync.RWMutex
Environment environment.ProcessEnvironment `json:"-"`
// contains filtered or unexported fields
}
Server 是受 Wings 控制的伺服器實例的高階定義。
func (*Server) Backup ¶
func (s *Server) Backup(b backup.BackupInterface) error
Backup 執行伺服器備份,並透過伺服器 websocket 發出事件。實際的備份系統 會負責通知面板狀態,但那並不會發出 websocket 事件。
func (*Server) CleanupForDestroy ¶
func (s *Server) CleanupForDestroy()
CleanupForDestroy 停止此伺服器所有使用伺服器結構上 context 的執行中背景工作。此函式也會取消伺服器任何正在執行中的安裝流程。
func (*Server) Config ¶
func (s *Server) Config() *Configuration
func (*Server) Context ¶
Context 回傳此伺服器的 context 實例。應使用此 context, 讓背景工作能在伺服器被移除時被取消。它只會在應用程式停止 或伺服器被刪除時才會被取消。
func (*Server) CreateEnvironment ¶
CreateEnvironment 初始化一個伺服器實例。此函式會走訪整個流程, 確保伺服器的環境已設定完成,且所有必要的檔案都已建立。
func (*Server) CtxCancel ¶
func (s *Server) CtxCancel()
CtxCancel 取消指派給此伺服器實例的 context。假設有背景工作 正在使用此伺服器的 context,那麼所有背景工作也都會因此被停止。
func (*Server) DestroyAllSinks ¶
func (s *Server) DestroyAllSinks()
DestroyAllSinks 走訪伺服器上所有已設定的 sink 並銷毀其實例。 請注意,若之後再次呼叫 Server.Sink() 將會導致 panic。 此函式僅在系統刪除某個伺服器時才會使用。
func (*Server) EnsureDataDirectoryExists ¶
EnsureDataDirectoryExists 確保此伺服器實例的資料目錄存在。
func (*Server) ExecutingPowerAction ¶
ExecutingPowerAction 檢查此伺服器目前是否有電源動作正在處理中。
func (*Server) Filesystem ¶
func (s *Server) Filesystem() *filesystem.Filesystem
Filesystem 回傳此伺服器的檔案系統實例。
func (*Server) GetEnvironmentVariables ¶
GetEnvironmentVariables 回傳所有應指派給執行中伺服器實例的環境變數。
func (*Server) HandlePowerAction ¶
func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error
HandlePowerAction 是一個輔助函式,可接收電源動作並處理該動作所需 執行的流程。這可防止有人同時呼叫兩次 Start(),或是在另一個重新啟動 流程正在進行時嘗試再次重新啟動。
不過,守護行程的程式碼設計確實仰賴使用者正確地呼叫此函式, 而不是直接呼叫環境結構上的 start/stop/restart 函式。
func (*Server) Install ¶
Install 執行伺服器處理程序的安裝流程。任何錯誤都會向上冒泡至 呼叫函式,該函式應負責聯絡面板以通知伺服器狀態。
傳入 true 作為第一個引數,可在執行流程前先執行一次伺服器同步, 以確保使用的是最新的資訊。
func (*Server) IsInProtectedState ¶
func (*Server) IsInstalling ¶
IsInstalling 透過檢查安裝器鎖定的狀態,回傳伺服器是否正在 主動執行安裝流程。
func (*Server) IsRestoring ¶
func (*Server) IsTransferring ¶
func (*Server) MemoryLimit ¶
func (*Server) Mounts ¶
func (s *Server) Mounts() []environment.Mount
Mounts 回傳此伺服器實例預設的容器掛載點,包含伺服器的資料目錄。 先前此函式也會掛載主機的時區檔案,不過我們已改採直接在容器中 設定 `TZ=Timezone` 環境變數的方式,這在大多數情境下都能運作良好。
func (*Server) NewRequestActivity ¶
func (s *Server) NewRequestActivity(user string, ip string) RequestActivity
func (*Server) OnStateChange ¶
func (s *Server) OnStateChange()
OnStateChange 在內部設定伺服器的狀態。此函式會處理當機偵測, 並向伺服器的事件監聽器回報狀態。
func (*Server) Proc ¶
func (s *Server) Proc() ResourceUsage
Proc 回傳此伺服器實例目前的資源使用統計資料。此函式回傳的是 所追蹤資源的一份副本,因此對回應內容做的任何變更多半不會 產生你想要的結果。
func (*Server) ProcessConfiguration ¶
func (s *Server) ProcessConfiguration() *remote.ProcessConfiguration
func (*Server) PublishConsoleOutputFromDaemon ¶
PublishConsoleOutputFromDaemon 將輸出傳送至伺服器主控台,並格式化為 看起來正確地由 Wings 所傳送。
func (*Server) ReadLogfile ¶
ReadLogfile 讀取伺服器的日誌檔案,最多至指定的位元組數。
func (*Server) RestoreBackup ¶
func (s *Server) RestoreBackup(b backup.BackupInterface, reader io.ReadCloser) (err error)
RestoreBackup 呼叫所提供備份的 Restore 函式。此還原程序完成後, 會向 websocket 發出事件以通知面板已完成。
除了 websocket 事件之外,也會觸發一次 API 呼叫以通知面板新的狀態。
func (*Server) SaveActivity ¶
func (s *Server) SaveActivity(a RequestActivity, event models.Event, metadata models.ActivityMeta)
SaveActivity 會在背景常式中將活動項目儲存至資料庫。若發生錯誤, 該錯誤只會被記錄下來,不會回傳給呼叫端。
func (*Server) SetInstalling ¶
func (*Server) SetRestoring ¶
func (*Server) SetTransferring ¶
func (*Server) Sftp ¶
func (s *Server) Sftp() *system.ContextBag
Sftp 回傳此伺服器實例的 SFTP 連線集合。此集合會依個別使用者 追蹤所有開啟中的 SFTP 連線,並允許其他處理程序中斷單一使用者 或全部使用者的連線。
func (*Server) StartEventListeners ¶
func (s *Server) StartEventListeners()
StartEventListeners 新增我們要用於伺服器的所有內部事件監聽器。 這些監聽器只能透過刪除伺服器來移除,因為它們應持續存在於 整個處理程序的生命週期中。
func (*Server) Sync ¶
Sync 將面板上伺服器的狀態與 Wings 同步。此舉確保我們永遠使用 來自面板的伺服器狀態,也讓我們無需依賴對 Wings 成功的 API 呼叫 就能完成某些操作。
這也代表可以在面板上對伺服器執行批次操作,這些伺服器會在 啟動時自動與 Wings 同步。
func (*Server) SyncInstallState ¶
SyncInstallState 向面板實例發出 HTTP 請求,通知其伺服器 已完成安裝流程,以及伺服器目前的狀態。
func (*Server) SyncWithConfiguration ¶
func (s *Server) SyncWithConfiguration(cfg remote.ServerConfigurationResponse) error
SyncWithConfiguration 接收伺服器的組態物件,並將所有數值與現有 伺服器狀態同步。此函式僅會替換伺服器現有的組態與處理程序組態, 不會影響底層環境。這是因為此函式有可能是在伺服器尚未完全初始化 的情境下被呼叫,因此諸如檔案系統與環境之類的其他項目 可能尚不存在。
func (*Server) SyncWithEnvironment ¶
func (s *Server) SyncWithEnvironment()
SyncWithEnvironment 更新伺服器的環境,使其與任何已變更的資料 保持一致。此函式會將新的設定與環境變數推送至環境中。此外, 也會呼叫環境上的就地更新方法,讓有支援此機制的環境(例如 Docker) 能夠立即套用某些設定,而不必等待伺服器重新啟動。
此功能讓伺服器的資源限制可以動態修改並立即套用, 從而支援動態資源分配,並能因應濫用資源的伺服器處理程序做出反應。
func (*Server) Throttler ¶
func (s *Server) Throttler() *ConsoleThrottle
Throttler 回傳此伺服器的節流器實例,若尚未建立則新建一個。
func (*Server) ToAPIResponse ¶
func (s *Server) ToAPIResponse() APIResponse
ToAPIResponse 將伺服器結構回傳為呼叫端可以使用的 API 物件。
func (*Server) UpdateConfigurationFiles ¶
func (s *Server) UpdateConfigurationFiles()
UpdateConfigurationFiles 自動更新伺服器所有已定義的組態檔, 確保它們永遠使用指定的數值。
func (*Server) Websockets ¶
func (s *Server) Websockets() *WebsocketBag
Websockets 回傳 websocket 連線集合,其中包含此伺服器實例 目前所有開啟中的 websocket 連線。
type WebsocketBag ¶
type WebsocketBag struct {
// contains filtered or unexported fields
}
func (*WebsocketBag) CancelAll ¶
func (w *WebsocketBag) CancelAll()
CancelAll 取消所有已儲存的取消函式,其效果是中斷此伺服器 每一個正在監聽中的 websocket。
func (*WebsocketBag) Len ¶
func (w *WebsocketBag) Len() int
func (*WebsocketBag) Push ¶
func (w *WebsocketBag) Push(u uuid.UUID, cancel *context.CancelFunc)
Push 將一個新的 websocket 連線加入堆疊的末端。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package transfer handles all logic related to transferring servers between two nodes.
|
Package transfer handles all logic related to transferring servers between two nodes. |