<commit_before>#!/usr/bin/env python3

from pathlib import Path

import yaml


SERVICE = """"
apiVersion: v1
kind: Service
metadata:
  name: {name}
  labels:
    name: {name}
spec:
  type: {service_type}
  ports:
  - port: {external_port}
    targetPort: {internal_port}
    protocol: TCP
    name: {name}
  selector:
    name: {name}
"""


HTTP_DEPLOYMENT = """
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{name}"
  labels:
    name: "{name}"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: "{name}"
    spec:
      containers:
      - name: {name}
        image: "{image}:latest"
        imagePullPolicy: Always
        ports:
        - containerPort: {port}
        livenessProbe:
          httpGet:
            path: /
            port: "{port}"
        readinessProbe:
          httpGet:
            path: /
            port: "{port}"
        env: {env}
"""


POSTGRES_DEPLOYMENT = """
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{name}"
  labels:
    name: "{name}"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: "{name}"
    spec:
      containers:
      - name: {name}
        image: "postgres:9.6"
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
"""


def ensure_requirements():
    """Make sure kubectl and minikube are available."""


def start_minikube():
    """Start minikube."""


def deploy(data):
    """Deploy current configuration to the minikube server."""
    for service in data:
        params = dict(name=service["name"])
        if service["type"] == "service":
            params["port"] = 80
            params["service_type"] = "NodePort"
            kubectl(params, [SERVICE, HTTP_DEPLOYMENT])
        elif service["type"] == "postgres":
            params["port"] = 5432
            params["service_type"] = "ClusterIP"
            kubectl(params, [SERVICE, POSTGRES_DEPLOYMENT])


def main():
    """Start minikube and deploy current config."""
    stacks = Path("stacks")
    for stack in stacks.iterdir():
        data = yaml.safe_read(stack.read_text())
        deploy(data)<commit_msg>Finish first pass of code.<commit_after>#!/usr/bin/env python3

from pathlib import Path
from subprocess import run
from tempfile import NamedTemporaryFile

import yaml


SERVICE = """"
apiVersion: v1
kind: Service
metadata:
  name: {name}
  labels:
    name: {name}
spec:
  type: {service_type}
  ports:
  - port: {external_port}
    targetPort: {internal_port}
    protocol: TCP
    name: {name}
  selector:
    name: {name}
"""


HTTP_DEPLOYMENT = """
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{name}"
  labels:
    name: "{name}"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: "{name}"
    spec:
      containers:
      - name: {name}
        image: "{image}:latest"
        imagePullPolicy: Always
        ports:
        - containerPort: {port}
        livenessProbe:
          httpGet:
            path: /
            port: "{port}"
        readinessProbe:
          httpGet:
            path: /
            port: "{port}"
        env: {env}
"""


POSTGRES_DEPLOYMENT = """
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{name}"
  labels:
    name: "{name}"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: "{name}"
    spec:
      containers:
      - name: {name}
        image: "postgres:9.6"
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
"""


def ensure_requirements():
    """Make sure kubectl and minikube are available."""
    uname = str(run(["uname"], stdout=PIPE, check=True).stdout.lower())
    for path, url in zip(
            [MINIKUBE, KUBECTL],
            ["https://storage.googleapis.com/minikube/releases/"
             "v0.13.1/minikube-{}-amd64",
             "https://storage.googleapis.com/kubernetes-release/"
             "release/v1.5.1/bin/{}/amd64/kubectl"]):
        if not path.exists():
            run(["curl", "--create-dirs", "-o", str(path), url.format(uname)])
            path.chmod(0o755)


def start_minikube():
    """Start minikube."""
    run([str(MINIKUBE), "start"])


def kubectl(params, configs):
    """Run kubectl on the given configs."""
    for config in configs:
        config = config.format(params)
        with NamedTemporaryFile(suffix="yaml") as f:
            f.write(yaml.safe_dump(config))
            f.close()
            run([str(KUBECTL), "apply", "-f", f.name], check=True)


def deploy(data):
    """Deploy current configuration to the minikube server."""
    for service in data:
        params = dict(name=service["name"])
        if service["type"] == "service":
            params["port"] = 80
            params["service_type"] = "NodePort"
            kubectl(params, [SERVICE, HTTP_DEPLOYMENT])
        elif service["type"] == "postgres":
            params["port"] = 5432
            params["service_type"] = "ClusterIP"
            kubectl(params, [SERVICE, POSTGRES_DEPLOYMENT])


def main():
    """Start minikube and deploy current config."""
    ensure_requirements()
    start_minikube()
    stacks = Path("stacks")
    for stack in stacks.iterdir():
        data = yaml.safe_read(stack.read_text())
        deploy(data)
{'fraction_non_alphanumeric': 0.13789195315451455, 'fraction_numerical': 0.01038911975821685, 'mean_word_length': 2.328095537397863, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2777097', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1688, 'n_words': 407}