def write_auth_profile(auth): """Write authorization file to disk""" # initialize locking - NOTE: make sure to release_local() prior to all exceptions/returns) lock = Lock(globals.AUTH_PROFILE_FILE_LOCK) # get lock (for concurrent datamodel access across) lock.get_lock() current_profile = get_auth_profiles() if len(current_profile) == 0: current_profile.append(auth) with open(globals.AUTH_PROFILE_FILE, 'w') as outfile: json.dump(current_profile, outfile) else: update_profile = [] flag_found = False for profile in current_profile: if profile['auth_name'] == auth['auth_name']: update_profile.append(auth) flag_found = True else: update_profile.append(profile) if not flag_found: update_profile.append(auth) with open(globals.AUTH_PROFILE_FILE, 'w') as outfile: json.dump(update_profile, outfile) # release lock lock.release_lock()
def write_cluster(cluster): if not os.path.isdir(globals.CONFIG_DIR): try: os.mkdir(globals.CONFIG_DIR) except: fail("failed to create directory: {}".format(globals.CONFIG_DIR)) # initialize locking - NOTE: make sure to release_local() prior to all exceptions/returns) lock = Lock(globals.CLUSTER_FILE_LOCK) # get lock (for concurrent datamodel access across) lock.get_lock() current_clusters = get_clusters(None) if len(current_clusters) == 0: current_clusters.append(cluster) with open(globals.CLUSTER_FILE, 'w') as outfile: json.dump(current_clusters, outfile) else: update_clusters = [] flag_found = False for c in current_clusters: if c['name'] == cluster['name']: update_clusters.append(cluster) flag_found = True else: update_clusters.append(c) if not flag_found: update_clusters.append(cluster) with open(globals.CLUSTER_FILE, 'w') as outfile: json.dump(update_clusters, outfile) # release lock lock.release_lock()
def test_locking(self): """Test wizard lock class""" self.log = logging.getLogger(inspect.currentframe().f_code.co_name) print(self.log) # make sure lock does not exist lock_file = "/tmp/wizard.lck" if os.path.isdir(lock_file): try: os.rmdir(lock_file) except: self.assertTrue(False) lock = Lock(lock_file) lock.get_lock() self.assertTrue(os.path.isdir(lock_file)) lock.release_lock() self.assertFalse(os.path.isdir(lock_file))
def write_host(host): if not os.path.isdir(globals.CONFIG_DIR): try: os.mkdir(globals.CONFIG_DIR) except: fail("failed to create directory: {}".format(globals.CONFIG_DIR)) # initialize locking - NOTE: make sure to release_local() prior to all exceptions/returns) lock = Lock(globals.HOST_FILE_LOCK) # get lock (for concurrent datamodel access across) lock.get_lock() # get all hosts current_hosts = get_hosts(None) if len(current_hosts) == 0: current_hosts.append(host) with open(globals.HOST_FILE, 'w') as outfile: json.dump(current_hosts, outfile) else: update_hosts = [] flag_found = False for h in current_hosts: if h['hostname'] == host['hostname'] and h['uuid'] == host['uuid']: update_hosts.append(host) flag_found = True elif h['hostname'] == host['hostname'] and h['ip'] == host[ 'ip'] and h['du_url'] == host['du_url']: update_hosts.append(host) flag_found = True else: update_hosts.append(h) if not flag_found: update_hosts.append(host) with open(globals.HOST_FILE, 'w') as outfile: json.dump(update_hosts, outfile) # release lock lock.release_lock()
def write_config(du): """Write config to disk""" # initialize locking - NOTE: make sure to release_local() prior to all exceptions/returns) lock = Lock(globals.CONFIG_FILE_LOCK) # get lock (for concurrent datamodel access across) lock.get_lock() # read du database if not os.path.isdir(globals.CONFIG_DIR): try: os.mkdir(globals.CONFIG_DIR) except: fail("failed to create directory: {}".format(globals.CONFIG_DIR)) current_config = get_configs() if len(current_config) == 0: current_config.append(du) with open(globals.CONFIG_FILE, 'w') as outfile: json.dump(current_config, outfile) else: update_config = [] flag_found = False for config in current_config: if config['url'] == du['url']: update_config.append(du) flag_found = True else: update_config.append(config) if not flag_found: update_config.append(du) with open(globals.CONFIG_FILE, 'w') as outfile: json.dump(update_config, outfile) # release lock lock.release_lock()