A little bit more work on configuration management

master
Vladimir Stackov 2015-01-16 18:18:06 +03:00
parent bd422b2f23
commit ab2568d38c
6 changed files with 131 additions and 100 deletions

115
config.cc
View File

@ -1,42 +1,84 @@
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "zbackup_base.hh"
#include "zbackup.pb.h"
#include "config.hh"
#include "debug.hh"
#include "config.hh"
/* Keyword tokens. */
bool ZConfig::parseOption( Config & config, const char * option )
typedef enum
{
oBadOption,
oChunk_max_size,
oBundle_max_payload_size,
oBundle_compression_method,
oDeprecated, oUnsupported
} OpCodes;
/* Textual representations of the tokens. */
static struct
{
const char * name;
OpCodes opcode;
const char * description;
} keywords[] = {
{
"chunk.max_size",
oChunk_max_size,
"Maximum chunk size used when storing chunks\n"
"Directly affects deduplication ratio"
},
{
"bundle.max_payload_size",
oBundle_max_payload_size,
"Maximum number of bytes a bundle can hold. Only real chunk bytes are\n"
"counted, not metadata. Any bundle should be able to contain at least\n"
"one arbitrary single chunk, so this should not be smaller than\n"
"chunk.max_size" },
{
"bundle.compression_method",
oBundle_compression_method,
"Compression method for new bundles"
},
{
"compression",
oBundle_compression_method,
"Shortcut for bundle.compression_method"
},
{ NULL, oBadOption }
};
bool Config::parseOption( const char * option )
{
dPrintf( "Parsing option \"%s\"...\n", option );
return false;
return true;
}
void ZConfig::showHelp()
void Config::showHelp()
{
fprintf( stderr,
"Available options overview:\n"
" help - show this message\n"
"Available options overview:\n\n"
"== help ==\n"
"shows this message\n"
"");
u_int i;
for ( i = 0; keywords[ i ].name; i++ )
{
fprintf( stderr, "\n== %s ==\n%s\n", keywords[ i ].name, keywords[ i ].description );
}
}
ZConfig::ZConfig( string const & storageDir, string const & password ):
ZBackupBase( storageDir, password, true )
{
}
ZConfig::ZConfig( string const & storageDir, string const & password, Config & configIn ):
ZBackupBase( storageDir, password, true )
{
}
bool ZConfig::parse( const string & str, google::protobuf::Message * mutable_message )
bool Config::parse( const string & str, google::protobuf::Message * mutable_message )
{
return google::protobuf::TextFormat::ParseFromString( str, mutable_message );
}
string ZConfig::toString( google::protobuf::Message const & message )
string Config::toString( google::protobuf::Message const & message )
{
std::string str;
google::protobuf::TextFormat::PrintToString( message, &str );
@ -44,39 +86,8 @@ string ZConfig::toString( google::protobuf::Message const & message )
return str;
}
void ZConfig::show()
{
printf( "%s", toString( extendedStorageInfo.config() ).c_str() );
}
bool ZConfig::validate( const string & configData, const string & newConfigData )
bool Config::validate( const string & configData, const string & newConfigData )
{
ConfigInfo newConfig;
return parse( newConfigData, &newConfig );
}
bool ZConfig::editInteractively()
{
string configData( toString( extendedStorageInfo.config() ) );
string newConfigData( configData );
if ( !spawnEditor( newConfigData, &validate ) )
return false;
ConfigInfo newConfig;
if ( !parse( newConfigData, &newConfig ) )
return false;
if ( toString( extendedStorageInfo.config() ) == toString( newConfig ) )
{
verbosePrintf( "No changes made to config\n" );
return false;
}
verbosePrintf( "Updating configuration...\n" );
extendedStorageInfo.mutable_config()->CopyFrom( newConfig );
verbosePrintf(
"Configuration successfully updated!\n"
"Updated configuration:\n\n%s", toString( extendedStorageInfo.config() ).c_str() );
return true;
}

View File

@ -5,51 +5,30 @@
#define CONFIG_HH_INCLUDED__
#include <string>
#include <vector>
#include <google/protobuf/text_format.h>
#include "zbackup.pb.h"
using std::string;
using std::vector;
/* This class is intentended only for use with storable options */
class ZConfig: public ZBackupBase
class Config
{
public:
typedef struct
{
uint32_t max_size;
} ChunkConfig;
typedef struct {
uint32_t max_payload_size;
string default_compression_method;
} BundleConfig;
typedef struct {
ChunkConfig chunkConfig;
BundleConfig bundleConfig;
} Config;
// Print current configuration to screen
void show();
// Edit current configuration
// returns true if configuration is changed
bool editInteractively();
typedef vector< string > Options;
// Validator for user-supplied configuration
static bool validate( const string &, const string & );
static bool parse( const string & str, google::protobuf::Message * mutable_message );
static bool parseOption( Config & config, const char * option );
static void showHelp();
ZConfig( string const & storageDir, string const & password );
ZConfig( string const & storageDir, string const & password, Config & );
private:
string toString( google::protobuf::Message const & message );
bool parseOption( const char * option );
Config config;
string toString( google::protobuf::Message const & message );
private:
Options options;
};
#endif

View File

@ -340,7 +340,7 @@ int main( int argc, char *argv[] )
vector< char const * > args;
vector< string > passwords;
bitset< BackupExchanger::Flags > exchange;
ZConfig::Config config;
Config config;
for( int x = 1; x < argc; ++x )
{
@ -487,12 +487,12 @@ int main( int argc, char *argv[] )
{
if ( strcmp( option, "help" ) == 0 )
{
ZConfig::showHelp();
Config::showHelp();
return EXIT_SUCCESS;
}
else
{
if ( !ZConfig::parseOption( config, option ) )
if ( !config.parseOption( option ) )
goto invalid_option;
}
}
@ -527,11 +527,10 @@ invalid_option:
" --cache-size <number> MB (default is %zu)\n"
" --exchange <backups|bundles|index> (can be\n"
" specified multiple times)\n"
" --compression <compression> <lzma|lzo> (default is lzma)\n"
" --help|-h show this message\n"
" -o <Option[=Value]> (overrides repository configuration,\n"
" for detailed options overview\n"
" try to run with -o help)\n"
" can be specified multiple times,\n"
" for detailed options overview run with -o help)\n"
" Commands:\n"
" init <storage path> - initializes new storage\n"
" backup <backup file name> - performs a backup from stdin\n"
@ -542,7 +541,7 @@ invalid_option:
" performs import from source to destination storage\n"
" gc <storage path> - performs chunk garbage collection\n"
" passwd <storage path> - changes repo info file passphrase\n"
" info <storage path> - shows information about storage\n"
" info <storage path> - shows repo information\n"
" config [show|edit|set] <storage path> - performs configuration\n"
" manipulations (default is show)\n"
" For export/import storage path must be valid (initialized) storage\n"
@ -696,6 +695,8 @@ invalid_option:
}
// TODO: implementation in ZBackupBase
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ 1 ], true ),
passwords[ 0 ], true );
fprintf( stderr, "NOT IMPLEMENTED YET!\n" );
return EXIT_FAILURE;
}
@ -721,23 +722,23 @@ invalid_option:
if ( args.size() > 2 && strcmp( args[ fieldAct ], "edit" ) == 0 )
{
ZConfig zc( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStor ], true ),
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStor ], true ),
passwords[ 0 ] );
if ( zc.editInteractively() )
zc.saveExtendedStorageInfo();
if ( zbb.editConfigInteractively() )
zbb.saveExtendedStorageInfo();
}
else
if ( args.size() > 2 && strcmp( args[ fieldAct ], "set" ) == 0 )
{
ZConfig zc( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStor ], true ),
passwords[ 0 ], config );
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStor ], true ),
passwords[ 0 ] );
// -o ... like sysctl -w
}
else
{
ZConfig zc( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStor ], true ),
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStor ], true ),
passwords[ 0 ] );
zc.show();
zbb.showConfig();
}
}
else

View File

@ -55,8 +55,8 @@ message BundleConfigInfo
// one arbitrary single chunk, so this should not be smaller than
// chunk_max_size
required uint32 max_payload_size = 2 [default = 0x200000];
// Default compression for new bundles
optional string default_compression_method = 3 [default = "lzma"];
// Compression method for new bundles
optional string compression_method = 3 [default = "lzma"];
}
message ConfigInfo

View File

@ -117,8 +117,8 @@ void ZBackupBase::initStorage( string const & storageDir,
extendedStorageInfo.config().chunk().max_size() );
extendedStorageInfo.mutable_config()->mutable_bundle()->set_max_payload_size(
extendedStorageInfo.config().bundle().max_payload_size() );
extendedStorageInfo.mutable_config()->mutable_bundle()->set_default_compression_method(
extendedStorageInfo.config().bundle().default_compression_method() );
extendedStorageInfo.mutable_config()->mutable_bundle()->set_compression_method(
extendedStorageInfo.config().bundle().compression_method() );
EncryptionKey encryptionkey = EncryptionKey::noKey();
@ -360,3 +360,34 @@ end:
return isChanged;
}
void ZBackupBase::showConfig()
{
printf( "%s", config.toString( extendedStorageInfo.config() ).c_str() );
}
bool ZBackupBase::editConfigInteractively()
{
string configData( config.toString( extendedStorageInfo.config() ) );
string newConfigData( configData );
if ( !spawnEditor( newConfigData, &config.validate ) )
return false;
ConfigInfo newConfig;
if ( !config.parse( newConfigData, &newConfig ) )
return false;
if ( config.toString( extendedStorageInfo.config() ) == config.toString( newConfig ) )
{
verbosePrintf( "No changes made to config\n" );
return false;
}
verbosePrintf( "Updating configuration...\n" );
extendedStorageInfo.mutable_config()->CopyFrom( newConfig );
verbosePrintf(
"Configuration successfully updated!\n"
"Updated configuration:\n%s", config.toString( extendedStorageInfo.config() ).c_str() );
return true;
}

View File

@ -9,6 +9,7 @@
#include "ex.hh"
#include "chunk_index.hh"
#include "config.hh"
struct Paths
{
@ -62,11 +63,19 @@ public:
bool spawnEditor( std::string & data, bool( * validator )
( string const &, string const & ) );
// Print current configuration to screen
void showConfig();
// Edit current configuration
// returns true if configuration is changed
bool editConfigInteractively();
StorageInfo storageInfo;
EncryptionKey encryptionkey;
ExtendedStorageInfo extendedStorageInfo;
TmpMgr tmpMgr;
ChunkIndex chunkIndex;
Config config;
private:
StorageInfo loadStorageInfo();