Skip to content

Commit

Permalink
Merge pull request #686 from bareos/dev/franku/master/setdevice-autos…
Browse files Browse the repository at this point in the history
…elect

Storage daemon DeviceResource: Set autoselect using a ui command
  • Loading branch information
franku committed Dec 15, 2020
2 parents 3a54c18 + 9105010 commit 1620c8f
Show file tree
Hide file tree
Showing 34 changed files with 540 additions and 82 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:

### Added

- Add console command setdevice [PR #686]
- Add support for Fedora 33 [PR #643]
- Add development tools for changelog-handling [PR #617]
- Added documentation and background information for the new python 3 plugin support [PR #599]
Expand Down
2 changes: 1 addition & 1 deletion core/src/console/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ static void ReadAndProcessInput(FILE* input, BareosSocket* UA_sock)
break; /* error or term */
} else if (status == BNET_SIGNAL) {
if (UA_sock->message_length == BNET_SUB_PROMPT) { at_prompt = true; }
Dmsg1(100, "Got poll %s\n", BnetSigToAscii(UA_sock));
Dmsg1(100, "Got poll %s\n", BnetSignalToString(UA_sock).c_str());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/dird/dird_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "lib/tls_conf.h"

class dlist;
class json_t;
struct json_t;

namespace directordaemon {

Expand Down
3 changes: 3 additions & 0 deletions core/src/dird/ua.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,8 @@ class RunContext {
~RunContext();
};

void FreeUaContext(UaContext* ua);
UaContext* new_ua_context(JobControlRecord* jcr);

} /* namespace directordaemon */
#endif /* BAREOS_DIRD_UA_H_ */
119 changes: 119 additions & 0 deletions core/src/dird/ua_cmds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "dird.h"
#include "dird/dird_globals.h"
#include "dird/backup.h"
#include "dird/ua_cmds.h"
#include "dird/ua_cmdstruct.h"
#include "dird/expand.h"
#include "dird/fd_cmds.h"
Expand All @@ -50,6 +51,7 @@
#include "dird/ua_run.h"
#include "include/auth_protocol_types.h"
#include "lib/bnet.h"
#include "lib/bool_string.h"
#include "lib/edit.h"
#include "lib/parse_conf.h"
#include "lib/util.h"
Expand Down Expand Up @@ -485,6 +487,9 @@ static struct ua_cmdstruct commands[] = {
NT_("level=<nn> trace=0/1 timestamp=0/1 client=<client-name> | dir | "
"storage=<storage-name> | all"),
true, true},
{NT_("setdevice"), SetDeviceCommand::Cmd, _("Sets device parameter"),
NT_("storage=<storage-name> device=<device-name> autoselect=<bool>"), true,
true},
{NT_("setip"), SetipCmd, _("Sets new client address -- if authorized"),
NT_(""), false, true},
{NT_("show"), show_cmd, _("Show resource records"),
Expand Down Expand Up @@ -1590,6 +1595,120 @@ static bool SetdebugCmd(UaContext* ua, const char* cmd)
return true;
}

SetDeviceCommand::ArgumentsList SetDeviceCommand::ScanCommandLine(UaContext* ua)
{
ArgumentsList arguments{{"storage", ""}, {"device", ""}, {"autoselect", ""}};

for (int i = 1; i < ua->argc; i++) {
try {
auto& value = arguments.at(ua->argk[i]);
int idx = FindArgWithValue(ua, NT_(ua->argk[i]));
if (idx >= 0) { value = ua->argv[idx]; }
} catch (std::out_of_range& e) {
ua->ErrorMsg("Wrong argument: %s\n", ua->argk[i]);
return ArgumentsList();
}
}

bool argument_missing = false;
for (const auto& arg : arguments) {
if (arg.second.empty()) { // value
ua->ErrorMsg("Argument missing: %s\n", arg.first.c_str());
argument_missing = true;
}
}
if (argument_missing) { return ArgumentsList(); }

try {
BoolString s{arguments["autoselect"].data()}; // throws
arguments["autoselect"].clear();
arguments["autoselect"] = s.get<bool>() == true ? "1" : "0";
} catch (const std::out_of_range& e) {
ua->ErrorMsg("Wrong argument: %s\n", arguments["autoselect"].c_str());
return ArgumentsList();
}

return arguments;
}

bool SetDeviceCommand::SendToSd(UaContext* ua,
StorageResource* store,
const ArgumentsList& arguments)
{
switch (store->Protocol) {
case APT_NDMPV2:
case APT_NDMPV3:
case APT_NDMPV4:
return true;
default:
break;
}

UnifiedStorageResource lstore;
lstore.store = store;
PmStrcpy(lstore.store_source, _("unknown source"));
SetWstorage(ua->jcr, &lstore);

/*
* Try connecting for up to 15 seconds
*/
ua->SendMsg(_("Connecting to Storage daemon %s at %s:%d\n"),
store->resource_name_, store->address, store->SDport);

if (!ConnectToStorageDaemon(ua->jcr, 1, 15, false)) {
ua->ErrorMsg(_("Failed to connect to Storage daemon.\n"));
return false;
}

Dmsg0(120, _("Connected to storage daemon\n"));
ua->jcr->store_bsock->fsend("setdevice device=%s autoselect=%d",
arguments.at("device").c_str(),
std::stoi(arguments.at("autoselect")));
if (ua->jcr->store_bsock->recv() >= 0) {
ua->SendMsg("%s", ua->jcr->store_bsock->msg);
}

ua->jcr->store_bsock->signal(BNET_TERMINATE);
ua->jcr->store_bsock->close();
delete ua->jcr->store_bsock;
ua->jcr->store_bsock = nullptr;

return true;
}


/**
* setdevice storage=<storage-name> device=<device-name> autoselect=<bool>
*/
bool SetDeviceCommand::Cmd(UaContext* ua, const char* cmd)
{
auto arguments = ScanCommandLine(ua);

if (arguments.empty()) {
ua->SendCmdUsage("");
return false;
}

if (ua->AclHasRestrictions(Storage_ACL)) {
if (!ua->AclAccessOk(Storage_ACL, arguments["storage"].c_str())) {
std::string err{"Access to storage "};
err += "\"" + arguments["storage"] + "\" forbidden\n";
ua->ErrorMsg(err.c_str());
return false;
}
}

StorageResource* sd = ua->GetStoreResWithName(arguments["storage"].c_str());

if (sd == nullptr) {
ua->ErrorMsg(_("Storage \"%s\" not found.\n"),
arguments["storage"].c_str());
return false;
}

return SendToSd(ua, sd, arguments);
}

/**
* Resolve a hostname.
*/
Expand Down
11 changes: 10 additions & 1 deletion core/src/dird/ua_cmds.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2019 Bareos GmbH & Co. KG
Copyright (C) 2018-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -30,6 +30,15 @@ class UaContext;
bool Do_a_command(UaContext* ua);
bool DotMessagesCmd(UaContext* ua, const char* cmd);

struct SetDeviceCommand {
using ArgumentsList = std::map<std::string, std::string>;
static ArgumentsList ScanCommandLine(UaContext* ua);
static bool Cmd(UaContext* ua, const char* cmd);
static bool SendToSd(UaContext* ua,
StorageResource* store,
const ArgumentsList& arguments);
};

void SetDoClientSetdebugFunction(std::function<void(UaContext* ua,
ClientResource* client,
int level,
Expand Down
7 changes: 6 additions & 1 deletion core/src/dird/ua_select.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2019 Bareos GmbH & Co. KG
Copyright (C) 2018-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -24,6 +24,11 @@

#include "dird/ua.h"
class alist;
struct PoolDbRecord;
struct MediaDbRecord;
struct StorageDbRecord;
struct ClientDbRecord;
struct JobDbRecord;

namespace directordaemon {

Expand Down
7 changes: 4 additions & 3 deletions core/src/dird/ua_server.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2018 Bareos GmbH & Co. KG
Copyright (C) 2018-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -22,12 +22,13 @@
#ifndef BAREOS_DIRD_UA_SERVER_H_
#define BAREOS_DIRD_UA_SERVER_H_

class BareosSocket;
class JobControlRecord;

namespace directordaemon {

void* HandleUserAgentClientRequest(BareosSocket* user);
UaContext* new_ua_context(JobControlRecord* jcr);
JobControlRecord* new_control_jcr(const char* base_name, int job_type);
void FreeUaContext(UaContext* ua);

} /* namespace directordaemon */
#endif // BAREOS_DIRD_UA_SERVER_H_
2 changes: 1 addition & 1 deletion core/src/lib/bareos_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class PoolMem;
class ConfigurationParser;
class OutputFormatterResource;
class ResourceItem;
struct ResourceItem;

#define MAX_RES_ITEMS 95 /* maximum resource items per BareosResource */

Expand Down
16 changes: 8 additions & 8 deletions core/src/lib/bnet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2019 Bareos GmbH & Co. KG
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -571,25 +571,25 @@ std::map<int, std::pair<std::string, std::string>> bnet_signal_to_text {
};
/* clang-format on */

const char* BnetSigToAscii(BareosSocket* bs)
{
return BnetSignalToString(bs->message_length).c_str();
}

std::string BnetSignalToString(int signal)
{
if (bnet_signal_to_text.find(signal) != bnet_signal_to_text.end()) {
return bnet_signal_to_text[signal].first;
}
return std::string("Unknown sig ") + std::to_string(signal);
return "Unknown sig " + std::to_string(signal);
}

std::string BnetSignalToString(const BareosSocket* bs)
{
return BnetSignalToString(bs->message_length);
}

std::string BnetSignalToDescription(int signal)
{
if (bnet_signal_to_text.find(signal) != bnet_signal_to_text.end()) {
return bnet_signal_to_text[signal].second;
}
return std::string("Unknown sig ") + std::to_string(signal);
return "Unknown sig " + std::to_string(signal);
}

bool ReadoutCommandIdFromMessage(const BStringList& list_of_arguments,
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/bnet.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2019 Bareos GmbH & Co. KG
Copyright (C) 2018-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -38,7 +38,7 @@ bool BnetTlsClient(BareosSocket* bsock,
int BnetGetPeer(BareosSocket* bs, char* buf, socklen_t buflen);
BareosSocket* dup_bsock(BareosSocket* bsock);
const char* BnetStrerror(BareosSocket* bsock);
const char* BnetSigToAscii(BareosSocket* bsock);
std::string BnetSignalToString(const BareosSocket*);
std::string BnetSignalToString(int signal);
std::string BnetSignalToDescription(int signal);
int BnetWaitData(BareosSocket* bsock, int sec);
Expand Down
57 changes: 57 additions & 0 deletions core/src/lib/bool_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2020-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/

#ifndef BAREOS_LIB_BOOL_STRING_H_
#define BAREOS_LIB_BOOL_STRING_H_

#include <set>
#include <stdexcept>
#include <string>

class BoolString {
public:
explicit BoolString(const std::string& s) : str_value(s)
{
if (true_values.find(str_value) == true_values.end()
&& false_values.find(str_value) == false_values.end()) {
std::string err = {"Wrong parameter: "};
throw std::out_of_range(err + str_value);
}
}
template <typename T = std::string>
T get() const
{
return str_value;
}

private:
std::string str_value;
const std::set<std::string> true_values{"true", "yes", "1"};
const std::set<std::string> false_values{"false", "no", "0"};
};

template <>
inline bool BoolString::get<bool>() const
{
return true_values.find(str_value) != true_values.end();
}

#endif // BAREOS_LIB_BOOL_STRING_H_
Loading

0 comments on commit 1620c8f

Please sign in to comment.