Skip to content

Commit

Permalink
Enable VLAB in CI (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostman authored Sep 28, 2023
1 parent e15a175 commit f6f1f52
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,72 @@ jobs:
with:
limit-access-to-actor: true

vlab:
runs-on: ["vlab"]
# needs:
# - cache-tools

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: stable
cache: true

- name: Setup git for private Go modules
env:
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
run: git config --global url.https://[email protected]/.insteadOf https://github.com/

- name: Build hhfab
run: |
make hhfab
- name: Login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to lab registry
uses: docker/login-action@v3
with:
registry: m.l.hhdev.io:31000
username: lab
password: ${{ secrets.LAB_REGISTRY_TOKEN }}

- name: hhfab init and build
run: |
export XDG_CONFIG_HOME=$(pwd)/.config
export HHFAB_BASE_REPO=m.l.hhdev.io:31000/githedgehog
bin/hhfab init -p vlab -v
bin/hhfab build -v
ls -lah .hhfab
bin/hhfab vlab up -v --dry-run
sudo bin/hhfab vlab up -v --run-complete hack/vlab-test.sh
- name: Setup tmate session for debug
if: ${{ failure() && github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
uses: mxschmitt/action-tmate@v3
timeout-minutes: 30
with:
limit-access-to-actor: true

publish:
# runs-on: ["lab", "simple", "jammy"]
runs-on: ubuntu-latest
if: startsWith(github.event.ref, 'refs/tags/') && github.event_name == 'push'
needs:
# - cache-tools
- build
- vlab

steps:
- name: Checkout repository
Expand Down
11 changes: 10 additions & 1 deletion cmd/hhfab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ func main() {
Name: "compact",
Usage: "run more lightweight vms, small risks",
},
&cli.BoolFlag{
Name: "install-complete",
Usage: "run installer and complete vlab (for testing)",
},
&cli.StringFlag{
Name: "run-complete",
Usage: "run installer, run provided script and than complete vlab (for testing)",
},
},
Before: func(ctx *cli.Context) error {
return setupLogger(verbose, brief)
Expand All @@ -284,7 +292,8 @@ func main() {

killStaleVMs := cCtx.Bool("kill-stale-vms")

return errors.Wrap(svc.StartServer(killStaleVMs, cCtx.Bool("compact")), "error starting vlab")
return errors.Wrap(svc.StartServer(killStaleVMs, cCtx.Bool("compact"),
cCtx.Bool("install-complete"), cCtx.String("run-complete")), "error starting vlab")
},
},
{
Expand Down
13 changes: 13 additions & 0 deletions hack/vlab-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -ex

function control {
bin/hhfab vlab ssh --vm control-1 PATH=/opt/bin "$@"
}

control kubectl fabric vpc create --name vpc-1 --subnet 10.90.0.1/24
control kubectl fabric vpc attach --vpc vpc-1 --conn server-1--mclag--switch-1--switch-2
control kubectl fabric vpc attach --vpc vpc-1 --conn server-2--mclag--switch-1--switch-2

control kubectl get agent -o wide
6 changes: 5 additions & 1 deletion pkg/fab/vlab/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type Service struct {
type Config struct {
DryRun bool
Compact bool
InstallComplete bool
RunComplete string
Basedir string
Wiring *wiring.Data
ControlIgnition string
Expand Down Expand Up @@ -173,8 +175,10 @@ func (svc *Service) checkForStaleVMs(ctx context.Context, killStaleVMs bool) err
return nil
}

func (svc *Service) StartServer(killStaleVMs bool, compact bool) error {
func (svc *Service) StartServer(killStaleVMs bool, compact bool, installComplete bool, runComplete string) error {
svc.cfg.Compact = compact
svc.cfg.InstallComplete = installComplete
svc.cfg.RunComplete = runComplete

for _, cmd := range RequiredCommands {
_, err := exec.LookPath(cmd)
Expand Down
41 changes: 33 additions & 8 deletions pkg/fab/vlab/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (vm *VM) Run(ctx context.Context, eg *errgroup.Group) {

func (vm *VM) RunVM(ctx context.Context) func() error {
return func() error {
if vm.Cfg.DryRun {
return nil
}

// <-vm.TPMReady
time.Sleep(3 * time.Second) // TODO please, no!

Expand Down Expand Up @@ -171,10 +175,6 @@ func (vm *VM) RunVM(ctx context.Context) func() error {
return errors.Errorf("error running control vm: no ssh port found")
}

if vm.Cfg.DryRun {
return nil
}

return errors.Wrapf(execCmd(ctx, vm.Basedir, true, "qemu-system-x86_64", []string{}, args...), "error running vm")
}
}
Expand All @@ -185,12 +185,12 @@ func (vm *VM) RunInstall(ctx context.Context) func() error {
return nil
}

if vm.Cfg.DryRun {
if vm.Installed.Is() {
slog.Debug("Control node is already installed", "name", vm.Name)
return nil
}

if vm.Installed.Is() {
slog.Debug("Control node is already installed", "name", vm.Name)
if vm.Cfg.DryRun {
return nil
}

Expand Down Expand Up @@ -256,6 +256,27 @@ func (vm *VM) RunInstall(ctx context.Context) func() error {
return errors.Wrapf(err, "error marking control node as installed")
}

if vm.Cfg.InstallComplete {
// TODO do graceful shutdown
slog.Info("Exiting after control node installation as requested")
os.Exit(0)
}

if vm.Cfg.RunComplete != "" {
slog.Info("Running script after control node installation as requested")

err = execCmd(ctx, "", false, vm.Cfg.RunComplete, []string{
"KUBECONFIG=" + filepath.Join(vm.Cfg.Basedir, "kubeconfig.yaml"),
})
if err != nil {
return errors.Wrapf(err, "error running script after control node installation")
}

// TODO do graceful shutdown
slog.Info("Exiting after script after control node installation as requested")
os.Exit(0)
}

return nil
}

Expand All @@ -271,6 +292,10 @@ func (vm *VM) RunInstall(ctx context.Context) func() error {

func (vm *VM) RunTPM(ctx context.Context) func() error {
return func() error {
if vm.Cfg.DryRun {
return nil
}

err := execCmd(ctx, vm.Basedir, true, "swtpm", []string{}, "socket", "--tpm2", "--tpmstate", "dir=tpm",
"--ctrl", "type=unixio,path=tpm.sock.ctrl", "--server", "type=unixio,path=tpm.sock", "--pid", "file=tpm.pid",
"--log", "level=1", "--flags", "startup-clear")
Expand Down Expand Up @@ -492,7 +517,7 @@ func execCmd(ctx context.Context, basedir string, quiet bool, name string, env [

outputs := []io.Writer{logFile}

if !quiet {
if !quiet || slog.Default().Enabled(ctx, slog.LevelDebug) {
outputs = append(outputs, os.Stdout)
}

Expand Down

0 comments on commit f6f1f52

Please sign in to comment.