Documentation
¶
Index ¶
- Constants
- func ConfigureDocker(ctx context.Context) error
- func Docker() (*client.Client, error)
- type Allocations
- type Configuration
- func (c *Configuration) Allocations() Allocations
- func (c *Configuration) EnvironmentVariables() []string
- func (c *Configuration) Labels() map[string]string
- func (c *Configuration) Limits() Limits
- func (c *Configuration) Mounts() []Mount
- func (c *Configuration) SetEnvironmentVariables(ev []string)
- func (c *Configuration) SetSettings(s Settings)
- type Limits
- type Mount
- type NetworkStats
- type ProcessEnvironment
- type Settings
- type Stats
- type Variables
Constants ¶
const ( StateChangeEvent = "state change" ResourceEvent = "resources" DockerImagePullStarted = "docker image pull started" DockerImagePullStatus = "docker image pull status" DockerImagePullCompleted = "docker image pull completed" )
const ( ProcessOfflineState = "offline" ProcessStartingState = "starting" ProcessRunningState = "running" ProcessStoppingState = "stopping" )
Variables ¶
This section is empty.
Functions ¶
func ConfigureDocker ¶
ConfigureDocker 為 docker 環境設定所需的網路。
Types ¶
type Allocations ¶
type Allocations struct {
// ForceOutgoingIP 會導致為伺服器建立一個專用的橋接網路,並使用
// 特殊選項,讓 Docker 將傳出流量 SNAT 至 DefaultMapping 的 IP。
// 這對於依賴外部服務檢查伺服器 IP 的伺服器來說很重要
// (例如 Source Engine 伺服器)。
ForceOutgoingIP bool `json:"force_outgoing_ip"`
// 定義此伺服器應使用的預設配置(分配)。在修改設定檔或伺服器的
// 啟動參數時,這就是 {SERVER_IP} 與 {SERVER_PORT} 所使用的值。
DefaultMapping struct {
Ip string `json:"ip"`
Port int `json:"port"`
} `json:"default"`
// Mappings 包含所有應指派給特定伺服器的連接埠,並依照其對應的
// IP 附加在一起。
Mappings map[string][]int `json:"mappings"`
}
Allocations 定義了給定伺服器可用的配置(分配)。當使用 Docker 環境 驅動程式時,這些會對應到允許外部連線的容器連接埠映射。
func (*Allocations) Bindings ¶
func (a *Allocations) Bindings() nat.PortMap
Bindings 將伺服器的配置(分配)映射轉換成 Docker 可以理解的格式。雖然 我們力求支援多種環境,但使用 Docker 標準化的綁定格式,確實讓管理 這些事情變得輕鬆一些。
如果你需要將 127.0.0.1 重新映射到 Docker 介面,可以使用 DockerBindings()。
func (*Allocations) DockerBindings ¶
func (a *Allocations) DockerBindings() nat.PortMap
DockerBindings 以 Docker 能正確支援的方式回傳伺服器的綁定。這會將 任何對 127.0.0.1 的參照,替換成 pterodactyl0 網路介面的 IP,讓伺服器 能夠在本機位址上運作,同時仍可被其他容器存取。
func (*Allocations) Exposed ¶
func (a *Allocations) Exposed() nat.PortSet
Exposed 將伺服器的配置(分配)映射轉換成 Docker 能理解的 PortSet。 這個格式與「Bindings」略有不同,因為它應該回傳空結構而不是綁定。
為了達成這點,我們只需從「DockerBindings」取得值,然後將它們設為 空結構即可。反正也沒理由不這麼做。
type Configuration ¶
type Configuration struct {
// contains filtered or unexported fields
}
Configuration 定義了環境的實際設定結構,其中包含了所有 定義於其中的設定。
func NewConfiguration ¶
func NewConfiguration(s Settings, envVars []string) *Configuration
NewConfiguration 回傳一個新的環境設定,其中包含指定的設定與 環境變數。
func (*Configuration) Allocations ¶
func (c *Configuration) Allocations() Allocations
Allocations 回傳與此環境相關聯的配置(分配)。
func (*Configuration) EnvironmentVariables ¶
func (c *Configuration) EnvironmentVariables() []string
EnvironmentVariables 回傳與此實例相關聯的環境變數。
func (*Configuration) Labels ¶
func (c *Configuration) Labels() map[string]string
Labels 回傳與此實例相關聯的容器標籤。
func (*Configuration) SetEnvironmentVariables ¶
func (c *Configuration) SetEnvironmentVariables(ev []string)
SetEnvironmentVariables 藉由以新的陣列取代整個環境變數陣列, 更新與此環境相關聯的環境變數。
func (*Configuration) SetSettings ¶
func (c *Configuration) SetSettings(s Settings)
SetSettings 即時更新此環境的設定結構。這讓已修改的伺服器 能夠自動將這些變更推送到環境中。
type Limits ¶
type Limits struct {
// 此伺服器在主機系統上允許使用的記憶體總量,單位為 MiB。
MemoryLimit int64 `json:"memory_limit"`
// 提供給容器實例的額外置換(swap)空間量。
Swap int64 `json:"swap"`
// 容器中 IO 操作的相對權重。此值是相對於系統上其他容器而言,
// 應介於 10 到 1000 之間。
IoWeight uint16 `json:"io_weight"`
// 此實例相對於主機所允許使用的 CPU 百分比。200% 的值代表完全
// 使用兩個核心。此值應介於 1 到 THREAD_COUNT * 100 之間。
CpuLimit int64 `json:"cpu_limit"`
// 伺服器允許使用的磁碟空間量,單位為 MB。
DiskSpace int64 `json:"disk_space"`
// 設定 docker 實例可以使用哪些 CPU 執行緒。
Threads string `json:"threads"`
OOMDisabled bool `json:"oom_disabled"`
}
Limits 是給定伺服器的建置設定,會影響 docker 容器的建立以及 伺服器實例的資源限制。
func (Limits) AsContainerResources ¶
AsContainerResources 以 Docker 能理解的格式回傳容器的可用資源。
func (Limits) BoundedMemoryLimit ¶
func (Limits) ConvertedCpuLimit ¶
ConvertedCpuLimit 將伺服器建置設定的 CPU 限制轉換成 Docker 環境 更能理解的數字。若未設定限制,則回傳 -1,向 Docker 表示該伺服器 擁有無限制的 CPU 配額。
func (Limits) ConvertedSwap ¶
ConvertedSwap 以位元組總量的形式回傳可用的置換(swap)空間量。這是 以伺服器最初可用的記憶體量,加上要額外納入的置換空間量所計算而得, 這也是 Docker 所使用的格式。
func (Limits) MemoryOverheadMultiplier ¶
MemoryOverheadMultiplier 將記憶體使用量的硬限制設為比指派給伺服器的 記憶體量多 5%。若伺服器的記憶體限制小於 4G,則使用 10%,若小於 2G 則使用 15%。這可以避免像 Java 這類會超出限制執行的程序發生 意外的當機。
func (Limits) ProcessLimit ¶
ProcessLimit 回傳容器的程序數量限制。目前這是在系統層級定義的, 而非依照每個伺服器個別設定。
type Mount ¶
type Mount struct {
// 在 Docker 環境中這沒有差別,但在非 Docker 環境中,你應該將
// 「Default」掛載視為伺服器的根目錄。其他所有掛載都只是在此
// 基礎上額外附加的,通常是像共用地圖或時區資料之類的東西。
Default bool `json:"-"`
// 系統上的目標路徑。對所有伺服器的 Default 掛載來說,這會是
// 「/home/container」,但在非容器環境中,你大概可以忽略目標,
// 只需處理 source 即可。
Target string `json:"target"`
// 讀取檔案來源的目錄。在 Docker 環境中,這是我們要掛載到容器中
// Target 位置的目錄。
Source string `json:"source"`
// 此目錄是否以唯讀方式掛載。是否正確處理此值並確保符合安全性
// 要求,取決於各個環境自行實作。
ReadOnly bool `json:"read_only"`
}
type NetworkStats ¶
type ProcessEnvironment ¶
type ProcessEnvironment interface {
// Type 回傳此環境的名稱。
Type() string
// Config 將環境設定回傳給呼叫端。
Config() *Configuration
// Events 回傳一個事件發射器實例,可用來監聽此環境所觸發的
// 各種事件。此方法不應允許外部發布事件,只能訂閱事件。
Events() *events.Bus
// Exists 判斷伺服器實例是否存在。舉例來說,在 docker 環境中,
// 這應該要確認容器已建立且處於可啟動的狀態。在
// 基本的 CLI 環境中,這裡大概可以直接回傳 true。
Exists() (bool, error)
// IsRunning 判斷此環境目前是否正在為這個特定的伺服器實例
// 執行伺服器程序。
IsRunning(ctx context.Context) (bool, error)
// InSituUpdate 在不實際停止伺服器程序的情況下,更新伺服器的資源限制。
// 只有在該環境支援此功能時才會執行,否則將不做任何事。
InSituUpdate() error
// OnBeforeStart 會在環境啟動前執行。若回傳錯誤,則啟動將
// 不會發生,否則將正常繼續。
OnBeforeStart(ctx context.Context) error
// Start 啟動一個伺服器實例。若伺服器實例目前的狀態
// 無法啟動,應回傳錯誤。
Start(ctx context.Context) error
// Stop 停止一個伺服器實例。若伺服器已經處於停止狀態,
// 則不會回傳錯誤,此函式將視為無動作。
Stop(ctx context.Context) error
// WaitForStop 等待伺服器實例正常地停止。若在經過 "duration" 之後
// 仍偵測到伺服器在執行,則會依照第二個參數的值回傳錯誤,或是將
// 伺服器強制終止。若提供的 context 被取消,底層的等待條件將會
// 停止,整個迴圈也將結束(可能在尚未停止或終止的情況下)。
WaitForStop(ctx context.Context, duration time.Duration, terminate bool) error
// Terminate 使用提供的訊號停止正在執行的伺服器實例。若伺服器
// 已經處於停止狀態,此函式將不做任何事。
Terminate(ctx context.Context, signal string) error
// Destroy 銷毀此環境,移除所有已建立的容器(至少在 Docker
// 環境中是如此)。
Destroy() error
// ExitState 回傳此程序的結束狀態。第一個回傳值為結束代碼,第二個
// 則表示此程序是否被系統的 OOM killer 所終止。
ExitState() (uint32, bool, error)
// Create 建立執行伺服器程序所需的環境。舉例來說,
// 在 Docker 環境中,create 將會為伺服器建立一個新的
// 容器實例。
Create() error
// Attach 附加到伺服器主控台環境,並允許將輸出透過管線
// 傳送到 websocket 或其他內部工具以監控輸出。之後也能夠
// 傳送資料進入該環境的 stdin。
Attach(ctx context.Context) error
// SendCommand 將提供的指令傳送給正在執行的伺服器實例。
SendCommand(string) error
// Readlog 從尾端往回讀取此程序的日誌檔案,直到達到提供的
// 行數為止。
Readlog(int) ([]string, error)
// State 回傳此環境目前的狀態。
State() string
// SetState 設定此環境目前的狀態。一般來說應該讓環境
// 自行處理,但在某些情況下,讓伺服器從外部更新狀態
// 會有所助益(例如 starting -> started)。
SetState(string)
// Uptime 回傳此環境目前的正常執行時間(以毫秒為單位),也就是
// 自上次啟動以來所經過的時間。
Uptime(ctx context.Context) (int64, error)
// SetLogCallback 設定容器日誌輸出將傳送到的回呼函式。
SetLogCallback(func([]byte))
}
ProcessEnvironment 定義了所有環境都必須實作的基本介面, 以便伺服器能夠被正確地控制。
type Settings ¶
type Settings struct {
Mounts []Mount
Allocations Allocations
Limits Limits
Labels map[string]string
}
type Stats ¶
type Stats struct {
// 此伺服器實例正在消耗的記憶體總量,單位為位元組。此值的計算方式
// 與直接使用容器回傳的原始 Memory 欄位略有不同,詳情請查看設定
// 此值的程式碼是如何計算的。
Memory uint64 `json:"memory_bytes"`
// 此容器或資源可使用的記憶體總量。在 Docker 中,這個值會比你
// 預期的還要高,因為我們會自動為容器分配額外的開銷空間,因此
// 不會完全精確對應。
MemoryLimit uint64 `json:"memory_limit_bytes"`
// 絕對 CPU 使用量是相對於整個系統所使用的 CPU 量,並不考慮
// 伺服器程序本身的任何限制。
CpuAbsolute float64 `json:"cpu_absolute"`
// 容器目前的網路傳入與傳出流量。
Network NetworkStats `json:"network"`
// 容器目前的正常執行時間,單位為毫秒。
Uptime int64 `json:"uptime"`
}
Stats 定義了給定伺服器實例目前的資源使用情況。