<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#

import time

from kubernetes import K8sObject
from kubernetes.K8sExceptions import TimedOutException
from kubernetes.models.v1.PersistentVolume import PersistentVolume
from kubernetes.models.v1.Volume import Volume

READY_WAIT_TIMEOUT_SECONDS = 60


class K8sPersistentVolume(K8sObject):
    VALID_VOLUME_TYPES = filter(lambda x: x not in ['emptyDir', 'gitRepo', 'secret'], Volume.VOLUME_TYPES_TO_SOURCE_MAP.keys())

    def __init__(self, config=None, name=None, type=None):
        super(K8sPersistentVolume, self).__init__(config=config, name=name, obj_type='PersistentVolume')
        self._type = None
        self.model = PersistentVolume()
        self.name = name
        self.type = type

    # ------------------------------------------------------------------------------------- api calls

    def create(self):
        super(K8sPersistentVolume, self).create()
        self._wait_for_available()
        return self

    def get(self):
        self.model = PersistentVolume(model=self.get_model())
        return self

    def _wait_for_available(self):
        start_time = time.time()
        while not self.model.status.phase == 'Available':
            self.get()
            elapsed_time = time.time() - start_time
            if elapsed_time >= READY_WAIT_TIMEOUT_SECONDS:  # timeout
                raise TimedOutException(
                    "Timed out waiting on readiness of PersistentVolume: [ {} ]".format(self.name)
                )

    # ------------------------------------------------------------------------------------- name

    @property
    def name(self):
        return self.model.metadata.name

    @name.setter
    def name(self, name=None):
        self.model.metadata.name = name
        self.model.metadata.labels['name'] = name

    # ------------------------------------------------------------------------------------- type

    @property
    def type(self):
        return self._type

    @type.setter
    def type(self, t=None):
        if t not in self.VALID_VOLUME_TYPES:
            raise SyntaxError('K8sPersistentVolume: type: [ {} ] is invalid.'.format(t))
        self._type = t
        setattr(self.model.spec, t, Volume.vol_type_to_source(t))

    # ------------------------------------------------------------------------------------- source

    @property
    def source(self):
        return getattr(self.model.spec, self._type, None)

    @source.setter
    def source(self, s=None):
        raise NotImplementedError()

    # ------------------------------------------------------------------------------------- accessModes

    @property
    def access_modes(self):
        return self.model.spec.access_modes

    @access_modes.setter
    def access_modes(self, modes=None):
        self.model.spec.access_modes = modes

    # ------------------------------------------------------------------------------------- capacity

    @property
    def capacity(self):
        return self.model.spec.capacity

    @capacity.setter
    def capacity(self, cap=None):
        self.model.spec.capacity = cap

    # ------------------------------------------------------------------------------------- path (hostPath)

    @property
    def path(self):
        if not hasattr(self.source, 'path'):
            raise NotImplementedError()
        return self.source.path

    @path.setter
    def path(self, p=None):
        if not hasattr(self.source, 'path'):
            raise NotImplementedError()
        self.source.path = p

    # ------------------------------------------------------------------------------------- volume_id (AWS)

    # http://kubernetes.io/docs/user-guide/volumes/#awselasticblockstore
    # - the nodes on which pods are running must be AWS EC2 instances
    # - those instances need to be in the same region and availability-zone as the EBS volume
    # - EBS only supports a single EC2 instance mounting a volume

    # Pod creation will timeout waiting for readiness if not on AWS; unschedulable.

    @property
    def volume_id(self):
        if not hasattr(self.source, 'volume_id'):
            raise NotImplementedError()
        return self.source.volume_id

    @volume_id.setter
    def volume_id(self, vid=None):
        if not hasattr(self.source, 'volume_id'):
            raise NotImplementedError()
        self.source.volume_id = vid

    # ------------------------------------------------------------------------------------- pd_name (GCE)

    # http://kubernetes.io/docs/user-guide/volumes/#gcepersistentdisk
    # - the nodes on which pods are running must be GCE VMs
    # - those VMs need to be in the same GCE project and zone as the PD

    # Pod creation will timeout waiting for readiness if not on GCE; unschedulable.

    @property
    def pd_name(self):
        if not hasattr(self.source, 'pd_name'):<commit_msg>K8sPersistentVolume: move comments out to README.md<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#

import time

from kubernetes import K8sObject
from kubernetes.K8sExceptions import TimedOutException
from kubernetes.models.v1.PersistentVolume import PersistentVolume
from kubernetes.models.v1.Volume import Volume

READY_WAIT_TIMEOUT_SECONDS = 60


class K8sPersistentVolume(K8sObject):
    VALID_VOLUME_TYPES = filter(lambda x: x not in ['emptyDir', 'gitRepo', 'secret'], Volume.VOLUME_TYPES_TO_SOURCE_MAP.keys())

    def __init__(self, config=None, name=None, type=None):
        super(K8sPersistentVolume, self).__init__(
            config=config,
            name=name,
            obj_type='PersistentVolume'
        )

        self._type = None
        self.model = PersistentVolume()
        self.name = name
        self.type = type

    # ------------------------------------------------------------------------------------- api calls

    def create(self):
        super(K8sPersistentVolume, self).create()
        self._wait_for_available()
        return self

    def get(self):
        self.model = PersistentVolume(model=self.get_model())
        return self

    def _wait_for_available(self):
        start_time = time.time()
        while not self.model.status.phase == 'Available':
            self.get()
            elapsed_time = time.time() - start_time
            if elapsed_time >= READY_WAIT_TIMEOUT_SECONDS:  # timeout
                raise TimedOutException(
                    "Timed out waiting on readiness of PersistentVolume: [ {} ]".format(self.name)
                )

    # ------------------------------------------------------------------------------------- name

    @property
    def name(self):
        return self.model.metadata.name

    @name.setter
    def name(self, name=None):
        self.model.metadata.name = name
        self.model.metadata.labels['name'] = name

    # ------------------------------------------------------------------------------------- type

    @property
    def type(self):
        return self._type

    @type.setter
    def type(self, t=None):
        if t not in self.VALID_VOLUME_TYPES:
            raise SyntaxError('K8sPersistentVolume: type: [ {} ] is invalid.'.format(t))
        self._type = t
        setattr(self.model.spec, t, Volume.vol_type_to_source(t))

    # ------------------------------------------------------------------------------------- source

    @property
    def source(self):
        return getattr(self.model.spec, self._type, None)

    @source.setter
    def source(self, s=None):
        raise NotImplementedError()

    # ------------------------------------------------------------------------------------- accessModes

    @property
    def access_modes(self):
        return self.model.spec.access_modes

    @access_modes.setter
    def access_modes(self, modes=None):
        self.model.spec.access_modes = modes

    # ------------------------------------------------------------------------------------- capacity

    @property
    def capacity(self):
        return self.model.spec.capacity

    @capacity.setter
    def capacity(self, cap=None):
        self.model.spec.capacity = cap

    # ------------------------------------------------------------------------------------- path (hostPath)

    @property
    def path(self):
        if not hasattr(self.source, 'path'):
            raise NotImplementedError()
        return self.source.path

    @path.setter
    def path(self, p=None):
        if not hasattr(self.source, 'path'):
            raise NotImplementedError()
        self.source.path = p

    # ------------------------------------------------------------------------------------- volume_id (AWS)

    @property
    def volume_id(self):
        if not hasattr(self.source, 'volume_id'):
            raise NotImplementedError()
        return self.source.volume_id

    @volume_id.setter
    def volume_id(self, vid=None):
        if not hasattr(self.source, 'volume_id'):
            raise NotImplementedError()
        self.source.volume_id = vid

    # ------------------------------------------------------------------------------------- pd_name (GCE)

    @property
    def pd_name(self):
        if not hasattr(self.source, 'pd_name'):
{'fraction_non_alphanumeric': 0.24401356219538037, 'fraction_numerical': 0.002860775588048315, 'mean_word_length': 1.4885315054046928, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4419657', 'n_tokens_mistral': 2470, 'n_tokens_neox': 2383, 'n_words': 596}