From bdce0bb0234da782a7eeb2294a3de2f0267b504f Mon Sep 17 00:00:00 2001 From: Udith Indrakantha <45740208+Udith-Gayan@users.noreply.github.com> Date: Thu, 4 Aug 2022 12:11:14 +0530 Subject: [PATCH] Checking for Existing Sashimono Instances with Same Name (#169) --- src/hp_manager.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/hp_manager.cpp b/src/hp_manager.cpp index e09edf1..7acdbe1 100644 --- a/src/hp_manager.cpp +++ b/src/hp_manager.cpp @@ -56,6 +56,7 @@ namespace hp constexpr const char *CONTRACT_ID_INVALID = "contractid_bad_format"; constexpr const char *DOCKER_IMAGE_INVALID = "docker_image_invalid"; constexpr const char *DOCKER_CONTAINER_NOT_FOUND = "container_not_found"; + constexpr const char *INSTANCE_ALREADY_EXISTS = "instance_already_exists"; // Cgrules check related constants. constexpr const char *CGRULE_ACTIVE = "service=$(grep \"ExecStart.*=.*/cgrulesengd$\" /etc/systemd/system/*.service | head -1 | awk -F : ' { print $1 } ') && [ ! -z $service ] && systemctl is-active $(basename $service)"; @@ -120,7 +121,16 @@ namespace hp */ int create_new_instance(std::string &error_msg, instance_info &info, std::string_view container_name, std::string_view owner_pubkey, const std::string &contract_id, const std::string &image_key) { - // If the max alloved instance count is already allocated. We won't allow more. + // Creating an instance with same name is not allowed. + hp::instance_info existing_instance; + if (sqlite::get_instance(db, container_name, existing_instance) == 0) + { + error_msg = INSTANCE_ALREADY_EXISTS; + LOG_ERROR << "Found another instance with name: " << container_name << "."; + return -1; + } + + // If the max allowed instance count is already allocated. We won't allow more. const int allocated_count = sqlite::get_allocated_instance_count(db); if (allocated_count == -1) {