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

feat: support to configure max database connections for site database users #2316

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
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
19 changes: 13 additions & 6 deletions dashboard/src2/components/SiteDatabaseAccessDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Dialog
:options="{
title: 'Manage Database Users',
size: planSupportsDatabaseAccess ? '2xl' : 'xl'
size: planSupportsDatabaseAccess ? '3xl' : 'xl'
}"
v-model="show"
>
Expand Down Expand Up @@ -122,7 +122,7 @@ export default {
site: this.site,
status: ['!=', 'Archived']
},
searchField: 'username',
searchField: 'label',
filterControls() {
return [
{
Expand All @@ -135,9 +135,9 @@ export default {
},
columns: [
{
label: 'Username',
fieldname: 'username',
width: 1
label: 'Label',
fieldname: 'label',
width: '150px'
},
{
label: 'Status',
Expand All @@ -146,6 +146,13 @@ export default {
align: 'center',
type: 'Badge'
},
{
label: 'DB Connections',
fieldname: 'max_connections',
width: 0.5,
align: 'center',
format: value => `${value} Connection` + (value > 1 ? 's' : '')
},
{
label: 'Mode',
fieldname: 'mode',
Expand All @@ -168,7 +175,7 @@ export default {
}
],
rowActions: ({ row, listResource, documentResource }) => {
if (row.status === 'Archived') {
if (row.status === 'Archived' || row.status === 'Pending') {
return [];
}
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
type="error"
>
</AlertBanner>
<FormControl
class="mt-2"
type="text"
size="sm"
variant="subtle"
label="Label (to identify the user)"
v-model="label"
/>
<FormControl
type="select"
:options="[
Expand All @@ -40,6 +48,14 @@
label="Access Mode"
v-model="mode"
/>
<FormControl
class="mt-2"
type="number"
size="sm"
variant="subtle"
label="Database Connections"
v-model="database_connections"
/>
<!-- Permission configuration for Granular Mode -->
<div v-if="mode == 'granular'">
<div
Expand Down Expand Up @@ -130,7 +146,9 @@ export default {
},
data() {
return {
label: '',
mode: 'read_only',
database_connections: 1,
permissions: [],
lastGeneratedRowId: 0
};
Expand Down Expand Up @@ -163,6 +181,7 @@ export default {
name: this.db_user_name,
auto: false,
onSuccess: data => {
this.label = data?.label;
this.mode = data?.mode;
let fetched_permissions = (data?.permissions ?? []).map(x => {
return {
Expand All @@ -171,6 +190,7 @@ export default {
};
});
this.permissions = fetched_permissions;
this.database_connections = data?.max_connections ?? 1;
}
};
},
Expand All @@ -192,10 +212,12 @@ export default {
return {
doc: {
doctype: 'Site Database User',
label: this.label,
team: this.$team.doc.name,
site: this.site,
mode: this.mode,
permissions: permissions
permissions: permissions,
max_connections: parseInt(this.database_connections || 1)
}
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
Password: {{ databaseCredential?.password }}
</p>
<p class="ml-1 font-mono text-sm">Use SSL: Yes</p>
<p class="ml-1 font-mono text-sm">
Max Database Connection{{
databaseCredential?.max_connections > 1 ? 's' : ''
}}: {{ databaseCredential?.max_connections }}
</p>
</div>
<div class="pb-2 pt-5">
<p class="mb-2 text-base font-semibold text-gray-700">
Expand Down
11 changes: 10 additions & 1 deletion press/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,21 @@ def remove_ssh_user(self, bench):
)

def add_proxysql_user(
self, site, database, username, password, database_server, reference_doctype=None, reference_name=None
self,
site,
database: str,
username: str,
password: str,
max_connections: int,
database_server,
reference_doctype=None,
reference_name=None,
):
data = {
"username": username,
"password": password,
"database": database,
"max_connections": max_connections,
"backend": {"ip": database_server.private_ip, "id": database_server.server_id},
}
return self.create_agent_job(
Expand Down
11 changes: 1 addition & 10 deletions press/press/doctype/agent_job/agent_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@
process_setup_erpnext_site_job_update,
)
from press.press.doctype.site.site import (
process_add_proxysql_user_job_update,
process_archive_site_job_update,
process_complete_setup_wizard_job_update,
process_create_user_job_update,
Expand All @@ -905,7 +904,6 @@
process_move_site_to_bench_job_update,
process_new_site_job_update,
process_reinstall_site_job_update,
process_remove_proxysql_user_job_update,
process_rename_site_job_update,
process_restore_job_update,
process_restore_tables_job_update,
Expand Down Expand Up @@ -979,16 +977,9 @@
process_add_ssh_user_job_update(job)
elif job.job_type == "Remove User from Proxy":
process_remove_ssh_user_job_update(job)
elif job.job_type == "Add User to ProxySQL":
elif job.job_type == "Add User to ProxySQL" or job.job_type == "Remove User from ProxySQL":

Check warning on line 980 in press/press/doctype/agent_job/agent_job.py

View check run for this annotation

Codecov / codecov/patch

press/press/doctype/agent_job/agent_job.py#L980

Added line #L980 was not covered by tests
if job.reference_doctype == "Site Database User":
SiteDatabaseUser.process_job_update(job)
else:
process_add_proxysql_user_job_update(job)
elif job.job_type == "Remove User from ProxySQL":
if job.reference_doctype == "Site Database User":
SiteDatabaseUser.process_job_update(job)
else:
process_remove_proxysql_user_job_update(job)
elif job.job_type == "Reload NGINX":
process_update_nginx_job_update(job)
elif job.job_type == "Move Site to Bench":
Expand Down
23 changes: 0 additions & 23 deletions press/press/doctype/site/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,6 @@ frappe.ui.form.on('Site', {
});
frm.toggle_enable(['host_name'], frm.doc.status === 'Active');

if (frm.doc.is_database_access_enabled) {
frm.add_custom_button(
__('Show Database Credentials'),
() =>
frm.call('get_database_credentials').then((r) => {
let message = `Host: ${r.message.host}

Port: ${r.message.port}

Database: ${r.message.database}

Username: ${r.message.username}

Password: ${r.message.password}

\`\`\`\nmysql -u ${r.message.username} -p${r.message.password} -h ${r.message.host} -P ${r.message.port} --ssl --ssl-verify-server-cert\n\`\`\``;

frappe.msgprint(frappe.markdown(message), 'Database Credentials');
}),
__('Actions'),
);
}

frm.add_custom_button(
__('Replicate Site'),
() => {
Expand Down
44 changes: 8 additions & 36 deletions press/press/doctype/site/site.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@
"setup_wizard_status_check_retries",
"setup_wizard_status_check_next_retry_on",
"database_section",
"is_database_access_enabled",
"database_access_mode",
"column_break_lfuz",
"database_access_user",
"database_access_password",
"database_access_connection_limit",
"saas_section",
"is_standby",
"standby_for",
Expand Down Expand Up @@ -452,13 +448,6 @@
"fieldtype": "Section Break",
"label": "Database Access"
},
{
"default": "0",
"fieldname": "is_database_access_enabled",
"fieldtype": "Check",
"label": "Is Database Access Enabled",
"read_only": 1
},
{
"fieldname": "standby_for",
"fieldtype": "Link",
Expand Down Expand Up @@ -528,29 +517,6 @@
"fieldtype": "Check",
"label": "Hide Config"
},
{
"fieldname": "database_access_password",
"fieldtype": "Password",
"label": "Database Access Password",
"read_only": 1
},
{
"fieldname": "database_access_mode",
"fieldtype": "Select",
"label": "Database Access Mode",
"options": "\nread_only\nread_write",
"read_only": 1
},
{
"fieldname": "column_break_lfuz",
"fieldtype": "Column Break"
},
{
"fieldname": "database_access_user",
"fieldtype": "Data",
"label": "Database Access User",
"read_only": 1
},
{
"collapsible": 1,
"fieldname": "backups_section",
Expand Down Expand Up @@ -628,6 +594,12 @@
"fieldname": "setup_wizard_status_check_next_retry_on",
"fieldtype": "Datetime",
"label": "Next Retry On"
},
{
"default": "16",
"fieldname": "database_access_connection_limit",
"fieldtype": "Int",
"label": "Database Access Connection Limit"
}
],
"links": [
Expand Down Expand Up @@ -702,7 +674,7 @@
"link_fieldname": "site"
}
],
"modified": "2024-11-04 09:40:44.252728",
"modified": "2024-11-28 15:40:02.431631",
"modified_by": "Administrator",
"module": "Press",
"name": "Site",
Expand Down
18 changes: 2 additions & 16 deletions press/press/doctype/site/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ class Site(Document, TagHelpers):
current_cpu_usage: DF.Int
current_database_usage: DF.Int
current_disk_usage: DF.Int
database_access_mode: DF.Literal["", "read_only", "read_write"]
database_access_password: DF.Password | None
database_access_user: DF.Data | None
database_access_connection_limit: DF.Int
database_name: DF.Data | None
domain: DF.Link | None
erpnext_consultant: DF.Link | None
Expand All @@ -136,7 +134,6 @@ class Site(Document, TagHelpers):
hide_config: DF.Check
host_name: DF.Data | None
hybrid_saas_pool: DF.Link | None
is_database_access_enabled: DF.Check
is_erpnext_setup: DF.Check
is_standby: DF.Check
notify_email: DF.Data | None
Expand Down Expand Up @@ -189,7 +186,7 @@ class Site(Document, TagHelpers):
"cluster",
"bench",
"group",
"is_database_access_enabled",
"database_access_connection_limit",
"trial_end_date",
"tags",
"server",
Expand Down Expand Up @@ -3038,17 +3035,6 @@ def process_rename_site_job_update(job): # noqa: C901
create_site_status_update_webhook_event(job.site)


# TODO
def process_add_proxysql_user_job_update(job):
if job.status == "Success":
frappe.db.set_value("Site", job.site, "is_database_access_enabled", True)


def process_remove_proxysql_user_job_update(job):
if job.status == "Success":
frappe.db.set_value("Site", job.site, "is_database_access_enabled", False)


def process_move_site_to_bench_job_update(job):
updated_status = {
"Pending": "Pending",
Expand Down
18 changes: 17 additions & 1 deletion press/press/doctype/site_database_user/site_database_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"engine": "InnoDB",
"field_order": [
"status",
"label",
"mode",
"site",
"team",
"column_break_udtx",
"max_connections",
"username",
"password",
"user_created_in_database",
Expand Down Expand Up @@ -125,6 +127,20 @@
"options": "Team",
"reqd": 1,
"search_index": 1
},
{
"default": "16",
"fieldname": "max_connections",
"fieldtype": "Int",
"label": "Max Connections",
"set_only_once": 1
},
{
"fieldname": "label",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Label",
"reqd": 1
}
],
"index_web_pages_for_search": 1,
Expand All @@ -135,7 +151,7 @@
"link_fieldname": "reference_name"
}
],
"modified": "2024-11-07 13:03:27.265288",
"modified": "2024-11-29 16:29:57.632579",
"modified_by": "Administrator",
"module": "Press",
"name": "Site Database User",
Expand Down
Loading
Loading