Instance users and dockerd. (#18)

This commit is contained in:
Savinda Senevirathne
2021-06-25 14:16:22 +05:30
committed by GitHub
parent 2392e6f1f2
commit 7dd0ea97b5
15 changed files with 607 additions and 159 deletions

View File

@@ -21,9 +21,9 @@ namespace sqlite
constexpr const char *INSTANCE_TABLE = "instances";
constexpr const char *INSERT_INTO_HP_INSTANCE = "INSERT INTO instances("
"owner_pubkey, time, status, name, ip,"
"owner_pubkey, time, username, status, name, ip,"
"peer_port, user_port, pubkey, contract_id"
") VALUES(?,?,?,?,?,?,?,?,?)";
") VALUES(?,?,?,?,?,?,?,?,?,?)";
constexpr const char *GET_VACANT_PORTS_FROM_HP = "SELECT DISTINCT peer_port, user_port FROM "
"instances WHERE status == ? AND user_port NOT IN"
@@ -35,10 +35,12 @@ namespace sqlite
constexpr const char *UPDATE_CURRENT_STATUS_IN_HP = "UPDATE instances SET current_status = ? WHERE name = ?";
constexpr const char *IS_CONTAINER_EXISTS = "SELECT * FROM instances WHERE name = ?";
constexpr const char *IS_CONTAINER_EXISTS = "SELECT username, status, peer_port, user_port FROM instances WHERE name = ?";
constexpr const char *GET_RUNNING_INSTANCE_NAMES = "SELECT name FROM instances WHERE status = ?";
constexpr const char *GET_RUNNING_INSTANCE_USER_AND_NAME_LIST = "SELECT username,name FROM instances WHERE status = ?";
constexpr const char *IS_TABLE_EXISTS = "SELECT * FROM sqlite_master WHERE type='table' AND name = ?";
/**
@@ -284,6 +286,7 @@ namespace sqlite
const std::vector<table_column_info> columns{
table_column_info("owner_pubkey", COLUMN_DATA_TYPE::TEXT),
table_column_info("time", COLUMN_DATA_TYPE::INT),
table_column_info("username", COLUMN_DATA_TYPE::TEXT),
table_column_info("status", COLUMN_DATA_TYPE::TEXT),
table_column_info("current_status", COLUMN_DATA_TYPE::TEXT),
table_column_info("name", COLUMN_DATA_TYPE::TEXT, true),
@@ -313,13 +316,14 @@ namespace sqlite
if (sqlite3_prepare_v2(db, INSERT_INTO_HP_INSTANCE, -1, &stmt, 0) == SQLITE_OK && stmt != NULL &&
sqlite3_bind_text(stmt, 1, info.owner_pubkey.data(), info.owner_pubkey.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 2, util::get_epoch_milliseconds()) == SQLITE_OK &&
sqlite3_bind_text(stmt, 3, info.status.data(), info.status.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 4, info.name.data(), info.name.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 5, info.ip.data(), info.ip.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 6, info.assigned_ports.peer_port) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 7, info.assigned_ports.user_port) == SQLITE_OK &&
sqlite3_bind_text(stmt, 8, info.pubkey.data(), info.pubkey.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 9, info.contract_id.data(), info.contract_id.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 3, info.username.data(), info.username.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 4, info.status.data(), info.status.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 5, info.container_name.data(), info.container_name.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 6, info.ip.data(), info.ip.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 7, info.assigned_ports.peer_port) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 8, info.assigned_ports.user_port) == SQLITE_OK &&
sqlite3_bind_text(stmt, 9, info.pubkey.data(), info.pubkey.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 10, info.contract_id.data(), info.contract_id.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_step(stmt) == SQLITE_DONE)
{
sqlite3_finalize(stmt);
@@ -346,9 +350,10 @@ namespace sqlite
sqlite3_step(stmt) == SQLITE_ROW)
{
// Populate only the necessary fields.
info.status = std::string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)));
info.assigned_ports.peer_port = sqlite3_column_int64(stmt, 6);
info.assigned_ports.user_port = sqlite3_column_int64(stmt, 7);
info.username = std::string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
info.status = std::string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)));
info.assigned_ports.peer_port = sqlite3_column_int64(stmt, 2);
info.assigned_ports.user_port = sqlite3_column_int64(stmt, 3);
// Finalize and distroys the statement.
sqlite3_finalize(stmt);
@@ -484,4 +489,31 @@ namespace sqlite
// Finalize and distroys the statement.
sqlite3_finalize(stmt);
}
/**
* Populate the given vector with user name and name of running hp instances.
* @param db Database connection.
* @param running_instances Vector to hold user name and name of instances.
*/
void get_running_instance_user_and_name_list(sqlite3 *db, std::vector<std::pair<const std::string, const std::string>> &running_instances)
{
running_instances.clear();
sqlite3_stmt *stmt;
std::string_view running_status(hp::CONTAINER_STATES[hp::STATES::RUNNING]);
if (sqlite3_prepare_v2(db, GET_RUNNING_INSTANCE_USER_AND_NAME_LIST, -1, &stmt, 0) == SQLITE_OK && stmt != NULL &&
sqlite3_bind_text(stmt, 1, running_status.data(), running_status.length(), SQLITE_STATIC) == SQLITE_OK)
{
while (stmt != NULL && sqlite3_step(stmt) == SQLITE_ROW)
{
const std::string username(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
const std::string name(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)));
running_instances.push_back({username, name});
}
}
// Finalize and distroys the statement.
sqlite3_finalize(stmt);
}
}