feat: provision to deploy packages

This commit is contained in:
Saurabh 2020-04-15 16:36:41 +05:30
parent 23a76cea20
commit 08d7f905b5
12 changed files with 80 additions and 206 deletions

View file

@ -1,63 +1,46 @@
{
"actions": [],
"autoname": "field:instance",
"creation": "2020-04-06 15:34:44.428007",
"creation": "2020-03-18 18:25:02.024237",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"instance",
"user",
"instance_url",
"username",
"password"
],
"fields": [
{
"fieldname": "instance",
"fieldname": "instance_url",
"fieldtype": "Data",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Instance URL",
"unique": 1
"reqd": 1
},
{
"fieldname": "user",
"fieldname": "username",
"fieldtype": "Data",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "User",
"options": "Email"
"label": "Username",
"reqd": 1
},
{
"fieldname": "password",
"fieldtype": "Password",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Password"
"label": "Password",
"reqd": 1
}
],
"istable": 1,
"links": [],
"modified": "2020-04-06 15:40:39.242881",
"modified": "2020-04-14 13:56:31.167730",
"modified_by": "Administrator",
"module": "Custom",
"name": "Deploy Instance",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1,
"track_views": 1
"track_changes": 1
}

View file

@ -3,12 +3,6 @@
frappe.ui.form.on('Package', {
refresh: function(frm) {
if (frm.doc.package_details) {
frm.add_custom_button(__("Go to Release"), function() {
frappe.set_route("Form", "Release", "Release");
});
}
frm.set_query("document_type", "package_details", function () {
return {
filters: {
@ -16,13 +10,29 @@ frappe.ui.form.on('Package', {
}
};
});
frappe.realtime.on("package", (data) => {
frm.dashboard.show_progress(data.prefix, data.progress / data.total * 100, __("{0}", [data.message]));
if ((data.progress+1) != data.total) {
frm.dashboard.show_progress(data.prefix, data.progress / data.total * 100, __("{0}", [data.message]));
} else {
frm.dashboard.hide_progress();
}
});
if(frm.doc.instances){
frm.add_custom_button(__("Deploy"), function() {
frm.call("deploy_package");
});
}
},
import: function(frm) {
frm.call("import_from_package");
}
});
frappe.ui.form.on('Package Details', {
frappe.ui.form.on('Package Detail', {
form_render: function (frm, cdt, cdn) {
function _show_filters(filters, table) {
table.find('tbody').empty();

View file

@ -6,6 +6,8 @@
"engine": "InnoDB",
"field_order": [
"package_details",
"section_break_2",
"instances",
"import_section",
"attach",
"import"
@ -34,11 +36,21 @@
"fieldtype": "Table",
"label": "Package Details",
"options": "Package Detail"
},
{
"fieldname": "section_break_2",
"fieldtype": "Section Break"
},
{
"fieldname": "instances",
"fieldtype": "Table",
"label": "Instances",
"options": "Deploy Instance"
}
],
"issingle": 1,
"links": [],
"modified": "2020-04-06 14:32:42.202640",
"modified": "2020-04-14 13:50:50.779396",
"modified_by": "Administrator",
"module": "Custom",
"name": "Package",

View file

@ -11,9 +11,11 @@ from frappe.model.document import Document
from frappe.utils.file_manager import save_file, get_file
from frappe import _
from six import string_types
from frappe.frappeclient import FrappeClient
from frappe.model.naming import make_autoname
from frappe.utils.password import get_decrypted_password
class Package(Document):
def import_from_package(self):
filters = {"attached_to_doctype": "Package", "attached_to_name": "Package"}
files = frappe.get_list("File", filters=filters, limit=1, order_by="creation desc")
@ -27,6 +29,39 @@ class Package(Document):
frappe.msgprint(_("Package Imported."))
def deploy_package(self):
package = export_package()
for dt_file in frappe.get_list("File", filters={"attached_to_doctype": "Release", "attached_to_name": "Release"}):
frappe.delete_doc_if_exists("File", dt_file.name)
file_name = make_autoname("Package")
save_file(file_name, json.dumps(package), "Package", "Package")
length = len(self.instances)
for idx, instance in enumerate(self.instances):
frappe.publish_realtime("package", {"progress": idx, "total": length, "message": instance.instance_url, "prefix": _("Deploying")},
user=frappe.session.user)
self.install_package_to_remote(package, instance)
def install_package_to_remote(self, package, instance):
print((instance.doctype, instance.name))
try:
connection = FrappeClient(instance.instance_url, instance.username, get_decrypted_password(instance.doctype, instance.name))
except Exception:
frappe.log_error(frappe.get_traceback())
frappe.throw(_("Couldn't connect to site {0}. Please check Error Logs.").format(instance.instance_url))
try:
connection.post_request({
"cmd": "frappe.custom.doctype.package.package.import_package",
"package": json.dumps(package)
})
except Exception:
frappe.log_error(frappe.get_traceback())
frappe.throw(_("Error while installing package to site {0}. Please check Error Logs.").format(instance.instance_url))
@frappe.whitelist()
def export_package():
"""Export package as JSON."""

View file

@ -1,23 +0,0 @@
// Copyright (c) 2020, Frappe Technologies and contributors
// For license information, please see license.txt
frappe.ui.form.on('Release', {
refresh: function(frm) {
frm.add_custom_button(__("Go to Package"), function() {
frappe.set_route("Form", "Package", "Package");
});
frm.add_custom_button(__("Release"), function() {
frm.call("create_release");
});
frappe.realtime.on("package", (data) => {
frm.dashboard.show_progress(data.prefix, data.progress / data.total * 100, __("{0}", [data.message]));
if ((data.progress+1) != data.total) {
frm.dashboard.show_progress(data.prefix, data.progress / data.total * 100, __("{0}", [data.message]));
} else {
frm.dashboard.hide_progress();
}
});
}
});

View file

@ -1,41 +0,0 @@
{
"actions": [],
"creation": "2020-03-17 13:27:30.158389",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"instances"
],
"fields": [
{
"fieldname": "instances",
"fieldtype": "Table",
"label": "Instances",
"options": "Release Instance"
}
],
"issingle": 1,
"links": [],
"modified": "2020-04-06 14:56:56.206720",
"modified_by": "Administrator",
"module": "Custom",
"name": "Release",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"print": 1,
"read": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}

View file

@ -1,50 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe
import json
from frappe import _
from frappe.model.document import Document
from frappe.custom.doctype.package.package import export_package
from frappe.frappeclient import FrappeClient
from frappe.utils.file_manager import save_file
from frappe.model.naming import make_autoname
from frappe.utils.password import get_decrypted_password
class Release(Document):
def create_release(self):
package = export_package()
for dt_file in frappe.get_list("File", filters={"attached_to_doctype": "Release", "attached_to_name": "Release"}):
frappe.delete_doc_if_exists("File", dt_file.name)
file_name = make_autoname("Package")
save_file(file_name, json.dumps(package), "Release", "Release")
length = len(self.instances)
for idx, instance in enumerate(self.instances):
frappe.publish_realtime("package", {"progress": idx, "total": length, "message": instance.instance, "prefix": _("Deploying")},
user=frappe.session.user)
self.install_package_to_remote(package, instance)
def install_package_to_remote(self, package, instance):
remote = frappe.get_doc("Deploy Instance", instance.instance)
try:
connection = FrappeClient(remote.instance, remote.user, get_decrypted_password(remote.doctype, remote.name))
except Exception:
frappe.log_error(frappe.get_traceback())
frappe.throw(_("Couldn't connect to site {0}. Please check Error Logs.").format(remote.instance))
try:
connection.post_request({
"cmd": "frappe.custom.doctype.package.package.import_package",
"package": json.dumps(package)
})
except Exception:
frappe.log_error(frappe.get_traceback())
frappe.throw(_("Error while installing package to site {0}. Please check Error Logs.").format(remote.instance))

View file

@ -1,10 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and Contributors
# See license.txt
from __future__ import unicode_literals
# import frappe
import unittest
class TestRelease(unittest.TestCase):
pass

View file

@ -1,32 +0,0 @@
{
"actions": [],
"creation": "2020-03-18 18:25:02.024237",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"instance"
],
"fields": [
{
"fieldname": "instance",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Instance",
"options": "Deploy Instance",
"reqd": 1
}
],
"istable": 1,
"links": [],
"modified": "2020-04-06 15:37:07.099322",
"modified_by": "Administrator",
"module": "Custom",
"name": "Release Instance",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}

View file

@ -1,10 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
# import frappe
from frappe.model.document import Document
class ReleaseInstance(Document):
pass