Skip to content

Commit

Permalink
feat: provision to change mariadb data direcotry
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh6790 committed Sep 2, 2024
1 parent 0fc99f1 commit a49664a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
12 changes: 6 additions & 6 deletions press/playbooks/self_hosted_db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
- role: essentials
- role: user
- role: mariadb
- role: nginx
- role: agent
- role: node_exporter
- role: mysqld_exporter
- role: deadlock_logger
- role: filebeat
# - role: nginx
# - role: agent
# # - role: node_exporter
# - role: mysqld_exporter
# - role: deadlock_logger
# - role: filebeat
31 changes: 31 additions & 0 deletions press/press/doctype/database_server/database_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@ frappe.ui.form.on('Database Server', {
},
__('Dangerous Actions'),
);
frm.add_custom_button(
__('Change Mariadb Data Directory'),
() => {
const dialog = new frappe.ui.Dialog({
title: __('Update Memory Allocator'),
fields: [
{
fieldtype: 'Data',
label: __('Data Directory'),
fieldname: 'data_dir',
reqd: 1,
},
],
});

dialog.set_primary_action(__('Update'), (args) => {
frm.call({
method: 'change_data_directory',
doc: frm.doc,
args: args,
freeze: true,
callback: () => {
dialog.hide();
frm.refresh();
},
});
});
dialog.show();
},
__('Dangerous Actions'),
);
}
},

Expand Down
12 changes: 10 additions & 2 deletions press/press/doctype/database_server/database_server.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"self_hosted_mariadb_server",
"mariadb_root_password",
"data_dir",
"_old_data_directory",
"server_id",
"is_primary",
"column_break_12",
Expand Down Expand Up @@ -522,12 +523,19 @@
{
"fieldname": "data_dir",
"fieldtype": "Data",
"label": "Data Directory"
"label": "Data Directory",
"read_only": 1
},
{
"fieldname": "_old_data_directory",
"fieldtype": "Data",
"label": "_Old Data Directory",
"read_only": 1
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-08-28 10:03:56.428283",
"modified": "2024-08-29 14:35:57.852153",
"modified_by": "Administrator",
"module": "Press",
"name": "Database Server",
Expand Down
45 changes: 45 additions & 0 deletions press/press/doctype/database_server/database_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DatabaseServer(BaseServer):
)
from press.press.doctype.resource_tag.resource_tag import ResourceTag

_old_data_directory: DF.Data | None
agent_password: DF.Password | None
auto_add_storage_max: DF.Int
auto_add_storage_min: DF.Int
Expand Down Expand Up @@ -92,6 +93,7 @@ def validate(self):
self.validate_mariadb_root_password()
self.validate_server_id()
self.validate_mariadb_system_variables()
self.set_data_directory()

def validate_mariadb_root_password(self):
if not self.mariadb_root_password:
Expand All @@ -102,6 +104,15 @@ def validate_mariadb_system_variables(self):
for variable in self.mariadb_system_variables:
variable.validate()

def set_data_directory(self):
if not self.data_dir:
self.data_dir = "/var/lib/mysql/"

doc_before_save = self.get_doc_before_save()

if doc_before_save and self.has_value_changed("data_dir"):
self._old_data_directory = doc_before_save.data_dir

def on_update(self):
if self.flags.in_insert or self.is_new():
return
Expand Down Expand Up @@ -999,6 +1010,40 @@ def _update_memory_allocator(self, memory_allocator):
self.memory_allocator_version = query_result[0][0]["Value"]
self.save()

@frappe.whitelist()
def change_data_directory(self, data_dir):
self._old_data_directory = "/var/lib/mysql/" # self.data_dir
self.data_dir = data_dir
self.save()
self.load_from_db()

# if self._old_data_directory != self.data_dir:
frappe.enqueue_doc(
self.doctype,
self.name,
"_change_data_directory",
enqueue_after_commit=True,
)

def _change_data_directory(self):
ansible = Ansible(
playbook="mariadb_change_datadir.yml",
server=self,
user=self.ssh_user or "root",
port=self.ssh_port or 22,
variables={
"current_mariadb_datadir": self._old_data_directory,
"new_mariadb_datadir": self.data_dir,
},
)

play = ansible.run()

if play.status == "Failure":
log_error("MariaDB Data Directory Change Error", server=self.name)
self.status = "Broken"
self.save()


get_permission_query_conditions = get_permission_query_conditions_for_doctype(
"Database Server"
Expand Down

0 comments on commit a49664a

Please sign in to comment.