[pve-devel] apllied: [PATCH manager v4 1/2] add /node/{NODE}/hosts API Call

Dietmar Maurer dietmar at proxmox.com
Fri Sep 14 11:49:51 CEST 2018


applied with small improvements:

> On September 13, 2018 at 2:55 PM Dominik Csapak <d.csapak at proxmox.com> wrote:
> 
> 
> to get and set the content of /etc/hosts
> 
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
> changes from v3:
> * add digest code on read
>  PVE/API2/Nodes.pm | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 78 insertions(+)
> 
> diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
> index 836dccaf..c5b155bc 100644
> --- a/PVE/API2/Nodes.pm
> +++ b/PVE/API2/Nodes.pm
> @@ -186,6 +186,7 @@ __PACKAGE__->register_method ({
>  	    { name => 'firewall' },
>  	    { name => 'certificates' },
>  	    { name => 'config' },
> +	    { name => 'hosts' },
>  	    ];
>  
>  	return $result;
> @@ -1815,6 +1816,83 @@ __PACKAGE__->register_method ({
>  
>      }});
>  
> +__PACKAGE__->register_method ({
> +    name => 'get_etc_hosts',
> +    path => 'hosts',
> +    method => 'GET',
> +    proxyto => 'node',
> +    protected => 1,
> +    permissions => {
> +	check => ['perm', '/', [ 'Sys.Audit' ]],
> +    },
> +    description => "Get the content of /etc/hosts.",
> +    parameters => {
> +	additionalProperties => 0,
> +	properties => {
> +	    node => get_standard_option('pve-node'),
> +	},
> +    },
> +    returns => {
> +	type => 'object',
> +	properties => {
> +	    digest => get_standard_option('pve-config-digest'),
> +	    data => {
> +		type => 'string',
> +		description => 'The content of /etc/hosts.'
> +	    },
> +	},
> +    },
> +    code => sub {
> +	my ($param) = @_;
> +
> +	my $etchosts = PVE::INotify::read_file('etchosts');
> +
> +	return {
> +	    digest => Digest::SHA::sha1_hex($etchosts->{raw}),
> +	    data => $etchosts->{decoded},
> +	};
> +    }});
> +
> +__PACKAGE__->register_method ({
> +    name => 'write_etc_hosts',
> +    path => 'hosts',
> +    method => 'POST',
> +    proxyto => 'node',
> +    protected => 1,
> +    permissions => {
> +	check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
> +    },
> +    description => "Write /etc/hosts.",
> +    parameters => {
> +	additionalProperties => 0,
> +	properties => {
> +	    node => get_standard_option('pve-node'),
> +	    digest => get_standard_option('pve-config-digest'),
> +	    data => {
> +		type => 'string',
> +		description =>  'The target content of /etc/hosts.'
> +	    },
> +	},
> +    },
> +    returns => {
> +	type => 'null',
> +    },
> +    code => sub {
> +	my ($param) = @_;
> +
> +	PVE::Tools::lock_file('/var/lock/pve-etchosts.lck', undef, sub{
> +	    if ($param->{digest}) {
> +		my $hosts = PVE::Tools::file_get_contents('/etc/hosts');
> +		my $digest = Digest::SHA::sha1_hex($hosts);
> +		PVE::Tools::assert_if_modified($digest, $param->{digest});
> +	    }
> +	    PVE::INotify::write_file('etchosts', $param->{data});
> +	});

I use the following instead:

	PVE::Tools::lock_file('/var/lock/pve-etchosts.lck', undef, sub{
	    if ($param->{digest}) {
		my $hosts = PVE::INotify::read_file('etchosts');
		PVE::Tools::assert_if_modified($hosts->{digest}, $param->{digest});
	    }
	    PVE::INotify::write_file('etchosts', $param->{data});
	});


> +	die $@ if $@;
> +
> +	return undef;
> +    }});
> +
>  # bash completion helper
>  
>  sub complete_templet_repo {
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel




More information about the pve-devel mailing list