%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /scripts/
Upload File :
Create Path :
Current File : //scripts/migrate-api-tokens-for-service

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/migrate-api-tokens-for-service  Copyright 2020 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::migrate_api_tokens_for_service;

use strict;
use warnings;
use autodie;

#----------------------------------------------------------------------

=encoding utf-8

=head1 USAGE

    migrate-api-tokens-for-service [ --help ]

=head1 DESCRIPTION

This script migrates the API token datastore schema for v80.
It runs automatically on update to v80; there should be no need
to run it manually.

=cut

#----------------------------------------------------------------------

use parent qw( Cpanel::HelpfulScript );

use Cpanel::CommandQueue                                ();
use Cpanel::FileUtils::Dir                              ();
use Cpanel::Security::Authn::APITokens::Write::whostmgr ();

use constant _OPTIONS => ();

our $_OLD_DIR = '/var/cpanel/authn/api_tokens';

__PACKAGE__->new(@ARGV)->run() if !caller;

sub run {
    my $self = shift;

    # This is a one-time migration needed for the v78 -> v80 upgrade.

    my $whm_dir = Cpanel::Security::Authn::APITokens::Write::whostmgr->_base_dir();

    my $has_old_data = Cpanel::FileUtils::Dir::directory_has_nodes_if_exists($_OLD_DIR);
    my $has_new_data = Cpanel::FileUtils::Dir::directory_has_nodes_if_exists($whm_dir);

    # The following isn’t fully race-safe but has the advantage that
    # it leaves the existing “api_tokens” directory in place, which
    # avoids breakage in currently-running daemons that use API tokens.
    if ( $has_old_data && !$has_new_data ) {

        # This will create the new directory structure if needed.
        Cpanel::Security::Authn::APITokens::Write::whostmgr->_create_base_dirs();

        my $cq = Cpanel::CommandQueue->new();

        my $tmp_dirpath;

        $cq->add(
            sub {
                $tmp_dirpath = "$whm_dir.tmp2." . time;
                rename $whm_dir => $tmp_dirpath;
            },
            sub {
                rename $tmp_dirpath => $whm_dir;
            },
        );

        $cq->add(
            sub {
                symlink $_OLD_DIR, $whm_dir;

                $self->say('API Tokens datastore migration is complete.');

                CORE::rmdir($tmp_dirpath) or warn "rmdir($tmp_dirpath): $!";
            },
        );

        $cq->run();
    }

    return;
}

1;

Zerion Mini Shell 1.0