Permalink
Browse files

Made Sneaky Pete work with RDL as far as prepare call and most user ops.

* Ripped out nova::db::api stuff, which mimicked Nova API in Diablo.
* Added nova::guest::HeartBeat class which works with RDLs slimmed down db.
* Added "guest_id" property to FlagValues class.
* Removed guest_ethernet_device and preset_instance_id props from FlagValues.
* Fixed MySqlAppStatus to work with RDL. Mostly changed db names, and made it handle the "NEW" status.
* Added == operator to IsoDateTime (needed it for a HeartBeat unit test...).
* Changed nova::rpc::Receiver to send ack in the new nicer way.
* Log out Sneaky Pete intro flag to program start up to make it easier to see in the logs.
  • Loading branch information...
1 parent aabe019 commit 59658fe43a8134a6d89f29b5e88f0d72f0a157f9 @TimSimpsonR TimSimpsonR committed May 25, 2012
View
@@ -258,8 +258,8 @@ unit u_nova_guest_utils
: u_nova_guest_GuestException
;
-unit u_nova_db_api
- : src/nova/db/api.cc
+unit u_nova_guest_HeartBeat
+ : src/nova/guest/HeartBeat.cc
: u_nova_db_mysql
u_nova_guest_utils
;
@@ -408,7 +408,7 @@ unit u_nova_guest_diagnostics_InterrogatorMessageHandler
;
alias guest_lib
- : u_nova_db_api
+ : u_nova_guest_HeartBeat
u_nova_configfile
u_nova_flags
u_nova_guest_apt_apt
@@ -491,18 +491,17 @@ unit-test mysql_nova_updater_tests
: <define>BOOST_TEST_DYN_LINK
<testing.launcher>"BOOST_TEST_CATCH_SYSTEM_ERRORS=no valgrind --leak-check=full"
;
-explicit api_tests ;
-unit-test api_tests
+unit-test HeartBeat_tests
: u_nova_flags
- u_nova_db_api
+ u_nova_guest_HeartBeat
test_dependencies
- tests/nova/db/api_tests.cc
+ tests/nova/guest/HeartBeat_tests.cc
: <define>BOOST_TEST_DYN_LINK
<testing.launcher>"BOOST_TEST_CATCH_SYSTEM_ERRORS=no valgrind --leak-check=full"
;
-explicit api_tests ;
+explicit HeartBeat_tests ;
# Requires that mysql be utterly destroyed and reinstalled on the machine.
View
@@ -1,2 +1,2 @@
-mkdir /vz/private/$1/agent
-sudo cp -rf * /vz/private/$1/agent/
+mkdir /var/lib/vz/private/$1/agent
+sudo cp -rf * /var/lib/vz/private/$1/agent/
View
@@ -1,128 +0,0 @@
-#include "nova/db/api.h"
-#include "nova/flags.h"
-#include "nova/Log.h"
-#include "nova/db/mysql.h"
-#include <sstream>
-#include <string.h>
-#include "nova/guest/utils.h"
-
-using nova::flags::FlagValues;
-using namespace nova::db::mysql;
-using boost::optional;
-using nova::guest::utils::IsoDateTime;
-using std::string;
-using std::stringstream;
-
-namespace nova { namespace db {
-
-
-class ApiMySql : public Api {
-
-private:
- MySqlConnectionWithDefaultDbPtr con;
-
-public:
- ApiMySql(MySqlConnectionWithDefaultDbPtr con)
- : con(con)
- {
- }
-
- virtual ~ApiMySql() {}
-
- void ensure() {
- con->ensure();
- }
-
- virtual ServicePtr service_create(const NewService & new_service) {
- ServicePtr service = service_get_by_args(new_service);
- if (!!service) {
- return service;
- }
- ensure();
- MySqlPreparedStatementPtr stmt = con->prepare_statement(
- "INSERT INTO services "
- "(created_at, updated_at, deleted, report_count, disabled, "
- " availability_zone, services.binary, host, topic) "
- "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?);");
- IsoDateTime now;
- stmt->set_string(0, now.c_str());
- stmt->set_string(1, now.c_str());
- stmt->set_int(2, 0);
- stmt->set_int(3, 0);
- stmt->set_int(4, 0);
- stmt->set_string(5, new_service.availability_zone.c_str());
- stmt->set_string(6, new_service.binary.c_str());
- stmt->set_string(7, new_service.host.c_str());
- stmt->set_string(8, new_service.topic.c_str());
- stmt->execute();
- return service_get_by_args(new_service);
- }
-
- virtual ServicePtr service_get_by_args(const NewService & search) {
- ensure();
- //TODO(tim.simpson): Get the preparedstatement result set to work with
- // int return values.
- stringstream query;
- query << "SELECT disabled, id, report_count "
- "FROM services WHERE services.binary= \""
- << con->escape_string(search.binary.c_str())
- << "\" AND host= \"" << con->escape_string(search.host.c_str())
- << "\" AND services.topic = \""
- << con->escape_string(search.topic.c_str())
- << "\" AND availability_zone = \""
- << con->escape_string(search.availability_zone.c_str()) << "\"";
- MySqlResultSetPtr results = con->query(query.str().c_str());
- if (!results->next()) {
- return ServicePtr();
- } else {
- ServicePtr service(new Service());
- service->availability_zone = search.availability_zone;
- service->binary = search.binary;
- optional<int> d_int = results->get_int(0);
- if (d_int) {
- service->disabled = (d_int.get() != 0);
- } else {
- service->disabled = boost::none;
- }
- service->host = search.host;
- service->id = results->get_int_non_null(1);
- service->report_count = results->get_int_non_null(2);
- service->topic = search.topic;
- return service;
- }
- }
-
- virtual void service_update(Service & service) {
- ensure();
- stringstream query;
- query << "UPDATE services "
- "SET updated_at = ? , report_count = ? ";
- if (service.disabled) {
- query << ", disabled = ? ";
- }
- query << "WHERE services.binary= ? AND host= ? "
- "AND services.topic = ? AND availability_zone = ?";
- MySqlPreparedStatementPtr stmt = con->prepare_statement(
- query.str().c_str());
- IsoDateTime now;
- int index = 0;
- stmt->set_string(index ++, now.c_str());
- stmt->set_int(index ++, service.report_count);
- if (service.disabled) {
- stmt->set_int(index ++, service.disabled.get() ? 1 : 0);
- }
- stmt->set_string(index ++, service.binary.c_str());
- stmt->set_string(index ++, service.host.c_str());
- stmt->set_string(index ++, service.topic.c_str());
- stmt->set_string(index ++, service.availability_zone.c_str());
- stmt->execute();
- }
-
-};
-
-ApiPtr create_api(MySqlConnectionWithDefaultDbPtr con) {
- ApiPtr ptr(new ApiMySql(con));
- return ptr;
-}
-
-} } // end nova::db
View
@@ -1,50 +0,0 @@
-#ifndef __NOVA_DB_API
-#define __NOVA_DB_API
-
-#include "nova/flags.h"
-#include "nova/db/mysql.h"
-#include <boost/optional.hpp>
-#include <boost/smart_ptr.hpp>
-#include <string>
-
-
-namespace nova { namespace db {
-
-struct NewService {
- std::string availability_zone;
- std::string binary;
- std::string host;
- std::string topic;
-};
-
-struct Service : public NewService {
- int id;
- boost::optional<bool> disabled;
- int report_count;
-};
-
-typedef boost::shared_ptr<Service> ServicePtr;
-
-
-class Api {
-
-public:
- virtual ~Api() {}
-
- virtual ServicePtr service_create(const NewService & new_service) = 0;
-
- virtual ServicePtr service_get_by_args(const NewService & new_service) = 0;
-
- virtual void service_update(Service & service) = 0;
-
-};
-
-typedef boost::shared_ptr<Api> ApiPtr;
-
-
-ApiPtr create_api(nova::db::mysql::MySqlConnectionWithDefaultDbPtr con);
-
-
-}} // nova::db
-
-#endif
View
@@ -121,7 +121,7 @@ void FlagMap::add_from_file(const char * file_path) {
}
FlagMapPtr FlagMap::create_from_args(size_t count, char** argv,
- bool ignore_mismatch) {
+ bool ignore_mismatch) {
FlagMapPtr flags(new FlagMap());
for (size_t i = 0; i < count; i ++) {
flags->add_from_arg(argv[i], ignore_mismatch);
@@ -269,8 +269,8 @@ const char * FlagValues::db_backend() const {
return map->get("db_backend", "sqlalchemy");
}
-const char * FlagValues::guest_ethernet_device() const {
- return map->get("guest_ethernet_device", "eth0");
+const char * FlagValues::guest_id() const {
+ return map->get("guest_id");
}
optional<const char *> FlagValues::host() const {
@@ -336,10 +336,6 @@ unsigned long FlagValues::periodic_interval() const {
return get_flag_value(*map, "periodic_interval", (unsigned long) 60);
}
-optional<int> FlagValues::preset_instance_id() const {
- return get_flag_value<int>(*map, "preset_instance_id");
-}
-
size_t FlagValues::rabbit_client_memory() const {
return get_flag_value(*map, "rabbit_client_memory", (size_t) 4096);
}
View
@@ -101,7 +101,7 @@ class FlagValues {
const char * db_backend() const;
- const char * guest_ethernet_device() const;
+ const char * guest_id() const;
boost::optional<const char *> host() const;
@@ -135,8 +135,6 @@ class FlagValues {
unsigned long periodic_interval() const;
- boost::optional<int> preset_instance_id() const;
-
size_t rabbit_client_memory() const;
const char * rabbit_host() const;
@@ -0,0 +1,50 @@
+#include "nova/guest/HeartBeat.h"
+#include <boost/format.hpp>
+#include "nova/Log.h"
+#include "nova/db/mysql.h"
+#include <string.h>
+#include "nova/guest/utils.h"
+
+using boost::format;
+using namespace nova::db::mysql;
+using boost::optional;
+using nova::guest::utils::IsoDateTime;
+using std::string;
+
+namespace nova { namespace guest {
+
+
+HeartBeat::HeartBeat(MySqlConnectionWithDefaultDbPtr con, const char * guest_id)
+: con(con), guest_id(guest_id) {
+}
+
+HeartBeat::~HeartBeat() {
+}
+
+
+bool HeartBeat::exists() {
+ con->ensure();
+ string query = str(format("SELECT updated_at FROM agent_heartbeats "
+ "WHERE instance_id='%s'")
+ % con->escape_string(guest_id.c_str()));
+ MySqlResultSetPtr results = con->query(query.c_str());
+ return results->next();
+}
+
+void HeartBeat::update() {
+ IsoDateTime now;
+ const char * query = exists()
+ ? "UPDATE agent_heartbeats SET updated_at = ? "
+ "WHERE agent_heartbeats.instance_id = ?"
+ : "INSERT INTO agent_heartbeats (id, updated_at, instance_id) "
+ "VALUES(UUID(), ?, ?);"
+ ;
+ con->ensure();
+ MySqlPreparedStatementPtr stmt = con->prepare_statement(query);
+ stmt->set_string(0, now.c_str());
+ stmt->set_string(1, guest_id.c_str());
+ stmt->execute();
+}
+
+
+} } // end nova::guest
@@ -0,0 +1,32 @@
+#ifndef __NOVA_GUEST_HEARTBEAT
+#define __NOVA_GUEST_HEARTBEAT
+
+#include "nova/db/mysql.h"
+#include <string>
+
+
+namespace nova { namespace guest {
+
+/** Updates the agent_heartbeats table with the current time. Used to determine
+ * if Sneaky Pete is still alive. */
+class HeartBeat {
+public:
+ HeartBeat(nova::db::mysql::MySqlConnectionWithDefaultDbPtr con,
+ const char * guest_id);
+
+ ~HeartBeat();
+
+ void update();
+
+private:
+
+ nova::db::mysql::MySqlConnectionWithDefaultDbPtr con;
+
+ bool exists();
+
+ const std::string guest_id;
+};
+
+}} // nova::guest
+
+#endif
Oops, something went wrong.

0 comments on commit 59658fe

Please sign in to comment.