21 #include <glibmm/optioncontext.h> 23 #include "config/config.h" 24 #include "config/configerror.h" 25 #include "config/configloader.h" 26 #include "config/fileconfigloader.h" 27 #include "config/commandlineconfigsource.h" 28 #include "config/mainconfigsource.h" 29 #include "config/defaultconfigsource.h" 30 #include "config/configsource.h" 31 #include "config/configitem.h" 32 #include "config/configdefinition.h" 34 #include "dbus/softwarecontaineragentadaptor.h" 38 LOG_DEFINE_APP_IDS(
"SCAG",
"SoftwareContainer agent");
39 LOG_DECLARE_DEFAULT_CONTEXT(PAM_DefaultLogContext,
"MAIN",
"Main context");
48 int signalHandler(
void *data) {
49 log_debug() <<
"Caught signal, exiting!";
50 Glib::RefPtr<Glib::MainLoop> *ml =
static_cast<Glib::RefPtr<Glib::MainLoop> *
>(data);
56 int main(
int argc,
char **argv)
59 Glib::OptionEntry configOpt;
60 configOpt.set_long_name(
"config");
61 configOpt.set_short_name(
'c');
62 configOpt.set_arg_description(
"<path>");
63 configOpt.set_description(
"Path to SoftwareContainer configuration file," 64 " defaults to \"" + std::string(SC_CONFIG_FILE) +
"\"");
66 Glib::OptionEntry userOpt;
67 userOpt.set_long_name(
"user");
68 userOpt.set_short_name(
'u');
69 userOpt.set_arg_description(
"<uid>");
70 userOpt.set_description(
"Default user id to be used when starting processes in the container," 73 Glib::OptionEntry timeoutOpt;
74 timeoutOpt.set_long_name(
"timeout");
75 timeoutOpt.set_short_name(
't');
76 timeoutOpt.set_arg_description(
"<seconds>");
77 timeoutOpt.set_description(
"Timeout in seconds to wait for containers to shutdown," 78 " defaults to " + std::to_string(SC_SHUTDOWN_TIMEOUT));
80 Glib::OptionEntry serviceManifestDirOpt;
81 serviceManifestDirOpt.set_long_name(
"manifest-dir");
82 serviceManifestDirOpt.set_short_name(
'm');
83 serviceManifestDirOpt.set_arg_description(
"<filepath>");
84 serviceManifestDirOpt.set_description(
"Path to a file or directory where service manifest(s) " 85 "exist, defaults to \"" 86 + std::string(SC_SERVICE_MANIFEST_DIR) +
"\"");
88 Glib::OptionEntry defaultServiceManifestDirOpt;
89 defaultServiceManifestDirOpt.set_long_name(
"default-manifest-dir");
90 defaultServiceManifestDirOpt.set_short_name(
'd');
91 defaultServiceManifestDirOpt.set_arg_description(
"<filepath>");
92 defaultServiceManifestDirOpt.set_description(
"Path to a file or directory where default " 93 "service manifest(s) exist, defaults to \"" 94 + std::string(SC_DEFAULT_SERVICE_MANIFEST_DIR) +
"\"");
96 Glib::OptionEntry sessionBusOpt;
97 sessionBusOpt.set_long_name(
"session-bus");
98 sessionBusOpt.set_short_name(
'b');
99 sessionBusOpt.set_description(
"Use the session bus instead of the system bus, " 100 "defaults to " + std::string(SC_USE_SESSION_BUS ?
"true" :
"false"));
106 std::string configPath = ConfigDefinition::SC_CONFIG_PATH_INITIAL_VALUE;
108 int timeout = ConfigDefinition::SHUTDOWN_TIMEOUT_INITIAL_VALUE;
109 std::string serviceManifestDir = ConfigDefinition::SERVICE_MANIFEST_DIR_INITIAL_VALUE;
110 std::string defaultServiceManifestDir = ConfigDefinition::DEFAULT_SERVICE_MANIFEST_DIR_INITIAL_VALUE;
111 bool useSessionBus = ConfigDefinition::USE_SESSION_BUS_INITIAL_VALUE;
113 Glib::OptionGroup mainGroup(
"Options",
"Options for SoftwareContainer");
115 mainGroup.add_entry_filename(configOpt, configPath);
116 mainGroup.add_entry(userOpt, userID);
118 mainGroup.add_entry(timeoutOpt, timeout);
119 mainGroup.add_entry_filename(serviceManifestDirOpt, serviceManifestDir);
120 mainGroup.add_entry_filename(defaultServiceManifestDirOpt, defaultServiceManifestDir);
121 mainGroup.add_entry(sessionBusOpt, useSessionBus);
123 Glib::OptionContext optionContext;
124 optionContext.set_help_enabled(
true);
125 optionContext.set_ignore_unknown_options(
false);
126 optionContext.set_summary(
"SoftwareContainer Agent v" + std::string(PACKAGE_VERSION));
127 optionContext.set_description(
"Documentation is available at http://pelagicore.github.io/softwarecontainer");
128 optionContext.set_main_group(mainGroup);
131 optionContext.parse(argc, argv);
132 }
catch (Glib::OptionError &err) {
133 log_error() << err.what();
137 profilepoint(
"softwareContainerStart");
138 log_debug() <<
"Starting softwarecontainer agent";
143 Glib::RefPtr<Glib::MainContext> mainContext = Glib::MainContext::get_default();
144 Glib::RefPtr<Glib::MainLoop> ml = Glib::MainLoop::create(mainContext);
147 std::string configFileLocation;
148 if (configPath != ConfigDefinition::SC_CONFIG_PATH_INITIAL_VALUE) {
149 configFileLocation = configPath;
151 configFileLocation = std::string(SC_CONFIG_FILE );
157 std::vector<StringConfig> stringConfigs = std::vector<StringConfig>();
158 if (serviceManifestDir != ConfigDefinition::SERVICE_MANIFEST_DIR_INITIAL_VALUE) {
159 if (
'/' != serviceManifestDir.back()) {
160 serviceManifestDir.push_back(
'/');
164 ConfigDefinition::SC_SERVICE_MANIFEST_DIR_KEY,
166 stringConfigs.push_back(config);
168 if (defaultServiceManifestDir != ConfigDefinition::DEFAULT_SERVICE_MANIFEST_DIR_INITIAL_VALUE) {
169 if (
'/' != defaultServiceManifestDir.back()) {
170 defaultServiceManifestDir.push_back(
'/');
174 ConfigDefinition::SC_DEFAULT_SERVICE_MANIFEST_DIR_KEY,
175 defaultServiceManifestDir);
176 stringConfigs.push_back(config);
179 std::vector<IntConfig> intConfigs = std::vector<IntConfig>();
180 if (timeout != ConfigDefinition::SHUTDOWN_TIMEOUT_INITIAL_VALUE) {
181 IntConfig config(ConfigDefinition::SC_GROUP,
182 ConfigDefinition::SC_SHUTDOWN_TIMEOUT_KEY,
184 intConfigs.push_back(config);
187 std::vector<BoolConfig> boolConfigs = std::vector<BoolConfig>();
190 if (useSessionBus ==
true) {
192 ConfigDefinition::SC_USE_SESSION_BUS_KEY,
194 boolConfigs.push_back(config);
204 std::unique_ptr<ConfigLoader> loader(
new FileConfigLoader(configFileLocation));
205 std::unique_ptr<ConfigSource> mainConfigs(
new MainConfigSource(std::move(loader),
206 ConfigDefinition::typeMap()));
211 std::vector<std::unique_ptr<ConfigSource>> configSources;
212 configSources.push_back(std::move(cmdlineConfigs));
213 configSources.push_back(std::move(mainConfigs));
214 configSources.push_back(std::move(defaultConfigs));
216 std::shared_ptr<Config> config = std::make_shared<Config>(std::move(configSources),
217 ConfigDefinition::mandatory(),
218 ConfigDependencies());
219 auto factory = std::make_shared<SoftwareContainerFactory>();
220 auto utility = std::make_shared<ContainerUtilityInterface>(config);
227 useSessionBus = config->getBoolValue(ConfigDefinition::SC_GROUP,
228 ConfigDefinition::SC_USE_SESSION_BUS_KEY);
231 std::unique_ptr<SoftwareContainerAgentAdaptor> adaptor(
238 g_unix_signal_add(SIGINT, &signalHandler, &ml);
239 g_unix_signal_add(SIGTERM, &signalHandler, &ml);
242 log_debug() <<
"Exiting softwarecontainer agent";
245 log_error() << std::string(error.what());
Represents a config item of type bool.
Represents a config item of type string.
Represents the default config values.
A config source representing the commandline options.
Developers guide to adding a config item:
Represents a config item of type int.