softwarecontainer  0.18.0-739e8d7 2017-05-04
containeroptionparser.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 #include "containeroptions/containeroptionparser.h"
21 
22 #include "jsonparser.h"
23 
24 namespace softwarecontainer {
25 
26 
27 ContainerOptionParser::ContainerOptionParser() :
28  m_options(new DynamicContainerOptions())
29 {
30 }
31 
32 void ContainerOptionParser::readConfigElement(const json_t *element)
33 {
34  if (!json_is_object(element)) {
35  std::string errorMessage("Configure entry is not an object");
36  log_error() << errorMessage;
37  throw ContainerOptionParseError(errorMessage);
38  }
39 
40  bool writeBufferEnabled = false;
41  if(!JSONParser::read(element, "writeBufferEnabled", writeBufferEnabled)) {
42  std::string errorMessage("Could not parse config due to: 'writeBufferEnabled' not found.");
43  log_error() << errorMessage;
44  throw ContainerOptionParseError(errorMessage);
45  }
46 
47  m_options->setWriteBufferEnabled(writeBufferEnabled);
48 
49  if (writeBufferEnabled == true) {
50  bool enableTemporaryFilesystemWriteBuffer = false;
51  if(!JSONParser::read(element,
52  "temporaryFileSystemWriteBufferEnabled",
53  enableTemporaryFilesystemWriteBuffer))
54  {
55  log_warn() << "Could not parse config due to: 'enableTemporaryFilesystemWriteBuffer' not found.";
56  }
57  m_options->setTemporaryFileSystemWriteBufferEnabled(enableTemporaryFilesystemWriteBuffer);
58 
59  if (enableTemporaryFilesystemWriteBuffer) {
60  int temporaryFileSystemSize = DEFAULT_TEMPORARY_FILESYSTEM_SIZE;
61  if(!JSONParser::read(element,
62  "temporaryFileSystemSize",
63  temporaryFileSystemSize))
64  {
65  log_error() << "Could not parse config due to: 'temporaryFileSystemSize' not found.";
66  }
67  m_options->setTemporaryFileSystemSize(temporaryFileSystemSize);
68  }
69  }
70 
71 }
72 
73 std::unique_ptr<DynamicContainerOptions> ContainerOptionParser::parse(const std::string &config)
74 {
75  if (config.size() == 0) {
76  std::string errorMessage("Empty JSON config strings are not supported.");
77  log_error() << errorMessage;
78  throw ContainerOptionParseError(errorMessage);
79  }
80 
81  json_error_t error;
82  json_t *root = json_loads(config.c_str(), 0, &error);
83 
84  if (!root) {
85  std::string errorMessage("Could not parse config: "
86  + std::string(error.text)
87  + config);
88  log_error() << errorMessage;
89  throw ContainerOptionParseError(errorMessage);
90  }
91 
92  if (!json_is_array(root)) {
93  std::string errorMessage("Root JSON element is not an array");
94  log_error() << errorMessage;
95  json_decref(root);
96  throw ContainerOptionParseError(errorMessage);
97  }
98 
99  size_t index;
100  json_t *element;
101 
102  try {
103  json_array_foreach(root, index, element) {
104  readConfigElement(element);
105  }
106  } catch (ContainerOptionParseError &err) {
107  json_decref(root);
108  throw;
109  }
110  json_decref(root);
111 
112  std::unique_ptr<DynamicContainerOptions> ret = std::move(m_options);
113  m_options.reset(new DynamicContainerOptions());
114  return ret;
115 }
116 
117 } // namespace softwarecontainer
std::unique_ptr< DynamicContainerOptions > parse(const std::string &config)
Parse config needed for starting up the container in a correct manner.
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51