130 lines
3.6 KiB
Bash
Executable File
130 lines
3.6 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
|
|
|
|
/*
|
|
|
|
CREATEAGENT.HPP IS AUTOMATICALLY GENERATED FROM make_createagent.sh,
|
|
and recreates the files in /agent/ on a remote server.
|
|
|
|
DO NOT EDIT THIS FILE MANUALLY! Edit make_createagent.sh instead.
|
|
|
|
*/
|
|
|
|
#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>
|
|
|
|
|
|
/*
|
|
|
|
CREATEAGENT.CPP IS AUTOMATICALLY GENERATED FROM make_createagent.sh,
|
|
and recreates the files in /agent/ on a remote server.
|
|
|
|
DO NOT EDIT THIS FILE MANUALLY! Edit make_createagent.sh instead.
|
|
|
|
*/
|
|
|
|
|
|
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.
|
|
if (remote_write_file(server_env.get_SSH_INFO(), file) != 0) {
|
|
std::cerr << "Failed to write file " << file.filename << std::endl;
|
|
return 1;
|
|
}
|
|
std::cout << "Wrote file " << file.filename << " on " << server_name << std::endl;
|
|
}
|
|
|
|
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.
|
|
cat <<CPPEOF >> "$SCRIPT_DIR/src/utils/createagent.cpp"
|
|
}; // agent_files defined.
|
|
|
|
return 0;
|
|
} // end of load_agent_file_contents function.
|
|
|
|
} // namespace dropshell
|
|
|
|
CPPEOF
|