softwarecontainer  0.18.0-739e8d7 2017-05-04
configdefinition.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 
21 #include "configdefinition.h"
22 
23 
36 namespace softwarecontainer {
37 
38 typedef std::vector<std::tuple<std::string, std::string, ConfigType, MandatoryFlag>> ConfigItems;
39 
40 const MandatoryFlag Mandatory = true;
41 const MandatoryFlag Optional = false;
42 
43 const int ITEM_INDEX_GROUP = 0;
44 const int ITEM_INDEX_KEY = 1;
45 const int ITEM_INDEX_TYPE = 2;
46 const int ITEM_INDEX_MANDATORY = 3;
47 
48 // Illegal values used as initial command line option values to check if user
49 // set them or not
50 const std::string ConfigDefinition::SC_CONFIG_PATH_INITIAL_VALUE = "";
51 const int ConfigDefinition::SHUTDOWN_TIMEOUT_INITIAL_VALUE = -2;
52 const std::string ConfigDefinition::SERVICE_MANIFEST_DIR_INITIAL_VALUE = "";
53 const std::string ConfigDefinition::DEFAULT_SERVICE_MANIFEST_DIR_INITIAL_VALUE = "";
54 const bool ConfigDefinition::USE_SESSION_BUS_INITIAL_VALUE = false;
55 
56 // Config group "SoftwareContainer"
57 const std::string ConfigDefinition::SC_GROUP = "SoftwareContainer";
58 
59 // Config keys for SoftwareContainer group
60 const std::string ConfigDefinition::SC_USE_SESSION_BUS_KEY = "use-session-bus";
61 const std::string ConfigDefinition::SC_SHUTDOWN_TIMEOUT_KEY = "shutdown-timeout";
62 const std::string ConfigDefinition::SC_SHARED_MOUNTS_DIR_KEY = "shared-mounts-dir";
63 const std::string ConfigDefinition::SC_LXC_CONFIG_PATH_KEY = "deprecated-lxc-config-path";
64 const std::string ConfigDefinition::SC_SERVICE_MANIFEST_DIR_KEY = "service-manifest-dir";
65 const std::string ConfigDefinition::SC_DEFAULT_SERVICE_MANIFEST_DIR_KEY = "default-service-manifest-dir";
66 
67 #ifdef ENABLE_NETWORKGATEWAY
68 const std::string ConfigDefinition::SC_CREATE_BRIDGE_KEY = "create-bridge";
69 const std::string ConfigDefinition::SC_BRIDGE_DEVICE_KEY = "bridge-device";
70 const std::string ConfigDefinition::SC_BRIDGE_IP_KEY = "bridge-ip";
71 const std::string ConfigDefinition::SC_BRIDGE_NETADDR_KEY = "bridge-netaddr";
72 const std::string ConfigDefinition::SC_BRIDGE_NETMASK_KEY = "bridge-netmask";
73 const std::string ConfigDefinition::SC_BRIDGE_NETMASK_BITLENGTH_KEY = "bridge-netmask-bitlength";
74 #endif
75 
76 /*
77  * Used to create a mapping between group-key pairs and a type, as well as defining what configs
78  * are mandatory.
79  *
80  * Configs that might be mandatory, i.e. that fact is decided externally by CMake, need to be added
81  * with the definition and not the 'Optional' or 'Mandatory' flags directly.
82  */
83 const ConfigItems CONFIGS
84 {
85 #ifdef ENABLE_NETWORKGATEWAY
86  std::make_tuple(ConfigDefinition::SC_GROUP,
87  ConfigDefinition::SC_CREATE_BRIDGE_KEY,
88  ConfigType::Boolean,
89  ConfigDefinition::convertDefineToFlag(
90  SC_CREATE_BRIDGE_MANDATORY_FLAG /* set by cmake */)),
91  std::make_tuple(ConfigDefinition::SC_GROUP,
92  ConfigDefinition::SC_BRIDGE_DEVICE_KEY,
93  ConfigType::String,
94  ConfigDefinition::convertDefineToFlag(
95  SC_BRIDGE_DEVICE_MANDATORY_FLAG /* set by cmake */)),
96  std::make_tuple(ConfigDefinition::SC_GROUP,
97  ConfigDefinition::SC_BRIDGE_IP_KEY,
98  ConfigType::String,
99  ConfigDefinition::convertDefineToFlag(
100  SC_BRIDGE_IP_MANDATORY_FLAG /* set by cmake */)),
101  std::make_tuple(ConfigDefinition::SC_GROUP,
102  ConfigDefinition::SC_BRIDGE_NETMASK_BITLENGTH_KEY,
103  ConfigType::Integer,
104  ConfigDefinition::convertDefineToFlag(
105  SC_BRIDGE_NETMASK_BITLENGTH_MANDATORY_FLAG /* set by cmake */)),
106  std::make_tuple(ConfigDefinition::SC_GROUP,
107  ConfigDefinition::SC_BRIDGE_NETMASK_KEY,
108  ConfigType::String,
109  ConfigDefinition::convertDefineToFlag(
110  SC_BRIDGE_NETMASK_MANDATORY_FLAG /* set by cmake */)),
111  std::make_tuple(ConfigDefinition::SC_GROUP,
112  ConfigDefinition::SC_BRIDGE_NETADDR_KEY,
113  ConfigType::String,
114  ConfigDefinition::convertDefineToFlag(
115  SC_BRIDGE_NETADDR_MANDATORY_FLAG /* set by cmake */)),
116 #endif // ENABLE_NETWORKGATEWAY
117  std::make_tuple(ConfigDefinition::SC_GROUP,
118  ConfigDefinition::SC_USE_SESSION_BUS_KEY,
119  ConfigType::Boolean,
120  Optional),
121  std::make_tuple(ConfigDefinition::SC_GROUP,
122  ConfigDefinition::SC_SHUTDOWN_TIMEOUT_KEY,
123  ConfigType::Integer,
124  Optional),
125  std::make_tuple(ConfigDefinition::SC_GROUP,
126  ConfigDefinition::SC_SHARED_MOUNTS_DIR_KEY,
127  ConfigType::String,
128  Optional),
129  std::make_tuple(ConfigDefinition::SC_GROUP,
130  ConfigDefinition::SC_LXC_CONFIG_PATH_KEY,
131  ConfigType::String,
132  Optional),
133  std::make_tuple(ConfigDefinition::SC_GROUP,
134  ConfigDefinition::SC_SERVICE_MANIFEST_DIR_KEY,
135  ConfigType::String,
136  Optional),
137  std::make_tuple(ConfigDefinition::SC_GROUP,
138  ConfigDefinition::SC_DEFAULT_SERVICE_MANIFEST_DIR_KEY,
139  ConfigType::String,
140  Optional)
141 };
142 
143 MandatoryFlag ConfigDefinition::convertDefineToFlag(bool defined)
144 {
145  return defined ? Mandatory : Optional;
146 }
147 
148 MandatoryConfigs ConfigDefinition::mandatory()
149 {
150  MandatoryConfigs mandatoryConfigs = MandatoryConfigs();
151 
152  for (auto config : CONFIGS) {
153  std::string group = std::get<ITEM_INDEX_GROUP>(config);
154  std::string key = std::get<ITEM_INDEX_KEY>(config);
155  MandatoryFlag mandatoryFlag = std::get<ITEM_INDEX_MANDATORY>(config);
156 
157  if (Mandatory == mandatoryFlag) {
158  mandatoryConfigs.push_back(UniqueKey(group, key));
159  }
160  }
161 
162  return mandatoryConfigs;
163 }
164 
165 TypeMap ConfigDefinition::typeMap()
166 {
167  TypeMap typeMap = TypeMap();
168 
169  for (auto config : CONFIGS) {
170  std::string group = std::get<ITEM_INDEX_GROUP>(config);
171  std::string key = std::get<ITEM_INDEX_KEY>(config);
172  ConfigType configType = std::get<ITEM_INDEX_TYPE>(config);
173 
174  typeMap[UniqueKey(group, key)] = configType;
175  }
176 
177  return typeMap;
178 }
179 
180 } // namespace softwarecontainer
ConfigType
Represents the type of a config value.
Definition: configtypes.h:32
Developers guide to adding a config item: