Update 'freenas_network_unlock.py'

This commit is contained in:
2019-11-11 22:20:33 -05:00
parent 1bbf3db7de
commit 7d8b787ce0

View File

@@ -1,7 +1,7 @@
import requests # For the api calls import requests # For the api calls
import platform # For getting the operating system name import platform # For getting the operating system name
import subprocess # For executing a shell command import subprocess # For executing a shell command
import config # configuration file import config # configuration file
import logging import logging
# You must initialize logging, otherwise you'll not see debug output. # You must initialize logging, otherwise you'll not see debug output.
@@ -12,36 +12,36 @@ logging.basicConfig(level=logging.INFO,format='%(asctime)s - [%(levelname)s] %(m
#requests_log.propagate = True #requests_log.propagate = True
def ping(host): def ping(host):
""" """
Returns True if host (str) responds to a ping request. 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. 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 # Option for the number of packets as a function of
param = '-n' if platform.system().lower()=='windows' else '-c' param = '-n' if platform.system().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com" # Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host] command = ['ping', param, '1', host]
return subprocess.call(command) == 0 return subprocess.call(command) == 0
#if ping(config.HOSTNAME): #if ping(config.HOSTNAME):
VOLUMES = requests.get( VOLUMES = requests.get(
'https://{}/api/v1.0/storage/volume/'.format(config.HOSTNAME), 'https://{}/api/v1.0/storage/volume/'.format(config.HOSTNAME),
auth=('root', '{}'.format(config.ROOT_PASSWORD)), auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH), verify='{}'.format(config.CA_CERT_PATH),
) )
for volume in VOLUMES.json(): for volume in VOLUMES.json():
if volume['is_decrypted'] == False: if volume['is_decrypted'] == False:
logging.info('Pool {} is locked'.format(volume['name'])) logging.info('Pool {} is locked'.format(volume['name']))
response = requests.post( response = requests.post(
'https://{}/api/v1.0/storage/volume/{}/unlock/'.format(config.HOSTNAME,volume['name']), 'https://{}/api/v1.0/storage/volume/{}/unlock/'.format(config.HOSTNAME,volume['name']),
json={'passphrase': '{}'.format(config.POOLS[volume['name']])}, json={'passphrase': '{}'.format(config.POOLS[volume['name']])},
auth=('root', '{}'.format(config.ROOT_PASSWORD)), auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH), verify='{}'.format(config.CA_CERT_PATH),
) )
print(response.status_code) print(response.status_code)
print(response.text) print(response.text)
#else: #else:
#print("Host,{}, is not online".format(config.HOSTNAME)) #print("Host,{}, is not online".format(config.HOSTNAME))