Added tokens to salt grain info.

This commit is contained in:
Matthias Koefferlein 2017-10-09 00:11:55 +02:00
parent 4f9857ace9
commit 6c952921ea
6 changed files with 76 additions and 22 deletions

View File

@ -21,6 +21,7 @@
*/
#include "laySaltDownloadManager.h"
#include "laySaltManagerDialog.h"
#include "laySalt.h"
#include "tlFileUtils.h"
#include "tlWebDAV.h"
@ -119,9 +120,9 @@ SaltDownloadManager::SaltDownloadManager ()
}
void
SaltDownloadManager::register_download (const std::string &name, const std::string &url, const std::string &version)
SaltDownloadManager::register_download (const std::string &name, const std::string &token, const std::string &url, const std::string &version)
{
m_registry.push_back (Descriptor (name, url, version));
m_registry.push_back (Descriptor (name, token, url, version));
}
void
@ -194,7 +195,7 @@ SaltDownloadManager::compute_list (const lay::Salt &salt, const lay::Salt &salt_
if (tl::verbosity() >= 20) {
tl::log << "Considering for update as dependency: " << d->name << " (" << d->version << ") with URL " << d->url;
}
m_registry.push_back (Descriptor (d->name, d->url, d->version));
m_registry.push_back (Descriptor (d->name, std::string (), d->url, d->version));
} else {
if (tl::verbosity() >= 20) {
@ -207,7 +208,7 @@ SaltDownloadManager::compute_list (const lay::Salt &salt, const lay::Salt &salt_
if (tl::verbosity() >= 20) {
tl::log << "Considering for download as dependency: " << d->name << " (" << d->version << ") with URL " << d->url;
}
m_registry.push_back (Descriptor (d->name, d->url, d->version));
m_registry.push_back (Descriptor (d->name, std::string (), d->url, d->version));
}
@ -259,22 +260,24 @@ SaltDownloadManager::fetch_missing (const lay::Salt &salt, const lay::Salt &salt
++progress;
// If no URL is given, utilize the salt mine to fetch it
if (p->url.empty ()) {
tl_assert (! p->name.empty ());
// Add URL and token from the package index
if (! p->name.empty ()) {
const lay::SaltGrain *g = salt_mine.grain_by_name (p->name);
if (! g) {
throw tl::Exception (tl::to_string (QObject::tr ("Package '%1' not found in index - cannot resolve download URL").arg (tl::to_qstring (p->name))));
if (p->url.empty ()) {
throw tl::Exception (tl::to_string (QObject::tr ("Package '%1' not found in index - cannot resolve download URL").arg (tl::to_qstring (p->name))));
}
} else {
if (p->url.empty ()) {
if (tl::verbosity() >= 20) {
tl::log << "Resolved package URL for package " << p->name << ": " << g->url ();
}
p->url = g->url ();
}
p->token = g->token ();
}
if (tl::verbosity() >= 20) {
tl::log << "Resolved package URL for package " << p->name << ": " << g->url ();
}
p->url = g->url ();
}
try {
@ -351,7 +354,7 @@ SaltDownloadManager::make_confirmation_dialog (QWidget *parent, const lay::Salt
}
bool
SaltDownloadManager::execute (QWidget *parent, lay::Salt &salt)
SaltDownloadManager::execute (lay::SaltManagerDialog *parent, lay::Salt &salt)
{
bool result = true;
@ -388,13 +391,28 @@ SaltDownloadManager::execute (QWidget *parent, lay::Salt &salt)
target.set_path (g->path ());
}
int status = 1;
if (! salt.create_grain (p->grain, target)) {
dialog->mark_error (p->name);
result = false;
status = 0;
} else {
dialog->mark_success (p->name);
}
try {
// try to give feedback about successful installations
if (! p->token.empty ()) {
std::string fb_url = parent->salt_mine_url () + "?token=" + p->token + "&status=" + tl::to_string (status);
if (fb_url.find ("http:") == 0 || fb_url.find ("https:") == 0) {
tl::InputStream fb (fb_url);
fb.read_all ();
}
}
} catch (tl::Exception &ex) {
tl::error << ex.msg ();
}
dialog->separator ();
}

View File

@ -38,6 +38,7 @@ namespace lay
{
class Salt;
class SaltManagerDialog;
class ConfirmationDialog
: public QDialog, private Ui::SaltManagerInstallConfirmationDialog
@ -99,7 +100,7 @@ public:
*
* The target directory can be empty. In this case, the downloader will pick an approriate one.
*/
void register_download (const std::string &name, const std::string &url, const std::string &version);
void register_download (const std::string &name, const std::string &token, const std::string &url, const std::string &version);
/**
* @brief Computes the dependencies after all required packages have been registered
@ -126,18 +127,18 @@ public:
* if this dialog is confirmed. It will return false if the dialog was cancelled and an exception
* if something goes wrong.
*
* If parent is null, no confirmation dialog will be shown and installation happens in non-GUI
* If dialog is null, no confirmation dialog will be shown and installation happens in non-GUI
* mode.
*
* The return value will be true if the packages were installed successfully.
*/
bool execute (QWidget *parent, lay::Salt &salt);
bool execute (lay::SaltManagerDialog *dialog, lay::Salt &salt);
private:
struct Descriptor
{
Descriptor (const std::string &_name, const std::string &_url, const std::string &_version)
: name (_name), url (_url), version (_version), downloaded (false)
Descriptor (const std::string &_name, const std::string &_token, const std::string &_url, const std::string &_version)
: name (_name), token (_token), url (_url), version (_version), downloaded (false)
{ }
bool operator< (const Descriptor &other) const
@ -159,6 +160,7 @@ private:
}
std::string name;
std::string token;
std::string url;
std::string version;
bool downloaded;

View File

@ -72,6 +72,12 @@ SaltGrain::set_name (const std::string &n)
m_name = n;
}
void
SaltGrain::set_token (const std::string &t)
{
m_token = t;
}
void
SaltGrain::set_version (const std::string &v)
{
@ -367,6 +373,7 @@ SaltGrain::xml_elements ()
if (! sp_xml_elements) {
sp_xml_elements = new tl::XMLElementList (
tl::make_member (&SaltGrain::name, &SaltGrain::set_name, "name") +
tl::make_member (&SaltGrain::token, &SaltGrain::set_token, "token") +
tl::make_member (&SaltGrain::version, &SaltGrain::set_version, "version") +
tl::make_member (&SaltGrain::api_version, &SaltGrain::set_api_version, "api-version") +
tl::make_member (&SaltGrain::title, &SaltGrain::set_title, "title") +

View File

@ -99,6 +99,24 @@ public:
*/
void set_name (const std::string &p);
/**
* @brief Gets the token of the grain
*
* The grain's token is a unique identifier by which external systems can
* identify the package - for example for package voting.
* A token is not part of the package's identity. It's intended as a
* package identification safe against forgery.
*/
const std::string &token () const
{
return m_token;
}
/**
* @brief Sets the token of the grain
*/
void set_token (const std::string &t);
/**
* @brief Gets the title of the grain
*
@ -427,6 +445,7 @@ public:
private:
std::string m_name;
std::string m_token;
std::string m_version;
std::string m_api_version;
std::string m_path;

View File

@ -513,7 +513,7 @@ BEGIN_PROTECTED
SaltGrain *g = model->grain_from_index (index);
// NOTE: checking for valid_name prevents bad entries inside the download list
if (g && model->is_marked (g->name ()) && SaltGrain::valid_name (g->name ())) {
manager.register_download (g->name (), g->url (), g->version ());
manager.register_download (g->name (), g->token (), g->url (), g->version ());
any = true;
}
}

View File

@ -49,6 +49,14 @@ public:
*/
SaltManagerDialog (QWidget *parent, lay::Salt *salt, const std::string &salt_mine_url);
/**
* @brief Gets the URL for the package index
*/
const std::string &salt_mine_url () const
{
return m_salt_mine_url;
}
private slots:
/**
* @brief Called when the list of packages (grains) is about to change