Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add target for second machine and introduce different naming convention #183

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 94 additions & 67 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ MAX_RETRIES := 30

# Machine flavors
ifeq ($(MINI_LAB_FLAVOR),cumulus)
LAB_MACHINES=machine01,machine02
LAB_TOPOLOGY=mini-lab.cumulus.yaml
VRF=vrf20
else ifeq ($(MINI_LAB_FLAVOR),sonic)
LAB_MACHINES=machine01,machine02
LAB_TOPOLOGY=mini-lab.sonic.yaml
VRF=Vrf20
else
Expand All @@ -40,16 +38,17 @@ KIND_ARGS=--image kindest/node:v$(K8S_VERSION)
endif

ifeq ($(CI),true)
DOCKER_COMPOSE_TTY_ARG=-T
METALCTL=docker compose run --no-TTY metalctl
else
DOCKER_COMPOSE_TTY_ARG=
METALCTL=docker compose run --rm metalctl
endif

.PHONY: up
up: env control-plane-bake partition-bake
@chmod 600 files/ssh/id_rsa
docker compose up --remove-orphans --force-recreate control-plane partition
@$(MAKE) --no-print-directory start-machines
@$(MAKE) --no-print-directory start-vm01
@$(MAKE) --no-print-directory start-vm02
# for some reason an allocated machine will not be able to phone home
# without restarting the metal-core
# TODO: should be investigated and fixed if possible
Expand Down Expand Up @@ -124,24 +123,37 @@ cleanup-partition:

.PHONY: _privatenet
_privatenet: env
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network list --name user-private-network | grep user-private-network || docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network allocate --partition mini-lab --project 00000000-0000-0000-0000-000000000001 --name user-private-network
$(METALCTL) network list --name user-private-network | grep user-private-network || $(METALCTL) network allocate --partition mini-lab --project 00000000-0000-0000-0000-000000000001 --name user-private-network

.PHONY: _public_ips
_public_ips: env
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network ip list --name firewall | grep firewall || docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network ip create --network internet-mini-lab --project 00000000-0000-0000-0000-000000000001 --ipaddress 203.0.113.129 --name firewall
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network ip list --name machine | grep machine || docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network ip create --network internet-mini-lab --project 00000000-0000-0000-0000-000000000001 --ipaddress 203.0.113.130 --name machine
define create_public_ip
$(METALCTL) network ip list --name $(1) | grep $(1) || $(METALCTL) network ip create --network internet-mini-lab --project 00000000-0000-0000-0000-000000000001 --ipaddress $(2) --name $(1)
endef

.PHONY: machine
machine: _privatenet _public_ips
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl machine create --description test --name test --hostname test --project 00000000-0000-0000-0000-000000000001 --partition mini-lab --image $(MACHINE_OS) --size v1-small-x86 --userdata "@/tmp/ignition.json" --ips 203.0.113.130 --networks internet-mini-lab,$(shell docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network list --name user-private-network -o template --template '{{ .id }}')
define create_common_args
--description $(1) --name $(1) --hostname $(1) --project 00000000-0000-0000-0000-000000000001 --partition mini-lab --size v1-small-x86 --userdata "@/tmp/ignition.json"
endef

.PHONY: firewall
firewall: _privatenet _public_ips
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl firewall create --description fw --name fw --hostname fw --project 00000000-0000-0000-0000-000000000001 --partition mini-lab --image firewall-ubuntu-3.0 --size v1-small-x86 --userdata "@/tmp/ignition.json" --ips 203.0.113.129 --firewall-rules-file=/tmp/rules.yaml --networks internet-mini-lab,$(shell docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl network list --name user-private-network -o template --template '{{ .id }}')
firewall: _privatenet
$(call create_public_ip,firewall,203.0.113.129)
$(METALCTL) firewall create $(call create_common_args,firewall) --firewall-rules-file=/tmp/rules.yaml --image firewall-ubuntu-3.0 --ips 203.0.113.129 \
--networks internet-mini-lab,$(shell $(METALCTL) network list --name user-private-network -o template --template '{{ .id }}')

.PHONY: machine01
machine01: _privatenet
$(call create_public_ip,machine01,203.0.113.130)
$(METALCTL) machine create $(call create_common_args,machine01) --image $(MACHINE_OS) --ips 203.0.113.130 \
--networks internet-mini-lab,$(shell $(METALCTL) network list --name user-private-network -o template --template '{{ .id }}')

.PHONY: machine02
machine02: _privatenet
$(call create_public_ip,machine02,203.0.113.131)
$(METALCTL) machine create $(call create_common_args,machine02) --image $(MACHINE_OS) --ips 203.0.113.131 \
--networks internet-mini-lab,$(shell $(METALCTL) network list --name user-private-network -o template --template '{{ .id }}')

.PHONY: ls
ls: env
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl machine ls
$(METALCTL) machine ls

## SWITCH MANAGEMENT ##

Expand All @@ -166,84 +178,99 @@ ssh-leaf01:
ssh-leaf02:
ssh -o StrictHostKeyChecking=no -o "PubkeyAcceptedKeyTypes +ssh-rsa" -i files/ssh/id_rsa root@leaf02

## MACHINE MANAGEMENT ##
## MACHINE VM MANAGEMENT ##

.PHONY: start-machines
start-machines:
docker exec vms /mini-lab/manage_vms.py --names $(LAB_MACHINES) create
.PHONY: start-vm01
start-vm01:
docker exec vms /mini-lab/manage_vms.py --names vm01 create

.PHONY: start-vm02
start-vm02:
docker exec vms /mini-lab/manage_vms.py --names vm02 create

.PHONY: start-vm03
start-vm03:
docker exec vms /mini-lab/manage_vms.py --names vm03 create

.PHONY: _password
_password: env
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl machine consolepassword $(MACHINE_UUID)
$(METALCTL) machine consolepassword $(MACHINE_UUID)

.PHONY: password-machine01
password-machine01:
@$(MAKE) --no-print-directory _password MACHINE_UUID=e0ab02d2-27cd-5a5e-8efc-080ba80cf258
.PHONY: password-vm01
password-vm01:
@$(MAKE) --no-print-directory _password MACHINE_UUID=e0ab02d2-27cd-5a5e-8efc-080ba80cf258

.PHONY: password-machine02
password-machine02:
@$(MAKE) --no-print-directory _password MACHINE_UUID=2294c949-88f6-5390-8154-fa53d93a3313
.PHONY: password-vm02
password-vm02:
@$(MAKE) --no-print-directory _password MACHINE_UUID=2294c949-88f6-5390-8154-fa53d93a3313

.PHONY: password-machine03
password-machine03:
@$(MAKE) --no-print-directory _password MACHINE_UUID=2a92f14d-d3b1-4d46-b813-5d058103743e
.PHONY: password-vm03
password-vm03:
@$(MAKE) --no-print-directory _password MACHINE_UUID=2a92f14d-d3b1-4d46-b813-5d058103743e

.PHONY: _free-machine
_free-machine: env
docker compose run $(DOCKER_COMPOSE_TTY_ARG) metalctl machine rm $(MACHINE_UUID)
docker exec vms /mini-lab/manage_vms.py --names $(MACHINE_NAME) kill --with-disks
docker exec vms /mini-lab/manage_vms.py --names $(MACHINE_NAME) create
.PHONY: _free_vm
_free_vm: env
$(METALCTL) machine rm $(MACHINE_UUID)
docker exec vms /mini-lab/manage_vms.py --names $(VM_NAME) kill --with-disks
docker exec vms /mini-lab/manage_vms.py --names $(VM_NAME) create

.PHONY: free-machine01
free-machine01:
@$(MAKE) --no-print-directory _free-machine MACHINE_NAME=machine01 MACHINE_UUID=e0ab02d2-27cd-5a5e-8efc-080ba80cf258
.PHONY: free-vm01
free-vm01:
@$(MAKE) --no-print-directory _free_vm VM_NAME=vm01 MACHINE_UUID=e0ab02d2-27cd-5a5e-8efc-080ba80cf258

.PHONY: free-machine02
free-machine02:
@$(MAKE) --no-print-directory _free-machine MACHINE_NAME=machine02 MACHINE_UUID=2294c949-88f6-5390-8154-fa53d93a3313
.PHONY: free-vm02
free-vm02:
@$(MAKE) --no-print-directory _free_vm VM_NAME=vm02 MACHINE_UUID=2294c949-88f6-5390-8154-fa53d93a3313

.PHONY: free-machine03
free-machine03:
@$(MAKE) --no-print-directory _free-machine MACHINE_NAME=machine03 MACHINE_UUID=2a92f14d-d3b1-4d46-b813-5d058103743e
.PHONY: free-vm03
free-vm03:
@$(MAKE) --no-print-directory _free_vm VM_NAME=vm03 MACHINE_UUID=2a92f14d-d3b1-4d46-b813-5d058103743e

.PHONY: _console-machine
_console-machine:
.PHONY: _console-vm
_console-vm:
@echo "exit console with CTRL+5 and then quit telnet through q + ENTER"
@docker exec -it vms telnet 127.0.0.1 $(CONSOLE_PORT)

.PHONY: console-machine01
console-machine01:
@$(MAKE) --no-print-directory _console-machine CONSOLE_PORT=4000
.PHONY: console-vm01
console-vm01:
@$(MAKE) --no-print-directory _console-vm CONSOLE_PORT=4000

.PHONY: console-machine02
console-machine02:
@$(MAKE) --no-print-directory _console-machine CONSOLE_PORT=4001
.PHONY: console-vm02
console-vm02:
@$(MAKE) --no-print-directory _console-vm CONSOLE_PORT=4001

.PHONY: console-machine03
console-machine03:
@$(MAKE) --no-print-directory _console-machine CONSOLE_PORT=4002
.PHONY: console-vm03
console-vm03:
@$(MAKE) --no-print-directory _console-vm CONSOLE_PORT=4002

## SSH TARGETS FOR MACHINES ##
# Python code could be replaced by jq, but it is not preinstalled on Cumulus
define get-ipv6-link-local-address
$(shell ssh -F files/ssh/config leaf01 "vtysh -c 'show bgp $(if $(2),vrf $(2) )neighbors $(1) json' | \
python3 -c 'import sys, json; data = json.load(sys.stdin); key = next(iter(data)); print(data[key][\"bgpNeighborAddr\"] + \"%\" + key)'" \
)
endef

.PHONY: ssh-firewall
ssh-firewall:
$(eval fw = $(shell ssh -F files/ssh/config leaf01 "vtysh -c 'show bgp neighbors fw json' | \
python3 -c 'import sys, json; data = json.load(sys.stdin); key = next(iter(data)); print(data[key][\"bgpNeighborAddr\"] + \"%\" + key)'" \
))
ssh -F files/ssh/config $(fw) $(COMMAND)

.PHONY: ssh-machine
ssh-machine:
$(eval machine = $(shell ssh -F files/ssh/config leaf01 "vtysh -c 'show bgp vrf $(VRF) neighbors test json' | \
python3 -c 'import sys, json; data = json.load(sys.stdin); key = next(iter(data)); print(data[key][\"bgpNeighborAddr\"] + \"%\" + key)'" \
))
ssh -F files/ssh/config $(machine) $(COMMAND)
$(eval address = $(call get-ipv6-link-local-address,firewall))
ssh -F files/ssh/config $(address) $(COMMAND)

.PHONY: ssh-machine01
ssh-machine01:
$(eval address = $(call get-ipv6-link-local-address,machine01,$(VRF)))
ssh -F files/ssh/config $(address) $(COMMAND)

.PHONY: ssh-machine02
ssh-machine02:
$(eval address = $(call get-ipv6-link-local-address,machine02,$(VRF)))
ssh -F files/ssh/config $(address) $(COMMAND)

.PHONY: connect-to-cloudflare
connect-to-cloudflare:
@echo "Attempting to connect to Cloudflare..."
@for i in $$(seq 1 $(MAX_RETRIES)); do \
if $(MAKE) ssh-machine COMMAND="sudo curl --connect-timeout 1 --fail --silent https://1.1.1.1" > /dev/null 2>&1; then \
if $(MAKE) ssh-machine01 COMMAND="sudo curl --connect-timeout 1 --fail --silent https://1.1.1.1" > /dev/null 2>&1; then \
echo "Connected successfully"; \
exit 0; \
else \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ docker compose run --rm metalctl machine reinstall \

### Free machine

Free a machine with `make free-machine01` or
Free a machine with `make free-vm01` or

```bash
docker compose run --rm metalctl machine rm e0ab02d2-27cd-5a5e-8efc-080ba80cf258
Expand Down
18 changes: 9 additions & 9 deletions scripts/manage_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
import sys

VMS = {
"machine01": {
"name": "machine01",
"vm01": {
"name": "vm01",
"uuid": "e0ab02d2-27cd-5a5e-8efc-080ba80cf258",
"disk-path": "/machine01.img",
"disk-path": "/vm01.img",
"disk-size": "5G",
"memory": "2G",
"tap-index-fd": [(0, 30), (1, 40)],
"serial-port": 4000,
},
"machine02": {
"name": "machine02",
"vm02": {
"name": "vm02",
"uuid": "2294c949-88f6-5390-8154-fa53d93a3313",
"disk-path": "/machine02.img",
"disk-path": "/vm02.img",
"disk-size": "5G",
"memory": "2G",
"tap-index-fd": [(2, 50), (3, 60)],
"serial-port": 4001,
},
"machine03": {
"name": "machine03",
"vm03": {
"name": "vm03",
"uuid": "2a92f14d-d3b1-4d46-b813-5d058103743e",
"disk-path": "/machine03.img",
"disk-path": "/vm03.img",
"disk-size": "5G",
"memory": "2G",
"tap-index-fd": [(4, 70), (5, 80)],
Expand Down
4 changes: 2 additions & 2 deletions test/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ do
done
echo "$waiting/$minWaiting machines are waiting"

echo "Create firewall and machine"
echo "Create firewall and machine01"
make firewall
make machine
make machine01

echo "Waiting for machines to get to Phoned Home state"
phoned=$(docker compose run -T metalctl machine ls | grep Phoned | wc -l)
Expand Down