Skip to content

Commit 43571ab

Browse files
authored
feature: switching to XDG_CACHE_DIR and XDG_CONFIG_DIR everywhere (#28)
* switching to XDG_CACHE_DIR and XDG_CONFIG_DIR everywhere * fix colors * update docs and fix colors for console logging * fix help txt width
1 parent f38fa82 commit 43571ab

File tree

12 files changed

+205
-165
lines changed

12 files changed

+205
-165
lines changed

docs/quickstart.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The help should return this:
4242

4343
[<img src="https://raw.githubusercontent.com/jessebot/smol-k8s-lab/main/docs/screenshots/help_text.svg" alt="Output of smol-k8s-lab --help after cloning the directory and installing the prerequisites.">](https://raw.githubusercontent.com/jessebot/smol-k8s-lab/main/docs/screenshots/help_text.svg)
4444

45-
🔔 Then you *have to edit* the `~/.config/smol-k8s-lab/config.yaml` to be your own values (for _right now_).
45+
🔔 Then you *have to edit* your `$XDG_CONFIG_DIR` (usually `~/.config/smol-k8s-lab/config.yaml`) to be your own values. Better cli options and Interactive config setup coming soon!
4646

4747
Here's an example file already filled out with comments:
4848

@@ -73,8 +73,8 @@ external_secrets:
7373
log:
7474
# logging level, Options: debug, info, warn, error
7575
level: "info"
76-
# path of file to log to
77-
file: "./smol-k8s-log.log"
76+
# path of file to log to, disabled by default
77+
# file: "./smol-k8s-log.log"
7878
```
7979

8080
## Install a distro of k8s with smol-k8s-lab

docs/screenshots/help_text.svg

Lines changed: 109 additions & 97 deletions
Loading

poetry.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "smol_k8s_lab"
3-
version = "0.10.1"
3+
version = "0.10.2"
44
description = "bootstrap simple projects on kubernetes with kind and k3s"
55
authors = ["Jesse Hitch <[email protected]>"]
66
readme = "README.md"
@@ -28,6 +28,7 @@ click = "^8.1.3"
2828
bcrypt = "^4.0.1"
2929
requests = "^2.28.1"
3030
pyyaml = "^6.0"
31+
xdg = "^5.1.1"
3132

3233
[[tool.poetry.source]]
3334
name = "testpypi"

smol_k8s_lab/__init__.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@
1414
from rich.logging import RichHandler
1515
from sys import exit
1616

17-
from .console_logging import header, sub_header
18-
from .env_config import check_os_support, USR_CONFIG_FILE, VERSION, HOME_DIR
19-
from .env_config import CONSOLE
17+
# custom libs and constants
18+
from .console_logging import CONSOLE, header, sub_header
19+
from .env_config import check_os_support, HOME_DIR, USR_CONFIG_FILE, VERSION
20+
from .env_config import XDG_CACHE_DIR
2021
from .help_text import RichCommand, options_help
2122

2223

23-
SUPPORTED_DISTROS = ['k0s', 'k3s', 'kind']
24-
2524
HELP = options_help()
2625
HELP_SETTINGS = dict(help_option_names=['-h', '--help'])
26+
SUPPORTED_DISTROS = ['k0s', 'k3s', 'kind']
2727

2828

2929
def setup_logger(level="", log_file=""):
3030
"""
31-
Sets up rich logger and stores the values for it in a db for future import
32-
in other files. Returns logging.getLogger("rich")
31+
Sets up rich logger for the entire project.
32+
꒰ᐢ. ̫ .ᐢ꒱ <---- who is he? :3
33+
Returns logging.getLogger("rich")
3334
"""
3435
# determine logging level
3536
if not level:
@@ -76,8 +77,10 @@ def setup_logger(level="", log_file=""):
7677

7778
def install_k8s_distro(k8s_distro=""):
7879
"""
79-
install a specific distro of k8s
80-
options: k0s, k3s, kind
80+
Install a specific distro of k8s
81+
Takes one variable:
82+
k8s_distro - string. options: 'k0s', 'k3s', or 'kind'
83+
Returns True
8184
"""
8285
if k8s_distro == "kind":
8386
from .k8s_distros.kind import install_kind_cluster
@@ -88,6 +91,7 @@ def install_k8s_distro(k8s_distro=""):
8891
elif k8s_distro == "k0s":
8992
from .k8s_distros.k0s import install_k0s_cluster
9093
install_k0s_cluster()
94+
return True
9195

9296

9397
def delete_cluster(k8s_distro="k3s"):
@@ -102,8 +106,8 @@ def delete_cluster(k8s_distro="k3s"):
102106
uninstall_k3s()
103107

104108
elif k8s_distro == 'kind':
105-
from .k8s_distros.kind import uninstall_kind
106-
uninstall_kind()
109+
from .k8s_distros.kind import delete_kind_cluster
110+
delete_kind_cluster()
107111

108112
elif k8s_distro == 'k0s':
109113
from .k8s_distros.k0s import uninstall_k0s
@@ -112,7 +116,7 @@ def delete_cluster(k8s_distro="k3s"):
112116
else:
113117
header("┌(・o・)┘≡З Whoops. {k8s_distro} not YET supported.")
114118

115-
sub_header("[green]◝(ᵔᵕᵔ)◜ Success!")
119+
sub_header("[grn]◝(ᵔᵕᵔ)◜ Success![/grn]")
116120
exit()
117121

118122

@@ -149,36 +153,35 @@ def main(k8s: str = "",
149153
Quickly install a k8s distro for a homelab setup. Installs k3s
150154
with metallb, ingess-nginx, cert-manager, and argocd
151155
"""
152-
# setup logging immediately
153-
log = setup_logger(log_level, log_file)
154-
log.info("Logging configured")
155-
156156
# only return the version if --version was passed in
157157
if version:
158158
print(f'\n🎉 v{VERSION}\n')
159159
return True
160160

161+
# setup logging immediately
162+
log = setup_logger(log_level, log_file)
163+
log.info("Logging configured")
164+
161165
# make sure we got a valid k8s distro
162166
if k8s not in SUPPORTED_DISTROS:
163167
CONSOLE.print(f'\n☹ Sorry, "[b]{k8s}[/]" is not a currently supported '
164168
'k8s distro. Please try again with any of '
165169
f'{SUPPORTED_DISTROS}.\n')
166170
exit()
167171

168-
# before we do anything, we need to make sure this OS is supported
172+
# make sure this OS is supported
169173
check_os_support()
170174

171175
if delete:
172-
# this exist the script after deleting the cluster
176+
# exits the script after deleting the cluster
173177
delete_cluster(k8s)
174178

175-
# TODO: make this follow XDG base directory spec
176-
# make sure the cache directory exists, to store stuff
177-
Path(f"{HOME_DIR}/.cache/smol-k8s-lab").mkdir(exist_ok=True)
179+
# make sure the cache directory exists (typically ~/.cache/smol-k8s-lab)
180+
Path(XDG_CACHE_DIR).mkdir(exist_ok=True)
178181

179182
# install the actual KIND or k3s cluster
180183
header(f'Installing [green]{k8s}[/] cluster.')
181-
sub_header('This could take a min ʕ•́ᴥ•̀ʔっ♡ ', False)
184+
sub_header('This could take a min ʕ•́ ̫•̀ʔっ♡ ', False)
182185
install_k8s_distro(k8s)
183186

184187
# make sure helm is installed and the repos are up to date
@@ -190,27 +193,28 @@ def main(k8s: str = "",
190193
from .k8s_apps.metallb import configure_metallb
191194
configure_metallb(USR_CONFIG_FILE['metallb_address_pool'])
192195

193-
# this is so we can accept traffic from outside the cluster
196+
# ingress controller: so we can accept traffic from outside the cluster
194197
header("Installing [b]ingress-nginx-controller[/b]...")
195198
from .k8s_apps.nginx_ingress_controller import configure_ingress_nginx
196199
configure_ingress_nginx(k8s)
197200

198-
# this is for manager SSL/TLS certificates via lets-encrypt
201+
# manager SSL/TLS certificates via lets-encrypt
199202
header("Installing [b]cert-manager[/b] for TLS certificates...")
200203
from .k8s_apps.certmanager import configure_cert_manager
201204
configure_cert_manager(USR_CONFIG_FILE['email'])
202205

203-
# this is for external secrets, currently only supports gitlab
206+
# external secrets provider: currently only supports gitlab
204207
if external_secret_operator:
205-
external_secrets = USR_CONFIG_FILE['external_secrets']['gitlab']
206208
from .k8s_apps.external_secrets import configure_external_secrets
209+
external_secrets = USR_CONFIG_FILE['external_secrets']['gitlab']
207210
configure_external_secrets(external_secrets)
208211

212+
# kyverno: kubernetes native policy manager
209213
if kyverno:
210214
from .k8s_apps.kyverno import install_kyverno
211215
install_kyverno()
212216

213-
# then install argo CD ꒰ᐢ. ̫ .ᐢ꒱ <---- who is he? :3
217+
# 🦑 Install Argo CD: continuous deployment app for k8s
214218
if argo:
215219
argocd_fqdn = ".".join([USR_CONFIG_FILE['domain']['argo_cd'],
216220
USR_CONFIG_FILE['domain']['base']])

smol_k8s_lab/console_logging.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
from rich import print
88
from rich.console import Console
99
from rich.panel import Panel
10-
from rich.table import Table
10+
# from rich.table import Table
1111
from rich.theme import Theme
1212

1313

14-
# for console logging only
15-
CONSOLE = Console(theme=Theme({"warn": "bold yellow",
16-
"grn": "medium_spring_green",
17-
"ohno": "magenta"}))
14+
# this is for rich text, to pretty print things
15+
soft_theme = Theme({"info": "dim cornflower_blue",
16+
"grn": "medium_spring_green",
17+
"warn": "yellow on black",
18+
"danger": "bold magenta",
19+
"ohno": "pale_violet_red1"})
20+
CONSOLE = Console(theme=soft_theme)
1821

1922

2023
def header(text):

smol_k8s_lab/env_config.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/usr/bin/env python3.11
2+
"""
3+
NAME: env_config.py
4+
DESC: everything to do with initial configuration of a new environment
5+
"""
26

37
from importlib.metadata import version as get_version
48
from getpass import getuser
@@ -12,22 +16,17 @@
1216
from sys import exit
1317
import yaml
1418
from .console_logging import print_panel
19+
from xdg import xdg_cache_home, xdg_config_home
1520

1621

1722
HOME_DIR = getenv("HOME")
18-
USER = getuser()
19-
20-
# this is for rich text, to pretty print things
21-
soft_theme = Theme({"info": "dim cornflower_blue",
22-
"warn": "yellow on black",
23-
"danger": "bold magenta"})
24-
CONSOLE = Console(theme=soft_theme)
25-
2623
PWD = path.dirname(__file__)
24+
USER = getuser()
25+
XDG_CACHE_DIR = path.join(xdg_cache_home(), 'smol-k8s-lab')
26+
XDG_CONFIG_DIR = path.join(xdg_config_home(), 'smol-k8s-lab/config.yaml')
2727

2828

29-
def load_yaml(yaml_config_file=path.join(HOME_DIR,
30-
".config/smol-k8s-lab/config.yaml")):
29+
def load_yaml(yaml_config_file=XDG_CONFIG_DIR):
3130
"""
3231
load config yaml files for smol-k8s-lab and return as dicts
3332
"""

smol_k8s_lab/help_text.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# file for rich printing
2-
1+
"""
2+
Name: help_text
3+
Desc: the help text for smol-k8s-lab
4+
"""
35
import click
46
from rich.console import Console
57
from rich.highlighter import RegexHighlighter
@@ -8,7 +10,7 @@
810
from rich.text import Text
911
from rich.theme import Theme
1012

11-
from .env_config import VERSION
13+
from .env_config import VERSION, XDG_CONFIG_DIR
1214

1315

1416
def pretty_choices(default_list):
@@ -34,8 +36,7 @@ def options_help():
3436
'Install Argo CD as part of this script. Default: False',
3537

3638
'config':
37-
'Full path and name of yml to parse. '
38-
'Default: ~/.config/smol-k8s-lab/config.yaml\n'
39+
f'Full path and name of yml to parse. Default: {XDG_CONFIG_DIR}\n'
3940
'Example: smol-k8s-lab -f [light_steel_blue]/tmp/config.yaml[/]',
4041

4142
'delete':

smol_k8s_lab/k8s_apps/argocd.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
LICENSE: GNU AFFERO GENERAL PUBLIC LICENSE Version 3
77
"""
88
import bcrypt
9+
from os import path
10+
from yaml import dump
911
from ..bw_cli import BwCLI
1012
from ..console_logging import header, sub_header
13+
from ..env_config import XDG_CACHE_DIR
1114
from ..k8s_tools.homelabHelm import helm
12-
from yaml import dump
1315

1416

1517
def configure_argocd(argo_cd_domain="", password_manager=False):
@@ -60,7 +62,7 @@ def configure_argocd(argo_cd_domain="", password_manager=False):
6062
val['configs']['secret']['argocdServerAdminPassword'] = admin_pass
6163

6264
# this creates a values.yaml from from the val dict above
63-
values_file_name = '/tmp/smol-k8s-lab/argocd_values.yaml'
65+
values_file_name = path.join(XDG_CACHE_DIR, 'argocd_values.yaml')
6466
with open(values_file_name, 'w') as values_file:
6567
dump(val, values_file)
6668

smol_k8s_lab/k8s_distros/kind.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
#!/usr/bin/env python3.11
22
"""
33
Name: kind
4-
DESCRIPTION: install or uninstall kind cluster
5-
AUTHOR: @jessebot
4+
DESCRIPTION: create or delete a kind cluster
5+
AUTHOR: github.com/jessebot/smol-k8s-lab
66
LICENSE: GNU AFFERO GENERAL PUBLIC LICENSE Version 3
77
"""
88
from os import path
99
from shutil import which
10-
from ..env_config import PWD, CONSOLE
10+
from ..console_logging import sub_header
11+
from ..env_config import PWD
1112
from ..subproc import subproc
1213

1314

1415
def install_kind_cluster():
1516
"""
16-
python installation process for kind
17+
Run installation process for kind and create cluster
18+
returns True
1719
"""
1820
# make sure kind is installed first, and if not, install it
1921
if not which("kind"):
2022
msg = ("ʕ•́ᴥ•̀ʔ [b]kind[/b] is [warn]not installed[/warn]. "
2123
"[i]We'll install it for you.[/i] ʕᵔᴥᵔʔ")
22-
CONSOLE.print(msg, justify='center')
24+
sub_header(msg)
2325
subproc(['brew install kind'], spinner=True)
2426

2527
# then use our pre-configured kind file to install a small cluster
2628
full_path = path.join(PWD, 'distros/kind/kind_cluster_config.yaml')
2729
subproc([f"kind create cluster --config={full_path}"], spinner=True)
28-
return
2930

31+
return True
3032

31-
def uninstall_kind_cluster():
33+
34+
def delete_kind_cluster():
3235
"""
33-
uninstall kind cluster
36+
delete kind cluster
37+
returns True
3438
"""
3539
subproc(['kind delete cluster'])
40+
return True

0 commit comments

Comments
 (0)