Ejemplo n.º 1
0
def createhelmincluster(iotslice, cluster, helmpath, configfile, name):

    clusterhelmip = iotorchutils.getk8sclusterhelmip(cluster, configfile)

    clusterhelmport = iotorchutils.getk8sclusterhelmport(cluster, configfile)

    if clusterhelmip == None:
        print("IP Address for cluster %s not found" % cluster)
        return False

    releasename = name + "-" + iotslice

    chart = ChartBuilder({
        'name': name,
        'source': {
            'type': 'directory',
            'location': helmpath
        }
    })

    t = Tiller(host=clusterhelmip, port=clusterhelmport)

    t.install_release(chart.get_helm_chart(),
                      dry_run=False,
                      namespace=iotslice,
                      name=releasename)

    return True
Ejemplo n.º 2
0
Archivo: chart.py Proyecto: tclh123/dt
    def install(self,
                namespace,
                dry_run=False,
                name=None,
                values=None,
                wait=None):
        if self.sub:
            return [
                c.install(namespace, dry_run, name, values[i], wait)
                for i, c in enumerate(self.sub)
            ]

        try:
            chart = ChartBuilder(dict(name=self.name, source=self.source))
            res = self.get_tiller().install_release(chart.get_helm_chart(),
                                                    dry_run=dry_run,
                                                    namespace=namespace,
                                                    name=name,
                                                    values=values,
                                                    wait=wait)

            if not self.apply_extra_yaml(self.crd):
                logger.error('Failed to apply crd %s', self.crd)
            if not self.apply_extra_yaml(self.rbac):
                logger.error('Failed to apply rbac %s', self.rbac)
        except Exception:
            raise
        finally:
            chart.source_cleanup()

        return res
Ejemplo n.º 3
0
 def test_get_templates(self, _0, _1):
     cb = ChartBuilder({'name': '', 'source': {}})
     cb.source_directory = 'test'
     t = cb.get_templates()
     cb._logger.warn.assert_called()
     self.assertEquals(len(t), 1)
     self.assertIsInstance(t[0], Template)
 def getfiles(self):
     chart = ChartBuilder(
         {"name": self.chart_name, 
          "source": {
              "type": self.source_type, 
              "location": self.source_location
              }
         })
     return chart.get_files()
 def loadChart(self):
     chart = ChartBuilder(
         {"name": self.chart_name, 
          "source": {
              "type": self.source_type, 
              "location": self.source_location
              }
         })
     self.setChart(chart.get_helm_chart())
 def getvalues(self):
     chart = ChartBuilder(
         {"name": self.chart_name, 
          "source": {
              "type": self.source_type, 
              "location": self.source_location
              }
         })
     rawstring = chart.get_values()
     v = str(str(rawstring).split("raw: ",1)[1][1:][:-2].replace("\\n","\n"))
     ydata = yaml.safe_load(v)
     
     return ydata
Ejemplo n.º 7
0
def deploy():
    chart_path = from_repo('https://kubernetes-charts.storage.googleapis.com/',
                           'mariadb')
    #print(chart_path)
    chart = ChartBuilder({
        'name': 'mongodb',
        'source': {
            'type': 'directory',
            'location': '/tmp/pyhelm-dMLypC/mariadb'
        }
    })
    tiller_ins = Tiller('10.102.187.89', '44134')
    tiller_ins.install_release(chart.get_helm_chart(),
                               dry_run=False,
                               namespace='default')
    return "hello kuber"
Ejemplo n.º 8
0
 def test_unknown_type_with_parent(self):
     cb = ChartBuilder({'name': 'bar',
                        'parent': 'foo',
                        'source': {'location': 'test', 'type': 'none'}})
     self.assertIsNone(cb.source_directory)
     cb._logger.info.assert_called()
     cb._logger.exception.assert_called()
Ejemplo n.º 9
0
def deployTemplate(deploy_id):
    obj = Deployment.objects.get(id=deploy_id)
    # featureApp = obj.envTemplate.featureApp.all()
    tmp_dir = "/tmp/%s" % (obj.envName)
    if not os.path.exists(tmp_dir):
        os.mkdir(tmp_dir)

    images = obj.imageVersion.exclude(baseAppFlag=1)
    for image in images:
        chart_name = "%s-%s" % (obj.envName, image.chartAddress.split("/")[-1])
        repo = "/".join(image.chartAddress.split("/")[0:-1])

        t = Tiller(settings.TILLER_SERVER["address"], settings.TILLER_SERVER["port"])
        chart = ChartBuilder(
            {'name': chart_name, 'source': {'type': 'directory', 'location': "{}/helmCharts/{}/{}".format(settings.PROJECT_ROOT, repo.split("/")[-1], chart_name)}})
        # chart.get_metadata()
        print "{time} {fileName} {num} {func} {args} ".format(time=datetime.datetime.now(),
                                                              fileName=os.path.basename(__file__),
                                                              func=sys._getframe(
                                                              ).f_code.co_name,
                                                              num=sys._getframe().f_lineno,
                                                              args="start to install   {} chart in namespace  {}".format(
                                                                  chart_name, obj.envName))

        try:
            install_result = t.install_release(chart.get_helm_chart(), dry_run=False, namespace=obj.envName,
                                               name=chart_name,
                                               values={"service": {"type": "NodePort"}, "ingress": {"enabled": False}})

            print "{time} {fileName} {num} {func} {args} ".format(time=datetime.datetime.now(),
                                                                  fileName=os.path.basename(__file__),
                                                                  func=sys._getframe(
                                                                  ).f_code.co_name,
                                                                  num=sys._getframe().f_lineno,
                                                                  args="result of  installed chart: {}".format(
                                                                      install_result))
            DeployHistory.objects.create(deploy=obj, msg=str(install_result),
                                         chartTmpDir=chart_name)
        except Exception, e:
            print "{time} {fileName} {num} {func} {args} ".format(time=datetime.datetime.now(),
                                                                  fileName=os.path.basename(__file__),
                                                                  func=sys._getframe(
                                                                  ).f_code.co_name,
                                                                  num=sys._getframe().f_lineno,
                                                                  args="failed to install helm chart {} in  namespace {}, reson is {}".format(
                                                                      chart_name, obj.envName, e))
            return False
Ejemplo n.º 10
0
 def test_git(self, _0):
     cb = ChartBuilder({'name': 'foo',
                        'source': {'location': 'test',
                                   'type': 'git',
                                   'subpath': 'foo'}})
     self.assertEqual(cb.source_directory, '/test/foo')
     cb._logger.info.assert_called()
     cb._logger.exception.assert_not_called()
def install_release(chart_name, repo, namespace):
    """
    Installs a chart.

    Args:
        chart_name (str)
        repo (str)
        namespace (str)
    Returns:
        {
            'release': Name of the helm release,
            'status': Status of the installation
        }
    """
    try:
        chart_path = from_repo(repo, chart_name)
    except requests.RequestException as e:
        log(e)
        return {
            'Error': 'Helm repository unreachable.',
        }
    chart = ChartBuilder({
        'name': chart_name,
        'source': {
            'type': 'directory',
            'location': chart_path,
        },
    })
    try:
        tiller = get_tiller()
        response = tiller.install_release(chart.get_helm_chart(),
                                          dry_run=False,
                                          namespace=namespace)
        status_code = response.release.info.status.code
        return {
            'release': response.release.name,
            'status': response.release.info.status.Code.Name(status_code),
        }
    except grpc.RpcError as e:
        log(e.details())
        status_code = e.code()
        if grpc.StatusCode.UNAVAILABLE == status_code:
            return {
                'Error': 'Tiller unreachable.',
            }
Ejemplo n.º 12
0
 def get_chart(self, chart_name="", type="", location=""):
     chart = ChartBuilder({
         "name": chart_name,
         "source": {
             "type": type,
             "location": location
         }
     })
     return chart
Ejemplo n.º 13
0
 def test_source_cleanup(self, mock_repo):
     ChartBuilder({
         "name": "foo",
         "source": {
             "type": "directory",
             "location": "test"
         }
     }).source_cleanup()
     mock_repo.source_cleanup.assert_called()
Ejemplo n.º 14
0
 def test_source_cleanup(self, mock_repo):
     ChartBuilder({
         'name': 'foo',
         'source': {
             'type': 'directory',
             'location': 'test'
         }
     }).source_cleanup()
     mock_repo.source_cleanup.assert_called()
Ejemplo n.º 15
0
def chart_install(chart_data):
    """Third sample asynchronous task. Install a helm chart on a host"""
    chart = ChartBuilder(
        {
            "name": chart_data["name"],
            "source": {
                "type": chart_data["source_type"],
                "location": chart_data["source_location"],
            },
        }
    )
    if "dry_run" in chart_data:
        dry_run = True
    else:
        dry_run = False
    tiller = get_tiller(chart_data)
    return tiller.install_release(
        chart.get_helm_chart(), dry_run=dry_run, namespace=chart_data["namespace"]
    )
Ejemplo n.º 16
0
 def test_get_helm_chart_exists(self, _0, _1, _2, _3, _4):
     cb = ChartBuilder({'name': 'foo', 'source': {}, 'dependencies': [
         {'name': 'bar', 'source': {}}
     ]})
     cb._helm_chart = '123'
     self.assertEqual(cb.get_helm_chart(), '123')
     cb._helm_chart = None
     cb.get_helm_chart()
     cb._logger.info.assert_called()
Ejemplo n.º 17
0
 def test_directory(self):
     cb = ChartBuilder({
         'name': 'foo',
         'source': {
             'location': 'dir',
             'type': 'directory'
         }
     })
     self.assertEqual(cb.source_directory, 'dir/')
     cb._logger.info.assert_called()
     cb._logger.exception.assert_not_called()
Ejemplo n.º 18
0
 def test_directory(self):
     cb = ChartBuilder({
         "name": "foo",
         "source": {
             "location": "dir",
             "type": "directory"
         }
     })
     self.assertEqual(cb.source_directory, "dir/")
     cb._logger.info.assert_called()
     cb._logger.exception.assert_not_called()
Ejemplo n.º 19
0
 def test_repo(self, _0):
     cb = ChartBuilder({
         "name": "foo",
         "source": {
             "location": "test",
             "type": "repo"
         }
     })
     self.assertEqual(cb.source_directory, "/test/")
     cb._logger.info.assert_called()
     cb._logger.exception.assert_not_called()
Ejemplo n.º 20
0
 def test_unknown_type_with_parent(self):
     cb = ChartBuilder({
         "name": "bar",
         "parent": "foo",
         "source": {
             "location": "test",
             "type": "none"
         },
     })
     self.assertIsNone(cb.source_directory)
     cb._logger.info.assert_called()
     cb._logger.exception.assert_called()
    def createChart(self):
        chart = ChartBuilder(
            {"name": self.chart_name, 
             "source": {
                 "type": self.source_type, 
                 "location": self.source_location
                 }
            })

        rawstring = chart.get_values()
        v = str(str(rawstring).split("raw: ",1)[1][1:][:-2].replace("\\n","\n"))
        ydata = yaml.safe_load(v)    
        # print(ydata)
        ydata["host"] = "this.is.lit.local"
        newyaml = yaml.safe_dump(ydata)
        rawl = Config(raw=newyaml)

        dependencies = []

        
        helm_chart = Chart(
            metadata=chart.get_metadata(),
            templates=chart.get_templates(),
            dependencies=dependencies,
            values=rawl,
            files=chart.get_files(),
        )
        self.chart = helm_chart

        tiller = Tiller(self.tiller_host)
        tiller.install_release(
            self.chart, 
            dry_run=False, 
            namespace=self.kubernetes_namespace)
Ejemplo n.º 22
0
 def test_get_helm_chart_exists(self, _0, _1, _2, _3, _4):
     cb = ChartBuilder({
         "name": "foo",
         "source": {},
         "dependencies": [{
             "name": "bar",
             "source": {}
         }],
     })
     cb._helm_chart = "123"
     self.assertEqual(cb.get_helm_chart(), "123")
     cb._helm_chart = None
     cb.get_helm_chart()
     cb._logger.info.assert_called()
Ejemplo n.º 23
0
 def test_no_type(self):
     cb = ChartBuilder({'name': '', 'source': {}})
     self.assertIsNone(cb.source_directory)
     cb._logger.exception.assert_called()
Ejemplo n.º 24
0
def create_gcloud(num_workers, release, kubernetes_version, machine_type,
                  disk_size, num_cpus, num_gpus, gpu_type, zone, project,
                  preemptible, custom_value):
    from google.cloud import container_v1
    import google.auth
    from googleapiclient import discovery, http

    credentials, default_project = google.auth.default()

    if not project:
        project = default_project

    # create cluster
    gclient = container_v1.ClusterManagerClient()

    name = '{}-{}'.format(release, num_workers)
    name_path = 'projects/{}/locations/{}/'.format(project, zone)

    extraargs = {}

    if num_gpus > 0:
        extraargs['accelerators'] = [container_v1.types.AcceleratorConfig(
            accelerator_count=num_gpus, accelerator_type=gpu_type)]

    # delete existing firewall, if any
    firewalls = discovery.build(
        'compute', 'v1', cache_discovery=False).firewalls()

    existing_firewalls = firewalls.list(project=project).execute()
    fw_name = '{}-firewall'.format(name)

    if any(f['name'] == fw_name for f in existing_firewalls['items']):
        response = {}
        while not hasattr(response, 'status'):
            try:
                response = firewalls.delete(
                    project=project, firewall=fw_name).execute()
            except http.HttpError as e:
                if e.resp.status == 404:
                    response = {}
                    break
                click.echo("Wait for firewall to be available for deletion")
                sleep(5)
                response = {}
        while hasattr(response, 'status') and response.status < response.DONE:
            response = gclient.get_operation(
                None, None, None, name=response.selfLink)
            sleep(1)

    # create cluster
    cluster = container_v1.types.Cluster(
            name=name,
            initial_node_count=num_workers,
            node_config=container_v1.types.NodeConfig(
                machine_type=machine_type,
                disk_size_gb=disk_size,
                preemptible=preemptible,
                oauth_scopes=[
                    'https://www.googleapis.com/auth/devstorage.full_control',
                ],
                **extraargs
            ),
            addons_config=container_v1.types.AddonsConfig(
                http_load_balancing=container_v1.types.HttpLoadBalancing(
                    disabled=True,
                ),
                horizontal_pod_autoscaling=
                    container_v1.types.HorizontalPodAutoscaling(
                        disabled=True,
                    ),
                kubernetes_dashboard=container_v1.types.KubernetesDashboard(
                    disabled=True,
                ),
                network_policy_config=container_v1.types.NetworkPolicyConfig(
                    disabled=False,
                ),
            ),
            logging_service=None,
            monitoring_service=None
        )
    response = gclient.create_cluster(None, None, cluster, parent=name_path)

    # wait for cluster to load
    while response.status < response.DONE:
        response = gclient.get_operation(
            None, None, None, name=name_path + '/' + response.name)
        sleep(1)

    if response.status != response.DONE:
        raise ValueError('Cluster creation failed!')

    cluster = gclient.get_cluster(
        None, None, None, name=name_path + '/' + name)

    auth_req = google.auth.transport.requests.Request()
    credentials.refresh(auth_req)
    configuration = client.Configuration()
    configuration.host = f'https://{cluster.endpoint}:443'
    configuration.verify_ssl = False
    configuration.api_key = {'authorization': 'Bearer ' + credentials.token}
    client.Configuration.set_default(configuration)

    if num_gpus > 0:
        with request.urlopen(GCLOUD_NVIDIA_DAEMONSET) as r:
            dep = yaml.safe_load(r)
            dep['spec']['selector'] = {
                'matchLabels': dep['spec']['template']['metadata']['labels']
                }
            dep = client.ApiClient()._ApiClient__deserialize(dep, 'V1DaemonSet')
            k8s_client = client.AppsV1Api()
            k8s_client.create_namespaced_daemon_set('kube-system', body=dep)

    # create tiller service account
    client.CoreV1Api().create_namespaced_service_account(
        'kube-system',
        {
            'apiVersion': 'v1',
            'kind': 'ServiceAccount',
            'metadata': {
                'name': 'tiller',
                'generateName': 'tiller',
                'namespace': 'kube-system',
            },
        })

    client.RbacAuthorizationV1beta1Api().create_cluster_role_binding(
        {
            'apiVersion': 'rbac.authorization.k8s.io/v1beta1',
            'kind': 'ClusterRoleBinding',
            'metadata': {
                'name': 'tiller'
            },
            'roleRef': {
                'apiGroup': 'rbac.authorization.k8s.io',
                'kind': 'ClusterRole',
                'name': 'cluster-admin'
            },
            'subjects': [
                {
                    'kind': 'ServiceAccount',
                    'name': 'tiller',
                    'namespace': 'kube-system'
                }
            ]
        })

    # deploy tiller
    tiller_service = yaml.safe_load(TILLER_MANIFEST_SERVICE)
    tiller_dep = yaml.safe_load(TILLER_MANIFEST_DEPLOYMENT)
    client.CoreV1Api().create_namespaced_service(
        'kube-system',
        tiller_service)
    client.ExtensionsV1beta1Api().create_namespaced_deployment(
        'kube-system',
        tiller_dep)

    sleep(1)

    pods = client.CoreV1Api().list_namespaced_pod(
        namespace='kube-system',
        label_selector='app=helm'
    )

    tiller_pod = pods.items[0]

    while True:
        # Wait for tiller
        resp = client.CoreV1Api().read_namespaced_pod(
            namespace='kube-system',
            name=tiller_pod.metadata.name
        )
        if resp.status.phase != 'Pending':
            break
        sleep(5)

    # kubernetes python doesn't currently support port forward
    # https://github.com/kubernetes-client/python/issues/166
    ports = 44134

    # resp = stream(
    #     client.CoreV1Api().connect_get_namespaced_pod_portforward,
    #     name=tiller_pod.metadata.name,
    #     namespace=tiller_pod.metadata.namespace,
    #     ports=ports
    #     )

    with subprocess.Popen([
            'kubectl',
            'port-forward',
            '--namespace={}'.format(tiller_pod.metadata.namespace),
            tiller_pod.metadata.name, '{0}:{0}'.format(ports),
            '--server={}'.format(configuration.host),
            '--token={}'.format(credentials.token),
            '--insecure-skip-tls-verify=true']) as portforward:

        sleep(5)
        # install chart
        tiller = Tiller('localhost')
        chart = ChartBuilder(
            {
                'name': 'mlbench-helm',
                'source': {
                    'type': 'git',
                    'location': 'https://github.com/mlbench/mlbench-helm'
                }})

        values = {
            'limits': {
                'workers': num_workers - 1,
                'gpu': num_gpus,
                'cpu': num_cpus
            }
        }

        if custom_value:
            # merge custom values with values
            for cv in custom_value:
                key, v = cv.split("=", 1)

                current = values
                key_path = key.split(".")

                for k in key_path[:-1]:
                    if k not in current:
                        current[k] = {}

                    current = current[k]

                current[key_path[-1]] = v

        tiller.install_release(
            chart.get_helm_chart(),
            name=name,
            wait=True,
            dry_run=False,
            namespace='default',
            values=values)

        portforward.terminate()

    # open port in firewall
    mlbench_client = ApiClient(in_cluster=False, load_config=False)
    firewall_body = {
        "name": fw_name,
        "direction": "INGRESS",
        "sourceRanges": "0.0.0.0/0",
        "allowed": [
            {"IPProtocol": "tcp", "ports": [mlbench_client.port]}
        ]
    }

    firewalls.insert(project=project, body=firewall_body).execute()

    config = get_config()

    config.set('general', 'provider', 'gke')

    config.set('gke', 'cluster', cluster.endpoint)

    write_config(config)

    click.echo("MLBench successfully deployed")
Ejemplo n.º 25
0
 def test_get_templates(self, _0, _1, _2):
     t = ChartBuilder({'name': 'foo'}).get_templates()
     ChartBuilder._logger.warn.assert_called()
     self.assertEqual(len(t), 1)
     self.assertIsInstance(t[0], Template)
Ejemplo n.º 26
0
 def test_get_values(self, _0, _1, _2):
     v = ChartBuilder({}).get_values()
     self.assertIsInstance(v, Config)
Ejemplo n.º 27
0
 def test_get_values_not_found(self, _0):
     ChartBuilder({}).get_values()
     ChartBuilder._logger.warn.assert_called()
Ejemplo n.º 28
0
 def test_get_files(self, _0, _1, _2):
     f = ChartBuilder({}).get_files()
     self.assertEqual(len(f), 1)
     self.assertIsInstance(f[0], Any)
Ejemplo n.º 29
0
 def test_get_metadata(self, _0, _1):
     m = ChartBuilder({}).get_metadata()
     self.assertIsInstance(m, Metadata)
Ejemplo n.º 30
0
#!/usr/bin/python3
# By Tedezed
# Source: https://github.com/flaper87/pyhelm

from pyhelm.chartbuilder import ChartBuilder
from pyhelm.tiller import Tiller

import subprocess, time

tiller_host = '127.0.0.1'
mode_dry_run = False
mode_shell = True

subprocess.call('''kubectl port-forward $(kubectl get pod --all-namespaces | awk '{ print $2 }' | grep tiller) -n kube-system 44134:44134 &''', shell=mode_shell)
subprocess.call('''kubectl port-forward $(kubectl get pod --all-namespaces | awk '{ print $2 }' | grep tiller) -n kube-system 44135:44135 &''', shell=mode_shell)
time.sleep(3)

try:
	tiller = Tiller(tiller_host)
	chart = ChartBuilder({"name": "nginx-ingress", "source": {"type": "repo", "location": "https://kubernetes-charts.storage.googleapis.com"}})
	output_install = tiller.install_release(chart.get_helm_chart(), dry_run=mode_dry_run, name="test-tiller-python2", namespace="test-tiller-python")
	print(output_install)

	subprocess.call('''killall kubectl''', shell=mode_shell)
	print("[INFO] Success")

except Exception as e:
	subprocess.call('''killall kubectl''', shell=mode_shell)
	print("[ERROR] %s" % e)
Ejemplo n.º 31
0
# -*- coding:utf-8 -*-
#from pyhelm.repo import RepoUtils
from pyhelm.repo import from_repo
import pdb;pdb.set_trace()
from pyhelm.chartbuilder import ChartBuilder
from pyhelm.tiller import Tiller
import pdb;pdb.set_trace()
tiller_host='127.0.0.1'
tiller_port='44134'
#chart_path = RepoUtils.from_repo('https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts', 'mariadb')
chart_path = from_repo('https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts', 'mariadb')
chart = ChartBuilder({'name': 'mariadb', 'source': {'type': 'directory', 'location': chart_path}})
# 构建chart元数据  
#chart = ChartBuilder({'name': 'mongodb', 'source': {'type': 'directory', 'location': '/tmp/pyhelm-kibwtj8d/mongodb'}})  
#chart = ChartBuilder({'name': 'stable/mongodb', 'version':'0.4.27', 'source': {'type': 'repo', 'version': '0.4.27', 'location': 'https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts'}})
# 生成tiller连接实例  
tiller_ins = Tiller(tiller_host, tiller_port)  
import pdb;pdb.set_trace()
# 安装 release  
tiller_ins.install_release(chart.get_helm_chart(), dry_run=False, namespace='default')  
# 列出 release  
tiller_ins.list_releases(limit=0, status_codes=[], namespace=None)  
# 删除 release  
tiller_ins.uninstall_release(release_name)  
# 获得 release 版本历史  
tiller_ins.get_history(name, max=MAX_HISTORY) 
# 回滚 release  
tiller_ins.rollback_release(name, version, timeout=REQUEST_TIMEOUT)