Repository navigation
fcvm-metal-arm: a second public IPv4 address, to switch over to #666
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #!/usr/bin/env python3 | ||
| """Persist the metal host's Terraform-assigned secondary address without a network restart.""" | ||
| import ipaddress | ||
| import json | ||
| from pathlib import Path | ||
| import re | ||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| CONFIG = Path('/etc/netplan/61-fcvm-secondary-ipv4.yaml') | ||
|
|
||
|
|
||
| def configure(address, config=CONFIG, run=subprocess.run): | ||
| address = str(ipaddress.IPv4Address(address)) | ||
| routes = json.loads(run(['ip', '-j', '-4', 'route', 'show', 'default'], | ||
| check=True, capture_output=True, text=True).stdout) | ||
| if not routes: | ||
| raise RuntimeError('no primary IPv4 route') | ||
| interface = min(routes, key=lambda route: route.get('metric', 0))['dev'] | ||
| if not re.fullmatch(r'[A-Za-z0-9_.:-]+', interface): | ||
| raise ValueError('invalid primary interface') | ||
| # /32 adds the host address without replacing its DHCP subnet or default route. | ||
| wanted = (f'network:\n version: 2\n ethernets:\n {interface}:\n' | ||
| f' addresses:\n - {address}/32\n') | ||
| previous = config.read_text() if config.exists() else None | ||
| if previous != wanted: | ||
| config.parent.mkdir(parents=True, exist_ok=True) | ||
| temporary = config.with_suffix('.yaml.tmp') | ||
| temporary.write_text(wanted) | ||
| temporary.chmod(0o600) | ||
| temporary.replace(config) | ||
| try: | ||
| # Generate boot configuration; applying netplan live would disrupt SSH and VMs. | ||
| run(['netplan', 'generate'], check=True) | ||
| except subprocess.CalledProcessError: | ||
| if previous is None: | ||
| config.unlink(missing_ok=True) | ||
| else: | ||
| config.write_text(previous) | ||
| config.chmod(0o600) | ||
| run(['netplan', 'generate'], check=True) | ||
| raise | ||
| run(['ip', '-4', 'address', 'replace', f'{address}/32', 'dev', interface], check=True) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| configure(sys.argv[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #!/usr/bin/env python3 | ||
| """The added address survives boot and never restarts the live primary interface.""" | ||
| import importlib.util | ||
| import json | ||
| from pathlib import Path | ||
| import subprocess | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
| spec = importlib.util.spec_from_file_location('secondary_ipv4', ROOT / 'scripts/fcvm-secondary-ipv4.py') | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
|
|
||
|
|
||
| class SecondaryAddress(unittest.TestCase): | ||
| def test_persists_and_applies_without_replacing_dhcp_or_routes(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| config = Path(directory) / '61-fcvm-secondary-ipv4.yaml' | ||
| calls = [] | ||
| def run(args, **kwargs): | ||
| calls.append(args) | ||
| return subprocess.CompletedProcess(args, 0, json.dumps([{'dev': 'ens5', 'metric': 100}])) | ||
| module.configure('10.0.1.42', config, run) | ||
| first = config.read_bytes() | ||
| self.assertIn(b'ens5:\n addresses:\n - 10.0.1.42/32\n', first) | ||
| self.assertNotIn(b'dhcp4:', first) | ||
| self.assertEqual(config.stat().st_mode & 0o777, 0o600) | ||
| self.assertIn(['netplan', 'generate'], calls) | ||
| self.assertEqual(calls[-1], ['ip', '-4', 'address', 'replace', '10.0.1.42/32', 'dev', 'ens5']) | ||
| module.configure('10.0.1.42', config, run) | ||
| self.assertEqual(config.read_bytes(), first) | ||
| self.assertNotIn(['netplan', 'apply'], calls) | ||
| self.assertFalse(any('restart' in call for call in calls)) | ||
|
|
||
| def test_failed_boot_configuration_rolls_back_before_any_live_change(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| config = Path(directory) / '61-fcvm-secondary-ipv4.yaml' | ||
| original = 'network:\n version: 2\n' | ||
| config.write_text(original) | ||
| calls = [] | ||
| def run(args, **kwargs): | ||
| calls.append(args) | ||
| if args == ['netplan', 'generate'] and calls.count(args) == 1: | ||
| raise subprocess.CalledProcessError(1, args) | ||
| return subprocess.CompletedProcess(args, 0, '[{"dev":"ens5"}]') | ||
| with self.assertRaises(subprocess.CalledProcessError): | ||
| module.configure('10.0.1.42', config, run) | ||
| self.assertEqual(config.read_text(), original) | ||
| self.assertFalse(any('replace' in call for call in calls)) | ||
|
|
||
| def test_no_address_can_be_written_without_an_interface_or_valid_ip(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| config = Path(directory) / 'netplan.yaml' | ||
| def run(args, **kwargs): | ||
| return subprocess.CompletedProcess(args, 0, '[]') | ||
| with self.assertRaises(RuntimeError): | ||
| module.configure('10.0.1.42', config, run) | ||
| with self.assertRaises(ValueError): | ||
| module.configure('10.0.1.42; reboot', config, run) | ||
| self.assertFalse(config.exists()) | ||
|
|
||
| def test_managed_association_runs_the_script_on_the_arm_host(self): | ||
| tf = (ROOT / 'firecracker-dev.tf').read_text() | ||
| self.assertIn('resource "aws_ssm_association" "firecracker_dev_secondary_ipv4"', tf) | ||
| association = tf.split('resource "aws_ssm_association" "firecracker_dev_secondary_ipv4"', 1)[1].split('\n}\n', 1)[0] | ||
| self.assertIn('file("${path.module}/scripts/fcvm-secondary-ipv4.py")', association) | ||
| self.assertIn('aws_instance.firecracker_dev[0].id', association) | ||
| self.assertIn('wait_for_success_timeout_seconds', association) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main(buffer=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.