1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::System::Console::Command::Maint::SupportBundle::Generate;
10
11use strict;
12use warnings;
13
14use parent qw(Kernel::System::Console::BaseCommand);
15
16our @ObjectDependencies = (
17    'Kernel::Config',
18    'Kernel::System::Main',
19    'Kernel::System::SupportBundleGenerator',
20);
21
22sub Configure {
23    my ( $Self, %Param ) = @_;
24
25    $Self->Description('Generate a support bundle for this system.');
26    $Self->AddOption(
27        Name        => 'target-directory',
28        Description => "Specify a custom output directory.",
29        Required    => 0,
30        HasValue    => 1,
31        ValueRegex  => qr/.*/smx,
32    );
33
34    return;
35}
36
37sub PreRun {
38    my ( $Self, %Param ) = @_;
39
40    my $TargetDirectory = $Self->GetOption('target-directory');
41    if ( $TargetDirectory && !-d $TargetDirectory ) {
42        die "Directory $TargetDirectory does not exist.\n";
43    }
44
45    return;
46}
47
48sub Run {
49    my ( $Self, %Param ) = @_;
50
51    $Self->Print("<yellow>Generating support bundle...</yellow>\n");
52
53    my $Response = $Kernel::OM->Get('Kernel::System::SupportBundleGenerator')->Generate();
54
55    if ( !$Response->{Success} ) {
56        $Self->PrintError("Could not generate support bundle.");
57        return $Self->ExitCodeError();
58    }
59
60    my $FileData = $Response->{Data};
61
62    my $OutputDir = $Self->GetOption('target-directory') || $Kernel::OM->Get('Kernel::Config')->Get('Home');
63
64    my $FileLocation = $Kernel::OM->Get('Kernel::System::Main')->FileWrite(
65        Location   => $OutputDir . '/' . $FileData->{Filename},
66        Content    => $FileData->{Filecontent},
67        Mode       => 'binmode',
68        Permission => '644',
69    );
70
71    if ( !$FileLocation ) {
72        $Self->PrintError("Support bundle could not be saved.");
73        return $Self->ExitCodeError();
74    }
75
76    $Self->Print("<green>Support Bundle saved to:</green> <yellow>$FileLocation</yellow>\n");
77
78    return $Self->ExitCodeOk();
79}
80
81# sub PostRun {
82#     my ( $Self, %Param ) = @_;
83#
84#     # This will be called after Run() (even in case of exceptions). Perform any cleanups here.
85#
86#     return;
87# }
88
891;
90