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

75 lines
3.0 KiB
Python

#!/usr/bin/env python
import requests, platform, subprocess, config, logging, simplejson as json
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig(level=logging.DEBUG,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 ping(host):
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""
# Option for the number of packets as a function of
param = '-n' if platform.system().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host]
return subprocess.call(command) == 0
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.ok))
if r.ok:
try:
logging.debug('Request Returned JSON: {}'.format(r.json())
return r.json()
except:
logging.debug('Request Returned Text: {}'.format(r.text)
return r.text
raise ValueError(r)
#if ping(config.HOSTNAME):
POOLS = request('storage/volume/', 'GET')
logging.debug('POOL Information: {}'.format(POOLS)
#VOLUMES = requests.get(
# 'https://{}/api/v1.0/storage/volume/'.format(config.HOSTNAME),
# auth=('root', '{}'.format(config.ROOT_PASSWORD)),
# verify='{}'.format(config.CA_CERT_PATH),
#)
for volume in VOLUMES.json():
if volume['is_decrypted'] == False:
logging.info('Pool {} is locked'.format(volume['name']))
response = requests.post(
'https://{}/api/v1.0/storage/volume/{}/unlock/'.format(config.HOSTNAME,volume['name']),
json={'passphrase': '{}'.format(config.POOLS[volume['name']])},
auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH),
)
if response.status_code:
logging.info('Pool {} was unlocked successfully'.format(volume['name']))
else:
logging.error('Pool {} was NOT unlocked successfully'.format(volume['name']))
else:
logging.debug('Pool {} is already unlocked'.format(volume['name']))
#else:
#print("Host,{}, is not online".format(config.HOSTNAME))