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

Skip jq install if possible #53

Merged
merged 4 commits into from
Feb 24, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
source .venv/bin/activate
./example test --driver ${{ matrix.driver }} --connection ${{ matrix.connection }} -v &
vm="$HOME/.vmnet-helper/vms/test"
if ! timeout 5m bash -c "until test -f "$vm/ip-address"; do sleep 3; done"; then
if ! timeout 3m bash -c "until test -f "$vm/ip-address"; do sleep 3; done"; then
echo >&2 "Timeout waiting for $vm/ip-address"
exit 1
fi
Expand Down
42 changes: 22 additions & 20 deletions vmnet/cidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
}


def create_iso(vm_name, driver, mac_address):
def create_iso(vm):
"""
Create cloud-init iso image.

We create a new cidata.iso with new instance id for every run to update the
vm network configuration and report the vm ip address.
"""
vm_home = store.vm_path(vm_name)
vm_home = store.vm_path(vm.vm_name)
cidata = os.path.join(vm_home, "cidata.iso")
create_user_data(vm_name, driver)
create_meta_data(vm_name)
create_network_config(vm_name, mac_address)
create_user_data(vm)
create_meta_data(vm)
create_network_config(vm)
cmd = [
"mkisofs",
"-output",
Expand All @@ -56,65 +56,67 @@ def create_iso(vm_name, driver, mac_address):
return cidata


def create_user_data(vm_name, driver):
def create_user_data(vm):
"""
Create cloud-init user-data file.
"""
serial_console = f"/dev/{SERIAL_CONSOLE[driver][platform.machine()]}"
path = store.vm_path(vm_name, "user-data")
serial_console = f"/dev/{SERIAL_CONSOLE[vm.driver][platform.machine()]}"
data = {
"password": "password",
"chpasswd": {
"expire": False,
},
"ssh_authorized_keys": public_keys(),
"packages": [
"jq",
],
"package_update": False,
"package_upgrade": False,
"runcmd": [
"ip_address=$(ip -4 -j addr show dev vmnet0 | jq -r '.[0].addr_info[0].local')",
f"echo > {serial_console}",
f"echo {vm_name} address: $ip_address > {serial_console}",
f"echo {vm.vm_name} address: $ip_address > {serial_console}",
],
}

# Skip jq install if posisble to minimize startup time in the CI.
if vm.distro != "ubuntu":
data["packages"] = ["jq"]
data["package_update"] = False
data["package_upgrade"] = False

path = store.vm_path(vm.vm_name, "user-data")
with open(path, "w") as f:
f.write("#cloud-config\n")
yaml.dump(data, f, sort_keys=False)


def create_meta_data(vm_name):
def create_meta_data(vm):
"""
Create cloud-init meta-data file.
"""
path = store.vm_path(vm_name, "meta-data")
data = {
"instance-id": str(uuid.uuid4()),
"local-hostname": vm_name,
"local-hostname": vm.vm_name,
}
path = store.vm_path(vm.vm_name, "meta-data")
with open(path, "w") as f:
yaml.dump(data, f, sort_keys=False)


def create_network_config(vm_name, mac_address):
def create_network_config(vm):
"""
Create cloud-init network-config file.
"""
path = store.vm_path(vm_name, "network-config")
data = {
"version": 2,
"ethernets": {
"vmnet0": {
"match": {
"macaddress": mac_address,
"macaddress": vm.mac_address,
},
"set-name": "vmnet0",
"dhcp4": True,
"dhcp-identifier": "mac",
},
},
}
path = store.vm_path(vm.vm_name, "network-config")
with open(path, "w") as f:
yaml.dump(data, f, sort_keys=False)

Expand Down
10 changes: 5 additions & 5 deletions vmnet/disks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@
}


def create_disk(vm_name, distro):
def create_disk(vm):
"""
Create a disk from image using copy-on-write.
"""
image_info = IMAGES[distro][platform.machine()]
disk = {"image": store.vm_path(vm_name, "disk.img")}
image_info = IMAGES[vm.distro][platform.machine()]
disk = {"image": store.vm_path(vm.vm_name, "disk.img")}
if not os.path.isfile(disk["image"]):
image = create_image(image_info["image"], format="raw", size="20g")
print(f"Creating image '{disk['image']}'")
clone(image, disk["image"])
if "kernel" in image_info:
disk["kernel"] = store.vm_path(vm_name, "kernel")
disk["kernel"] = store.vm_path(vm.vm_name, "kernel")
if not os.path.isfile(disk["kernel"]):
kernel = create_image(image_info["kernel"])
print(f"Creating kernel '{disk['kernel']}'")
clone(kernel, disk["kernel"])
if "initrd" in image_info:
disk["initrd"] = store.vm_path(vm_name, "initrd")
disk["initrd"] = store.vm_path(vm.vm_name, "initrd")
if not os.path.isfile(disk["initrd"]):
initrd = create_image(image_info["initrd"])
print(f"Creating initrd '{disk['initrd']}'")
Expand Down
6 changes: 3 additions & 3 deletions vmnet/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def start(self):
"""
Starts a VM driver with fd or socket.
"""
self.disk = disks.create_disk(self.vm_name, self.distro)
self.cidata = cidata.create_iso(self.vm_name, self.driver, self.mac_address)
self.disk = disks.create_disk(self)
self.cidata = cidata.create_iso(self)
if self.driver == "vfkit":
cmd = self.vfkit_command()
elif self.driver == "krunkit":
Expand Down Expand Up @@ -83,7 +83,7 @@ def write_command(self, cmd):
with open(path, "w") as f:
f.write(data)

def wait_for_ip_address(self, timeout=300):
def wait_for_ip_address(self, timeout=180):
"""
Lookup the vm ip address in the serial log and write to
vm_home/ip-address.
Expand Down