view tasks.py @ 15:2c37fab420da

rm dead code
author drewp@bigasterisk.com
date Fri, 23 Jun 2023 23:53:13 -0700
parents b6720e379d5b
children f5777b65f035
line wrap: on
line source

import json
import sys
import tempfile
import time
import urllib.request
from pathlib import Path
from typing import Dict

from invoke import task

sys.path.append('/usr/lib/python3/dist-packages/')
import yaml

_tfs = []


def saveTmp(text):
    tf = tempfile.NamedTemporaryFile(mode='wt')
    _tfs.append(tf)
    tf.write(text)
    tf.flush()
    return Path(tf.name)


def writeConfigmap(ctx, files: Dict[str, Path]):
    arg = ','.join(f'{k}={v}' for k, v in files.items())
    ctx.run(
        f'kubectl create configmap victoriametrics-config --from-file {arg} -o yaml --dry-run=client | kubectl apply -f -'
    )


def reload(ctx, svc):
    host = ctx.run(f'khost {svc}').stdout
    path = {'victoriametrics': '/m/', 'vmalert': '/'}[svc]
    reload_url = f'http://{host}{path}-/reload'
    print(f'reload with POST {reload_url}')
    for workaround in [1]:
        print(' -> status',
              urllib.request.urlopen(reload_url, data=b'unused').status)
        time.sleep(0)



def hostsExpectedOnline(ctx):
    return ctx.run(
        'cd /my/serv/lanscape; pdm run python hosts_expected_online.py').stdout


def expectedK8sNodes(ctx):
    getNode = json.loads(ctx.run("kubectl get node -o json").stdout)
    hosts = [item['metadata']['name'] for item in getNode['items']]
    return yaml.dump({
        'groups': [{
            'name':
            'k8s_expected_nodes',
            'rules': [{
                'alert':
                'kube_node_log_size_report_' + h,
                'expr':
                'absent(kubelet_container_log_filesystem_used_bytes{instance="%s"})'
                % h,
                'for':
                '1h',
                'annotations': {
                    'summary': f"no recent k8s log size report from host {h}"
                }
            } for h in hosts]
        }]
    })


@task
def sync_config(ctx):
    config = Path('config')
    for workaround in [1]:
        writeConfigmap(
            ctx, {
#                'scrape_ssl.yaml': saveTmp(httpsCertProber()),
                'rules_expected_nodes.yaml': saveTmp(expectedK8sNodes(ctx)),
                'rules_expected_hosts.yaml': saveTmp(hostsExpectedOnline(ctx)),
            })
        reload(ctx, 'victoriametrics')

        # this reload doesn't get the new config- not sure if it's vmalert bug or k8s cm propogation problem
        # reload(ctx, 'vmalert')
        ctx.run('kubectl rollout restart deploy/vmalert')

@task
def build_config(ctx):
    with open('rules/build/expected_hosts.yaml', 'w') as out:
        out.write(hostsExpectedOnline(ctx))