Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/models/network_configuration.py
blob: 4b57da00ddbea34f8e1a62a2e2810c95d6e23633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Dict

from ... import log


class NicType(str, Enum):
	ISO = "iso"
	NM = "nm"
	MANUAL = "manual"


@dataclass
class NetworkConfiguration:
	type: NicType
	iface: str = None
	ip: str = None
	dhcp: bool = True
	gateway: str = None
	dns: List[str] = None

	def __str__(self):
		if self.is_iso():
			return "Copy ISO configuration"
		elif self.is_network_manager():
			return "Use NetworkManager"
		elif self.is_manual():
			if self.dhcp:
				return f'iface={self.iface}, dhcp=auto'
			else:
				return f'iface={self.iface}, ip={self.ip}, dhcp=staticIp, gateway={self.gateway}, dns={self.dns}'
		else:
			return 'Unknown type'

	# for json serialization when calling json.dumps(...) on this class
	def json(self):
		return self.__dict__

	@classmethod
	def parse_arguments(cls, config: Union[str,Dict[str, str]]) -> Optional["NetworkConfiguration"]:
		nic_type = config.get('type', None)

		if not nic_type:
			# old style definitions
			if isinstance(config,str): # is a ISO network
				return NetworkConfiguration(NicType.ISO)
			elif config.get('NetworkManager'): # is a network manager configuration
				return NetworkConfiguration(NicType.NM)
			elif 'ip' in config:
				return NetworkConfiguration(
					NicType.MANUAL,
					iface=config.get('nic', ''),
					ip=config.get('ip'),
					gateway=config.get('gateway', ''),
					dns=config.get('dns', []),
					dhcp=False
				)
			elif 'nic' in config:
				return NetworkConfiguration(
					NicType.MANUAL,
					iface=config.get('nic', ''),
					dhcp=True
				)
			else:  # not recognized
				return None

		try:
			type = NicType(nic_type)
		except ValueError:
			options = [e.value for e in NicType]
			log(_('Unknown nic type: {}. Possible values are {}').format(nic_type, options), fg='red')
			exit(1)

		if type == NicType.MANUAL:
			if config.get('dhcp', False) or not any([config.get(v) for v in ['ip', 'gateway', 'dns']]):
				return NetworkConfiguration(type, iface=config.get('iface', ''))

			ip = config.get('ip', '')
			if not ip:
				log('Manual nic configuration with no auto DHCP requires an IP address', fg='red')
				exit(1)

			return NetworkConfiguration(
				type,
				iface=config.get('iface', ''),
				ip=ip,
				gateway=config.get('gateway', ''),
				dns=config.get('dns', []),
				dhcp=False
			)
		else:
			return NetworkConfiguration(type)

	def is_iso(self) -> bool:
		return self.type == NicType.ISO

	def is_network_manager(self) -> bool:
		return self.type == NicType.NM

	def is_manual(self) -> bool:
		return self.type == NicType.MANUAL

	def config_installer(self, installation: 'Installer'):
		# If user selected to copy the current ISO network configuration
		# Perform a copy of the config
		if self.is_iso():
			installation.copy_iso_network_config(enable_services=True)  # Sources the ISO network configuration to the install medium.
		elif self.is_network_manager():
			installation.add_additional_packages("networkmanager")
			installation.enable_service('NetworkManager.service')
		# Otherwise, if a interface was selected, configure that interface
		elif self.is_manual():
			installation.configure_nic(self)
			installation.enable_service('systemd-networkd')
			installation.enable_service('systemd-resolved')

	def get(self, key :str, default_value :Any = None) -> Any:
		result = self.__getitem__(key)
		if result is None:
			return default_value
		else:
			return result

	def __getitem__(self, key :str) -> Any:
		if key == 'type':
			return self.type
		elif key == 'iface':
			return self.iface
		elif key == 'gateway':
			return self.gateway
		elif key == 'dns':
			return self.dns
		elif key == 'dhcp':
			return self.dhcp
		else:
			raise KeyError(f"key {key} not available at NetworkConfiguration")