dropshell/make_createagent.sh
Your Name 707e973130
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
./
2025-05-14 23:47:23 +12:00

98 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# This script creates two files:
# src/utils/createagent.hpp
# src/utils/createagent.cpp
#
SCRIPT_DIR=$(dirname "$0")
# heredoc to create the createagent.hpp file.
cat <<EOF > "$SCRIPT_DIR/src/utils/createagent.hpp"
#ifndef CREATEAGENT_HPP
#define CREATEAGENT_HPP
#include <string>
namespace dropshell {
int create_agent(const std::string &server_name);
}
#endif // CREATEAGENT_HPP
EOF
# heredoc to create the createagent.cpp file.
cat <<CPPEOF > "$SCRIPT_DIR/src/utils/createagent.cpp"
#include "createagent.hpp"
#include "utils.hpp"
#include "directories.hpp"
#include "execute.hpp"
#include "server_env_manager.hpp"
#include <vector>
#include <string>
#include <iostream>
namespace dropshell {
struct AgentFile {
std::string filename;
std::string content;
};
int load_agent_file_contents(std::vector<AgentFile> &agent_files);
int remote_write_file(const sSSHInfo &ssh_info, const AgentFile &file);
int create_agent(const std::string &server_name) {
// create the sh file on the remote server.
server_env_manager server_env(server_name);
if (!server_env.is_valid()) {
std::cerr << "Invalid server environment for " << server_name << std::endl;
return 1;
}
// create the agent directory.
std::string agent_dir = remotepath::agent(server_name);
execute_ssh_command(server_env.get_SSH_INFO(), sCommand("","mkdir -p " + agent_dir,{}), cMode::Silent);
std::vector<AgentFile> agent_files;
load_agent_file_contents(agent_files);
// create the sh file on the remote server.
for (const auto &file : agent_files) {
// write out the file contents in the file.
remote_write_file(server_env.get_SSH_INFO(), file);
}
return 0;
}
int remote_write_file(const sSSHInfo &ssh_info, const AgentFile &file)
{
std::string generate_command = "cat <<'GENERATE_EOF' > " + file.filename + "\n" + file.content + "\nGENERATE_EOF\n"; // use heredoc to write the file.
return execute_ssh_command(ssh_info, sCommand("",generate_command,{}), cMode::Defaults);
}
// auto generated load_agent_file_contents function goes here, followed by closing } for namespace dropshell.
int load_agent_file_contents(std::vector<AgentFile> &agent_files)
{
agent_files={
CPPEOF
AGENT_SOURCE_FOLDER="$SCRIPT_DIR/agent"
# find all the files in the agent source folder.
find "$AGENT_SOURCE_FOLDER" -type f | while read -r file; do
# get the filename without the path.
filename=$(basename "$file")
echo " AgentFile { \"$filename\", R\"AGENTFILE(" >> "$SCRIPT_DIR/src/utils/createagent.cpp"
cat "$file" >> "$SCRIPT_DIR/src/utils/createagent.cpp"
echo ")AGENTFILE\" }," >> "$SCRIPT_DIR/src/utils/createagent.cpp"
done
# close the namespace dropshell.
echo "}; return 0;} // namespace dropshell" >> "$SCRIPT_DIR/src/utils/createagent.cpp"