RPM-Packager

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    name: testpackage
    version: grep Changelog             # version string or some command to retrieve it
    os: el6
    dependencies:
      - perl-YAML > 0.5
      - perl-JSON
    files:
      bin: /usr/local/bin               # directory-based mapping.  RPM will install CWD/bin/* to /usr/local/bin.
    user: apache                        # specify the owner of files.  default: root
    group: apache                       # specify the group owner of files.  default: root
    sign:                               # optionally, gpg signing of RPM
      gpg_name: ED16CAB                 # provide the GPG key ID
      passphrase_cmd: cat secret_file   # command to retrieve the secret
    after_install: path/to/script       # shellscript to run after the package is installed (%post)
    architecture: noarch                # specify the architecture for this package (default: x86_64)

Then run:

    rpm_packager.pl <path_to_manifest.yml>

Note : You need to have fpm available in PATH.  For GPG signing, you need to have proper keys imported.

README  view on Meta::CPAN

        version => 'grep Changelog',
        files   => { bin => '/usr/local/bin' },
        dependencies => [
            'perl-YAML > 0.5',
            'perl-JSON'
        ],
        os      => 'el6',
        user    => 'apache',
        group   => 'apache',
        sign    => {
            'gpg_name' => 'ED16CAB',
            'passphrase_cmd' => 'cat secret_file'
        },
        after_install => 'foo/bar/baz.sh',
        architecture => 'noarch'
    );

    my $obj = RPM::Packager->new(%args);
    $obj->create_rpm();                           # RPM produced in CWD

INSTALLATION

lib/RPM/Packager.pm  view on Meta::CPAN

    name: testpackage
    version: grep Changelog             # version string or some command to retrieve it
    os: el6                             # optional, don't specify anything if package is os-independent
    dependencies:
      - perl-YAML > 0.5
      - perl-JSON
    files:
      bin: /usr/local/bin               # directory-based mapping.  RPM will install CWD/bin/* to /usr/local/bin.
    user: apache                        # specify the owner of files.  default: root
    group: apache                       # specify the group owner of files.  default: root
    sign:                               # optionally, gpg signing of RPM
      gpg_name: ED16CAB                 # provide the GPG key ID
      passphrase_cmd: cat secret_file   # command to retrieve the secret
    after_install: path/to/script       # shellscript to run after the package is installed (%post)
    architecture: noarch                # specify the architecture for this package (default: x86_64)

Then run:

    rpm_packager.pl <path_to_manifest.yml>

Note : You need to have fpm available in PATH.  For GPG signing, you need to have proper keys imported.

lib/RPM/Packager.pm  view on Meta::CPAN

        version => 'grep Changelog',
        files   => { bin => '/usr/local/bin' },
        dependencies => [
            'perl-YAML > 0.5',
            'perl-JSON'
        ],
        os      => 'el6',
        user    => 'apache',
        group   => 'apache',
        sign    => {
            'gpg_name' => 'ED16CAB',
            'passphrase_cmd' => 'cat secret_file'
        },
        after_install => 'foo/bar/baz.sh',
        architecture => 'noarch'
    );

    my $obj = RPM::Packager->new(%args);
    $obj->create_rpm();                           # RPM produced in CWD

=head1 SUBROUTINES/METHODS

lib/RPM/Packager.pm  view on Meta::CPAN


    for my $key ( keys %hash ) {
        my $dst        = $hash{$key};
        my $target_dir = "$tempdir$dst";
        make_path($target_dir);
        system("$self->{cp} -r $cwd/$key/* $target_dir");
    }
    return 1;
}

sub add_gpg_opts {
    my $self = shift;

    return unless ( $self->should_gpgsign() );

    my $gpg_name       = $self->{sign}->{gpg_name};
    my $passphrase_cmd = $self->{sign}->{passphrase_cmd};
    my $opts           = $self->{opts} || [];
    push @{$opts}, '--rpm-sign', '--rpm-rpmbuild-define', "'_gpg_name $gpg_name'";
    $self->{opts}           = $opts;
    $self->{gpg_passphrase} = RPM::Packager::Utils::eval_command($passphrase_cmd);
}

sub add_after_install {
    my $self = shift;
    return unless ( $self->{after_install} );

    my $opts = $self->{opts} || [];
    push @{$opts}, '--after-install', $self->{after_install};
    $self->{opts} = $opts;
}

lib/RPM/Packager.pm  view on Meta::CPAN

    my ( $user, $group ) = $self->generate_user_group();

    my @opts = (
        $self->{fpm}, '-v',          $version,   '--rpm-user', $user,         '--rpm-group',
        $group,       '--iteration', $iteration, '-n',         $self->{name}, $dependency_opts,
        '-s',         'dir',         '-t',       'rpm',        '-a',          $architecture,
        '-C',         $self->{tempdir}
    );

    $self->{opts} = [@opts];
    $self->add_gpg_opts();
    $self->add_after_install();
    push @{ $self->{opts} }, '.';    # relative to the temporary directory
}

sub handle_interactive_prompt {
    my $self = shift;
    my $opts = $self->{opts};
    my $cmd  = join( ' ', @{$opts} );
    my $pass = $self->{gpg_passphrase};

    my $exp = Expect->new();
    $exp->spawn($cmd);
    $exp->expect(
        undef,
        [
            qr/Enter pass phrase:/i => sub {
                my $exp = shift;
                $exp->send("$pass\n");
                exp_continue;
            }
        ]
    );
    return 1;
}

sub should_gpgsign {
    my $self           = shift;
    my $gpg_name       = $self->{sign}->{gpg_name};
    my $passphrase_cmd = $self->{sign}->{passphrase_cmd};
    ( $gpg_name && $passphrase_cmd ) ? 1 : 0;
}

=head2 create_rpm

Creates RPM based on the information in the object

=cut

sub create_rpm {
    my $self = shift;

    $self->copy_to_tempdir();
    $self->populate_opts();

    if ( $self->should_gpgsign() ) {
        $self->handle_interactive_prompt();
    }
    else {
        my $cmd = join( ' ', @{ $self->{opts} } );
        system($cmd);
    }
    return 1;
}

=head1 AUTHOR

t/01_packager.t  view on Meta::CPAN

    my $obj = RPM::Packager->new(%args);
    $obj->populate_opts();

    like(
        join( ' ', @{ $obj->{opts} } ),
        qr|/usr/local/bin/fpm -v 3.2.1 --rpm-user root --rpm-group root --iteration 1.el6 -n testpackage|,
        'options generated successfully'
    );
};

subtest 'add_gpg_opts', sub {
    my $gpg_name = 'E4D20D4C';
    my %args     = ( sign => { gpg_name => $gpg_name, passphrase_cmd => 'echo "foobar"' } );
    my $obj      = RPM::Packager->new(%args);
    $obj->add_gpg_opts();
    is( @{ $obj->{opts} }[2], "'_gpg_name $gpg_name'", 'got gpg name in the object' );
    is( $obj->{gpg_passphrase}, 'foobar', 'got passphrase for gpg' );
};

subtest 'add_after_install', sub {
    my %args = ( after_install => '/some/path/to/script' );
    my $obj = RPM::Packager->new(%args);
    $obj->add_after_install();
    is_deeply( $obj->{opts}, [ '--after-install', '/some/path/to/script' ] );
};

subtest 'handle_interactive_prompt', sub {
    my %args = ( sign => { gpg_name => 'foobar', passphrase_cmd => 'ls -la' } );
    my $obj = RPM::Packager->new(%args);
    $obj->add_gpg_opts();
    local *{'Expect::spawn'}  = sub { return ''; };
    local *{'Expect::expect'} = sub { return ''; };
    is( $obj->handle_interactive_prompt(), 1, "this method isn't really testable" );
};

subtest 'should_gpgsign', sub {
    my $obj = RPM::Packager->new();
    is( $obj->should_gpgsign(), 0, 'should not sign this' );

    my %args = ( sign => { gpg_name => 'foobar', passphrase_cmd => 'ls -la' } );
    $obj = RPM::Packager->new(%args);

    is( $obj->should_gpgsign(), 1, 'should sign this' );
};

subtest 'create_rpm', sub {
    my $obj = RPM::Packager->new();
    local *{'RPM::Packager::create_rpm'} = sub { return 1; };
    my $ret = $obj->create_rpm();
    is( $ret, 1, 'RPM created successfully' );
};

done_testing();



( run in 1.349 second using v1.01-cache-2.11-cpan-df04353d9ac )