Chapter 2. Basic API

Table of Contents

2.1. EBox::Global
2.2. Exceptions
2.3. Data validation
2.4. i18n
2.4.1. i18n developers
2.4.2. i18n for translators

2.1. EBox::Global

The EBox::Global class offers various module management functions. The most commonly used functions are those regarding module instantiation. EBox::Global works as a module factory, you get an instance of it using the getInstance static method and then you can use that instance to create modules. The factory comes in two flavors, a read-only flavor and a read-write one.

Calling getInstance without arguments will yield a read-write factory, which creates modules that let you make calls which change the configuration. A very important detail of read-write modules is that they return their latest configuration information, even if it has not been saved (which means that it could be revoked later by the user). This idea is important, configuration info reported by a read-write module should not be treated as final, unless that module has no changes waiting to be saved. You can see if a module has unsaved changes by calling the modIsChanged method in the EBox::Global class.

The idea behind the read-write modules behavior is to make it easy to build a configuration front-end. For example, if the user creates a new network object in the objects module, the new object will show up instantly in the firewall configuration, so he can create new firewall rules that use it. After all the desired changes have been made, the user saves the configuration. If he decides to cancel the changes he just made, both the new object and the firewall rules will be deleted.

There is one situation when you don't want to get information that has not been saved yet. That's when you are generating the configuration file for a daemon, setting up firewall rules, setting the address for a network interface, or any other activity that needs the real, final configuration info. This situation happens in system scripts (the boot script, cron jobs, etc). In these cases you need what we call read-only modules, they only report saved information and they do not allow method calls that change the configuration of the module. To obtain read-only modules you create EBox::Global instance setting its readonly parameter to true. Module instances returned by a factory created in this way will be read-only ones. This script shows how to get an instance of the squid module and tell it to restart itself:

Example 2.1. Creating a read-only module instance

#!/usr/bin/perl

use strict;
use warnings;

use EBox;
use EBox::Global;
use Error qw(:try);

EBox::init();

my $global = EBox::Global->getInstance(1);
my $squid = $global->modInstance('squid');

try {
	$squid->restartService();
} catch EBox::Exceptions::Base with {
	print STDERR "Squid module failed to restart.\n";
};

There are two functions that make it easy to perform two common tasks: getting an instance of every module and getting and instance of every module that implements some abstract class. These are modInstances and modInstancesOfType. Using them is straight forward:

Example 2.2. Functions for instantiating more than one module

my $global = EBox::Global->getInstance(1);

# restart all modules
foreach my $mod (@{$global->modInstances()}) {
	$mod->restartService();
}

# restart only modules that implement the NetworkObserver class
foreach my $mod (@{$global->modInstancesOfType('EBox::NetworkObserver')}) {
	$mod->restartService();
}

Restarting all the modules is even easier than that, EBox::Global provides the restartAllModules just for that. It's one of the methods that perform an operation on all the installed modules:

restartAllModules

Calls the restartService method on all the modules. This has the effect of restarting all the services handled by eBox. Config files are regenerated every time a service is restarted.

stopAllModules

Calls the stopService method on all the modules, this includes the web based administration interface, so be careful.

revokeAllModules

Cancels the configuration changes that have been made in all the modules since the last time the configuration was saved.

saveAllModules

Saves the config changes in all modules.