From da6e3500a3a7cca304eae6c5efe7e2573e4627dd Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 20 May 2021 16:46:22 -0400 Subject: [PATCH] fix(cgi-bin): add request body parsing to set_membership --- cgi-bin/set_membership | 79 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index d6c83ffb..c130ae64 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# Accepts a HTTP PUT request to set the cluster membership of a node. +# Accepts a HTTP PUT request to set the cluster membership of a host. # use strict; @@ -20,8 +20,40 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); +sub alter_host_membership +{ + # Expects the first of positional parameters to be a hash. + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + my $join_cluster = $parameters->{join_cluster}; + + $anvil->Log->variables({ + source => $THIS_FILE, + line => __LINE__, + level => 2, + list => { + host_membership_status => Dumper($anvil->data->{hosts}{host_uuid}{$host_uuid}) + } + }); +} + +# Re-adds a host to its anvil cluster. +# +# Note: not to be confused wtih adding a new host the an anvil. +#sub join_cluster +#{} + +# Removes a host from its anvil cluster. +# +# Note: does not permanently remove the host; can be re-added. +#sub leave_cluster +#{} + $anvil->Get->switches; +# TODO: remove when done; temporarily set log level to 2 +$anvil->Log->level({ set => 2 }); + $anvil->Database->connect; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); if (not $anvil->data->{sys}{database}{connections}) @@ -41,4 +73,49 @@ print $anvil->Template->get({file => "shared.html", name => "json_headers", show my $response_body = {}; +my $request_body; + +my $is_decode_json_success = eval { + $request_body = decode_json($anvil->data->{cgi}{PUTDATA}{value}); +}; + +if (not $is_decode_json_success) +{ + $anvil->Log->entry({ + source => $THIS_FILE, + line => __LINE__, + level => 0, + 'print' => 1, + priority => "err", + key => "error_0304", + variables => { request_body_string => $anvil->data->{cgi}{PUTDATA}{value}, json_decode_error => $_ } + }); + $anvil->nice_exit({ exit_code => 1 }); +} + +my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : ""; +# Defaults to join; will check whether host is already part of its anvil cluster. +my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : 1; + +if ($host_uuid) +{ + if (exists $anvil->data->{hosts}{host_uuid}{$host_uuid}) + { + alter_host_membership({ host_uuid => $host_uuid, join_cluster => $is_member }); + } + else + { + $anvil->Log->entry({ + source => $THIS_FILE, + line => __LINE__, + level => 0, + 'print' => 1, + priority => "err", + key => "error_0160", + variables => { name => "host UUID", uuid => $host_uuid } + }); + $anvil->nice_exit({ exit_code => 1 }); + } +} + print JSON->new->utf8->encode($response_body)."\n";