List implementation for Sashi CLI. (#46)

This commit is contained in:
Savinda Senevirathne
2021-08-02 16:30:11 +05:30
committed by GitHub
parent 15eb47b66b
commit 7302270f30
15 changed files with 204 additions and 114 deletions

View File

@@ -22,8 +22,8 @@ namespace sqlite
constexpr const char *INSERT_INTO_HP_INSTANCE = "INSERT INTO instances("
"owner_pubkey, time, username, status, name, ip,"
"peer_port, user_port, pubkey, contract_id"
") VALUES(?,?,?,?,?,?,?,?,?,?)";
"peer_port, user_port, pubkey, contract_id, image_name"
") VALUES(?,?,?,?,?,?,?,?,?,?,?)";
constexpr const char *GET_VACANT_PORTS_FROM_HP = "SELECT DISTINCT peer_port, user_port FROM "
"instances WHERE status == ? AND user_port NOT IN"
@@ -33,15 +33,13 @@ namespace sqlite
constexpr const char *UPDATE_STATUS_IN_HP = "UPDATE instances SET status = ? WHERE name = ?";
constexpr const char *UPDATE_CURRENT_STATUS_IN_HP = "UPDATE instances SET current_status = ? WHERE name = ?";
constexpr const char *IS_CONTAINER_EXISTS = "SELECT username, status, peer_port, user_port FROM instances WHERE name = ?";
constexpr const char *GET_ALOCATED_INSTANCE_COUNT = "SELECT COUNT(name) FROM instances WHERE status != ?";
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 *GET_INSTANCE_LIST = "SELECT name, username, user_port, peer_port, status, image_name FROM instances WHERE status != ?";
constexpr const char *IS_TABLE_EXISTS = "SELECT * FROM sqlite_master WHERE type='table' AND name = ?";
@@ -290,13 +288,13 @@ namespace sqlite
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),
table_column_info("ip", COLUMN_DATA_TYPE::TEXT),
table_column_info("peer_port", COLUMN_DATA_TYPE::INT),
table_column_info("user_port", COLUMN_DATA_TYPE::INT),
table_column_info("pubkey", COLUMN_DATA_TYPE::TEXT),
table_column_info("contract_id", COLUMN_DATA_TYPE::TEXT)};
table_column_info("contract_id", COLUMN_DATA_TYPE::TEXT),
table_column_info("image_name", COLUMN_DATA_TYPE::TEXT)};
if (create_table(db, INSTANCE_TABLE, columns) == -1 ||
create_index(db, INSTANCE_TABLE, "name", true) == -1 ||
@@ -326,6 +324,7 @@ namespace sqlite
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_bind_text(stmt, 11, info.image_name.data(), info.image_name.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_step(stmt) == SQLITE_DONE)
{
sqlite3_finalize(stmt);
@@ -389,28 +388,6 @@ namespace sqlite
return -1;
}
/**
* Update the current status of the given container to the new value.
* @param db Database connection.
* @param container_name Name of the container whose status should be updated.
* @param current_status The new status of the container.
* @return 0 on success and -1 on error.
*/
int update_current_status_in_container(sqlite3 *db, std::string_view container_name, std::string_view current_status)
{
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, UPDATE_CURRENT_STATUS_IN_HP, -1, &stmt, 0) == SQLITE_OK && stmt != NULL &&
sqlite3_bind_text(stmt, 1, current_status.data(), current_status.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_text(stmt, 2, container_name.data(), container_name.length(), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_step(stmt) == SQLITE_DONE)
{
sqlite3_finalize(stmt);
return 0;
}
LOG_ERROR << "Error updating container current status for " << container_name;
return -1;
}
/**
* Get the max peer and user ports assigned for instances excluding destroyed instances.
* @param db Database connection.
@@ -493,25 +470,28 @@ namespace sqlite
}
/**
* Populate the given vector with user name and name of running hp instances.
* Populate the given vector with the instance list except destroyed instances.
* @param db Database connection.
* @param running_instances Vector to hold user name and name of instances.
* @param running_instances Vector to hold instance details.
*/
void get_running_instance_user_and_name_list(sqlite3 *db, std::vector<std::pair<const std::string, const std::string>> &running_instances)
void get_instance_list(sqlite3 *db, std::vector<hp::instance_info> &instances)
{
running_instances.clear();
sqlite3_stmt *stmt;
std::string_view running_status(hp::CONTAINER_STATES[hp::STATES::RUNNING]);
std::string_view destroy_status(hp::CONTAINER_STATES[hp::STATES::DESTROYED]);
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)
if (sqlite3_prepare_v2(db, GET_INSTANCE_LIST, -1, &stmt, 0) == SQLITE_OK && stmt != NULL &&
sqlite3_bind_text(stmt, 1, destroy_status.data(), destroy_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});
hp::instance_info info;
info.container_name = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0));
info.username = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1));
info.assigned_ports.user_port = sqlite3_column_int64(stmt, 2);
info.assigned_ports.peer_port = sqlite3_column_int64(stmt, 3);
info.status = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 4));
info.image_name = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 5));
instances.push_back(info);
}
}