wireguard: Make setup idempotent

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy 2019-09-17 20:00:54 -04:00
parent aa66a9135c
commit a0ea33d9b6
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -106,15 +106,20 @@ def _generate_key_pair():
def subcommand_setup(_):
"""Setup WireGuard."""
# TODO: make idempotent
# Create interface.
try:
subprocess.run(['ip', 'link', 'show', SERVER_INTERFACE],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
check=True)
except subprocess.CalledProcessError:
subprocess.run(
['ip', 'link', 'add', 'dev', SERVER_INTERFACE, 'type',
'wireguard'], check=True)
# create interface
subprocess.run(
['ip', 'link', 'add', 'dev', SERVER_INTERFACE, 'type', 'wireguard'],
check=True)
_generate_key_pair()
if not (PUBLIC_KEY_PATH.exists() and PRIVATE_KEY_PATH.exists()):
_generate_key_pair()
# Configure interface.
subprocess.run(
['wg', 'set', SERVER_INTERFACE, 'listen-port', '51820', 'private-key',
str(PRIVATE_KEY_PATH)], check=True)