Skip to content

Commit 7d7ef1f

Browse files
authored
Merge pull request #17 from rackerlabs/config_dir
feat: parse meta_data.json and set hostname
2 parents 21e0b71 + 1ae762b commit 7d7ef1f

File tree

12 files changed

+201
-16
lines changed

12 files changed

+201
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ non-interactive.
5454

5555
```bash
5656
# using uvx
57-
uvx esxi-image --output esxi.img path/to/esxi.iso
57+
uvx esxi-img --output esxi.img path/to/esxi.iso
5858

5959
# using pip
6060
python -m venv .venv
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"random_seed": "yu5ZnkqF2CqnDZVAfZgarG...",
3+
"availability_zone": "nova",
4+
"keys": [
5+
{
6+
"data": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n",
7+
"type": "ssh",
8+
"name": "mykey"
9+
}
10+
],
11+
"hostname": "test.novalocal",
12+
"launch_index": 0,
13+
"meta": {
14+
"priority": "low",
15+
"role": "webserver"
16+
},
17+
"devices": [
18+
{
19+
"type": "nic",
20+
"bus": "pci",
21+
"address": "0000:00:02.0",
22+
"mac": "00:11:22:33:44:55",
23+
"tags": ["trusted"]
24+
},
25+
{
26+
"type": "disk",
27+
"bus": "ide",
28+
"address": "0:0",
29+
"serial": "disk-vol-2352423",
30+
"path": "/dev/sda",
31+
"tags": ["baz"]
32+
}
33+
],
34+
"project_id": "f7ac731cc11f40efbc03a9f9e1d1d21f",
35+
"public_keys": {
36+
"mykey": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n"
37+
},
38+
"name": "test"
39+
}
File renamed without changes.

packages/esxi-netinit/esxi_netinit/esxconfig.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from functools import cached_property
33

44
from .esxhost import ESXHost
5+
from .meta_data import MetaDataData
56
from .network_data import NetworkData
67
from .nic import NIC
78
from .nic_list import NICList
@@ -10,11 +11,17 @@
1011

1112

1213
class ESXConfig:
13-
def __init__(self, network_data: NetworkData, dry_run=False) -> None:
14+
def __init__(
15+
self, network_data: NetworkData, meta_data: MetaDataData, dry_run=False
16+
) -> None:
1417
self.network_data = network_data
18+
self.meta_data = meta_data
1519
self.dry_run = dry_run
1620
self.host = ESXHost(dry_run)
1721

22+
def configure_hostname(self):
23+
self.host.set_hostname(self.meta_data.metadata.hostname)
24+
1825
def clean_default_network_setup(self, portgroup_name, switch_name):
1926
"""Removes default networking setup left by the installer."""
2027
self.host.delete_vmknic(portgroup_name=portgroup_name)

packages/esxi-netinit/esxi_netinit/esxhost.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def __execute(self, cmd: list):
1818
logger.debug("Executing %s", cmd)
1919
subprocess.run(cmd, check=True) # noqa: S603
2020

21+
def set_hostname(self, hostname: str):
22+
cmd = [
23+
"/bin/esxcli",
24+
"system",
25+
"hostname",
26+
"set",
27+
"--fqdn",
28+
hostname,
29+
]
30+
return self.__execute(cmd)
31+
2132
def add_ip_interface(self, inf: str, portgroup_name: str, mac: str, mtu: int):
2233
"""Adds IP interface."""
2334
logger.info(

packages/esxi-netinit/esxi_netinit/main.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import logging.handlers
44
import os
55
import sys
6+
from pathlib import Path
67

78
from esxi_netinit.esxconfig import ESXConfig
9+
from esxi_netinit.meta_data import MetaDataData
810
from esxi_netinit.network_data import NetworkData
911

1012
OLD_MGMT_PG = "Management Network"
@@ -38,9 +40,24 @@ def setup_logger(log_level=logging.INFO):
3840
logger.error("Failed to setup syslog for logging")
3941

4042

41-
def main(json_file, dry_run):
42-
network_data = NetworkData.from_json_file(json_file)
43-
esx = ESXConfig(network_data, dry_run=dry_run)
43+
def main(config_dir, dry_run):
44+
config_path = Path(config_dir)
45+
network_data_file = config_path / "network_data.json"
46+
meta_data_file = config_path / "meta_data.json"
47+
48+
if not network_data_file.exists():
49+
logger.error("Missing network_data.json in %s", config_dir)
50+
sys.exit(1)
51+
52+
if not meta_data_file.exists():
53+
logger.error("Missing meta_data.json in %s", config_dir)
54+
sys.exit(1)
55+
56+
network_data = NetworkData.from_json_file(network_data_file)
57+
meta_data = MetaDataData.from_json_file(meta_data_file)
58+
59+
esx = ESXConfig(network_data, meta_data, dry_run=dry_run)
60+
esx.configure_hostname()
4461
esx.clean_default_network_setup(OLD_MGMT_PG, OLD_VSWITCH)
4562
esx.configure_vswitch(
4663
uplink=esx.identify_uplink(), switch_name=NEW_VSWITCH, mtu=9000
@@ -56,7 +73,11 @@ def main(json_file, dry_run):
5673

5774
if __name__ == "__main__":
5875
parser = argparse.ArgumentParser(description="Network configuration script")
59-
parser.add_argument("json_file", help="Path to the JSON configuration file")
76+
parser.add_argument(
77+
"config_dir",
78+
help="Path to the configuration dir containing "
79+
"network_data.json, meta_data.json",
80+
)
6081
parser.add_argument(
6182
"--dry-run",
6283
action="store_true",
@@ -67,7 +88,7 @@ def main(json_file, dry_run):
6788
setup_logger()
6889

6990
try:
70-
main(args.json_file, args.dry_run)
91+
main(args.config_dir, args.dry_run)
7192
except Exception:
7293
logger.exception("Error configuring network")
7394
sys.exit(1)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import logging
3+
4+
from esxi_netinit.metadata import MetaData
5+
6+
logger = logging.getLogger(__name__)
7+
8+
9+
class MetaDataData:
10+
"""Represents meta_data.json."""
11+
12+
def __init__(self, data: dict) -> None:
13+
self.metadata = MetaData(
14+
uuid=data["uuid"],
15+
hostname=data["hostname"],
16+
availability_zone=data.get("availability_zone"),
17+
public_keys=data.get("public_keys", {}),
18+
admin_pass=data["admin_pass"],
19+
project_id=data["project_id"],
20+
random_seed=data["random_seed"],
21+
launch_index=data["launch_index"],
22+
)
23+
24+
@staticmethod
25+
def from_json_file(path):
26+
with open(path) as f:
27+
data = json.load(f)
28+
return MetaDataData(data)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from dataclasses import dataclass
2+
from dataclasses import field
3+
from typing import Dict
4+
from typing import Optional
5+
6+
7+
@dataclass
8+
class MetaData:
9+
uuid: str
10+
admin_pass: str
11+
hostname: str
12+
project_id: str
13+
random_seed: str
14+
launch_index: Optional[int] = 0
15+
availability_zone: Optional[str] = None
16+
meta: Dict[str, str] = field(default_factory=dict)
17+
public_keys: Dict[str, str] = field(default_factory=dict)
18+
devices: "list | None" = field(default=None)
19+
dedicated_cpus: "list | None" = field(default=None)

packages/esxi-netinit/tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ def network_data_single():
1919
@pytest.fixture
2020
def network_data_multi():
2121
return _load_json(THIS_DIR / "data" / "net_data_multi.json")
22+
23+
24+
@pytest.fixture
25+
def meta_data():
26+
return _load_json(THIS_DIR / "data" / "meta_data.json")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"uuid": "47bb4c37-f60d-474f-8ce5-c7c1d9982585",
3+
"random_seed": "yu5ZnkqF2CqnDZVAfZgarG...",
4+
"availability_zone": "nova",
5+
"admin_pass": "changeit",
6+
"keys": [
7+
{
8+
"data": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n",
9+
"type": "ssh",
10+
"name": "mykey"
11+
}
12+
],
13+
"hostname": "test.novalocal",
14+
"launch_index": 0,
15+
"meta": {
16+
"priority": "low",
17+
"role": "webserver"
18+
},
19+
"devices": [
20+
{
21+
"type": "nic",
22+
"bus": "pci",
23+
"address": "0000:00:02.0",
24+
"mac": "00:11:22:33:44:55",
25+
"tags": ["trusted"]
26+
},
27+
{
28+
"type": "disk",
29+
"bus": "ide",
30+
"address": "0:0",
31+
"serial": "disk-vol-2352423",
32+
"path": "/dev/sda",
33+
"tags": ["baz"]
34+
}
35+
],
36+
"project_id": "f7ac731cc11f40efbc03a9f9e1d1d21f",
37+
"public_keys": {
38+
"mykey": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n"
39+
},
40+
"name": "test"
41+
}

0 commit comments

Comments
 (0)