<commit_before>"""
OpenShift cluster manager module that provides functionality to schedule jobs as well as
manage their state in the cluster.
"""

import yaml
import json
import os
from kubernetes import client as k_client
from openshift import client as o_client
from openshift import config


class OpenShiftManager(object):

    def __init__(self, project=None):
        self.openshift_client = None
        self.kube_client = None
        self.kube_v1_batch_client = None
        self.project = project or os.environ.get('OPENSHIFTMGR_PROJECT') or 'myproject'

        # init the openshift client
        self.init_openshift_client()

    def init_openshift_client(self):
        """
        Method to get a OpenShift client connected to remote or local OpenShift
        """
        kubecfg_path = os.environ.get('KUBECFG_PATH')
        if kubecfg_path is None:
            config.load_kube_config()
        else:
            config.load_kube_config(config_file=kubecfg_path)
        self.openshift_client = o_client.OapiApi()
        self.kube_client = k_client.CoreV1Api()
        self.kube_v1_batch_client = k_client.BatchV1Api()

    def schedule(self, image, command, name, mountdir=None):
        """
        Schedule a new job and returns the job object.
        """
        job_str = """
apiVersion: batch/v1
kind: Job
metadata:
    name: {name}
spec:
    parallelism: 1
    completions: 1
    activeDeadlineSeconds: 3600
    template:
        metadata:
            name: {name}
        spec:
            restartPolicy: Never
            initContainers:
            - name: init-storage
              image: fnndsc/ubuntu-python3
              command: ['sh', '-c', 'ls']
            containers:
            - name: publish
              image: fnndsc/ubuntu-python3
              command: ['sh', '-c', 'ls']
              lifecycle:
                preStop:
                  exec:
                    command: ['sh', '-c', 'ls', '-l']
            - name: {name}
              image: {image}
              command: {command}
""".format(name=name, command=str(command.split(" ")), image=image)

        if mountdir is not None:
            job_str = job_str + """
              volumeMounts:
              - mountPath: /share
                name: openshiftmgr-storage
            volumes:
            - name: openshiftmgr-storage
              hostPath:
                path: {mountdir}
""".format(mountdir=mountdir)

        job_yaml = yaml.load(job_str)
        job = self.kube_v1_batch_client.create_namespaced_job(namespace=self.project, body=job_yaml)
        return job
<commit_msg>Add shared emptydir to be shared between containers<commit_after>"""
OpenShift cluster manager module that provides functionality to schedule jobs as well as
manage their state in the cluster.
"""

import yaml
import json
import os
from kubernetes import client as k_client
from openshift import client as o_client
from openshift import config


class OpenShiftManager(object):

    def __init__(self, project=None):
        self.openshift_client = None
        self.kube_client = None
        self.kube_v1_batch_client = None
        self.project = project or os.environ.get('OPENSHIFTMGR_PROJECT') or 'myproject'

        # init the openshift client
        self.init_openshift_client()

    def init_openshift_client(self):
        """
        Method to get a OpenShift client connected to remote or local OpenShift
        """
        kubecfg_path = os.environ.get('KUBECFG_PATH')
        if kubecfg_path is None:
            config.load_kube_config()
        else:
            config.load_kube_config(config_file=kubecfg_path)
        self.openshift_client = o_client.OapiApi()
        self.kube_client = k_client.CoreV1Api()
        self.kube_v1_batch_client = k_client.BatchV1Api()

    def schedule(self, image, command, name):
        """
        Schedule a new job and returns the job object.
        """
        job_str = """
apiVersion: batch/v1
kind: Job
metadata:
  name: {name}
spec:
    parallelism: 1
    completions: 1
    activeDeadlineSeconds: 3600
    template:
        metadata:
          name: {name}
        spec:
            restartPolicy: Never
            initContainers:
            - name: init-storage
              image: fnndsc/ubuntu-python3
              command: ['touch', '/share/test']
              volumeMounts:
              - mountPath: /share
                name: shared-volume
            containers:
            - name: {name}
              image: {image}
              command: {command}
              volumeMounts:
              - mountPath: /share
                name: shared-volume
            - name: publish
              image: fnndsc/ubuntu-python3
              command: ['sh', '-c', 'ls -al /share']
              lifecycle:
                preStop:
                  exec:
                    command: ['touch', '/share/test']
              volumeMounts:
              - mountPath: /share
                name: shared-volume
""".format(name=name, command=str(command.split(" ")), image=image)
        job_str += """
            volumes:
            - name: shared-volume
              emptyDir: {}
"""

        job_yaml = yaml.load(job_str)
        job = self.kube_v1_batch_client.create_namespaced_job(namespace=self.project, body=job_yaml)
        return job

{'fraction_non_alphanumeric': 0.08945686900958466, 'fraction_numerical': 0.005262168765269686, 'mean_word_length': 1.925783397471138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3473195', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1491, 'n_words': 388}