diff --git a/utils/deactivate_device.py b/utils/deactivate_device.py index b67cd24..ef0dc04 100755 --- a/utils/deactivate_device.py +++ b/utils/deactivate_device.py @@ -1,58 +1,65 @@ #!/usr/bin/env python3 from phabricator import Phabricator import sys phab = Phabricator(host='https://c4science.ch/api/') phab.update_interfaces() # Disable write on device -def deactivate_write(binding): +def deactivate_write(action, binding): + if action == 'off': + transactions = [ + {'type': 'property.set', 'value': {'writable': False}}, + ] + else: + transactions = [ + {'type': 'property.set', 'value': {'writable': True}}, + ] print(phab.almanac.binding.edit( objectIdentifier = binding, - transactions = [ - #{'type': 'property.set', 'value': {'writable': True}}, - {'type': 'property.set', 'value': {'writable': False}}, - ], + transactions = transactions, )) # Get bindinds list def get_bindings(devices): device = phab.almanac.device.search( constraints = { 'names': devices, } ) if len(device.data) == 0: print('No device found') sys.exit(1) devices_phid = [a['phid'] for a in device.data] service = phab.almanac.service.search( constraints = { 'names': ['repo-cluster', 'repo-single'], } ) services_phid = [a['phid'] for a in service.data] bindings = phab.almanac.binding.search( constraints = { 'servicePHIDs': services_phid, 'devicePHIDs': devices_phid, }, ) if len(bindings.data) == 0: print('No binding found') sys.exit(1) return [a['phid'] for a in bindings.data] # MAIN -devices = sys.argv[1:] +action = sys.argv[1] +devices = sys.argv[2:] + if len(devices) == 0: - print('USAGE: {cmd} device...\nexample: {cmd} c4science-phd00 c4science-phd01'.format(cmd=sys.argv[0])) + print('USAGE: {cmd} [on|off] [device]...\nexample: {cmd} off c4science-phd00 c4science-phd01'.format(cmd=sys.argv[0])) sys.exit(1) bindings = get_bindings(devices) for binding in bindings: - deactivate_write(binding) + deactivate_write(action, binding)