Skip to content

Commit 00f2366

Browse files
authored
Feature Logs (#149)
* Stream logs * Logic for SSHing into a container
1 parent c635233 commit 00f2366

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

ckan_cloud_operator/providers/ckan/instance/cli.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,17 @@ def delete(instance_id_or_name, no_dry_run):
149149

150150

151151
@instance.command('logs')
152-
@click.option('--service', help='Service name. One of `ckan`, `giftless`, `jobs`, `jobs-db`, `redis`. Defaults to `ckan`')
152+
@click.argument('INSTANCE_ID')
153+
@click.option('--service', help='Service name. One of `ckan`, `giftless`, `jobs`, `jobs-db`, `redis`. Defaults to `ckan`', default='ckan')
153154
@click.option('--since', help='Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs.')
154155
@click.option('--follow', help='Specify if the logs should be streamed.')
155156
@click.option('--tail', help='Lines of recent log file to display. Defaults to -1 with no selector, showing all log lines otherwise 10, if a selector is provided.')
156157
@click.option('--container', help='Conainer name if multiple')
157-
@click.option('--grep', help='Filter logs by the given word (case insensitive)')
158-
def ckan_logs(command):
158+
def ckan_logs(instance_id, **kubectl_args):
159159
'''
160160
Check CKAN and other service container logs
161161
'''
162-
pass
163-
162+
manager.get_container_logs(instance_id, **kubectl_args)
164163

165164
@instance.command('ckan-exec')
166165
@click.argument('INSTANCE_ID')
@@ -181,13 +180,14 @@ def ckan_exec(instance_id, command, use_paster):
181180

182181

183182
@instance.command('ssh')
184-
@click.option('--service', help='Service name. One of `ckan`, `giftless`, `jobs`, `jobs-db`, `redis`. Defaults to `ckan`')
185-
@click.option('--command', help='One of `bash`, `sh`. Defaults to `bash`')
186-
def ckan_ssh(service, command):
183+
@click.argument('INSTANCE_ID')
184+
@click.option('--service', help='Service name. One of `ckan`, `giftless`, `jobs`, `jobs-db`, `redis`. Defaults to `ckan`', default='ckan')
185+
@click.option('--command', help='One of `bash`, `sh`. Defaults to `bash`', default='bash')
186+
def ckan_ssh(instance_id, service, command):
187187
'''
188188
SSH into the running container.
189189
'''
190-
pass
190+
manager.ssh_into_container(instance_id, service, command)
191191

192192

193193
@instance.command('shell')

ckan_cloud_operator/providers/ckan/instance/manager.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,25 @@ def run_ckan_commands(instance_id_or_name, command, dry_run=False, use_paster=Fa
437437
logs.info(str(line))
438438

439439

440-
def _get_running_pod_name(instance_id):
440+
def get_container_logs(instance_id, **kubectl_args):
441+
service = kubectl_args.pop('service')
442+
pod_name = _get_running_pod_name(instance_id, service=service)
443+
stream_logs = '-f ' if kubectl_args.pop('follow', None) else ''
444+
k_args = [f'--{k}={v}' for k,v in kubectl_args.items() if v is not None]
445+
full_args = stream_logs + ' '.join(k_args)
446+
_stream_logs(f'kubectl -n {instance_id} logs {pod_name} {full_args}')
447+
448+
449+
def ssh_into_container(instance_id, service, command):
450+
pod_name = _get_running_pod_name(instance_id, service=service)
451+
subprocess.run(f'kubectl -n {instance_id} exec -it {pod_name} {command}', shell=True)
452+
453+
454+
def _get_running_pod_name(instance_id, service='ckan'):
441455
pod_name = None
442456
while not pod_name:
443457
try:
444-
pod_name = kubectl.get_deployment_pod_name('ckan', instance_id, use_first_pod=True, required_phase='Running')
458+
pod_name = kubectl.get_deployment_pod_name(service, instance_id, use_first_pod=True, required_phase='Running')
445459
break
446460
except Exception as e:
447461
logs.warning('Failed to find running ckan pod', str(e))
@@ -484,3 +498,9 @@ def _get_instance_id_and_type(instance_id_or_name=None, instance_id=None, requir
484498

485499
def _generate_password(length):
486500
return binascii.hexlify(os.urandom(length)).decode()
501+
502+
503+
def _stream_logs(command):
504+
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
505+
for c in iter(lambda: process.stdout.read(1), b''):
506+
sys.stdout.buffer.write(c)

0 commit comments

Comments
 (0)