Skip to content

Commit

Permalink
5 ➡️ 6
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <[email protected]>
  • Loading branch information
chapulina committed Mar 4, 2021
2 parents dddb1ef + 4bb5bfa commit 042d1c2
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 48 deletions.
1 change: 1 addition & 0 deletions .github/ci/packages.apt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ libcurl4-openssl-dev
libgflags-dev
libignition-cmake2-dev
libignition-common4-dev
libignition-math6-dev
libignition-msgs7-dev
libignition-tools-dev
libjsoncpp-dev
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ ign_find_package(ZIP REQUIRED PRIVATE)
ign_find_package(ignition-common4 REQUIRED PRIVATE)
set(IGN_COMMON_MAJOR_VER ${ignition-common4_VERSION_MAJOR})

#--------------------------------------
# Find ignition-math
ign_find_package(ignition-math6 REQUIRED PRIVATE)
set(IGN_MSGS_MAJOR_VER ${ignition-math6_VERSION_MAJOR})

#--------------------------------------
# Find ignition-msgs
ign_find_package(ignition-msgs7 REQUIRED PRIVATE)
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ Test coverage reports are available at Codecov:

# Building and installing

See the [installation tutorial](https://ignitionrobotics.org/api/fuel_tools/4.0/install.html).
See the [installation tutorial](https://ignitionrobotics.org/api/fuel_tools/5.0/install.html).

Make sure `IGN_CONFIG_PATH` is set to the right install location so that `ign fuel` will work.
Default is `/usr/local/share/ignition`.

## Examples

Expand Down
2 changes: 1 addition & 1 deletion bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pipelines:
libignition-cmake2-dev
libignition-common4-dev
libignition-math6-dev
# libignition-msgs5-dev
libignition-msgs6-dev
# Ignition tools
- git clone http://github.com/ignitionrobotics/ign-tools -b master
- cd ign-tools
Expand Down
4 changes: 4 additions & 0 deletions include/ignition/fuel_tools/ClientConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ namespace ignition
/// \return The list of servers.
public: std::vector<ServerConfig> Servers() const;

/// \brief List of servers the client will connect to.
/// \return The list of servers.
public: std::vector<ServerConfig> & MutableServers() const;

/// \brief Add a server to the list.
/// \param[in] _srv The server config.
public: void AddServer(const ServerConfig &_srv);
Expand Down
7 changes: 7 additions & 0 deletions include/ignition/fuel_tools/FuelClient.hh
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ namespace ignition
public: bool ParseCollectionUrl(const common::URI &_url,
CollectionIdentifier &_id);

/// \brief Checked if there is any header already specify
/// \param[in] _serverConfig Server configuration
/// \param[inout] _headers Vector with headers to check
private: void AddServerConfigParametersToHeaders(
const ignition::fuel_tools::ServerConfig &_serverConfig,
std::vector<std::string> &_headers) const;

/// \brief PIMPL
private: std::unique_ptr<FuelClientPrivate> dataPtr;
};
Expand Down
30 changes: 28 additions & 2 deletions src/ClientConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ bool ClientConfig::LoadConfig(const std::string &_file)
tokens.push("root");
std::string serverURL = "";
std::string cacheLocationConfig = "";
std::string privateToken = "";

do
{
Expand Down Expand Up @@ -325,12 +326,18 @@ bool ClientConfig::LoadConfig(const std::string &_file)
{
// Sanity check: Make sure that the server is not already stored.
bool repeated = false;
for (auto const savedServer : this->Servers())
for (auto & savedServer : this->MutableServers())
{
if (savedServer.Url().Str() == serverURL)
{
if (!privateToken.empty())
{
ignmsg << "Set private token for " << serverURL << " server."
<< std::endl;
savedServer.SetApiKey(privateToken);
}
ignwarn << "URL [" << serverURL << "] already exists. "
<< "Ignoring server" << std::endl;
<< "Ignoring server" << std::endl;
repeated = true;
break;
}
Expand All @@ -340,6 +347,12 @@ bool ClientConfig::LoadConfig(const std::string &_file)
// Add the new server.
ServerConfig newServer;
newServer.SetUrl(common::URI(serverURL));
if (!privateToken.empty())
{
ignmsg << "Set private token for " << serverURL << " server."
<< std::endl;
newServer.SetApiKey(privateToken);
}
this->AddServer(newServer);
}
}
Expand Down Expand Up @@ -372,6 +385,13 @@ bool ClientConfig::LoadConfig(const std::string &_file)
cacheLocationConfig = path;
tokens.pop();
}
else if (!tokens.empty() && tokens.top() == "private-token")
{
std::string token(
reinterpret_cast<const char *>(event.data.scalar.value));
privateToken = token;
tokens.pop();
}
else
{
std::string key(
Expand Down Expand Up @@ -431,6 +451,12 @@ std::vector<ServerConfig> ClientConfig::Servers() const
return this->dataPtr->servers;
}

//////////////////////////////////////////////////
std::vector<ServerConfig> & ClientConfig::MutableServers() const
{
return this->dataPtr->servers;
}

//////////////////////////////////////////////////
void ClientConfig::AddServer(const ServerConfig &_srv)
{
Expand Down
57 changes: 51 additions & 6 deletions src/FuelClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,11 @@ Result FuelClient::ModelDetails(const ModelIdentifier &_id,
common::URIPath path;
path = path / _id.Owner() / "models" / _id.Name();

std::vector<std::string> headersIncludingServerConfig = _headers;
AddServerConfigParametersToHeaders(
_id.Server(), headersIncludingServerConfig);
resp = rest.Request(HttpMethod::GET, serverUrl, version,
path.Str(), {}, _headers, "");
path.Str(), {}, headersIncludingServerConfig, "");
if (resp.statusCode != 200)
return Result(ResultType::FETCH_ERROR);

Expand Down Expand Up @@ -464,9 +467,13 @@ Result FuelClient::UploadModel(const std::string &_pathToModelDir,
if (!this->dataPtr->FillModelForm(_pathToModelDir, _id, _private, form))
return Result(ResultType::UPLOAD_ERROR);

std::vector<std::string> headersIncludingServerConfig = _headers;
AddServerConfigParametersToHeaders(
_id.Server(), headersIncludingServerConfig);
// Send the request.
resp = rest.Request(HttpMethod::POST_FORM, _id.Server().Url().Str(),
_id.Server().Version(), "models", {}, _headers, "", form);
_id.Server().Version(), "models", {},
headersIncludingServerConfig, "", form);

if (resp.statusCode != 200)
{
Expand Down Expand Up @@ -503,6 +510,27 @@ Result FuelClient::DeleteModel(const ModelIdentifier &)
return Result(ResultType::DELETE_ERROR);
}

void FuelClient::AddServerConfigParametersToHeaders(
const ignition::fuel_tools::ServerConfig &_serverConfig,
std::vector<std::string> &_headers) const
{
bool privateTokenDefined = false;
for (auto header : _headers)
{
if (header.find("Private-token:") != std::string::npos)
{
privateTokenDefined = true;
}
}
if (!privateTokenDefined)
{
if (!_serverConfig.ApiKey().empty())
{
_headers.push_back("Private-token: " + _serverConfig.ApiKey());
}
}
}

//////////////////////////////////////////////////
Result FuelClient::DeleteUrl(const ignition::common::URI &_uri,
const std::vector<std::string> &_headers)
Expand All @@ -518,13 +546,16 @@ Result FuelClient::DeleteUrl(const ignition::common::URI &_uri,

ModelIdentifier modelId;
WorldIdentifier worldId;
std::vector<std::string> headersIncludingServerConfig = _headers;
if (this->ParseModelUrl(_uri, modelId))
{
type = "model";
name = modelId.UniqueName();
server = modelId.Server().Url().Str();
version = modelId.Server().Version();
path = path / modelId.Owner() / "models" / modelId.Name();
AddServerConfigParametersToHeaders(
modelId.Server(), headersIncludingServerConfig);
}
else if (this->ParseWorldUrl(_uri, worldId))
{
Expand All @@ -533,6 +564,8 @@ Result FuelClient::DeleteUrl(const ignition::common::URI &_uri,
server = worldId.Server().Url().Str();
version = worldId.Server().Version();
path = path / worldId.Owner() / "worlds" / worldId.Name();
AddServerConfigParametersToHeaders(
worldId.Server(), headersIncludingServerConfig);
}
else
{
Expand All @@ -542,7 +575,7 @@ Result FuelClient::DeleteUrl(const ignition::common::URI &_uri,

// Send the request.
resp = rest.Request(HttpMethod::DELETE, server, version, path.Str(), {},
_headers, "", {});
headersIncludingServerConfig, "", {});

if (resp.statusCode != 200)
{
Expand Down Expand Up @@ -586,11 +619,15 @@ Result FuelClient::DownloadModel(const ModelIdentifier &_id,

ignmsg << "Downloading model [" << _id.UniqueName() << "]" << std::endl;

std::vector<std::string> headersIncludingServerConfig = _headers;
AddServerConfigParametersToHeaders(
_id.Server(), headersIncludingServerConfig);
// Request
ignition::fuel_tools::Rest rest;
RestResponse resp;
resp = rest.Request(HttpMethod::GET, _id.Server().Url().Str(),
_id.Server().Version(), route.Str(), {}, _headers, "");
_id.Server().Version(), route.Str(), {},
headersIncludingServerConfig, "");
if (resp.statusCode != 200)
{
ignerr << "Failed to download model." << std::endl
Expand Down Expand Up @@ -698,11 +735,16 @@ Result FuelClient::DownloadWorld(WorldIdentifier &_id)

ignmsg << "Downloading world [" << _id.UniqueName() << "]" << std::endl;

std::vector<std::string> headersIncludingServerConfig;
AddServerConfigParametersToHeaders(
_id.Server(), headersIncludingServerConfig);

// Request
ignition::fuel_tools::Rest rest;
RestResponse resp;
resp = rest.Request(HttpMethod::GET, _id.Server().Url().Str(),
_id.Server().Version(), route.Str(), {}, {}, "");
_id.Server().Version(), route.Str(), {},
headersIncludingServerConfig, "");
if (resp.statusCode != 200)
{
ignerr << "Failed to download world." << std::endl
Expand Down Expand Up @@ -1300,8 +1342,11 @@ Result FuelClient::PatchModel(
form.emplace("private", _model.Private() ? "1" : "0");
}

std::vector<std::string> headersIncludingServerConfig = _headers;
AddServerConfigParametersToHeaders(
_model.Server(), headersIncludingServerConfig);
resp = rest.Request(HttpMethod::PATCH_FORM, serverUrl, version,
path.Str(), {}, _headers, "", form);
path.Str(), {}, headersIncludingServerConfig, "", form);

if (resp.statusCode != 200)
return Result(ResultType::PATCH_ERROR);
Expand Down
18 changes: 16 additions & 2 deletions src/LocalCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <ignition/common/Filesystem.hh>
#include <ignition/common/StringUtils.hh>
#include <ignition/common/Util.hh>
#include <ignition/math/SemanticVersion.hh>

#include "ignition/fuel_tools/ClientConfig.hh"
#include "ignition/fuel_tools/Helpers.hh"
Expand Down Expand Up @@ -462,11 +463,24 @@ bool LocalCachePrivate::FixPaths(const std::string &_modelVersionedDir,

// Get the <sdf> element with the highest (most recent) version.
tinyxml2::XMLElement *sdfElementLatest = nullptr;
double maxVersion = 0.0;
math::SemanticVersion maxVersion{"0.0"};
tinyxml2::XMLElement *sdfElement = modelElement->FirstChildElement("sdf");
while (sdfElement)
{
double version = std::stod(sdfElement->Attribute("version"));
math::SemanticVersion version;

auto versionAttribute = sdfElement->Attribute("version");
if (nullptr == versionAttribute)
{
version.Parse("0.0.1");
ignwarn << "<sdf> element missing version attribute, assuming version ["
<< version << "]" << std::endl;
}
else
{
version.Parse(versionAttribute);
}

if (version > maxVersion)
{
maxVersion = version;
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/cmdfuel.rb.in
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ COMMANDS = { 'fuel' =>
" -v [ --verbose ] [arg] Adjust the level of console output (0~4). \n"\
" The default verbosity is 1, use -v without \n"\
" arguments for level 3. \n" +
COMMON_OPTIONS
COMMON_OPTIONS + "\n\n" +
"Environment variables: \n"\
" IGN_FUEL_CACHE_PATH Path to the cache where resources are \n"\
" downloaded to. Defaults to $HOME/.ignition/fuel \n"
}

SUBCOMMANDS = {
Expand Down
Loading

0 comments on commit 042d1c2

Please sign in to comment.