Exemplo n.º 1
0
def deleteDployTemplate(deploy_id):
    if Deployment.objects.filter(id=deploy_id):
        obj = Deployment.objects.get(id=deploy_id)
        featureApp = obj.envTemplate.featureApp.all()
        tmp_dir = "/tmp/%s" % (obj.envName)
        t = Tiller(settings.TILLER_SERVER["address"], settings.TILLER_SERVER["port"])

        if featureApp:
            for app in featureApp:
                if obj.imageVersion.filter(appName=app.name).exists():
                    image = obj.imageVersion.filter(appName=app.name)[0]
                    chart_name = image.chartAddress.split("/")[-1]
                    delete_rs = t.uninstall_release(release="%s-%s" % (obj.envName, chart_name))
                    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 uninstall_release {} : {} ".format(
                                                                              "%s-%s" % (obj.envName, chart_name),
                                                                              delete_rs))
        crt = DeployHistory.objects.create(deploy=obj, msg=u"删除环境%s" % obj.envName,
                                           chartTmpDir=os.path.join(tmp_dir, "deploy_record.log"))
        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 DeployHistory.objects.create: ".format(
                                                                  crt))

        return True
    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)
Exemplo n.º 3
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
 def listCharts(self):
     tiller = Tiller(self.tiller_host)
     chartData = tiller.list_charts()
     for x in chartData:
         find_release_name = re.findall("'(.*?)'",str(str(x).splitlines()[0]).partition('\n')[0])
         release_name = find_release_name[0]
         find_chart_name = re.findall('"([^"]*)"',str(str(x).splitlines()[1]).partition('\n')[0])
         chart_name = find_chart_name[0]
         self.chartReleaseNameDict[release_name] = chart_name
 def loadReleasesForNamespaceDict(self):
     tiller = Tiller(self.tiller_host)
     releaseData = tiller.list_releases(namespace=self.kubernetes_namespace)
     for x in releaseData:
         length_lines = len(str(x).split('\n'))
         find_namespace = re.findall('"([^"]*)"',str(str(x).splitlines()[length_lines-2]).partition('\n')[0])
         namespace = find_namespace[0]
         find_name = re.findall('"([^"]*)"',str(x).partition('\n')[0])
         release_name = find_name[0]
         self.releaseNamespaceDict[release_name] = namespace
Exemplo n.º 6
0
def get_tiller(data):
    """Use connection details for tiller as given by...
       > kubectl describe deploy tiller-deploy --namespace=kube-system
       TLS setup is required for access from outside the Kubernetes cluster
    """
    tls = data.get("tls", "")
    if tls:
        return Tiller(data["host"], port=data["port"])
    else:
        return Tiller(data["host"], port=data["port"], tls_config=tls)
Exemplo n.º 7
0
def deletehelmincluster(iotslice, cluster, configfile, name):

    clusterhelmip = iotorchutils.getk8sclusterhelmip(cluster, configfile)

    clusterhelmport = iotorchutils.getk8sclusterhelmport(cluster, configfile)

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

    releasename = name + "-" + iotslice

    t.uninstall_release(release=releasename)

    return True
Exemplo n.º 8
0
def main(args=None):
    parser = cmd.get_parser()
    args = parser.parse_args(args)

    logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
                        level=logging.DEBUG if args.debug else logging.INFO)

    config_str = ', '.join(f'{k}={v}' for k, v in sorted(vars(args).items()))
    logger.info(f'Janitor v{__version__} started with {config_str}')

    if args.dry_run:
        logger.info('**DRY-RUN**: no deletions will be performed!')

    if args.rules_file:
        rules = load_rules_from_file(args.rules_file)
        logger.info(f'Loaded {len(rules)} rules from file {args.rules_file}')
    else:
        rules = []

    if args.tiller_host and args.tiller_port:
        tiller = Tiller(args.tiller_host, args.tiller_port)
    else:
        tiller = None

    return run_loop(args.once, args.include_resources, args.exclude_resources,
                    args.include_namespaces, args.exclude_namespaces, rules,
                    args.interval, args.delete_notification, args.dry_run,
                    tiller)
Exemplo n.º 9
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"
Exemplo n.º 10
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
Exemplo n.º 11
0
class HelmApi(object):
    def __init__(self):
        #nginx-ingress asign tcp 38888 to tiller-deploy 43134
        self.tiller = Tiller("192.168.21.145", port=38888)

    #type=repo,directory,git
    def get_chart(self, chart_name="", type="", location=""):
        chart = ChartBuilder({
            "name": chart_name,
            "source": {
                "type": type,
                "location": location
            }
        })
        return chart

    def install_release(self, chart, namespace="", release_name=""):
        if hasattr(chart, "get_helm_chart"):
            self.tiller.install_release(chart.get_helm_chart(),
                                        dry_run=False,
                                        namespace=namespace,
                                        name=release_name)
        return

    def upgrade_release(self, chart, namespace="", release_name=""):
        if hasattr(chart, "get_helm_chart"):
            self.tiller.update_release(chart.get_helm_chart(),
                                       dry_run=False,
                                       namespace=namespace,
                                       name=release_name)
        return

    def delete_release(self, release_name=""):
        self.tiller.uninstall_release(release_name)
        return
Exemplo n.º 12
0
def helm_list(request):
    ins = Tiller(K8S_HOST, K8S_PORT)
    if request.method == 'GET':
        # print 'GET'
        # chart_path = RepoUtils.from_repo(CHART_REPO,'jenkins')
        # print chart_path


        action = request.GET.get('action', False)
        name = request.GET.get('name', None)
        if action == 'detail':
            release_status = ins.get_release_status(name)
            release_content = ins.get_release_content(name)
            release_history = ins.get_history(name)
            # print release_history.releases[0]
            resource = [
                {
                    "type": yaml.load(x)['kind'],
                    "content": x
                }
                for x in release_content.release.manifest.split('---') if yaml.load(x)
            ]
            # print resource
            # resource = [ x for x in release_content.release.manifest.split('---')]
            # release_history.releases[0]
            return render(
                request,'helm_detail.html',
                {
                    "status": release_status,
                    "content": release_content,
                    "history": release_history.releases,
                    "resource": resource,
                    "request": request
                }

            )
        list = ins.list_releases()
        return render(
            request,'helm_publish.html',
            {
                "list_name": list,
                "request": request
            })
    action = request.POST.get('action', None)
    name = request.POST.get('name',None)
    if action == 'rollback':
        version = request.POST.get('version',None)
        # print type(version)
        try:
            rollback_result = ins.rollback_release(name=name,version=int(version))
        except Exception,e:
            print e
        print rollback_result
        return JsonResponse({'result': rollback_result})
Exemplo n.º 13
0
    def get_releases(self):
        if not hasattr(self, 'releases'):
            tiller = Tiller('127.0.0.1')
            self.releases = tiller.list_releases(namespace=self.namespace)

        return self.releases
Exemplo n.º 14
0
def deployCommonApp(deploy_id):
    obj = Deployment.objects.get(id=deploy_id)
    charts = obj.commonApp.split(",")
    repo = settings.LOCAL_HARBOR_LIBRARY_REPO
    if charts:
        for ch in charts:
            chart_name = ch.strip()
            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="chart_name of deploy  :  {}".format(
                                                                      chart_name))
            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)}})
            try:
                if chart_name == "redis":
                    install_result = t.install_release(chart.get_helm_chart(), dry_run=False, namespace=obj.envName,
                                                       name=chart_name,
                                                       values={
                                                           "master": {
                                                               "service": {
                                                                   "type": "NodePort"
                                                               }
                                                           },
                                                           "slave": {
                                                               "service": {
                                                                   "type": "NodePort"
                                                               }
                                                           },
                                                           "ingress": {
                                                               "enabled": False
                                                           }
                                                       })
                elif chart_name == "nginx":
                    install_result = "nginx pass ~ "
                else:
                    install_result = t.install_release(chart.get_helm_chart(), dry_run=False, namespace=obj.envName,
                                                       name=chart_name,
                                                       values={
                                                           "service": {
                                                               "type": "Cluster"
                                                           },
                                                           "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
 def uninstallRelease(self, release_name):
     tiller = Tiller(self.tiller_host)
     tiller.uninstall_release(release=release_name, disable_hooks=False, purge=True)
Exemplo n.º 16
0
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)
Exemplo n.º 17
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")
def get_tiller():
    """
    Creates a tiller object.
    """
    tiller_host, tiller_port = unitdata.kv().get('tiller-service').split(':')
    return Tiller(host=tiller_host, port=tiller_port)
Exemplo n.º 19
0
from pyhelm.chartbuilder import ChartBuilder
from pyhelm.tiller import Tiller

tiller = Tiller(TILLER_HOST)
chart = ChartBuilder({
    "name": "nginx-ingress",
    "source": {
        "type": "repo",
        "location": "https://kubernetes-charts.storage.googleapis.com"
    }
})
tiller.install_release(chart.get_helm_chart(),
                       dry_run=False,
                       namespace='default')
Exemplo n.º 20
0
def deployBaseApp(deploy_id):
    obj = Deployment.objects.get(id=deploy_id)
    images = obj.imageVersion.exclude(baseAppFlag=0)
    for image in images:
        appName = image.chartAddress.split("/")[-1]
        chart_name = "%s-%s" % (obj.envName, appName)
        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)}})
        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:
            if chart_name == "evo-rcs":
                install_result = t.install_release(chart.get_helm_chart(), dry_run=False, namespace=obj.envName,
                                                   name=chart_name,
                                                   values={
                                                       "env": {
                                                           "SPRING_PROFILES_ACTIVE": "k8s",
                                                           "db_url": "mysql:3306",
                                                           "db_username": "******",
                                                           "db_password": "******",
                                                           "registry": "registry:8761",
                                                           "host_ip": "",
                                                           "redis_host": "redis",
                                                           "release_name": appName,
                                                           "rmqnamesrv": "rmq:9876"
                                                       },
                                                       "ingress": {
                                                           "enabled": False,
                                                           "hosts": []
                                                       },
                                                       "service": {
                                                           "type": "NodePort"
                                                       }
                                                   })
            else:
                install_result = t.install_release(chart.get_helm_chart(), dry_run=False, namespace=obj.envName,
                                                   name=chart_name,
                                                   values={
                                                       "env": {
                                                           "SPRING_PROFILES_ACTIVE": "k8s",
                                                           "db_url": "mysql:3306",
                                                           "db_username": "******",
                                                           "db_password": "******",
                                                           "registry": "registry:8761",
                                                           "host_ip": "",
                                                           "redis_host": "redis",
                                                           "release_name": appName,
                                                           "rmqnamesrv": "rmq:9876"
                                                       },
                                                       "ingress": {
                                                           "enabled": False,
                                                           "hosts": []
                                                       },
                                                       "service": {
                                                           "type": "Cluster"
                                                       }
                                                   })

            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
Exemplo n.º 21
0
Arquivo: chart.py Projeto: tclh123/dt
 def get_tiller(self):
     return Tiller(TILLER_HOST)
Exemplo n.º 22
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 google.auth.exceptions import DefaultCredentialsError
    from googleapiclient import discovery, http

    try:
        credentials, default_project = google.auth.default()
    except DefaultCredentialsError:
        raise click.UsageError(
            "Couldn't find gcloud credentials. Install the gcloud"
            " sdk ( https://cloud.google.com/sdk/docs/quickstart-linux ) and "
            "run 'gcloud auth application-default login' to login and create "
            "your credentials.")

    assert num_workers >= 2, "Number of workers should be at least 2"

    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")
Exemplo n.º 23
0
 def __init__(self):
     #nginx-ingress asign tcp 38888 to tiller-deploy 43134
     self.tiller = Tiller("192.168.21.145", port=38888)
Exemplo n.º 24
0
def InstallNginxChartNew(envName):
    deploy = Deployment.objects.filter(envName=envName)
    if deploy:
        images = deploy[0].imageVersion.all()
    appList = []
    for image in images:
        appList.append(image.name.split(":")[0])
    os.system(
        "/bin/rm -rf /usr/local/devops/flashholdDevops/tmp/{}-nginx".format(
            envName))

    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="appList: {}".format(appList))

    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="copy nginx chart to {}  ".format(envName))

    try:
        shutil.copytree(
            "{}/helmCharts/library/nginx".format(settings.PROJECT_ROOT),
            "{}/tmp/{}-nginx".format(settings.PROJECT_ROOT, envName))
    except OSError as exc:  # python >2.5
        if exc.errno == errno.ENOTDIR:
            shutil.copy(
                "{}/helmCharts/library/nginx".format(settings.PROJECT_ROOT),
                "{}/tmp/{}-nginx".format(settings.PROJECT_ROOT, envName))
        else:
            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="Exception when Create nginx chart: %s\n" % exc)
            return 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="start to create evo.conf and  upstream.conf ")

    try:
        evoConf = "{}/tmp/{}-nginx/conf/evo.conf".format(
            settings.PROJECT_ROOT, envName)
        upstreamConf = "{}/tmp/{}-nginx/conf/upstream.conf".format(
            settings.PROJECT_ROOT, envName)
        if os.path.exists(evoConf):
            os.remove(evoConf)

        if os.path.exists(upstreamConf):
            os.remove(upstreamConf)

        wconf = open(evoConf, "a+")
        upconf = open(upstreamConf, "a+")

        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="写evo.conf第一部分 ")

        wconf.write(first_content)

        for app in appList:
            if ":" in app:
                appName = app.split(":")[0]
            else:
                appName = app

            # if appName == "evo-agv-simulation-carrier":
            #     continue

            if not appName in settings.SERVICE_PORT.keys():
                continue

            appPort = str(settings.SERVICE_PORT[appName])
            appPrefix = appName.replace("evo-", "")

            bb = second_conf.replace("{appName}", appName).replace(
                "{appPort}", appPort).replace("{appPrefix}", appPrefix)
            if appName == "evo-basic":
                bb = basic_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)
            if appName == "evo-station":
                bb = station_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)
            if appName == "evo-agv-simulation-carrier":
                bb = agv_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)

            if appName == "evo-wcs-g2p":
                bb = evo_wcs_g2p_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)

            if appName == "evo-wcs-si":
                bb = evo_wcs_si_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)

            if appName == "evo-wcs-engine":
                bb = evo_wcs_engine_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)

            if appName == "evo-auth":
                bb = evo_auth_conf.replace("{appName}", appName).replace(
                    "{appPort}", appPort).replace("{appPrefix}", appPrefix)

            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="追加evo.conf 内容: {}".format(bb))

            wconf.write(bb)
            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="追加upstream.conf ")

            uc = upstream_cf.replace("{appName}",
                                     appName).replace("{appPort}", appPort)
            upconf.write(uc)

        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="写evo.conf 和 upstream.conf 结尾部分 ")

        wconf.write(third_conf)
        upconf.write(up_second_conf)
        wconf.close()
        upconf.close()
        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="evo.conf 和 upstream.conf 写入完毕,下面开始打包chart")

        valueConf = open(
            "{}/tmp/{}-nginx/values.yaml".format(settings.PROJECT_ROOT,
                                                 envName), "r")
        content = valueConf.read()
        valueConf.close()
        hostName = content.replace("evo23.flashhold.com",
                                   "{}.flashhold.com".format(envName))
        with open(
                "{}/tmp/{}-nginx/values.yaml".format(settings.PROJECT_ROOT,
                                                     envName),
                "w") as newValue:
            newValue.write(hostName)
        newValue.close()

        t = Tiller(settings.TILLER_SERVER["address"],
                   settings.TILLER_SERVER["port"])
        chart = ChartBuilder({
            'name': 'nginx',
            'source': {
                'type':
                'directory',
                'location':
                "{}/tmp/{}-nginx".format(settings.PROJECT_ROOT, envName)
            }
        })
        # 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 nginx chart: ")

        install_result = t.install_release(chart.get_helm_chart(),
                                           dry_run=False,
                                           namespace=envName,
                                           name='{}-nginx'.format(envName))

        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=
            "success to install chart, next to start to add dns for {}.k8s: {}"
            .format(envName, install_result))

        dns = DnsApi()
        dns.update(domain="{deploy_name}".format(deploy_name=envName))

        return True

    except Exception as 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="Exception when install nginx chart: %s\n" % e)

        return False
 def installChart(self):
     tiller = Tiller(self.tiller_host)
     tiller.install_release(
         self.chart, 
         dry_run=False, 
         namespace=self.kubernetes_namespace)
Exemplo n.º 26
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)
Exemplo n.º 27
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)