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()
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()
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 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
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 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"
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
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.', }
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"] )
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 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
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')
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
# -*- 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)
try: rollback_result = ins.rollback_release(name=name,version=int(version)) except Exception,e: print e print rollback_result return JsonResponse({'result': rollback_result}) if action == 'install': # pass namespace = request.POST.get('namespace', None) chartname = request.POST.get('chartname', None) deployname = request.POST.get('deployname', None) values = request.POST.get('values', None) chart_path = RepoUtils.from_repo(CHART_REPO, chartname) chart = ChartBuilder({'name': chartname, 'source': {'type': 'directory', 'location': chart_path}}) try: result = ins.install_release(chart.get_helm_chart(),namespace,name=deployname,values=values) except Exception,e: result = e return JsonResponse({'result': result}) if action == 'update': chartname = request.POST.get('chartname', None) deployname = request.POST.get('deployname', None) values = request.POST.get('values', None) chart_path = RepoUtils.from_repo(CHART_REPO, chartname) chart = ChartBuilder({'name': chartname, 'source': {'type': 'directory', 'location': chart_path}}) try: result = ins.update_release(chart.get_helm_chart(), deployname, values=values) except Exception,e: result = e return JsonResponse({'result': result})
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")
#!/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)
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