This repository has been archived on 2020-09-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FreeNAS-Network-Unlock/freenas_network_unlock.py

72 lines
3.3 KiB
Python

#!/usr/bin/env python
import requests, platform, subprocess, config, logging, simplejson as json, argparse, from subprocess import call
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig(level=logging.INFO,format='%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
#logging.getLogger().setLevel(logging.DEBUG)
#requests_log = logging.getLogger("requests.packages.urllib3")
#requests_log.setLevel(logging.DEBUG)
#requests_log.propagate = True
def request(resource, method='GET', data=None):
if data is None:
data = ''
url = 'https://{}/api/v1.0/{}'.format(config.HOSTNAME, resource)
logging.debug('Request URL: {}'.format(url))
logging.debug('Request Data: {}'.format(data))
logging.debug('CA Certificate Path: {}'.format(config.CA_CERT_PATH))
r = requests.request(
method,
url,
data=json.dumps(data),
headers={'Content-Type': "application/json"},
auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH)
)
logging.debug('Request Status Code: {}'.format(r.status_code))
if r.ok:
try:
logging.debug('Request Returned JSON: {}'.format(r.json()))
return {'ok': r.ok, 'status_code': r.status_code, 'response': r.json()}
except:
logging.debug('Request Returned Text: {}'.format(r.text))
return {'ok': r.ok, 'status_code': r.status_code, 'response': r.text}
raise ValueError(r)
#if __name__ == "__main__":
# parser = argparse.ArgumentParser(description='Unlock FreeNAS Pools')
# group = parser.add_mutually_exclusive_group()
# group.add_argument('-s', '--server', action='store_true', help='server (Usually runs on another system where passwords are stored)')
# group.add_argument('-c', '--client', action='store_true', help='client (Usually runs on the FreeNAS server)')
# parser.add_argument('-ip', '--host', type=str, help='Hostname/IP of the host running the unlock script (Required for client)')
# parser.add_argument('-f', '--filePath', type=str, help='Absolute path to the script on the host (Required for client)')
# args = parser.parse_args()
# if args.client and (args.host is None or args.filePath is None):
# parser.error("--client requires --host and --filePath.")
#POOLS = request('storage/volume/', 'GET')
#for pool in POOLS['response']:
# if pool['is_decrypted'] == False:
# logging.info('Pool {} is locked'.format(pool['name']))
# response = request('storage/volume/{}/unlock/'.format(pool['name']), 'POST', {'passphrase': '{}'.format(config.ENCRYPTION_PASSPHRASES[pool['name']])})
# if response['ok']:
# logging.info('Pool {} was unlocked successfully'.format(pool['name']))
# else:
# logging.error('Pool {} was NOT unlocked successfully'.format(pool['name']))
# else:
# logging.debug('Pool {} is already unlocked'.format(pool['name']))
# Create a small ramdrive to store our recovery keys temporarily
rc = call("mkdir /mnt/ramfs", shell=True)
rc = call("mdmfs -s 1m md /mnt/ramfs", shell=True)
# Send our unlock/mount script to the pi and execute it on the pi using ssh
rc = call("ssh root@<PI-IP-ADDRESS> 'bash -s'", shell=True)