* 合并devcontainer web相关的源文件,简化目录结构 * devcontainer、ssh_key_pair和devcontainer.cloud * fixed bug:创建容器时Host为localhost时创建失败的问题 * 删除了死代码,更新了一些命名(主要是去掉devstar字符串) * 常量名全大写 * devcontainer HOST改为用户设置的域名或IP * 安装时如没有配置devcontainer则默认设置为docker方式 * 直接使用kubernetes和docker简化代码提高可读性 * 去除services/devstar_devcontainer文件夹名中不必要的devstar字符串 * 去除services/devstar_devcontainer文件夹名中不必要的devstar字符串 * 文件名中去掉不必要的devstar字符串 * 变量名中删除不必要的Devstar字符串 * Merge branch 'dev' into feature-websshAndUpdateImage * change pages style * change Structure * fix bug * websshAndUpdateImage
178 lines
5.3 KiB
Go
178 lines
5.3 KiB
Go
package setting
|
|
|
|
import (
|
|
"code.gitea.io/gitea/modules/log"
|
|
)
|
|
|
|
const (
|
|
KUBERNETES = "kubernetes" // 支持 "k8s" 和 "kubernetes"
|
|
DOCKER = "docker"
|
|
)
|
|
|
|
// 检查用户输入的 DevContainer Agent 是否有效
|
|
func isValidAgent(agent string) bool {
|
|
return agent == "k8s" || agent == KUBERNETES || agent == DOCKER
|
|
}
|
|
|
|
const (
|
|
CLOUD_PROVIDER_TENCENT = "tencent"
|
|
DEVCONTAINER_CLOUD_NAT_RULE_DESCRIPTION_PREFIX = "DevContainer: "
|
|
)
|
|
|
|
// validCloudProviderSet 私有 Set 结构,标识目前系统所有支持的 Cloud Provider 类型
|
|
var validCloudProviderSet = map[string]struct{}{
|
|
CLOUD_PROVIDER_TENCENT: {},
|
|
}
|
|
|
|
type DevcontainerType struct {
|
|
Enabled bool
|
|
Host string
|
|
Agent string
|
|
Namespace string
|
|
TimeoutSeconds int64
|
|
|
|
DefaultGitBranchName string
|
|
DefaultDevcontainerImageName string
|
|
DockerHost string
|
|
}
|
|
|
|
type SSHKeyPairType struct {
|
|
KeySize int
|
|
}
|
|
|
|
type CloudType struct {
|
|
Enabled bool
|
|
Provider string
|
|
Tencent CloudProviderTencentType `ini:"devcontainer.cloud.tencent"`
|
|
}
|
|
|
|
type CloudProviderTencentType struct {
|
|
Endpoint string
|
|
Region string
|
|
NatGatewayId string
|
|
PublicIpAddress string
|
|
PrivateIpAddress string
|
|
IpProtocol string
|
|
SecretId string
|
|
SecretKey string
|
|
}
|
|
|
|
var Devcontainer = DevcontainerType{
|
|
Enabled: false,
|
|
Namespace: "default",
|
|
TimeoutSeconds: 900, // Max wait time for DevContainer to be ready (blocking), default is 15 minutes, can be overridden by app.ini
|
|
|
|
DefaultGitBranchName: "main", // Default branch name for .devcontainer/devcontainer.json
|
|
|
|
DefaultDevcontainerImageName: "devstar.cn/public/base-ssh-devcontainer:ubuntu-20.04-20241014", // Default image if not specified
|
|
}
|
|
|
|
var SSHKeypair = SSHKeyPairType{
|
|
KeySize: 2048, // Size of the SSH key
|
|
}
|
|
|
|
var Cloud = CloudType{
|
|
Enabled: false, // Cloud feature toggle
|
|
}
|
|
|
|
// validateDevcontainerSettings 检查从 ini 配置文件中读取 DevStar DevContainer 配置信息,若数据无效,则自动禁用 DevContainer
|
|
func validateDevcontainerSettings() {
|
|
|
|
// 检查 Host 是否为空,若为空,则自动将 DevContainer 设置为禁用
|
|
if len(Devcontainer.Host) == 0 {
|
|
log.Warn("INVALID config 'host' for DevStar DevContainer")
|
|
Devcontainer.Enabled = false
|
|
}
|
|
|
|
// 检查用户输入的 DevContainer Agent 是否存在支持列表,若不支持,则将 DevContainer 设置为禁用
|
|
if !isValidAgent(Devcontainer.Agent) {
|
|
log.Warn("Invalid config 'agent' for DevStar DevContainer")
|
|
Devcontainer.Enabled = false
|
|
}
|
|
|
|
// 检查默认分支名称设置
|
|
if len(Devcontainer.DefaultGitBranchName) == 0 {
|
|
log.Warn("INVALID config 'DefaultGitBranchName' for DevStar DevContainer")
|
|
Devcontainer.Enabled = false
|
|
}
|
|
|
|
// 检查默认 DevContainer Image
|
|
if len(Devcontainer.DefaultDevcontainerImageName) == 0 {
|
|
log.Warn("INVALID config 'DefaultGitBranchNameDefaultDevcontainerImageName' for DevStar DevContainer")
|
|
Devcontainer.Enabled = false
|
|
}
|
|
|
|
if Devcontainer.Enabled == false {
|
|
log.Warn("DevStar DevContainer Service Disabled")
|
|
} else {
|
|
log.Info("DevStar DevContainer Service Enabled")
|
|
}
|
|
}
|
|
|
|
// validateSSHKeyPairSettings 检查从 ini 配置文件中读取 DevStar SSH Key Pair 配置信息
|
|
func validateSSHKeyPairSettings() {
|
|
if SSHKeypair.KeySize < 1024 {
|
|
SSHKeypair.KeySize = 1024
|
|
}
|
|
}
|
|
|
|
// validateDevcontainerCloudSettings 检查从 ini 配置文件中读取 DevStar Cloud 配置信息
|
|
func validateDevcontainerCloudSettings() {
|
|
switch Cloud.Provider {
|
|
case CLOUD_PROVIDER_TENCENT:
|
|
// 腾讯云配置检查
|
|
|
|
if len(Cloud.Tencent.NatGatewayId) < 4 {
|
|
log.Warn("INVALID NAT Gateway ID '%v' for DevStar Cloud Provider Tencent", Cloud.Tencent.NatGatewayId)
|
|
Cloud.Enabled = false
|
|
}
|
|
|
|
if Cloud.Tencent.IpProtocol != "TCP" && Cloud.Tencent.IpProtocol != "UDP" {
|
|
log.Warn("INVALID IP Protocol '%v' for DevStar Cloud Provider Tencent", Cloud.Tencent.IpProtocol)
|
|
Cloud.Enabled = false
|
|
}
|
|
|
|
if len(Cloud.Tencent.Region) < 3 || len(Cloud.Tencent.Endpoint) == 0 {
|
|
log.Warn("INVALID (Region, Endpoint) pair ('%v', '%v') for DevStar Cloud Provider Tencent",
|
|
Cloud.Tencent.Region, Cloud.Tencent.Endpoint)
|
|
Cloud.Enabled = false
|
|
}
|
|
|
|
if len(Cloud.Tencent.PrivateIpAddress) == 0 || len(Cloud.Tencent.PublicIpAddress) == 0 {
|
|
log.Warn("INVALID (PublicIpAddress, PrivateIpAddress) pair ('%v', '%v') for DevStar Cloud Provider Tencent",
|
|
Cloud.Tencent.PublicIpAddress, Cloud.Tencent.PrivateIpAddress)
|
|
Cloud.Enabled = false
|
|
}
|
|
|
|
if len(Cloud.Tencent.SecretId) == 0 || len(Cloud.Tencent.SecretKey) == 0 {
|
|
log.Warn("INVALID (SecretId, SecretKey) pair for DevStar Cloud Provider Tencent")
|
|
Cloud.Enabled = false
|
|
}
|
|
|
|
default:
|
|
// 无效 Cloud Provider 名称
|
|
log.Warn("INVALID config '%v' for DevStar Cloud", Cloud.Provider)
|
|
Cloud.Enabled = false
|
|
}
|
|
|
|
if Cloud.Enabled == false {
|
|
log.Warn("DevStar Cloud Provider Service Disabled")
|
|
} else {
|
|
log.Info("DevStar Cloud Provider '%v' Enabled", Cloud.Provider)
|
|
}
|
|
|
|
}
|
|
|
|
func loadDevcontainerFrom(rootCfg ConfigProvider) {
|
|
mustMapSetting(rootCfg, "devcontainer", &Devcontainer)
|
|
validateDevcontainerSettings()
|
|
|
|
mustMapSetting(rootCfg, "ssh_key_pair", &SSHKeypair)
|
|
validateSSHKeyPairSettings()
|
|
|
|
if Devcontainer.Agent == "k8s" || Devcontainer.Agent == KUBERNETES {
|
|
mustMapSetting(rootCfg, "devcontainer.cloud", &Cloud)
|
|
validateDevcontainerCloudSettings()
|
|
}
|
|
}
|