add CLI command

pull/38/head
Max Erenberg 1 year ago
parent 470b442e4c
commit 0153cac644
  1. 2
      ceo/cli/entrypoint.py
  2. 42
      ceo/cli/k8s.py
  3. 1
      ceod/model/KubernetesService.py
  4. 1
      ceod/model/MailService.py
  5. 1
      ceod/model/VHostManager.py
  6. 4
      ceod/model/templates/kubeconfig.j2
  7. 21
      tests/ceo/cli/test_cloud.py
  8. 4
      tests/ceod/api/test_cloud.py

@ -8,6 +8,7 @@ from .mysql import mysql
from .postgresql import postgresql
from .mailman import mailman
from .cloud import cloud
from .k8s import k8s
@click.group()
@ -23,3 +24,4 @@ cli.add_command(mysql)
cli.add_command(postgresql)
cli.add_command(mailman)
cli.add_command(cloud)
cli.add_command(k8s)

@ -0,0 +1,42 @@
import os
import traceback
import click
from ..utils import http_post
from .utils import handle_sync_response
@click.group(short_help='Manage your CSC Kubernetes resources')
def k8s():
pass
@k8s.group(short_help='Manage your CSC Kubernetes account')
def account():
pass
@account.command(short_help='Obtain a kubeconfig')
def activate():
kubedir = os.path.join(os.environ['HOME'], '.kube')
if not os.path.isdir(kubedir):
os.mkdir(kubedir)
kubeconfig = os.path.join(kubedir, 'config')
resp = http_post('/api/cloud/k8s/accounts/create')
result = handle_sync_response(resp)
try:
if os.path.isfile(kubeconfig):
kubeconfig_bak = os.path.join(kubedir, 'config.bak')
os.rename(kubeconfig, kubeconfig_bak)
with open(kubeconfig, 'w') as fo:
fo.write(result['kubeconfig'])
except Exception:
click.echo(traceback.format_exc())
click.echo("We weren't able to write the kubeconfig file, so here it is.")
click.echo("Make sure to paste this into your ~/.kube/config.")
click.echo()
click.echo(result['kubeconfig'])
return
click.echo("Congratulations! You have a new kubeconfig in ~/.kube/config.")
click.echo("Run `kubectl cluster-info` to make sure everything is working.")

@ -25,6 +25,7 @@ class KubernetesService:
self.server_url = cfg.get('k8s_server_url')
self.jinja_env = jinja2.Environment(
loader=jinja2.PackageLoader('ceod.model'),
keep_trailing_newline=True,
)
def _run(self, args: List[str], check=True, **kwargs) -> subprocess.CompletedProcess:

@ -35,6 +35,7 @@ class MailService:
self.base_domain = cfg.get('base_domain')
self.jinja_env = jinja2.Environment(
loader=jinja2.PackageLoader('ceod.model'),
keep_trailing_newline=True,
)
def send(self, _from: str, to: str, headers: Dict[str, str], content: str):

@ -60,6 +60,7 @@ class VHostManager:
self.jinja_env = jinja2.Environment(
loader=jinja2.PackageLoader('ceod.model'),
keep_trailing_newline=True,
)
rate_limit_secs = cfg.get('cloud vhosts_rate_limit_seconds')

@ -9,8 +9,8 @@ contexts:
cluster: kubernetes
namespace: {{ namespace }}
user: {{ username }}
name: {{ namespace }}
current-context: {{ namespace }}
name: {{ username }}
current-context: {{ username }}
kind: Config
preferences: {}
users:

@ -1,3 +1,5 @@
import os
from click.testing import CliRunner
from ...utils import gssapi_token_ctx
@ -55,3 +57,22 @@ def test_cloud_vhosts(cli_setup, new_user, cfg):
expected = 'Done.\n'
assert result.exit_code == 0
assert result.output == expected
def test_k8s_account_activate(cli_setup, new_user):
uid = new_user.uid
runner = CliRunner()
old_home = os.environ['HOME']
os.environ['HOME'] = new_user.home_directory
try:
with gssapi_token_ctx(uid):
result = runner.invoke(cli, ['k8s', 'account', 'activate'])
finally:
os.environ['HOME'] = old_home
expected = (
"Congratulations! You have a new kubeconfig in ~/.kube/config.\n"
"Run `kubectl cluster-info` to make sure everything is working.\n"
)
assert result.exit_code == 0
assert result.output == expected
assert os.path.isfile(os.path.join(new_user.home_directory, '.kube', 'config'))

@ -203,3 +203,7 @@ def test_k8s_create_account(client, new_user, ldap_conn):
assert status == 200
assert data['status'] == 'OK'
assert 'kubeconfig' in data
expire_member(new_user, ldap_conn)
status, _ = client.post('/api/cloud/k8s/accounts/create', principal=uid)
assert status == 403

Loading…
Cancel
Save