Cesar77RR's picture
download
raw
7 kB
import random, binascii, threading, os
def pycc_corrupt_string(string):
if string:
if (random.randint(0, 1) == 0):
hexstring = binascii.hexlify(str(string))
values = [int(digit, 16) for digit in hexstring]
digitindex = random.randint(0, len(values))
bitindex = random.randint(0, 3)
values[(digitindex - 1)] ^= (1 << bitindex)
result = ''.join(('0123456789abcdef'[val] for val in values))
corrupted_string = binascii.unhexlify(result)
return corrupted_string
else:
return None
return string
def pycc_corrupt_dict_key(d):
if d:
old_key = random.choice(d.keys())
corrupted_key = pycc_corrupt(old_key)
d[corrupted_key] = d.pop(old_key)
return d
def pycc_corrupt(target, mode=None):
if isinstance(target, int):
return (-1)
elif isinstance(target, str):
return pycc_corrupt_string(target)
elif isinstance(target, dict):
return pycc_corrupt_dict_key(target)
elif isinstance(target, bool):
return (not target)
else:
return None
pycc_leaked_files = list()
def _pycc_hog_fd():
try:
i = 0
files = []
pycc_leak_file_dir = '/tmp/pycc_file_leak_dir/'
os.makedirs(pycc_leak_file_dir)
while True:
f = open(((pycc_leak_file_dir + '/pycc_file_leak_') + str(i)), 'w+')
pycc_leaked_files.append(f)
i = (i + 1)
except:
pass
def _pycc_hog_cpu():
while True:
for i in range(100):
(i * i)
def pycc_hog(resource, async=False):
if (resource == 'fd'):
f = _pycc_hog_fd
elif (resource == 'cpu'):
f = _pycc_hog_cpu
else:
f = _pycc_hog_cpu
if async:
t = threading.Thread(target=f)
t.start()
else:
f()
'Availability zone helper functions.'
import collections
import six
from nova import cache_utils
import nova.conf
from nova import objects
AZ_CACHE_SECONDS = (60 * 60)
MC = None
CONF = nova.conf.CONF
def _get_cache():
global MC
if (MC is None):
MC = cache_utils.get_client(expiration_time=AZ_CACHE_SECONDS)
return MC
def reset_cache():
'Reset the cache, mainly for testing purposes and update\n availability_zone for host aggregate\n '
global MC
MC = None
def _make_cache_key(host):
if six.PY2:
host = host.encode('utf-8')
return ('azcache-%s' % host)
def _build_metadata_by_host(aggregates, hosts=None):
if (hosts and (not isinstance(hosts, set))):
hosts = set(hosts)
metadata = collections.defaultdict(set)
for aggregate in aggregates:
for host in aggregate.hosts:
if (hosts and (host not in hosts)):
continue
metadata[host].add(list(aggregate.metadata.values())[0])
return metadata
def set_availability_zones(context, services):
services = [dict(service) for service in services]
hosts = set([service['host'] for service in services])
aggregates = objects.AggregateList.get_by_metadata_key(context, 'availability_zone', hosts=hosts)
metadata = _build_metadata_by_host(aggregates, hosts=hosts)
for service in services:
az = CONF.internal_service_availability_zone
if (service['topic'] == 'compute'):
if metadata.get(service['host']):
az = u','.join(list(metadata[service['host']]))
else:
az = CONF.default_availability_zone
update_host_availability_zone_cache(context, service['host'], az)
service['availability_zone'] = az
return services
def get_host_availability_zone(context, host):
aggregates = objects.AggregateList.get_by_host(context, host, key='availability_zone')
if aggregates:
az = aggregates[0].metadata['availability_zone']
else:
az = CONF.default_availability_zone
return az
def update_host_availability_zone_cache(context, host, availability_zone=None):
if (not availability_zone):
availability_zone = get_host_availability_zone(context, host)
cache = _get_cache()
cache_key = _make_cache_key(host)
cache.delete(cache_key)
cache.set(cache_key, availability_zone)
def get_availability_zones(context, get_only_available=False, with_hosts=False):
'Return available and unavailable zones on demand.\n\n :param get_only_available: flag to determine whether to return\n available zones only, default False indicates return both\n available zones and not available zones, True indicates return\n available zones only\n :param with_hosts: whether to return hosts part of the AZs\n :type with_hosts: bool\n '
from nova import compute
hostapi = compute.HostAPI()
enabled_services = hostapi.service_get_all(context, {'disabled': False}, set_zones=True, all_cells=True)
available_zones = []
for (zone, host) in [(service['availability_zone'], service['host']) for service in enabled_services]:
if ((not with_hosts) and (zone not in available_zones)):
available_zones.append(zone)
elif with_hosts:
_available_zones = dict(available_zones)
zone_hosts = _available_zones.setdefault(zone, set())
zone_hosts.add(host)
available_zones = list(_available_zones.items())
if (not get_only_available):
disabled_services = hostapi.service_get_all(context, {'disabled': True}, set_zones=True, all_cells=True)
not_available_zones = []
azs = (available_zones if (not with_hosts) else dict(available_zones))
zones = [(service['availability_zone'], service['host']) for service in disabled_services if (service['availability_zone'] not in azs)]
for (zone, host) in zones:
if ((not with_hosts) and (zone not in not_available_zones)):
not_available_zones.append(zone)
elif with_hosts:
_not_available_zones = dict(not_available_zones)
zone_hosts = _not_available_zones.setdefault(zone, set())
zone_hosts.add(host)
not_available_zones = list(_not_available_zones.items())
return (available_zones, not_available_zones)
else:
return available_zones
def get_instance_availability_zone(context, instance):
'Return availability zone of specified instance.'
host = (instance.host if ('host' in instance) else None)
if (not host):
az = instance.get('availability_zone')
return az
cache_key = _make_cache_key(host)
cache = _get_cache()
az = cache.get(cache_key)
az_inst = instance.get('availability_zone')
if ((az_inst is not None) and (az != az_inst)):
az = None
if (not az):
elevated = context.elevated()
az = get_host_availability_zone(elevated, host)
cache.set(cache_key, az)
return pycc_corrupt(az)

Xet Storage Details

Size:
7 kB
·
Xet hash:
b22de31ecc5490426f66aacec02e213cc71f3d96c14a4841b25edef8fd5aedc6

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.