Add command for showing action logs

Added `ccp action log <action-name>` command for getting stdout of
action container.

Change-Id: Ib4693483e889f69c9392597d71de1f7148ba5cca
This commit is contained in:
Sergey Reshetnyak 2017-01-26 17:33:39 +03:00
parent 2303b0300a
commit f665ed6ad1
4 changed files with 50 additions and 9 deletions

View File

@ -190,7 +190,7 @@ class Action(object):
class ActionStatus(object):
@classmethod
def get_actions(cls, action_name):
def get_actions(cls, action_name=None):
selector = "ccp-action=true"
if action_name:
selector += "," + "app=%s" % action_name
@ -202,6 +202,7 @@ class ActionStatus(object):
return actions
def __init__(self, k8s_spec):
self._spec = k8s_spec
self.name = k8s_spec.name
self.component = k8s_spec.labels["ccp-component"]
self.date = k8s_spec.obj["metadata"]["creationTimestamp"]
@ -223,6 +224,17 @@ class ActionStatus(object):
return "wip"
return "ok"
def log(self):
if self._spec.kind == "Pod":
return self._spec.logs()
else:
pod_selector = "job-name=%s" % self._spec.name
pods = kubernetes.list_cluster_pods(raw_selector=pod_selector)
for pod in pods:
if pod.obj['status']['phase'] == "Failed":
continue
return pod.logs()
def list_actions():
"""List of available actions.
@ -269,5 +281,13 @@ def run_action(action_name):
action.run()
def list_action_status(action_name=None):
return ActionStatus.get_actions(action_name)
def list_action_status(action_type=None):
return ActionStatus.get_actions(action_type)
def get_action_status_by_name(action_name):
for action in list_action_status():
if action.name == action_name:
return action
raise exceptions.NotFoundException("Action with name \"%s\" not found" % (
action_name))

View File

@ -1,3 +1,5 @@
from __future__ import print_function
import argparse
import logging
import os.path
@ -267,6 +269,21 @@ class ActionList(BaseCommand, lister.Lister):
return ("Name", "Component"), [(a.name, a.component) for a in actions]
class ActionLog(BaseCommand):
"""Show action container stdout"""
def get_parser(self, *args, **kwargs):
parser = super(ActionLog, self).get_parser(*args, **kwargs)
parser.add_argument("action",
help="Show action container stdout")
return parser
def take_action(self, parsed_args):
self._fetch_repos()
action_obj = action.get_action_status_by_name(parsed_args.action)
print(action_obj.log())
class ActionShow(BaseCommand, show.ShowOne):
"""Show action"""

View File

@ -159,12 +159,15 @@ def list_cluster_deployments():
selector="ccp=true")
def list_cluster_pods(service=None, selector=None):
ccp_selector = "ccp=true"
if service:
ccp_selector = ",".join((ccp_selector, "app=%s" % service))
if selector:
ccp_selector += "," + selector
def list_cluster_pods(service=None, selector=None, raw_selector=None):
if raw_selector is not None:
ccp_selector = raw_selector
else:
ccp_selector = "ccp=true"
if service:
ccp_selector = ",".join((ccp_selector, "app=%s" % service))
if selector:
ccp_selector += "," + selector
client = get_client()
return pykube.Pod.objects(client).filter(
namespace=CONF.kubernetes.namespace,

View File

@ -28,6 +28,7 @@ console_scripts =
ccp = fuel_ccp.cli:main
ccp.cli =
action_list = fuel_ccp.cli:ActionList
action_log = fuel_ccp.cli:ActionLog
action_run = fuel_ccp.cli:ActionRun
action_show = fuel_ccp.cli:ActionShow
action_status = fuel_ccp.cli:ActionStatus