<jupyter_start><jupyter_text>```pythonphysical_reservations = []for phys_res_str in parsed_args.physical_reservations:    err_msg = ("Invalid physical-reservation argument '%s'. "               "Reservation arguments must be of the "               "form --physical-reservation <min=int,max=int,"               "hypervisor_properties=str,resource_properties=str>"               % phys_res_str)    phys_res_info = {"min": "", "max": "", "hypervisor_properties": "",                     "resource_properties": ""}    prog = re.compile('^(\w+)=(\w+|\[[^]]+\])(?:,(.+))?$')    def parse_params(params):        match = prog.search(params)        if match:            self.log.info("Matches: %s", match.groups())            k, v = match.group(1, 2)            if k in phys_res_info:                phys_res_info[k] = v            else:                raise exception.IncorrectLease(err_msg)            if len(match.groups()) == 3 and match.group(3) is not None:                parse_params(match.group(3))    parse_params(phys_res_str)    if not phys_res_info['min'] and not phys_res_info['max']:        raise exception.IncorrectLease(err_msg)     NOTE(sbauza): The resource type should be conf-driven mapped with                   blazar.conf file but that's potentially on another                   host    phys_res_info['resource_type'] = 'physical:host'    physical_reservations.append(phys_res_info)if physical_reservations:    params['reservations'] += physical_reservations```<jupyter_code>BLAZAR_TIME_FORMAT = '%Y-%m-%d %H:%M'

def random_base32(n_bytes):
    tok = secrets.token_bytes(n_bytes)
    return base64.b32encode(tok).decode('ascii').strip('=')

def lease_create_args(name=None, start='now', length=60*60*24, nodes=1, resource_properties=''):
    if name is None:
        name = 'lease-{}'.format(random_base32(6))

    if start == 'now':
        start = datetime.datetime.now(tz=tz.tzutc()) + datetime.timedelta(seconds=70)
    if isinstance(length, numbers.Number):
        length = datetime.timedelta(seconds=length)
    
    end = start + length
        
    if resource_properties:
        resource_properties = json.dumps(resource_properties)

    reservations = [{
        'resource_type': 'physical:host',
        'resource_properties': resource_properties,
        'hypervisor_properties': '',
        'min': str(nodes), 'max': str(nodes),
    }]
        
    query = {
        'name': name,
        'start': start.strftime(BLAZAR_TIME_FORMAT),
        'end': end.strftime(BLAZAR_TIME_FORMAT), 
        'reservations': reservations,
        'events': [],
    }
    return query
node_types = {
    'compute',
    'compute_ib',
    'storage',
    'storage_hierarchy',
    'gpu_p100',
    'gpu_k80',
    'gpu_m40',
    'fpga',
    'lowpower_xeon',
    'atom',
    'arm64',
}

def lease_create_nodetype(*args, **kwargs):
    try:
        node_type = kwargs.pop('node_type')
    except KeyError:
        raise ValueError('no node_type specified')
    if node_type not in node_types:
        raise ValueError('unknown node_type ("{}")'.format(node_type))
    kwargs['resource_properties'] = ['=', '$node_type', node_type]
    return lease_create_args(*args, **kwargs)
args = lease_create_nodetype(node_type='compute')
args
my_lease = bc.lease.create(**args)
my_lease = bc.lease.get(my_lease['id'])#['status_reason']
my_lease['action'], my_lease['status'] == ('START', 'COMPLETE')
my_lease['id']
nc.images.find(name='CC-CentOS7')
nc.images.find(id='CC-CentOS7')
import novaclient
def resolve_image_idname(novaclient, idname):
    try:
        return novaclient.images.find(id=idname)
    except novaexceptions.NotFound:
        return novaclient.images.find(name=idname)
DEFAULT_IMAGE = '0f216b1f-7841-451b-8971-d383364e01a6'
DEFAULT_IMAGE = 'CC-CentOS7'

def instance_create_args(lease, name=None, image=DEFAULT_IMAGE, key=None):
    if name is None:
        name = 'instance-{}'.format(random_base32(6))

    return {
        'name': name,
        'flavor': 'baremetal',
        'image': image,
        # 'reservation_id': lease['reservations'][0]['id'],
        'scheduler_hints': {
            'reservation': lease['reservations'][0]['id'],
        },
        # 'nics': '', # automatically binds one, not needed unless want non-default?
        'key_name': key,
    }
from glanceclient.client import Client as GlanceClient
gc = GlanceClient('1', session=sess)
centos = gc.images.find(name='CC-CentOS7')
inst_image = resolve_image_idname(nc, 'CC-CentOS7')
server_args = instance_create_args(my_lease, key='<KEY>', image=inst_image)
print(server_args)
my_instance = nc.servers.create(**server_args)
my_instance
import uuid
inst_image = resolve_image_idname(nc, 'CC-CentOS7')
fail_args = instance_create_args(my_lease, key='<KEY>', image=inst_image)
fail_args['scheduler_hints']['reservation'] = str(uuid.uuid4())
print(fail_args)
my_failinstance = nc.servers.create(**fail_args)
my_failinstance
my_failinstance.get()
my_failinstance.fault
my_failinstance.id
import datetime
import time
datetime.datetime.now().isoformat()
for _ in range(200):
    time.sleep(10)
    my_instance.get()
    print('{} stat:{} upd:{}'.format(datetime.datetime.now().isoformat(), my_instance.status, my_instance.updated))
    if my_instance.status != 'BUILD':
        break
my_instance.update()
my_instance.to_dict()
my_instance.status
my_instance.delete()
my_oob = nc.servers.get('00dcccc7-2ba6-4965-a0ce-3fe901e933c3')
fips = nc.floating_ips.list()
fips
from neutronclient.v2_0.client import Client as NeutronClient
# from neutronclient.client import SessionClient as NeutronClient
# nec = NeutronClient(session=sess)
# nec.create_floatingip()
free_fips = list(fip for fip in nc.floating_ips.list() if fip.instance_id is None)
free_fips
for fip in free_fips:
    fip.delete()
def get_create_floatingip(nc):
    created = False
    ips = nc.floating_ips.list()
    unbound = (ip for ip in ips if ip.instance_id is None)
    try:
        fip = next(unbound)
    except StopIteration:
        fip = nc.floating_ips.create('ext-net')
        created = True
    return created, fip

created, fip = get_create_floatingip(nc)
from blazarclient.client import Client as _BlazarClient # installed from github

class BlazarClient(object):
    """
    Current BlazarClient doesn't support sessions, just a token, so it
    behaves poorly after its token expires. This is a thin wrapper that 
    recreates the real client every X minutes to avoid expiration.
    """
    def __init__(self, version, session):
        self._version = version
        self._session = session
        self._client_age = None
        self._create_client()

    def _create_client(self):
        self._bc = _BlazarClient(
            self._version, 
            blazar_url=self._session.get_endpoint(service_type='reservation'), 
            auth_token=self._session.get_token(),
        )
        self._client_age = time.monotonic()
        
    def __getattr__(self, attr):
        if time.monotonic() - self._client_age > 20*60:
            self._create_client()
        return getattr(self._bc, attr)
bc = BlazarClient('1', sess)
fip.delete()
bc.lease.delete('820bae2f-a58f-4205-840d-d58494de779a')
# pool = nc.floating_ip_pools.find(name='ext-net')

fip.ip
import fabric
created, fip = get_create_floatingip(nc)

my_oob.add_floating_ip(fip)
fip.get()
fip
my_instance.metadata['hello'] = 'world'
my_instance.to_dict()
bc.lease.get(my_lease['id'])['status_reason']
bc.lease.delete(gpu100_1['id'])
nc.servers.create()
my_inst = nc.servers.get('f41f9901-5b12-4cd0-9da5-94cc66b2655a')
gputest = nc.images.find(id='87224200-ea8c-4286-8e2b-e5ac5b089b36')
gputest
xenial = nc.images.find(name='CC-Ubuntu16.04')
my_inst.rebuild(xenial)
import errno
import time

from paramiko import SSHException
from fabric.network import NetworkError
my_inst.get()
my_inst.status
my_inst.rebuild(nc.images.find(name='test2-CC-Ubuntu16.04-CUDA8'))
old_status = my_inst.status
for _ in range(100):
    time.sleep(10)
    my_inst.get()
    if my_inst.status == 'ACTIVE':
        print()
        break
    old_status = my_inst.status
    print('.', end='')

print('{} {} -> {}'.format(datetime.datetime.now().isoformat(), old_status, my_inst.status))
while True:
    try:
        with fapi.settings(abort_on_prompts=True):
            fapi.run('', quiet=True)
        break
    except OSError as e:
        if e.errno == errno.ENETUNREACH:
            pass
        raise
    except (NetworkError, SSHException):
        pass
    print('.', end='')
    time.sleep(10)
fapi.run('touch /home/cc/.cloud-warnings.skip', quiet=True)
fapi.run('nvidia-smi')
print(f'glance --os-auth-url {sess.auth.auth_url} \
--os-user-id {sess.get_user_id()} \
--os-username {sess.auth.auth_ref.username} \
--os-auth-token {<PASSWORD>()} \
--os-project-id {sess.get_project_id()} \
image-list')
147456 / 1024 / 16
image_name = 'test4-CC-Ubuntu16.04-CUDA8'
image = nc.images.find(name=image_name)
with fcm.shell_env(**rc):#, fapi.cd('/home/cc/build'):
    out = fapi.run('glance image-download --file {}.qcow2 {}'.format(image_name, image.id))
#                    '--file /tmp/tmp.kTi8NRm9CS/common/CC-Ubuntu16.04.qcow2')
# fapi.run('mkdir mount')
fapi.sudo(f'guestmount -a /home/cc/{image_name}.qcow2 -m /dev/sdb1 /home/cc/mount')
from fabric import api as fapi
from fabric import context_managers as fcm

fapi.env.warn_only = True
fapi.env.use_ssh_config = True
user = fapi.env.user = 'cc'
host = fapi.env.host_string = '172.16.58.3'

image_loc = '/tmp/tmp.HXN40mQ9SY/common/CC-Ubuntu16.04-CUDA8.qcow2'

with fcm.shell_env(**rc):#, fapi.cd('/home/cc/build'):
    out = fapi.run('glance image-create '
                   '--name "test8-CC-Ubuntu16.04-CUDA8" '
                   '--disk-format qcow2 '
                   '--container-format bare '
                   '--file {}'.format(image_loc))
#                    '--file /tmp/tmp.kTi8NRm9CS/common/CC-Ubuntu16.04.qcow2')
image_data = {}
for line in out.splitlines():
    parts = [p.strip() for p in line.strip(' |\n\t').split('|')]
    if len(parts) != 2:
        continue
    key, value = parts
    if key == 'Property':
        continue
    image_data[key] = value
    
image_data['id']
my_inst.rebuild(image_data['id'])
my_inst.get()
my_inst.status
my_inst.image
nc.images.find(id=my_inst.image['id'])
my_inst.reboot()
fapi.run('lspci | grep -i nvid')<jupyter_output>[172.16.58.3] run: lspci | grep -i nvid
[172.16.58.3] out: 03:00.0 3D controller: NVIDIA Corporation Device 15f8 (rev a1)
[172.16.58.3] out: 82:00.0 3D controller: NVIDIA Corporation Device 15f8 (rev a1)
[172.16.58.3] out:
{'fraction_non_alphanumeric': 0.14464218099204848, 'fraction_numerical': 0.02811435062476335, 'mean_word_length': 3.409432387312187, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '324394', 'license': 'Apache-2.0', 'n_tokens_mistral': 3888, 'n_tokens_neox': 3665, 'n_words': 695, 'path': 'ChameleonLifecycle.ipynb', 'repo_name': 'ChameleonCloud/bag-o-hammers', 'size': '10598'}