Split host into host, network, and (optional) daemon name parts. The network part can be an IP, CIDR, or ceph addrvec like '[v2:1.2.3.4:3300,v1:1.2.3.4:6789]'. e.g., "myhost" "myhost=name" "myhost:1.2.3.4" "myhost:1.2.3.4=name"
(cls, host, require_network=True)
| 145 | |
| 146 | @classmethod |
| 147 | def parse(cls, host, require_network=True): |
| 148 | # type: (str, bool) -> HostPlacementSpec |
| 149 | """ |
| 150 | Split host into host, network, and (optional) daemon name parts. The network |
| 151 | part can be an IP, CIDR, or ceph addrvec like '[v2:1.2.3.4:3300,v1:1.2.3.4:6789]'. |
| 152 | e.g., |
| 153 | "myhost" |
| 154 | "myhost=name" |
| 155 | "myhost:1.2.3.4" |
| 156 | "myhost:1.2.3.4=name" |
| 157 | "myhost:1.2.3.0/24" |
| 158 | "myhost:1.2.3.0/24=name" |
| 159 | "myhost:[v2:1.2.3.4:3000]=name" |
| 160 | "myhost:[v2:1.2.3.4:3000,v1:1.2.3.4:6789]=name" |
| 161 | """ |
| 162 | # Matches from start to : or = or until end of string |
| 163 | host_re = r'^(.*?)(:|=|$)' |
| 164 | # Matches from : to = or until end of string |
| 165 | ip_re = r':(.*?)(=|$)' |
| 166 | # Matches from = to end of string |
| 167 | name_re = r'=(.*?)$' |
| 168 | |
| 169 | # assign defaults |
| 170 | host_spec = cls('', '', '') |
| 171 | |
| 172 | match_host = re.search(host_re, host) |
| 173 | if match_host: |
| 174 | # Lowercase for case-insensitive matching |
| 175 | host_spec = host_spec._replace(hostname=normalize_hostname(match_host.group(1))) |
| 176 | |
| 177 | name_match = re.search(name_re, host) |
| 178 | if name_match: |
| 179 | host_spec = host_spec._replace(name=name_match.group(1)) |
| 180 | |
| 181 | ip_match = re.search(ip_re, host) |
| 182 | if ip_match: |
| 183 | host_spec = host_spec._replace(network=ip_match.group(1)) |
| 184 | |
| 185 | if not require_network: |
| 186 | return host_spec |
| 187 | |
| 188 | networks = list() # type: List[str] |
| 189 | network = host_spec.network |
| 190 | # in case we have [v2:1.2.3.4:3000,v1:1.2.3.4:6478] |
| 191 | if ',' in network: |
| 192 | networks = [x for x in network.split(',')] |
| 193 | else: |
| 194 | if network != '': |
| 195 | networks.append(network) |
| 196 | |
| 197 | for network in networks: |
| 198 | # only if we have versioned network configs |
| 199 | if network.startswith('v') or network.startswith('[v'): |
| 200 | # if this is ipv6 we can't just simply split on ':' so do |
| 201 | # a split once and rsplit once to leave us with just ipv6 addr |
| 202 | network = network.split(':', 1)[1] |
| 203 | network = network.rsplit(':', 1)[0] |
| 204 | try: |