chore: remove unused scripts
This commit is contained in:
@@ -1,122 +0,0 @@
|
||||
import base64
|
||||
import logging
|
||||
|
||||
import kubernetes.client
|
||||
import urllib3.response
|
||||
import yaml
|
||||
from kubernetes.client import ApiException
|
||||
from kubernetes.client.rest import RESTResponse
|
||||
|
||||
_result_deleted = {
|
||||
'api_version': 'v1',
|
||||
'code': None,
|
||||
'details': None,
|
||||
'kind': 'Namespace',
|
||||
'message': None,
|
||||
'metadata': {
|
||||
'_continue': None,
|
||||
'remaining_item_count': None,
|
||||
'resource_version': '4424',
|
||||
'self_link': '/api/v1/namespaces/mydep3',
|
||||
},
|
||||
'reason': None,
|
||||
'status': "{'phase': 'Terminating'}",
|
||||
}
|
||||
_result_not_found = ApiException(
|
||||
RESTResponse(
|
||||
urllib3.response.HTTPResponse(
|
||||
body=b'{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"namespaces \\"mydep3\\" not found","reason":"NotFound","details":{"name":"mydep3","kind":"namespaces"},"code":404}',
|
||||
status=404,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
kubeconfig = yaml.load(
|
||||
'''
|
||||
apiVersion: v1
|
||||
clusters:
|
||||
- cluster:
|
||||
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUREekNDQWZlZ0F3SUJBZ0lVVjhlN3pIa0Q5SitzcnJuN1Ywb0Zka2ZTaFQ0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd0Z6RVZNQk1HQTFVRUF3d01NVEF1TVRVeUxqRTRNeTR4TUI0WERUSXlNREl3TnpBME1qY3dNVm9YRFRNeQpNREl3TlRBME1qY3dNVm93RnpFVk1CTUdBMVVFQXd3TU1UQXVNVFV5TGpFNE15NHhNSUlCSWpBTkJna3Foa2lHCjl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUE0UDNFSkozUXlxbzF4c2ducTh1dXY5Yk9yVks1QmQzOTBwWGcKUVlpMTBnZmd5UzBrWnZ6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VORCBDRVJUSUZJQ0FURS0tLS0tCg==
|
||||
server: https://142.132.167.243:16443
|
||||
name: microk8s-cluster
|
||||
contexts:
|
||||
- context:
|
||||
cluster: microk8s-cluster
|
||||
user: admin
|
||||
name: microk8s
|
||||
current-context: microk8s
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: admin
|
||||
user:
|
||||
token: VU96c211SXRDNlg0WUZOTUh0dHpYd2luNy84Ulh0Y2Nka09wWGlmOTQ1ST0K
|
||||
'''
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class KubernetesService:
|
||||
def __init__(self, client: kubernetes.client.CoreV1Api):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def new(cls, kubeconfig: dict):
|
||||
api_client = kubernetes.config.new_client_from_config_dict(kubeconfig)
|
||||
v1_client = kubernetes.client.CoreV1Api(api_client)
|
||||
return cls(v1_client)
|
||||
|
||||
def delete_namespace(self, namespace_name: str) -> None:
|
||||
# just in case
|
||||
assert (
|
||||
namespace_name
|
||||
not in [
|
||||
'kube-system',
|
||||
'kube-public',
|
||||
'kube-node-lease',
|
||||
'default',
|
||||
]
|
||||
or not namespace_name.startswith('kube-')
|
||||
), 'protected namespaces cannot be deleted'
|
||||
|
||||
try:
|
||||
_ = self.client.delete_namespace(namespace_name)
|
||||
except ApiException as e:
|
||||
if e.status != 404: # nocover
|
||||
raise
|
||||
logger.error(
|
||||
'Namespace not found',
|
||||
extra={
|
||||
'namespace_name': namespace_name,
|
||||
'response_body': e.body,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
kubernetes.config.load_kube_config_from_dict(kubeconfig)
|
||||
client = kubernetes.client.CoreV1Api()
|
||||
env = {
|
||||
'AWS_ACCESS_KEY': 'aws',
|
||||
'AWS_ACCESS_SECRET_KEY': 'secret',
|
||||
}
|
||||
secret = client.read_namespaced_secret('env', 'default')
|
||||
secret_names = list(secret.data.keys())
|
||||
# ['AWS_ACCESS_KEY', 'AWS_ACCESS_SECRET_KEY']
|
||||
secret = kubernetes.client.V1Secret(
|
||||
api_version='v1',
|
||||
metadata={'name': 'env'},
|
||||
data={k: base64.b64encode(v.encode()).decode() for k, v in env.items()},
|
||||
immutable=True,
|
||||
kind='Secret',
|
||||
type='Opaque',
|
||||
)
|
||||
res = client.create_namespaced_secret('default', secret)
|
||||
print(res)
|
||||
|
||||
|
||||
#
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,54 +0,0 @@
|
||||
from unittest import mock
|
||||
|
||||
import kubernetes.client.api.core_v1_api
|
||||
import pytest
|
||||
|
||||
import urllib3.response
|
||||
from kubernetes.client import ApiException
|
||||
from kubernetes.client.rest import RESTResponse
|
||||
from kube import KubernetesService
|
||||
|
||||
# fmt: off
|
||||
_result_deleted = {'api_version': 'v1', 'code': None, 'details': None, 'kind': 'Namespace', 'message': None, 'metadata': {'_continue': None, 'remaining_item_count': None, 'resource_version': '4424', 'self_link': '/api/v1/namespaces/mydep3',}, 'reason': None, 'status': "{'phase': 'Terminating'}",}
|
||||
_result_not_found = ApiException(status=404, http_resp=RESTResponse(urllib3.response.HTTPResponse(body=b'{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"namespaces \\"mydep3\\" not found","reason":"NotFound","details":{"name":"mydep3","kind":"namespaces"},"code":404}')))
|
||||
# fmt: on
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_k8s():
|
||||
yield mock.MagicMock(autospec=kubernetes.client.api.core_v1_api.CoreV1Api)
|
||||
|
||||
|
||||
def test_delete_forbidden_namespace(mock_k8s):
|
||||
service = KubernetesService(mock_k8s)
|
||||
|
||||
with pytest.raises(AssertionError):
|
||||
service.delete_namespace('default')
|
||||
|
||||
|
||||
def test_delete_namespace(mock_k8s):
|
||||
mock_k8s.delete_namespace.return_value = _result_deleted
|
||||
service = KubernetesService(mock_k8s)
|
||||
|
||||
service.delete_namespace('app-uuid')
|
||||
|
||||
mock_k8s.delete_namespace.assert_called_once_with('app-uuid')
|
||||
|
||||
|
||||
def test_deleting_nonexistent_namespace_is_ok(mock_k8s):
|
||||
mock_k8s.delete_namespace.side_effect = _result_not_found
|
||||
service = KubernetesService(mock_k8s)
|
||||
|
||||
with mock.patch('logging.Logger.error') as m_log:
|
||||
service.delete_namespace('app-uuid')
|
||||
|
||||
m_log.assert_called()
|
||||
|
||||
|
||||
def test_any_other_error_bubbles_up(mock_k8s):
|
||||
mock_k8s.delete_namespace.side_effect = ApiException(status=500, http_resp=urllib3.response.HTTPResponse(body=b''))
|
||||
|
||||
service = KubernetesService(mock_k8s)
|
||||
|
||||
with pytest.raises(ApiException):
|
||||
service.delete_namespace('app-uuid')
|
||||
@@ -1,31 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
cluster_name=sandbox
|
||||
namespace=default
|
||||
service_account=demo-readonly
|
||||
server=http://0.0.0.0:58253
|
||||
|
||||
secret_name=$(kubectl --namespace $namespace get serviceaccount $service_account -o jsonpath='{.secrets[0].name}')
|
||||
ca=$(kubectl --namespace $namespace get secret/$secret_name -o jsonpath='{.data.ca\.crt}')
|
||||
token=$(kubectl --namespace $namespace get secret/$secret_name -o jsonpath='{.data.token}' | base64 --decode)
|
||||
|
||||
echo "apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- name: ${cluster_name}
|
||||
cluster:
|
||||
certificate-authority-data: ${ca}
|
||||
server: ${server}
|
||||
contexts:
|
||||
- name: ${service_account}@${cluster_name}
|
||||
context:
|
||||
cluster: ${cluster_name}
|
||||
namespace: ${namespace}
|
||||
user: ${service_account}
|
||||
users:
|
||||
- name: ${service_account}
|
||||
user:
|
||||
token: ${token}
|
||||
current-context: ${service_account}@${cluster_name}
|
||||
"
|
||||
@@ -1,55 +0,0 @@
|
||||
from os import PathLike
|
||||
from pathlib import Path
|
||||
import re
|
||||
from playwright.sync_api import sync_playwright, Browser
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
@contextmanager
|
||||
def run_browser(data_dir: PathLike, extension_path: PathLike) -> Browser:
|
||||
with sync_playwright() as pw:
|
||||
with pw.chromium.launch_persistent_context(
|
||||
user_data_dir=data_dir,
|
||||
headless=False,
|
||||
args=[
|
||||
f"--disable-extensions-except={extension_path}",
|
||||
f"--load-extension={extension_path}",
|
||||
],
|
||||
) as browser:
|
||||
yield browser
|
||||
|
||||
|
||||
def find_sales_performance(browser: Browser, url: str) -> dict:
|
||||
with browser.new_page() as page:
|
||||
page.goto(url)
|
||||
text = page.wait_for_selector("#h10-product-score").inner_text()
|
||||
|
||||
amazon_id = re.search(r'/dp/([^?]+)', url).group(1)
|
||||
score = float(re.search(r"(\d+\.?\d*)", text).group(1))
|
||||
try:
|
||||
sales = int(re.search(r"Sales\s*(\d+[,.]?\d*)", text, re.MULTILINE).group(1).replace(',', ""))
|
||||
except:
|
||||
sales = -1
|
||||
|
||||
return {
|
||||
'amazon_id': amazon_id,
|
||||
'sales': sales,
|
||||
'score': score,
|
||||
}
|
||||
|
||||
|
||||
extension_path = Path(r"~/Desktop/amazon").expanduser().resolve()
|
||||
data_dir = Path(r'~/Desktop/amazondemo').expanduser()
|
||||
|
||||
def main():
|
||||
urls = [
|
||||
"https://www.amazon.co.uk/dp/B07B9G7V3P?psc=1",
|
||||
]
|
||||
with run_browser(data_dir=data_dir, extension_path=extension_path) as browser:
|
||||
for url in urls:
|
||||
perf = find_sales_performance(browser=browser, url=url)
|
||||
print(perf)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"version": 2,
|
||||
"builds": [
|
||||
{
|
||||
"src": "./app.py",
|
||||
"use": "@vercel/python"
|
||||
}
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"src": "/(.*)",
|
||||
"dest": "/app.py"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
import flask
|
||||
import huey
|
||||
import sentry_sdk
|
||||
import sentry_sdk.integrations.flask
|
||||
from huey import signals
|
||||
|
||||
sentry_sdk.init(
|
||||
"https://379e370d4dee471b8ca5c7884d2137c0@o271257.ingest.sentry.io/6214795",
|
||||
# Set traces_sample_rate to 1.0 to capture 100%
|
||||
# of transactions for performance monitoring.
|
||||
# We recommend adjusting this value in production.
|
||||
traces_sample_rate=1.0,
|
||||
with_locals=True,
|
||||
max_breadcrumbs=100,
|
||||
integrations=[sentry_sdk.integrations.flask.FlaskIntegration()],
|
||||
)
|
||||
|
||||
worker = huey.SqliteHuey(filename='worker.sqlite3')
|
||||
|
||||
|
||||
@worker.signal(signals.SIGNAL_EXECUTING, signals.SIGNAL_LOCKED, signals.SIGNAL_CANCELED, signals.SIGNAL_REVOKED)
|
||||
def task_executing_handler(signal: str, task: huey.api.Task):
|
||||
sentry_sdk.add_breadcrumb(
|
||||
category='worker',
|
||||
data=dict(
|
||||
signal=signal,
|
||||
task_id=task.id,
|
||||
task_name=task.name,
|
||||
task_retries=task.retries,
|
||||
task_args=task.args,
|
||||
task_kwargs=task.kwargs,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@worker.signal(signals.SIGNAL_ERROR)
|
||||
def task_not_executed_handler(signal: str, task: huey.api.Task, exc: Exception):
|
||||
with sentry_sdk.push_scope():
|
||||
sentry_sdk.capture_exception(exc)
|
||||
|
||||
|
||||
class SomeService:
|
||||
@classmethod
|
||||
@worker.task()
|
||||
def do_something(cls, *args, **kwargs):
|
||||
print(args, kwargs)
|
||||
|
||||
@classmethod
|
||||
@worker.task()
|
||||
def fail_something(cls, *args, **kwargs):
|
||||
raise Exception('boom')
|
||||
|
||||
def do(self):
|
||||
self.do_something(2, a=1)
|
||||
|
||||
def fail(self):
|
||||
self.fail_something(2, a=1)
|
||||
|
||||
|
||||
def test_request():
|
||||
flask_app = flask.Flask(__name__)
|
||||
|
||||
@flask_app.post('/')
|
||||
def fail():
|
||||
SomeService().fail()
|
||||
return {'a': 5}
|
||||
|
||||
flask_app.add_url_rule('/', 'home', fail)
|
||||
flask_app.test_client().post('/', json={'a': 10})
|
||||
@@ -1,4 +0,0 @@
|
||||
from worker import SomeService
|
||||
|
||||
if __name__ == '__main__':
|
||||
SomeService().fail()
|
||||
@@ -1,19 +0,0 @@
|
||||
from wsgiref.util import setup_testing_defaults
|
||||
from wsgiref.simple_server import make_server
|
||||
|
||||
|
||||
def simple_app(environ, start_response):
|
||||
setup_testing_defaults(environ)
|
||||
|
||||
status = "200 OK"
|
||||
headers = [("Content-type", "text/plain; charset=utf-8")]
|
||||
|
||||
start_response(status, headers)
|
||||
|
||||
ret = [("%s: %s\n" % (key, value)).encode("utf-8") for key, value in environ.items()]
|
||||
return ret
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with make_server("", 8001, simple_app) as httpd:
|
||||
httpd.handle_request()
|
||||
Reference in New Issue
Block a user