Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
basic functionality for github action deploy
  • Loading branch information
DaeunYim committed May 3, 2021
commit 12c67d6bc540c335f0e321bd040a165603f8e3fd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime as dt
from datetime import datetime
import random
import subprocess
from knack.log import get_logger
from azure.core.paging import ItemPaged
from azure.cli.core.commands import LongRunningOperation, _is_poller
Expand Down Expand Up @@ -340,3 +341,14 @@ def _resolve_api_version(client, provider_namespace, resource_type, parent_path)
raise RequiredArgumentMissingError(
'API version is required and could not be resolved for resource {}'
.format(resource_type))


def run_subprocess(command):
commands = command.split()
subprocess.run(commands, shell=True)

def run_subprocess_get_output(command):
commands = command.split()
process = subprocess.Popen(commands, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
return process
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 @@ -465,5 +465,17 @@ def _flexible_server_params(command_group):
with self.argument_context('{} flexible-server replica stop-replication'.format(command_group)) as c:
c.argument('server_name', options_list=['--name', '-n'], help='Name of the replica server.')

with self.argument_context('{} deploy setup'.format(command_group)) as c:
c.argument('server_name', id_part='name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type)
c.argument('database_name', options_list=['--database-name', '-d'], help='the name of the database')
c.argument('administrator_login', options_list=['--admin-user', '-u'], arg_group='Authentication', arg_type=administrator_login_arg_type,
help='Administrator username for the server.')
c.argument('administrator_login_password', options_list=['--admin-password', '-p'], help='The password of the administrator.')
c.argument('file_path', options_list=['--file-path', '-f'], help='the name of file')
c.argument('action_name', options_list=['--action-name'], help='the')

with self.argument_context('{} deploy run'.format(command_group)) as c:
c.argument('action_name', options_list=['--action-name'], help='the')

_flexible_server_params('postgres')
_flexible_server_params('mysql')
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,9 @@ def load_flexibleserver_command_table(self, _):
is_preview=True) as g:
g.custom_command('create', 'flexible_replica_create', supports_no_wait=True)
g.custom_command('stop-replication', 'flexible_replica_stop', confirmation=True)

with self.command_group('postgres deploy',
custom_command_type=flexible_servers_custom_postgres,
is_preview=True) as g:
g.custom_command('setup', 'github_actions_setup')
g.custom_command('run', 'github_actions_run')
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,52 @@ def user_confirmation(message, yes=False):
except NoTTYException:
raise CLIError(
'Unable to prompt for confirmation as no tty available. Use --yes.')


def github_actions_setup(cmd, client, resource_group_name, server_name, database_name, administrator_login, administrator_login_password, sql_file_path, repository, action_name=None, branch=None, allow_push=None):

if allow_push and not branch:
raise RequiredArgumentMissingError("Provide remote branch name to allow pushing the action file to your remote branch.")
if action_name is None:
action_name = server.name + '_' + database_name + "_deploy"
gitcli_check_and_login()

server = client.get(resource_group_name, server_name)
fill_action_template(cmd,
server=server,
database_name=database_name,
administrator_login=administrator_login,
administrator_login_password=administrator_login_password,
file_name=sql_file_path,
repository=repository,
action_name=action_name)


action_path = get_git_root_dir() + GITHUB_ACTION_PATH + action_name + '.yml'
logger.warning("Making git commit for file {}".format(action_path))
run_subprocess("git add {}".format(action_path))
run_subprocess("git commit -m \"Add github action file\"")

if allow_push:
logger.warning("Pushing the created action file to origin {} branch".format(branch))
run_subprocess("git push origin {}".format(branch))
github_actions_run(action_name, branch)
else:
logger.warning('You did not set --allow-push parameter. Please push the prepared file {} to your remote repo and run "deploy run" command to activate the workflow.'.format(action_path))


def github_actions_run(action_name, branch):

gitcli_check_and_login()
logger.warning("Created event for {}.yml in branch {}".format(action_name, branch))
run_subprocess("gh workflow run {}.yml --ref {}".format(action_name, branch))


def gitcli_check_and_login():
output = run_subprocess_get_output("gh")
if output.returncode:
raise ClientRequestError('Please install "Github CLI" to run this command.')

output = run_subprocess_get_output("gh auth status")
if output.returncode:
run_subprocess("gh auth login", stdout_show=True)
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
from azure.cli.core.local_context import ALL
from azure.cli.core.util import CLIError, sdk_no_wait
from azure.core.exceptions import ResourceNotFoundError
from azure.cli.core.azclierror import RequiredArgumentMissingError
from azure.cli.core.azclierror import RequiredArgumentMissingError, ClientRequestError
from azure.mgmt.rdbms import postgresql_flexibleservers
from ._client_factory import cf_postgres_flexible_firewall_rules, get_postgresql_flexible_management_client, cf_postgres_flexible_db, cf_postgres_check_resource_availability
from .flexible_server_custom_common import user_confirmation
from ._flexible_server_util import generate_missing_parameters, resolve_poller, create_firewall_rule, \
parse_public_access_input, generate_password, parse_maintenance_window, get_postgres_list_skus_info, \
DEFAULT_LOCATION_PG
DEFAULT_LOCATION_PG, run_subprocess, run_subprocess_get_output
from .flexible_server_virtual_network import prepare_private_network, prepare_private_dns_zone
from .validators import pg_arguments_validator, validate_server_name


logger = get_logger(__name__)
DEFAULT_DB_NAME = 'flexibleserverdb'
DELEGATION_SERVICE_NAME = "Microsoft.DBforPostgreSQL/flexibleServers"
Expand Down Expand Up @@ -472,6 +471,24 @@ def _update_local_contexts(cmd, server_name, resource_group_name, location, user
cmd.cli_ctx.local_context.set([ALL], 'resource_group_name', resource_group_name)


def github_actions_setup(cmd, server_name, database_name, administrator_login, file_path, action_name, administrator_login_password=None):

output = run_subprocess_get_output("gh")
if output.returncode:
raise ClientRequestError('Please install "Github CLI" to run this command.')

run_subprocess("gh auth login")


def github_actions_run(cmd, action_name):

output = run_subprocess_get_output("gh")
if output.returncode:
raise ClientRequestError('Please install "Github CLI" to run this command.')

run_subprocess("gh auth login")


# pylint: disable=too-many-instance-attributes, too-few-public-methods, useless-object-inheritance
class DbContext(object):
def __init__(self, azure_sdk=None, logging_name=None, cf_firewall=None, cf_db=None,
Expand Down