App-Yabsm

 view release on metacpan or  search on metacpan

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN

                 #              HELPERS             #
                 ####################################

sub initialize_yabsmd_runtime_environment {

    # Initialize yabsmd's runtime environment:
    #
    # * Install the signal handlers that remove the PID file before exiting
    # * Create dirs needed for performing snaps, ssh_backups, and local_backups
    # * Create the yabsm user and group if they don't already exists
    # * If $create_log_file, create /var/log/yabsm if it does not exist and chown it to yabsm:yabsm
    # * If $create_pid_file, create the (empty) file /run/yabsmd.pid and chown it to yabsm:yabsm
    # * Create the yabsm users SSH keys if they don't already exist
    # * Set this processes UID and GID to yabsm:yabsm

    arg_count_or_die(3, 3, @_);

    my $create_log_file = shift;
    my $create_pid_file = shift;
    my $config_ref      = shift;

    i_am_root_or_die();

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN

    open my $sudoer_fh, '>', '/etc/sudoers.d/yabsm-btrfs'
      or die "yabsm: error: cannot open '/etc/sudoers.d/yabsm-btrfs' for writing";
    my $btrfs_bin = `which btrfs 2>/dev/null`;
    print $sudoer_fh "yabsm ALL=(root) NOPASSWD: $btrfs_bin";
    close $sudoer_fh;

    if ($create_log_file) {
        open my $log_fh, '>>', '/var/log/yabsm'
          or confess q(yabsm: internal error: cannot open file '/var/log/yabsm' for writing);
        close $log_fh;
        chown $yabsm_uid, $yabsm_gid, '/var/log/yabsm';
        chmod 0644, '/var/log/yabsm';
    }

    if ($create_pid_file) {
        open my $pid_fh, '>', '/run/yabsmd.pid'
          or confess q(yabsm: internal error: cannot not open file '/run/yabsmd.pid' for writing);
        close $pid_fh;
        chown $yabsm_uid, $yabsm_gid, '/run/yabsmd.pid';
        chmod 0644, '/run/yabsmd.pid';
    }

    POSIX::setgid($yabsm_gid);
    POSIX::setuid($yabsm_uid);

    create_yabsm_user_ssh_key(0, $config_ref);

    return 1;
}

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN

        }

        my $yabsm_user_home = yabsm_user_home($config_ref);

        my $ssh_dir  = "$yabsm_user_home/.ssh";
        my $priv_key = "$ssh_dir/id_ed25519";
        my $pub_key  = "$ssh_dir/id_ed25519.pub";

        unless (-f $priv_key && -f $pub_key) {
            system_or_die('ssh-keygen', '-t', 'ed25519', '-f', $priv_key, '-N', '');
            chown $yabsm_uid, $yabsm_gid, $priv_key, $pub_key;
            chmod 0600, $priv_key;
            chmod 0644, $pub_key;
        }

        return 1;
    }

    return 0;
}

t/SSHBackup.t  view on Meta::CPAN

$f = \&App::Yabsm::Backup::SSH::ssh_system_or_die;
lives_and { is $f->($SSH, 'echo foo'), "foo\n" } "$n - returns correct output in scalar context";
lives_and { is_deeply [$f->($SSH, 'echo foo; echo bar')], ["foo\n","bar\n"] } "$n - returns correct output in list context";
throws_ok { $f->($SSH, 'false') } qr/remote command 'false' failed/, "$n - dies if command fails";

$n = 'check_ssh_backup_config_or_die';
$f = \&App::Yabsm::Backup::SSH::check_ssh_backup_config_or_die;
throws_ok { $f->($SSH, 'foo_ssh_backup', \%TEST_CONFIG) } qr/no directory '$BACKUP_DIR_BASE' that is readable\+writable by user 'yabsm'/, "$n - dies unless backup dir exists";
make_path_or_die($BACKUP_DIR_BASE);
throws_ok { $f->($SSH, 'foo_ssh_backup', \%TEST_CONFIG) } qr/no directory '$BACKUP_DIR_BASE' that is readable\+writable by user 'yabsm'/, "$n - dies unless backup dir is readable and writable by remote user";
system_or_die(qq(chown -R yabsm '$BTRFS_DIR'));
lives_and { is $f->($SSH, 'foo_ssh_backup', \%TEST_CONFIG), 1 } "$n - lives if properly configured";

$n = 'the_remote_bootstrap_snapshot';
$f = \&App::Yabsm::Backup::SSH::the_remote_bootstrap_snapshot;
lives_and { is $f->($SSH, 'foo_ssh_backup', \%TEST_CONFIG), undef } "$n - returns undef if no remote boot snap";

$n = 'do_ssh_backup';
$f = \&App::Yabsm::Backup::SSH::do_ssh_backup;
throws_ok { $f->($SSH, 'foo_ssh_backup', '5minute', \%TEST_CONFIG) } qr/no directory '$TMP_DIR' that is readable by user/, "$n - dies if tmp dir doesn't exist";
make_path_or_die($TMP_DIR);



( run in 0.792 second using v1.01-cache-2.11-cpan-71847e10f99 )