Skip to content

Commit

Permalink
feat(Multi server): Sync single cluster netdevopsbr#33
Browse files Browse the repository at this point in the history
Signed-off-by: devopstales <42894256+devopstales@users.noreply.github.com>
  • Loading branch information
devopstales committed Jul 9, 2023
1 parent 482c570 commit 0002900
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
29 changes: 26 additions & 3 deletions netbox_proxbox/proxbox_api/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def run_process_in_thread(proxmox_session, key, result, index, **kwargs):
def all(**kwargs):
run_with_threads = kwargs.get("run_with_threads", True)
start_time = time.time()
result = []
results = []
if run_with_threads:
print("Start process with threading")
threads = [None] * len(proxmox_sessions)
Expand All @@ -498,14 +498,37 @@ def all(**kwargs):
session = proxmox_sessions[key]
print("Processing data for: {0}".format(key))
output = process_all_in_session(session, **kwargs)
result.append(output)
results.append(output)
except Exception as e:
message = "OS error: {0}".format(e)
print(message)
output = {
'message': message
}
result.append(output)
results.append(output)

print("--- %s seconds ---" % (time.time() - start_time))
return results

def single(**kwargs):
proxmox_domain = kwargs.get("proxmox_domain", False)
start_time = time.time()
results = []
for key in proxmox_sessions:
if key == proxmox_domain:
print("Start process Sequential")
try:
session = proxmox_sessions[key]
print("Processing data for: {0}".format(key))
output = process_all_in_session(session, **kwargs)
results.append(output)
except Exception as e:
message = "OS error: {0}".format(e)
print(message)
output = {
'message': message
}
results.append(output)

print("--- %s seconds ---" % (time.time() - start_time))
return results
Expand Down
19 changes: 18 additions & 1 deletion netbox_proxbox/templates/netbox_proxbox/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,24 @@ <h2>
{% endif %}
</tr>
</table>
<div align="right">
<div class="d-flex justify-content-between">
{% if px.domain %}
<form action="single_update/" method="POST" target="_blank">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<input type="hidden" name="proxmox_domain" value="{{ px.domain }}" />
<a
class="btn btn-primary"
href="#"
onclick="event.preventDefault(); this.parentNode.submit()"
>Proxmox Update</a>
</form>
{% else %}
<a
target="_blank"
class="btn btn-primary"
href="{% url 'plugins:netbox_proxbox:proxmoxvm_single_update' domain=default_config.proxmox.domain %}"
>Proxmox Update</a>
{% endif %}
<button class="btn btn-outline-primary" type="button">
<a
style="text-decoration: none; color: inherit"
Expand Down
4 changes: 3 additions & 1 deletion netbox_proxbox/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ProxmoxVMListView,
ProxmoxVMView,
ProxmoxFullUpdate,
ProxmoxSingleUpdate,
)

from netbox_proxbox import proxbox_api
Expand All @@ -33,5 +34,6 @@

# Proxbox API full update
#path("full_update/", ProxmoxVMFullUpdate.as_view(), name="proxmoxvm_full_update")
path("full_update/", ProxmoxFullUpdate.as_view(), name="proxmoxvm_full_update")
path("full_update/", ProxmoxFullUpdate.as_view(), name="proxmoxvm_full_update"),
path("single_update/", ProxmoxSingleUpdate.as_view(), name="proxmoxvm_single_update"),
]
26 changes: 23 additions & 3 deletions netbox_proxbox/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,36 @@ def table_data():
return [virtualmachines_table, nodes_table]
'''

class ProxmoxSingleUpdate(PermissionRequiredMixin, View):
"""Update a single Proxmox information on Netbox."""

# Define permission
permission_required = "netbox_proxbox.view_proxmoxvm"

# service incoming POST HTTP requests
# 'proxmox_domain' value is passed to post()
def post(self, request):
"""Post request."""

proxmox_domain = self.request.POST.get('proxmox_domain')
json_result = proxbox_api.update.single(remove_unused = True, proxmox_domain = proxmox_domain)

return render(
request,
"netbox_proxbox/proxmox_vm_full_update.html",
{
"proxmox": json_result,
"proxmox_json": json.dumps(json_result, indent=4)
},
)


class ProxmoxFullUpdate(PermissionRequiredMixin, View):
"""Full Update of Proxmox information on Netbox."""

# Define permission
permission_required = "netbox_proxbox.view_proxmoxvm"




# service incoming GET HTTP requests
# 'pk' value is passed to get() via URL defined in urls.py
def get(self, request):
Expand Down

0 comments on commit 0002900

Please sign in to comment.