Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec: retrive log files from api_server minio address #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ctl/lib/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
hex_chars = re.compile('[0-9a-fA-F]+')

def addv(version: str) -> str:
if version == "nightly":
return version
if not version.startswith('v'):
return f"v{version}"
return version
Expand Down
32 changes: 26 additions & 6 deletions ctl/lib/get_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from .cli_parse import Config, select, shift
from .cli_parse import Config, select, shift, default_config
from .saved_clusters import from_cli_desc
from typing import List
from colorama import Fore
Expand Down Expand Up @@ -27,13 +27,32 @@ def last_part(path: str) -> str :
class FileReader:
def __init__(self, config: Config):
self._config = config
self._default_config = default_config()
self._client = Minio(config.s3_endpoint,
access_key=config.s3_access_key,
secret_key=config.s3_secret_key,
secure=False)

def get_artifacts_dir_of(self, cluster_id: int) -> Object:
self._api_server_client = Minio(self._default_config.s3_endpoint,
access_key=self._default_config.s3_access_key,
secret_key=self._default_config.s3_secret_key,
secure=False)
self.use_self_client = True

def set_client(self, cluster_id: int):
objs = list(self._client.list_objects(artifacts_bucket, f"{cluster_id}/"))
if len(objs) == 0:
# set client with api_server minio address to get cluster logs
objs = list(self._api_server_client.list_objects(artifacts_bucket, f"{cluster_id}/"))
if len(objs) > 0 :
self.use_self_client = False

def get_client(self) -> Object:
if self.use_self_client:
return self._client
return self._api_server_client

def get_artifacts_dir_of(self, cluster_id: int) -> Object:
objs = list(self.get_client().list_objects(artifacts_bucket, f"{cluster_id}/"))
if len(objs) == 0:
return None
if len(objs) > 1:
Expand All @@ -42,7 +61,7 @@ def get_artifacts_dir_of(self, cluster_id: int) -> Object:

def select_file(self, dir: str) -> Object:
dir = f"{dir}/" if not dir.endswith("/") else dir
objs : List[Object] = list(self._client.list_objects(artifacts_bucket, dir))
objs : List[Object] = list(self.get_client().list_objects(artifacts_bucket, dir))
print(f"current path: {Fore.GREEN}{dir}{Fore.RESET}")
new_obj = select(objs, lambda o: f"{last_part(o.object_name)}")
if new_obj.is_dir:
Expand All @@ -55,20 +74,21 @@ def query_file(self, cluster_id: int):
logging.error(f"the cluster {cluster_id} seems has no artifacts, maybe it hasn't end yet?")
exit(1)
obj = self.select_file(artifact.object_name)
data = self._client.get_object(obj.bucket_name, obj.object_name)
data = self.get_client().get_object(obj.bucket_name, obj.object_name)
proc = subprocess.Popen(["less"], stdin = subprocess.PIPE)
# this would load the total file into memory, if the file is big,
# we might meet some error, but don't worry for now.
proc.communicate(data.read())

def run(self):
cluster = from_cli_desc(shift())
self.set_client(cluster)
file = shift()
if file is None:
self.query_file(cluster)
return
artifact = self.get_artifacts_dir_of(cluster)
obj = self._client.get_object(artifacts_bucket, f"{artifact.object_name}{file}")
obj = self.get_client().get_object(artifacts_bucket, f"{artifact.object_name}{file}")
try:
for d in obj.stream(32 * 1024):
sys.stdout.write(d.decode('utf-8'))
Expand Down
4 changes: 2 additions & 2 deletions ctl/lib/init_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def prompt_config() -> Config:
old_config = get_config()
show_config(old_config)
print(f"{Fore.GREEN}you can leave any field empty to leave this field untouched then.{Fore.RESET}")
api_server = input("Please input your api server: ") or old_config.api_server
s3_endpoint = input("Please input your s3 endpoint (without http:// prefix, TLS isn't supported for now): ") or old_config.s3_endpoint
api_server = (input("Please input your api server: ") or old_config.api_server).strip()
s3_endpoint = (input("Please input your s3 endpoint (without http:// prefix, TLS isn't supported for now): ") or old_config.s3_endpoint).strip()
s3_access_key = input("Please input your s3 access key: ") or old_config.s3_access_key
s3_secret_key = getpass("Please input your s3 secret access key: ") or old_config.s3_secret_key
logging.info("dialing to s3 storage using the current config...")
Expand Down