Skip to content

Commit 0544670

Browse files
committed
Had Codex refactor helper script to implement testing and to search all drives for network-config. Also updated plugin loading order so script run script before other cloudbase_plugins.
Tested eval unattend, but requires testing with licensed version.
1 parent 5b9608e commit 0544670

6 files changed

Lines changed: 351 additions & 36 deletions

File tree

images/capi/ansible/windows/roles/providers/files/proxmox/cloudbase_helper.py

Lines changed: 103 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Apply Proxmox NoCloud network data staged as NETWORK_CONFIG."""
15+
"""Apply Proxmox NoCloud network data from a config-drive file."""
1616

1717
import logging
1818
import os
19+
import string
1920
import sys
2021

21-
from cloudbaseinit.plugins.common import networkconfig
22-
from cloudbaseinit.metadata.services.nocloudservice import NoCloudNetworkConfigParser
23-
from cloudbaseinit.utils import serialization
24-
2522
try:
2623
from oslo_log import log as oslo_logging
2724

@@ -31,43 +28,120 @@
3128
LOG = logging.getLogger(__name__)
3229

3330

34-
NETWORK_DATA_PATH = r"D:\NETWORK_CONFIG"
31+
DEFAULT_NETWORK_DATA_FILENAMES = ("NETWORK_CONFIG", "network-config")
3532

3633

37-
def main():
38-
try:
39-
if not os.path.exists(NETWORK_DATA_PATH):
40-
LOG.info("No Proxmox network data found at %s", NETWORK_DATA_PATH)
41-
return 0
34+
def _iter_search_roots():
35+
search_roots = os.environ.get("PROXMOX_NETWORK_DATA_SEARCH_ROOTS")
36+
if search_roots:
37+
for root in search_roots.split(os.pathsep):
38+
root = root.strip()
39+
if root:
40+
yield root
41+
return
4242

43-
with open(NETWORK_DATA_PATH, "r", encoding="utf-8") as network_data_file:
44-
raw_network_data = network_data_file.read()
45-
except Exception:
46-
LOG.exception("Failed to load Proxmox network data from %s", NETWORK_DATA_PATH)
47-
return 0
43+
for drive_letter in string.ascii_uppercase:
44+
yield "%s:\\" % drive_letter
4845

49-
try:
50-
network_data = serialization.parse_json_yaml(raw_network_data)
51-
except serialization.YamlParserConfigError:
52-
LOG.exception("Proxmox network data could not be parsed as YAML or JSON")
53-
return 0
5446

47+
def _iter_candidate_paths():
48+
override_path = os.environ.get("PROXMOX_NETWORK_DATA_PATH", "").strip()
49+
if override_path:
50+
yield override_path
51+
return
52+
53+
for root in _iter_search_roots():
54+
normalized_root = root.rstrip("\\/")
55+
for filename in DEFAULT_NETWORK_DATA_FILENAMES:
56+
yield "%s\\%s" % (normalized_root, filename)
57+
58+
59+
def find_network_data_path(path_exists=os.path.exists):
60+
for candidate_path in _iter_candidate_paths():
61+
if path_exists(candidate_path):
62+
return candidate_path
63+
return None
64+
65+
66+
def load_network_data(network_data_path, open_file=open, parser=None):
67+
if parser is None:
68+
from cloudbaseinit.utils import serialization
69+
70+
parser = serialization.parse_json_yaml
71+
72+
with open_file(network_data_path, "r", encoding="utf-8") as network_data_file:
73+
raw_network_data = network_data_file.read()
74+
75+
network_data = parser(raw_network_data)
5576
if not isinstance(network_data, dict):
56-
LOG.error("Proxmox network data parsed into %r, expected dict", type(network_data))
57-
return 0
77+
raise ValueError(
78+
"Proxmox network data parsed into %r, expected dict" %
79+
type(network_data)
80+
)
81+
82+
return network_data
5883

59-
network_details = NoCloudNetworkConfigParser.parse(network_data)
84+
85+
def apply_network_data(network_data, network_parser=None, plugin_factory=None):
86+
if network_parser is None:
87+
from cloudbaseinit.metadata.services.nocloudservice import (
88+
NoCloudNetworkConfigParser,
89+
)
90+
91+
network_parser = NoCloudNetworkConfigParser.parse
92+
93+
if plugin_factory is None:
94+
from cloudbaseinit.plugins.common import networkconfig
95+
96+
plugin_factory = networkconfig.NetworkConfigPlugin
97+
98+
network_details = network_parser(network_data)
6099
if not network_details:
61100
LOG.warning("NoCloud network parser returned no interfaces")
101+
return False
102+
103+
plugin = plugin_factory()
104+
process_network_details = getattr(plugin, "_process_network_details_v2", None)
105+
if process_network_details is None:
106+
raise AttributeError(
107+
"Cloudbase-Init network plugin is missing _process_network_details_v2"
108+
)
109+
110+
process_network_details(network_details)
111+
return True
112+
113+
114+
def main():
115+
network_data_path = find_network_data_path()
116+
if not network_data_path:
117+
LOG.info(
118+
"No Proxmox network data found in candidate paths: %s",
119+
", ".join(_iter_candidate_paths()),
120+
)
121+
return 0
122+
123+
try:
124+
network_data = load_network_data(network_data_path)
125+
except Exception:
126+
LOG.exception(
127+
"Failed to load Proxmox network data from %s", network_data_path
128+
)
62129
return 0
63130

64-
plugin = networkconfig.NetworkConfigPlugin()
65-
if not hasattr(plugin, "_process_network_details_v2"):
66-
LOG.error("Cloudbase-Init network plugin is missing _process_network_details_v2")
131+
try:
132+
LOG.info("Applying Proxmox network data from %s", network_data_path)
133+
applied = apply_network_data(network_data)
134+
except Exception:
135+
LOG.exception(
136+
"Failed to apply Proxmox network data from %s", network_data_path
137+
)
67138
return 0
68139

69-
LOG.info("Applying Proxmox network data from %s", NETWORK_DATA_PATH)
70-
plugin._process_network_details_v2(network_details)
140+
if not applied:
141+
LOG.warning(
142+
"No network interfaces were applied from %s", network_data_path
143+
)
144+
71145
return 0
72146

73147

images/capi/packer/goss/goss-vars.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,4 @@ windows:
707707
exists: true
708708
filetype: file
709709
contains:
710-
- "Apply Proxmox NoCloud network data staged as NETWORK_CONFIG."
710+
- "Apply Proxmox NoCloud network data from a config-drive file."

images/capi/packer/proxmox/packer-windows.json.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"cloudbase_nocloud_metadata_file": "META_DATA",
154154
"cloudbase_nocloud_networkdata_file": "NETWORK_CONFIG",
155155
"cloudbase_nocloud_userdata_file": "USER_DATA",
156-
"cloudbase_plugins": "cloudbaseinit.plugins.windows.createuser.CreateUserPlugin, cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin, cloudbaseinit.plugins.common.mtu.MTUPlugin, cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin, cloudbaseinit.plugins.common.networkconfig.NetworkConfigPlugin, cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin, cloudbaseinit.plugins.common.ephemeraldisk.EphemeralDiskPlugin, cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin, cloudbaseinit.plugins.common.userdata.UserDataPlugin, cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin, cloudbaseinit.plugins.windows.createuser.CreateUserPlugin, cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin",
156+
"cloudbase_plugins": "cloudbaseinit.plugins.windows.createuser.CreateUserPlugin, cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin, cloudbaseinit.plugins.common.mtu.MTUPlugin, cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin, cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin, cloudbaseinit.plugins.common.networkconfig.NetworkConfigPlugin, cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin, cloudbaseinit.plugins.common.ephemeraldisk.EphemeralDiskPlugin, cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin, cloudbaseinit.plugins.common.userdata.UserDataPlugin, cloudbaseinit.plugins.windows.createuser.CreateUserPlugin, cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin",
157157
"cloudbase_plugins_unattend": "cloudbaseinit.plugins.common.mtu.MTUPlugin",
158158
"cloudbase_real_time_clock_utc": "true",
159159
"containerd_url": "",

images/capi/packer/proxmox/windows-2022.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"iso_virtio": "local:iso/virtio-win-0.1.285.iso",
1010
"cpu_type": "host",
1111
"goss_version": "0.4.9",
12-
"cloudbase_init_version": "1.1.6"
12+
"cloudbase_init_version": "1.1.6",
13+
"cloudbase_nocloud_metadata_file": "META_DATA",
14+
"cloudbase_nocloud_networkdata_file": "NETWORK_CONFIG",
15+
"cloudbase_nocloud_userdata_file": "USER_DATA"
1316
}

0 commit comments

Comments
 (0)