-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd21977
commit 172b921
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,93 @@ | ||
#!/usr/bin/python3 | ||
|
||
# | ||
# Copyright (C) 2023 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-2.0-only | ||
# | ||
|
||
import base64 | ||
import json | ||
import os | ||
import shutil | ||
import subprocess | ||
import sys | ||
|
||
from nethsec import utils | ||
|
||
MIGRATE_DIR= '/tmp/migrate/' | ||
MIGRATE_PATH = '/tmp/migrate/migration.tar.gz' | ||
|
||
|
||
def restore_backup(passphrase=None): | ||
try: | ||
if passphrase is not None: | ||
subprocess.run([ | ||
'/usr/bin/gpg', | ||
'--decrypt', | ||
'--batch', | ||
'--passphrase', passphrase, | ||
'--output', f'{BACKUP_PATH}.decrypted', | ||
'--yes', | ||
BACKUP_PATH | ||
], check=True, capture_output=True) | ||
shutil.move(f'{BACKUP_PATH}.decrypted', BACKUP_PATH) | ||
|
||
# run sysupgrade to restore backup file | ||
subprocess.run(['/sbin/sysupgrade', '-r', BACKUP_PATH], check=True, capture_output=True) | ||
|
||
except subprocess.CalledProcessError as error: | ||
if error.cmd[0] == '/usr/bin/gpg': | ||
raise RuntimeError('decryption failed') | ||
elif error.cmd[0] == '/sbin/sysupgrade': | ||
raise RuntimeError('restore failed') | ||
|
||
|
||
cmd = sys.argv[1] | ||
|
||
if cmd == 'list': | ||
print(json.dumps({ | ||
'upload': { | ||
'archive': 'str', | ||
}, | ||
'list-devices': {}, | ||
'migrate': { } | ||
})) | ||
elif cmd == 'call': | ||
action = sys.argv[2] | ||
if action == 'migrate': | ||
try: | ||
#FIXME: add -moldmac=newmac | ||
#subprocess.run(['/usr/sbin/ns-import', MIGRATE_PATH], check=True, capture_output=True) | ||
# return content | ||
print(json.dumps({'result': 'success'})) | ||
except RuntimeError as error: | ||
print(json.dumps(utils.generic_error(error.args[0]))) | ||
|
||
elif action == 'upload': | ||
try: | ||
# decode input, write backup in default path | ||
data = json.load(sys.stdin) | ||
os.makedirs(MIGRATE_DIR, exist_ok=True) | ||
# open(MIGRATION_PATH, 'wb').write(base64.b64decode(data['archive'])) | ||
# reboot takes a few seconds to complete, enough to send the response | ||
print(json.dumps({'devices': [ | ||
{"name": "enp1s0", "hwaddr": "xx:yy:xx", "ipaddr": "1.2.3.4", "role": "green"}, | ||
{"name": "en1", "hwaddr": "xx:zz:zz", "ipaddr": "4.5.6.7", "role": "red"} | ||
]})) | ||
except RuntimeError as error: | ||
print(json.dumps(utils.generic_error(error.args[0]))) | ||
|
||
elif action == 'list-devices': | ||
ret = [] | ||
try: | ||
data = json.loads(subprocess.run(["/sbin/ip", "--json", "link"], check=True, capture_output=True, text=True).stdout) | ||
for i in data: | ||
if i['ifname'] == "lo" or i['ifname'].startswith('br-') or i['ifname'].startswith('tun-') or i['ifname'].startswith('ifb-'): | ||
continue | ||
ret.append({'name': i['ifname'], 'hwaddr': i['address']}) | ||
|
||
print(json.dumps({'devices': ret})) | ||
except KeyError as error: | ||
print(error) | ||
print(json.dumps(utils.validation_error('passphrase', 'required'))) | ||
|
This file contains 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,13 @@ | ||
{ | ||
"migration-manager": { | ||
"description": "Manages migration", | ||
"write": {}, | ||
"read": { | ||
"ubus": { | ||
"ns.migration": [ | ||
"*" | ||
] | ||
} | ||
} | ||
} | ||
} |