Cli: Add 'repository commit --clear' clearing pending changes

refs #7455
This commit is contained in:
Michael Friedrich 2014-10-27 17:55:58 +01:00
parent 4554b70a97
commit ad99d022ed
3 changed files with 59 additions and 28 deletions

View File

@ -55,7 +55,8 @@ void RepositoryCommitCommand::InitParameters(boost::program_options::options_des
boost::program_options::options_description& hiddenDesc) const
{
visibleDesc.add_options()
("simulate", "Simulate to-be-committed changes");
("simulate", "Simulate to-be-committed changes")
("clear", "Clear all to-be-committed changes");
}
ImpersonationLevel RepositoryCommitCommand::GetImpersonationLevel(void) const
@ -76,6 +77,9 @@ int RepositoryCommitCommand::Run(const boost::program_options::variables_map& vm
std::cout << "Simulation not yet implemented (#)\n";
//TODO
return 1;
} else if (vm.count("clear")) {
std::cout << "Clearing all remaining changes\n";
RepositoryUtility::ClearChangeLog();
} else {
RepositoryUtility::PrintChangeLog(std::cout);
std::cout << "\n";

View File

@ -24,6 +24,7 @@
#include "base/convert.hpp"
#include "base/json.hpp"
#include "base/netstring.hpp"
#include "base/tlsutility.hpp"
#include "base/stdiostream.hpp"
#include "base/debug.hpp"
#include "base/objectlock.hpp"
@ -157,10 +158,10 @@ void RepositoryUtility::PrintChangeLog(std::ostream& fp)
}
/* modify objects and write changelog */
bool RepositoryUtility::AddObject(const String& name, const String& type, const Dictionary::Ptr& attr)
bool RepositoryUtility::AddObject(const String& name, const String& type, const Dictionary::Ptr& attrs)
{
/* add a new changelog entry by timestamp */
String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast<long>(Utility::GetTime())) + ".change";
String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast<long>(Utility::GetTime())) + "-" + SHA256(name) + ".change";
Dictionary::Ptr change = make_shared<Dictionary>();
@ -168,15 +169,15 @@ bool RepositoryUtility::AddObject(const String& name, const String& type, const
change->Set("name", name);
change->Set("type", type);
change->Set("command", "add");
change->Set("attr", attr);
change->Set("attrs", attrs);
return WriteObjectToRepositoryChangeLog(path, change);
}
bool RepositoryUtility::RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attr)
bool RepositoryUtility::RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attrs)
{
/* add a new changelog entry by timestamp */
String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast<long>(Utility::GetTime())) + ".change";
String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast<long>(Utility::GetTime())) + "-" + SHA256(name) + ".change";
Dictionary::Ptr change = make_shared<Dictionary>();
@ -184,7 +185,7 @@ bool RepositoryUtility::RemoveObject(const String& name, const String& type, con
change->Set("name", name);
change->Set("type", type);
change->Set("command", "remove");
change->Set("attr", attr); //required for service->host_name
change->Set("attrs", attrs); //required for service->host_name
return WriteObjectToRepositoryChangeLog(path, change);
}
@ -195,6 +196,13 @@ bool RepositoryUtility::SetObjectAttribute(const String& name, const String& typ
return true;
}
bool RepositoryUtility::ClearChangeLog(void)
{
GetChangeLog(boost::bind(RepositoryUtility::ClearChange, _1, _2));
return true;
}
/* commit changelog */
bool RepositoryUtility::CommitChangeLog(void)
{
@ -246,16 +254,16 @@ Dictionary::Ptr RepositoryUtility::GetObjectFromRepositoryChangeLog(const String
}
/* internal implementation when changes are committed */
bool RepositoryUtility::AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr)
bool RepositoryUtility::AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attrs)
{
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
String path = GetRepositoryObjectConfigPath(type, attrs) + "/" + name + ".conf";
return WriteObjectToRepository(path, name, type, attr);
return WriteObjectToRepository(path, name, type, attrs);
}
bool RepositoryUtility::RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr)
bool RepositoryUtility::RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attrs)
{
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
String path = GetRepositoryObjectConfigPath(type, attrs) + "/" + name + ".conf";
return RemoveObjectFileInternal(path);
}
@ -276,10 +284,10 @@ bool RepositoryUtility::RemoveObjectFileInternal(const String& path)
return true;
}
bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const String& type, const String& key, const Value& val, const Dictionary::Ptr& attr)
bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const String& type, const String& key, const Value& val, const Dictionary::Ptr& attrs)
{
//Fixme
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
String path = GetRepositoryObjectConfigPath(type, attrs) + "/" + name + ".conf";
Dictionary::Ptr obj = GetObjectFromRepository(path); //TODO
@ -305,7 +313,8 @@ bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const Str
bool RepositoryUtility::WriteObjectToRepository(const String& path, const String& name, const String& type, const Dictionary::Ptr& item)
{
Log(LogInformation, "cli", "Dumping config items to file '" + path + "'");
Log(LogInformation, "cli")
<< "Dumping config object '" << name << "' to file '" << path << "'";
Utility::MkDirP(Utility::DirName(path), 0755);
@ -390,7 +399,9 @@ void RepositoryUtility::CollectChangeLog(const String& change_file, std::vector<
String file = Utility::BaseName(change_file);
boost::algorithm::replace_all(file, ".change", "");
Log(LogDebug, "cli", "Adding change file: " + file);
Log(LogDebug, "cli")
<< "Adding change file: '" << file << "'.";
changelog.push_back(file);
}
@ -400,7 +411,7 @@ void RepositoryUtility::CollectChange(const Dictionary::Ptr& change, Array::Ptr&
}
/*
* Commit Changelog
* Commit Changelog entry
*/
void RepositoryUtility::CommitChange(const Dictionary::Ptr& change, const String& path)
{
@ -410,19 +421,19 @@ void RepositoryUtility::CommitChange(const Dictionary::Ptr& change, const String
String name = change->Get("name");
String type = change->Get("type");
String command = change->Get("command");
Dictionary::Ptr attr;
Dictionary::Ptr attrs;
if (change->Contains("attr")) {
attr = change->Get("attr");
if (change->Contains("attrs")) {
attrs = change->Get("attrs");
}
bool success = false;
if (command == "add") {
success = AddObjectInternal(name, type, attr);
success = AddObjectInternal(name, type, attrs);
}
else if (command == "remove") {
success = RemoveObjectInternal(name, type, attr);
success = RemoveObjectInternal(name, type, attrs);
}
if (success) {
@ -432,6 +443,20 @@ void RepositoryUtility::CommitChange(const Dictionary::Ptr& change, const String
}
}
/*
* Clear Changelog entry
*/
void RepositoryUtility::ClearChange(const Dictionary::Ptr& change, const String& path)
{
Log(LogDebug, "cli")
<< "Clearing change " << change->Get("name");
Log(LogInformation, "cli")
<< "Removing changelog file '" << path << "'.";
RemoveObjectFileInternal(path);
}
/*
* Print Changelog helpers
*/
@ -447,7 +472,7 @@ void RepositoryUtility::FormatChangelogEntry(std::ostream& fp, const Dictionary:
String type = change->Get("type");
boost::algorithm::to_lower(type);
Dictionary::Ptr attrs = change->Get("attr");
Dictionary::Ptr attrs = change->Get("attrs");
fp << " " << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << type << ConsoleColorTag(Console_Normal) << " '";
fp << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << change->Get("name") << ConsoleColorTag(Console_Normal) << "'";

View File

@ -50,12 +50,13 @@ public:
static void PrintChangeLog(std::ostream& fp);
static bool AddObject(const String& name, const String& type, const Dictionary::Ptr& attr);
static bool RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attr);
static bool AddObject(const String& name, const String& type, const Dictionary::Ptr& attrs);
static bool RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attrs);
static bool SetObjectAttribute(const String& name, const String& type, const String& attr, const Value& val);
static bool CommitChangeLog(void);
static bool ClearChangeLog(void);
static std::vector<String> GetObjects(void);
private:
@ -63,10 +64,10 @@ private:
static bool RemoveObjectFileInternal(const String& path);
static bool AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr);
static bool RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr);
static bool AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attrs);
static bool RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attrs);
static bool SetObjectAttributeInternal(const String& name, const String& type, const String& key,
const Value& val, const Dictionary::Ptr& attr);
const Value& val, const Dictionary::Ptr& attrs);
/* repository.d */
static void CollectObjects(const String& object_file, std::vector<String>& objects);
@ -81,6 +82,7 @@ private:
static bool GetChangeLog(const boost::function<void (const Dictionary::Ptr&, const String&)>& callback);
static void CollectChange(const Dictionary::Ptr& change, Array::Ptr& changes);
static void CommitChange(const Dictionary::Ptr& change, const String& path);
static void ClearChange(const Dictionary::Ptr& change, const String& path);
static void FormatChangelogEntry(std::ostream& fp, const Dictionary::Ptr& change);