Skip to content

Commit ccbb7b9

Browse files
authored
feat: Support direct image access (#173)
When bootstrapping test infra, allow pulling from image registry when PAT is set. Clickup: https://app.clickup.com/t/869c079a0 --------- Signed-off-by: NautiluX <2600004+NautiluX@users.noreply.github.com> Co-authored-by: NautiluX <2600004+NautiluX@users.noreply.github.com>
1 parent 6644405 commit ccbb7b9

File tree

4 files changed

+125
-377
lines changed

4 files changed

+125
-377
lines changed

cli/cmd/bootstrap_gcp.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ func AddBootstrapGcpCmd(parent *cobra.Command, opts *GlobalOptions) {
7878
flags.StringVar(&bootstrapGcpCmd.InputRegistryType, "registry-type", "local-container", "Container registry type to use (options: local-container, artifact-registry) (default: artifact-registry)")
7979
flags.BoolVar(&bootstrapGcpCmd.CodesphereEnv.WriteConfig, "write-config", true, "Write generated install config to file (default: true)")
8080
flags.BoolVar(&bootstrapGcpCmd.SSHQuiet, "ssh-quiet", true, "Suppress SSH command output (default: true)")
81+
flags.StringVar(&bootstrapGcpCmd.CodesphereEnv.GitHubPAT, "github-pat", "", "GitHub Personal Access Token to use for direct image access. Scope required: package read (optional)")
82+
flags.StringVar(&bootstrapGcpCmd.CodesphereEnv.RegistryUser, "registry-user", "", "Custom Registry username (only for GitHub registry type) (optional)")
8183

8284
util.MarkFlagRequired(bootstrapGcpCmd.cmd, "project-name")
8385
util.MarkFlagRequired(bootstrapGcpCmd.cmd, "billing-account")
@@ -100,6 +102,12 @@ func (c *BootstrapGcpCmd) BootstrapGcp() error {
100102
}
101103

102104
c.CodesphereEnv.RegistryType = gcp.RegistryType(c.InputRegistryType)
105+
if c.CodesphereEnv.GitHubPAT != "" {
106+
c.CodesphereEnv.RegistryType = gcp.RegistryTypeGitHub
107+
if c.CodesphereEnv.RegistryUser == "" {
108+
return fmt.Errorf("registry-user must be set when using GitHub registry type")
109+
}
110+
}
103111

104112
err = bs.Bootstrap()
105113
envBytes, err2 := json.MarshalIndent(bs.Env, "", " ")
@@ -131,6 +139,14 @@ func (c *BootstrapGcpCmd) BootstrapGcp() error {
131139
log.Println(envString)
132140
log.Printf("Infrastructure details written to %s", infraFilePath)
133141
log.Printf("Start the Codesphere installation using OMS from the jumpbox host:\nssh-add $SSH_KEY_PATH; ssh -o StrictHostKeyChecking=no -o ForwardAgent=yes -o SendEnv=OMS_PORTAL_API_KEY root@%s", bs.Env.Jumpbox.GetExternalIP())
142+
packageName := "<package-name>-installer"
143+
installCmd := "oms-cli install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt"
144+
if gcp.RegistryType(bs.Env.RegistryType) == gcp.RegistryTypeGitHub {
145+
log.Printf("You set a GitHub PAT for direct image access. Make sure to use a lite package, as VM root disk sizes are reduced.")
146+
installCmd += " -s load-container-images"
147+
packageName += "-lite"
148+
}
149+
log.Printf("example install command:\n%s -p %s.tar.gz", installCmd, packageName)
134150

135151
return nil
136152
}

docs/oms-cli_beta_bootstrap-gcp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ oms-cli beta bootstrap-gcp [flags]
2626
--folder-id string GCP Folder ID (optional)
2727
--github-app-client-id string Github App Client ID (required)
2828
--github-app-client-secret string Github App Client Secret (required)
29+
--github-pat string GitHub Personal Access Token to use for direct image access. Scope required: package read (optional)
2930
-h, --help help for bootstrap-gcp
3031
--install-codesphere-version string Codesphere version to install (default: none)
3132
--install-config string Path to install config file (optional) (default "config.yaml")
3233
--preemptible Use preemptible VMs for Codesphere infrastructure (default: false)
3334
--project-name string Unique GCP Project Name (required)
3435
--region string GCP Region (default: europe-west4) (default "europe-west4")
3536
--registry-type string Container registry type to use (options: local-container, artifact-registry) (default: artifact-registry) (default "local-container")
37+
--registry-user string Custom Registry username (only for GitHub registry type) (optional)
3638
--secrets-dir string Directory for secrets (default: /etc/codesphere/secrets) (default "/etc/codesphere/secrets")
3739
--secrets-file string Path to secrets files (optional) (default "prod.vault.yaml")
3840
--ssh-private-key-path string SSH Private Key Path (default: ~/.ssh/id_rsa) (default "~/.ssh/id_rsa")

internal/bootstrap/gcp/gcp.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type RegistryType string
3030
const (
3131
RegistryTypeLocalContainer RegistryType = "local-container"
3232
RegistryTypeArtifactRegistry RegistryType = "artifact-registry"
33+
RegistryTypeGitHub RegistryType = "github"
3334
)
3435

3536
type VMDef struct {
@@ -81,6 +82,8 @@ type CodesphereEnvironment struct {
8182
GatewayIP string `json:"gateway_ip"`
8283
PublicGatewayIP string `json:"public_gateway_ip"`
8384
RegistryType RegistryType `json:"registry_type"`
85+
GitHubPAT string `json:"-"`
86+
RegistryUser string `json:"-"`
8487

8588
// Config
8689
InstallConfigPath string `json:"-"`
@@ -215,6 +218,13 @@ func (b *GCPBootstrapper) Bootstrap() error {
215218
}
216219
}
217220

221+
if b.Env.RegistryType == RegistryTypeGitHub {
222+
err = b.stlog.Step("Ensure GitHub access configured", b.EnsureGitHubAccessConfigured)
223+
if err != nil {
224+
return fmt.Errorf("failed to update install config: %w", err)
225+
}
226+
}
227+
218228
if b.Env.WriteConfig {
219229
err = b.stlog.Step("Update install config", b.UpdateInstallConfig)
220230
if err != nil {
@@ -563,6 +573,10 @@ func (b *GCPBootstrapper) EnsureComputeInstances() error {
563573
wg := sync.WaitGroup{}
564574
errCh := make(chan error, len(vmDefs))
565575
resultCh := make(chan vmResult, len(vmDefs))
576+
rootDiskSize := int64(200)
577+
if b.Env.RegistryType == RegistryTypeGitHub {
578+
rootDiskSize = 50
579+
}
566580
for _, vm := range vmDefs {
567581
wg.Add(1)
568582
go func(vm VMDef) {
@@ -574,7 +588,7 @@ func (b *GCPBootstrapper) EnsureComputeInstances() error {
574588
Type: protoString("PERSISTENT"),
575589
InitializeParams: &computepb.AttachedDiskInitializeParams{
576590
DiskType: &diskType,
577-
DiskSizeGb: protoInt64(200),
591+
DiskSizeGb: protoInt64(rootDiskSize),
578592
SourceImage: protoString("projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts"),
579593
},
580594
},
@@ -917,15 +931,28 @@ func (b *GCPBootstrapper) EnsureLocalContainerRegistry() error {
917931

918932
return nil
919933
}
934+
func (b *GCPBootstrapper) EnsureGitHubAccessConfigured() error {
935+
if b.Env.GitHubPAT == "" {
936+
return fmt.Errorf("GitHub PAT is not set")
937+
}
938+
b.Env.InstallConfig.Registry.Server = "ghcr.io"
939+
b.Env.InstallConfig.Registry.Username = b.Env.RegistryUser
940+
b.Env.InstallConfig.Registry.Password = b.Env.GitHubPAT
941+
b.Env.InstallConfig.Registry.ReplaceImagesInBom = false
942+
b.Env.InstallConfig.Registry.LoadContainerImages = false
943+
return nil
944+
}
920945

921946
func (b *GCPBootstrapper) UpdateInstallConfig() error {
922947
// Update install config with necessary values
923948
b.Env.InstallConfig.Datacenter.ID = b.Env.DatacenterID
924949
b.Env.InstallConfig.Datacenter.City = "Karlsruhe"
925950
b.Env.InstallConfig.Datacenter.CountryCode = "DE"
926951
b.Env.InstallConfig.Secrets.BaseDir = b.Env.SecretsDir
927-
b.Env.InstallConfig.Registry.ReplaceImagesInBom = true
928-
b.Env.InstallConfig.Registry.LoadContainerImages = true
952+
if b.Env.RegistryType != RegistryTypeGitHub {
953+
b.Env.InstallConfig.Registry.ReplaceImagesInBom = true
954+
b.Env.InstallConfig.Registry.LoadContainerImages = true
955+
}
929956

930957
if b.Env.InstallConfig.Postgres.Primary == nil {
931958
b.Env.InstallConfig.Postgres.Primary = &files.PostgresPrimaryConfig{
@@ -1245,12 +1272,22 @@ func (b *GCPBootstrapper) EnsureDNSRecords() error {
12451272
}
12461273

12471274
func (b *GCPBootstrapper) InstallCodesphere() error {
1248-
err := b.Env.Jumpbox.RunSSHCommand("root", "oms-cli download package "+b.Env.InstallCodesphereVersion)
1275+
packageFile := "installer.tar.gz"
1276+
skipSteps := ""
1277+
if b.Env.RegistryType == RegistryTypeGitHub {
1278+
skipSteps = " -s load-container-images"
1279+
packageFile = "installer-lite.tar.gz"
1280+
}
1281+
1282+
downloadCmd := "oms-cli download package -f " + packageFile + " " + b.Env.InstallCodesphereVersion
1283+
err := b.Env.Jumpbox.RunSSHCommand("root", downloadCmd)
12491284
if err != nil {
12501285
return fmt.Errorf("failed to download Codesphere package from jumpbox: %w", err)
12511286
}
12521287

1253-
err = b.Env.Jumpbox.RunSSHCommand("root", "oms-cli install codesphere -c /etc/codesphere/config.yaml -k "+b.Env.SecretsDir+"/age_key.txt -p "+b.Env.InstallCodesphereVersion+".tar.gz")
1288+
installCmd := fmt.Sprintf("oms-cli install codesphere -c /etc/codesphere/config.yaml -k %s/age_key.txt -p %s-%s%s",
1289+
b.Env.SecretsDir, b.Env.InstallCodesphereVersion, packageFile, skipSteps)
1290+
err = b.Env.Jumpbox.RunSSHCommand("root", installCmd)
12541291
if err != nil {
12551292
return fmt.Errorf("failed to install Codesphere from jumpbox: %w", err)
12561293
}

0 commit comments

Comments
 (0)