Skip to content

Commit

Permalink
[RDBMS] az mysql flexible-server server-logs: Add server logs for M…
Browse files Browse the repository at this point in the history
…ySQL Flexible Server (#23185)
  • Loading branch information
alanenriqueo committed Jul 25, 2022
1 parent 5bb5710 commit 32028f9
Show file tree
Hide file tree
Showing 16 changed files with 3,586 additions and 6,532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ def cf_mysql_flexible_location_capabilities(cli_ctx, _):
return get_mysql_flexible_management_client(cli_ctx).location_based_capabilities


def cf_mysql_flexible_log(cli_ctx, _):
return get_mysql_flexible_management_client(cli_ctx).log_files


def cf_mysql_check_resource_availability(cli_ctx, _):
return get_mysql_flexible_management_client(cli_ctx).check_name_availability

Expand Down
25 changes: 25 additions & 0 deletions src/azure-cli/azure/cli/command_modules/rdbms/_helptext_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,28 @@
- name: Run an existing workflow in your github repository
text: az mysql flexible-server deploy run --action-name testAction --branch userBranch
"""

helps['mysql flexible-server server-logs'] = """
type: group
short-summary: Manage server logs.
"""

helps['mysql flexible-server server-logs download'] = """
type: command
short-summary: Download log files.
examples:
- name: Download log files f1 and f2 to the current directory from the server 'testsvr'.
text: az mysql flexible-server server-logs download -g testgroup -s testsvr -n f1.log f2.log
"""

helps['mysql flexible-server server-logs list'] = """
type: command
short-summary: List log files for a server.
examples:
- name: List log files for 'testsvr' modified in the last 72 hours (default value).
text: az mysql flexible-server server-logs list -g testgroup -s testsvr
- name: List log files for 'testsvr' modified in the last 10 hours.
text: az mysql flexible-server server-logs list -g testgroup -s testsvr --file-last-written 10
- name: List log files for 'testsvr' less than 30Kb in size.
text: az mysql flexible-server server-logs list -g testgroup -s testsvr --max-file-size 30
"""
12 changes: 12 additions & 0 deletions src/azure-cli/azure/cli/command_modules/rdbms/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,18 @@ def _flexible_server_params(command_group):
c.argument('action_name', options_list=['--action-name'], help='The name of the github action')
c.argument('branch', options_list=['--branch'], help='The name of the branch you want upload github action file. The default will be your current branch.')

# logs
if command_group == 'mysql':
with self.argument_context('{} flexible-server server-logs download'.format(command_group)) as c:
c.argument('server_name', id_part='name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type, help='Name of the Server.')
c.argument('file_name', options_list=['--name', '-n'], nargs='+', help='Space-separated list of log filenames on the server to download.')

with self.argument_context('{} flexible-server server-logs list'.format(command_group)) as c:
c.argument('server_name', id_part='name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type, help='Name of the Server.')
c.argument('filename_contains', help='The pattern that file name should match.')
c.argument('file_last_written', type=int, help='Integer in hours to indicate file last modify time.', default=72)
c.argument('max_file_size', type=int, help='The file size limitation to filter files.')

handle_migration_parameters(command_group, server_name_arg_type, migration_id_arg_type)

def handle_migration_parameters(command_group, server_name_arg_type, migration_id_arg_type):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
cf_mysql_flexible_db,
cf_mysql_flexible_replica,
cf_mysql_flexible_location_capabilities,
cf_mysql_flexible_log,
cf_postgres_flexible_servers,
cf_postgres_flexible_firewall_rules,
cf_postgres_flexible_config,
Expand Down Expand Up @@ -62,6 +63,11 @@ def load_flexibleserver_command_table(self, _):
client_factory=cf_mysql_flexible_location_capabilities
)

mysql_flexible_log_sdk = CliCommandType(
operations_tmpl='azure.mgmt.rdbms.mysql_flexibleservers.operations#LogFilesOperations.{}',
client_factory=cf_mysql_flexible_log
)

postgres_flexible_servers_sdk = CliCommandType(
operations_tmpl='azure.mgmt.rdbms.postgresql_flexibleservers.operations#ServersOperations.{}',
client_factory=cf_postgres_flexible_servers
Expand Down Expand Up @@ -236,3 +242,9 @@ def load_flexibleserver_command_table(self, _):
client_factory=cf_mysql_flexible_servers) as g:
g.custom_command('setup', 'github_actions_setup')
g.custom_command('run', 'github_actions_run')

with self.command_group('mysql flexible-server server-logs', mysql_flexible_log_sdk,
custom_command_type=flexible_server_custom_common,
client_factory=cf_mysql_flexible_log) as g:
g.custom_command('list', 'flexible_server_log_list')
g.custom_command('download', 'flexible_server_log_download')
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
# pylint: disable=unused-argument, line-too-long

import os
import re
import json
import uuid
from datetime import datetime
from datetime import datetime, timedelta
from dateutil.tz import tzutc
from knack.log import get_logger
from knack.util import CLIError
from urllib.request import urlretrieve
from azure.cli.core.azclierror import MutuallyExclusiveArgumentError
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import send_raw_request
Expand Down Expand Up @@ -317,3 +320,37 @@ def gitcli_check_and_login():
output = run_subprocess_get_output("gh auth status")
if output.returncode:
run_subprocess("gh auth login", stdout_show=True)


# Custom functions for server logs
def flexible_server_log_download(client, resource_group_name, server_name, file_name):

files = client.list_by_server(resource_group_name, server_name)

for f in files:
if f.name in file_name:
urlretrieve(f.url, f.name)


def flexible_server_log_list(client, resource_group_name, server_name, filename_contains=None,
file_last_written=None, max_file_size=None):

all_files = client.list_by_server(resource_group_name, server_name)
files = []

if file_last_written is None:
file_last_written = 72
time_line = datetime.utcnow().replace(tzinfo=tzutc()) - timedelta(hours=file_last_written)

for f in all_files:
if f.last_modified_time < time_line:
continue
if filename_contains is not None and re.search(filename_contains, f.name) is None:
continue
if max_file_size is not None and f.size_in_kb > max_file_size:
continue

del f.created_time
files.append(f)

return files

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 32028f9

Please sign in to comment.