dodo  0.0.1
A C++ library to create containerized Linux services
application.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file application.cpp
20  * Implements the dodo::common::Application class.
21  */
22 
23 #include <common/application.hpp>
24 #include <common/logger.hpp>
25 #include <dodo.hpp>
26 
27 #include <csignal>
28 
29 namespace dodo::common {
30 
31  Application* Application::application_ = nullptr;
32 
34  application_ = this;
35  has_stop_request_ = false;
39  Config* config = Config::initialize( param.config );
40  logger_ = Logger::initialize( *config );
41  logger_->log( Logger::LogLevel::Info, Puts() << config->getAppName() << " started (" << getHostTypeAsString(hosttype_) << ")" );
42  }
43 
45  if ( Logger::logger_ ) delete Logger::logger_;
46  if ( Config::config_ ) delete Config::config_;
48  }
49 
51  switch ( ht ) {
52  case HostType::BareMetal: return "bare metal";
53  case HostType::Docker: return "Docker container";
54  case HostType::VirtualBox: return "VirtualBox";
55  case HostType::VMWare: return "VMWare";
56  case HostType::KVM: return "KVM";
57  case HostType::GenericVM: return "Generic VM";
58  default: return "unknown";
59  }
60  }
61 
62  void Application::signal_handler( int signal ) {
63  application_->onSignal( signal );
64  }
65 
67  std::signal( SIGINT, signal_handler );
68  std::signal( SIGQUIT, signal_handler );
69  std::signal( SIGTERM, signal_handler );
70  }
71 
72  void Application::onSignal( int signal ) {
73  log_Warning( "caught signal " << signal );
74  switch ( signal ) {
75  case SIGINT:
76  case SIGQUIT:
77  case SIGTERM:
78  has_stop_request_ = true;
79  break;
80  default: return;
81  }
82  }
83 
84 
86  const std::string initcgroup = "/proc/1/cgroup";
87  if ( common::fileReadAccess( initcgroup ) ) {
88  auto res = fileReadStrings( initcgroup, std::regex("^0::/docker$") );
89  if ( res.size() == 1 ) return HostType::Docker;
90  }
91 
92  const std::string product_name = "/sys/class/dmi/id/product_name";
93  if ( common::fileReadAccess( product_name ) ) {
94  auto res = fileReadStrings( product_name, std::regex("^VirtualBox$") );
95  if ( res.size() == 1 ) return HostType::VirtualBox;
96 
97  res = fileReadStrings( product_name, std::regex("^VMware Virtual Platform$") );
98  if ( res.size() == 1 ) return HostType::VMWare;
99 
100  res = fileReadStrings( product_name, std::regex("^KVM$") );
101  if ( res.size() == 1 ) return HostType::KVM;
102  }
103 
104  const std::string cpuinfo = "/proc/cpuinfo";
105  if ( common::fileReadAccess( cpuinfo ) ) {
106  auto res = fileReadStrings( cpuinfo, std::regex("^flags.*:.*hypervisor") );
107  if ( res.size() > 0 ) return HostType::GenericVM;
108  }
109 
110  return HostType::BareMetal;
111  }
112 
113 }
dodo::common::Application::HostType::VirtualBox
@ VirtualBox
VirtualBox.
dodo::common::Application::StartParameters
Start parameters for the Application.
Definition: application.hpp:79
dodo::common::Config::getAppName
std::string getAppName() const
Return the application name.
Definition: config.hpp:174
dodo::common::Config::config_
static Config * config_
The singleton pointer.
Definition: config.hpp:238
dodo::common::Application::detectHostType
HostType detectHostType()
Detect the HostType of the host the Application is running on.
Definition: application.cpp:85
dodo::common::Application::hosttype_
HostType hosttype_
The HostType, detected once in the Application constructor.
Definition: application.hpp:165
dodo::common::Logger::log
void log(LogLevel level, const std::string &message)
Log a log entry.
Definition: logger.cpp:88
dodo::common::Application::signal_handler
static void signal_handler(int signal)
Signal handler called by the OS.
Definition: application.cpp:62
dodo::common::Logger::logger_
static Logger * logger_
The singleton.
Definition: logger.hpp:228
dodo::common::Application::HostType::VMWare
@ VMWare
VMWare.
dodo::common::Application::application_
static Application * application_
Singleton Application object.
Definition: application.hpp:155
dodo::common::Application::HostType::GenericVM
@ GenericVM
An indeterminate virtual machine.
dodo::common::Application::HostType::Docker
@ Docker
Docker container.
dodo::common::Application::onSignal
void onSignal(int signal)
Called by signal_handler.
Definition: application.cpp:72
dodo::common::Application::logger_
Logger * logger_
The Logger.
Definition: application.hpp:175
dodo.hpp
Includes all dodo headers.
dodo::common::Application::HostType::BareMetal
@ BareMetal
Bare metal deployment.
dodo::common::Application::getHostTypeAsString
static std::string getHostTypeAsString(HostType ht)
Convert a HostType to a human readable string.
Definition: application.cpp:50
dodo::common::Config
Singleton interface to a (read-only) deployment configuration, combining data from the deployment con...
Definition: config.hpp:52
dodo::common::Logger::LogLevel::Info
@ Info
The program signaled an informational message.
dodo::common::Application::~Application
virtual ~Application()
Destructor.
Definition: application.cpp:44
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::Logger::initialize
static Logger * initialize(const Config &config)
Initialize the Logger singleton.
Definition: logger.cpp:77
application.hpp
dodo::common::Puts
Helper class to write strings in stream format, eg.
Definition: puts.hpp:46
dodo::initLibrary
void initLibrary()
Initialize the dodo library.
Definition: dodo.hpp:41
dodo::common::Application::has_stop_request_
bool has_stop_request_
True when the Application main pid got a signal to stop (SIGTERM).
Definition: application.hpp:170
logger.hpp
dodo::common::fileReadStrings
std::vector< std::string > fileReadStrings(const std::string &filename)
Read the file as vector of strings.
Definition: util.cpp:170
dodo::closeLibrary
void closeLibrary()
Close the dodo library.
Definition: dodo.hpp:51
dodo::common::Application::Application
Application(const StartParameters &param)
Construct an Application instance.
Definition: application.cpp:33
dodo::common::fileReadAccess
bool fileReadAccess(const std::string &path)
Return true when the file exists and the calling user has read access.
Definition: util.cpp:220
dodo::common::Config::initialize
static Config * initialize(const std::string path)
Initialize the singleton.
Definition: config.cpp:62
log_Warning
#define log_Warning(what)
Macro to log Warning.
Definition: logger.hpp:283
dodo::common::Application::HostType::KVM
@ KVM
RedHat KVM.
dodo::common::Application::installSignalHandlers
void installSignalHandlers()
Install the signal handlers.
Definition: application.cpp:66
dodo::common::Application::StartParameters::config
std::string config
Configuration file.
Definition: application.hpp:80
dodo::common::Application::HostType
HostType
The type of host the Application is running on.
Definition: application.hpp:67