1
0
This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
dtux__mkauto_inst/main.cpp

590 lines
12 KiB
C++
Raw Normal View History

2022-08-16 23:33:14 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <array>
#include <memory>
#include <stdexcept>
#include <getopt.h>
2022-08-21 23:11:35 +02:00
#include <experimental/filesystem>
2022-08-16 23:33:14 +02:00
#include <cstdlib>
2022-08-21 23:11:35 +02:00
#include <functional>
2022-08-16 23:33:14 +02:00
using namespace std;
2022-08-21 23:11:35 +02:00
namespace fs = experimental::filesystem;
2022-08-16 23:33:14 +02:00
// Global variables
int verbose = false;
2022-08-21 23:11:35 +02:00
string tab = "\t\t\t\t\t";
bool verif = false;
bool list = false;
bool mountpoints = false;
bool partitions = false;
bool generateauto_inst = false;
2022-08-16 23:33:14 +02:00
void help(char * cmd)
{
printf("Usage: %s [-lavVf]\n", cmd);
2022-08-21 23:11:35 +02:00
printf("-a Execute all commands\n" );
printf("-f Verify the modified files and save them\n");
printf("-g Generate auto_inst.cfg.pl\n");
printf("-h Display this help\n");
printf("-l List of the first level packages installed\n");
printf("-m Keep mount points\n");
printf("-p Keep partitioning\n");
printf("-v Verbose\n");
printf("-V Display the version of this software\n");
2022-08-16 23:33:14 +02:00
exit(EXIT_FAILURE);
}
2022-08-21 23:11:35 +02:00
void whatprovides(string filepath)
{
string cmd = "urpmi_rpm-find-leaves";
cmd += " >" + filepath;
system(cmd.c_str());
}
string array2string(array<char, 256> buffer)
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
int i = 0;
2022-08-16 23:33:14 +02:00
string line;
2022-08-21 23:11:35 +02:00
while(buffer[i] != '\n' )
{
line.push_back(buffer[i]);
i++;
}
return line;
}
bool savenonemptydir(string filepath, string needle="*")
{
int rsyncErrors = 0;
int pos;
string rsyncdestination = "files/";
string rsynccmd = "rsync -aqP --relative ";
if (fs::is_directory(filepath))
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
pos = filepath.find(needle);
if (pos != string::npos and (pos == filepath.length() - 2) and !fs::is_empty(filepath) )
{
cout << filepath << endl;
2022-08-16 23:33:14 +02:00
}
2022-08-21 23:11:35 +02:00
}
return rsyncErrors;
}
bool fstabAnanlyze()
{
string fstabpath = "/etc/fstab";
string line;
bool returncode = EXIT_SUCCESS;
int pos = 0;
int posstart = 0;
int npos = 0;
vector<string> fstabarray;
ifstream file;
int nline = 0;
array<string, 15> mountpoints{"/", "/home /var, "};
file.open(fstabpath);
if(file.good())
{
while (getline(file, line))
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
while ((pos = line.find(" ") != string::npos))
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
fstabarray.push_back(line.substr(posstart, pos - posstart));
posstart = pos;
2022-08-16 23:33:14 +02:00
}
2022-08-21 23:11:35 +02:00
npos = 0;
if ()
2022-08-16 23:33:14 +02:00
}
2022-08-21 23:11:35 +02:00
}else
{
cout << "Error opening " + fstabpath << endl;
exit(EXIT_FAILURE);
}
return returncode;
}
bool savedirs()
{
bool returncode = false;
string filepath;
string line;
string rsyncdestination = "files/";
string rsynccmd = "rsync -aqP --relative -m ";
int rsyncErrors = 0;
ifstream confFile("config/savedir.conf");
if (confFile.is_open())
{
while (getline(confFile, line))
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
filepath = line;
//if (fs::exists(filepath))
//{
if (verbose) cout << "Directory to save: " + filepath << endl;
rsynccmd = "rsync -aqP --relative " + filepath + " " + rsyncdestination;
if (verbose) cout << "rsync command: " + rsynccmd << endl;
if (system(rsynccmd.c_str()) != 0)
{
rsyncErrors ++;
}
//}else cout << filepath << "not exists"<< endl;
}
}else
{
cout << "can't open config/savedir.conf" << endl;
exit(EXIT_FAILURE);
}
return returncode;
}
int recursedir(string dir, string needle, function<bool(string,string)> func)
{
int returncode = EXIT_SUCCESS;
string filepath;
for (const auto & file : fs::recursive_directory_iterator(dir))
{
filepath = (string ) file.path();
cout << filepath << endl;
returncode |= func(dir, needle);
}
return returncode;
}
/*
int saveOptLocal()
{
int result;
int rsyncErrors = EXIT_SUCCESS;
string cmd;
string rsynccmd = "rsync -aqP --relative -m ";
string rsyncdestination = "files/";
for(auto var : dirlist)
{
cout << "Saving" + var << endl;
cmd = rsynccmd + var + " " + rsyncdestination;
if (verbose) printf("rsync command: %s\n", cmd.c_str());
if ((result = system(cmd.c_str())) != 0)
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
rsyncErrors ++;
2022-08-16 23:33:14 +02:00
}
}
2022-08-21 23:11:35 +02:00
return rsyncErrors;
2022-08-16 23:33:14 +02:00
}
2022-08-21 23:11:35 +02:00
*/
2022-08-16 23:33:14 +02:00
void verification()
{
int result;
int i = 0;
int rsyncErrors = 0;
2022-08-21 23:11:35 +02:00
array<char, 256> buffer;
2022-08-16 23:33:14 +02:00
string cmd = "rpm -a --verify";
string line;
string filepath;
string rsyncdestination = "files/";
2022-08-21 23:11:35 +02:00
string rsynccmd = "rsync -aqP --relative ";
string etcdir = "/etc";
string dir;
int log;
if (verbose) cout << "save '.d' non empty directories";
recursedir(etcdir, ".d",&savenonemptydir);
savedirs();
2022-08-16 23:33:14 +02:00
printf("Verifying packages installed\n");
2022-08-21 23:11:35 +02:00
2022-08-16 23:33:14 +02:00
flush(cout);
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while(fgets(buffer.data(), buffer.size(), pipe.get()) != NULL)
{
2022-08-21 23:11:35 +02:00
line = array2string(buffer);
log = line.find(".log");
//cout << "log = " << line.length() << " ---> ";
//printf("%s\n", line.c_str());
if (line.at(2) == '5' and log != (line.length() - 4))
2022-08-16 23:33:14 +02:00
{
line.erase(0, 13);
2022-08-21 23:11:35 +02:00
if (verbose) cout << "file to save: " + line<< endl;
cmd = rsynccmd + line + " " + rsyncdestination;
if (verbose) printf("rsync command: %s\n", cmd.c_str());
if ((result = system(cmd.c_str())) != 0)
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
printf("Rsync error: %i\n", result);
2022-08-16 23:33:14 +02:00
rsyncErrors ++;
}
}
buffer.fill(0);
}
2022-08-21 23:11:35 +02:00
// save all non empties directories finishing by ".d" in /etc
2022-08-16 23:33:14 +02:00
if (rsyncErrors != 0) printf("rsync returned %i errors", rsyncErrors);
}
2022-08-21 23:11:35 +02:00
bool enabledmedias(string filename)
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
bool returnCode = EXIT_SUCCESS;
bool start = false;
bool flag = false;
bool ignore = false;
string temp = "";
string medias = "\t\t'enabled_media' => [\n";
string line;
int i = 0;
2022-08-16 23:33:14 +02:00
2022-08-21 23:11:35 +02:00
printf("searching enabled medias\n");
ifstream file("/etc/urpmi/urpmi.cfg");
if (file.is_open())
{
while (getline(file, line))
{
//printf("%s\n", line.c_str());
//cout << "start =" << start;
if (start == false)
{
if (line.front() != '{' and line.front() != '\0' and line.front() != '}')
{
//cout << "passed";
start = true;
flag = false;
i = 0;
do
{
i = line.find(" ", i+1);
if (line[i-1] == '\\')
{
flag = true;
line.erase(i-1, 1);
}else
{
break;
}
}while(flag == true);
line.erase(i);
temp = line;
printf("%s", temp.c_str());
line.clear();
}else
{
continue;
}
}else
{ array <char, 2048> buffer;
2022-08-16 23:33:14 +02:00
2022-08-21 23:11:35 +02:00
if (line.front() == '}')
{
if (!temp.empty() and ignore != true)
{
cout << " ==> adding to medias list\n";
medias += tab + temp + ",\n";
}else
{
cout << endl;
}
start = false;
temp.clear();
ignore = false;
continue;
}
if (line.find("ignore") != string::npos)
{
//cout << "found 'ignore'\n";
ignore = true;
}
}
}
printf("Medias:\n%s\n", medias.c_str());
file.close();
medias += tab + "],\n";
ofstream file(filename, ofstream::app);
file << medias.c_str();
file.close();
}else return EXIT_FAILURE;
2022-08-16 23:33:14 +02:00
return returnCode;
}
2022-08-21 23:11:35 +02:00
bool keyboardlayout(string filename)
{
bool returncode = EXIT_SUCCESS;
int i;
string cmd = "setxkbmap -query";
string line;
string str;
array<char, 256> buffer;
printf("looking for keyboard layout\n");
flush(cout);
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while(fgets(buffer.data(), buffer.size(), pipe.get()) != NULL)
{
line = array2string(buffer);
cout << line << endl;
i = line.find("layout:");
if (i != string::npos)
{
i = line.find_last_of(' ');
line.erase(0, i+1);
break;
}
cout << line << endl;
line.clear();
buffer.fill(0);
}
cout << "result =>" << line << endl;
ofstream file(filename, ofstream::app);
if(file.is_open())
{
str = "\t\t'keyboard' => {\n" + tab + "'GRP_TOGGLE' => '',\n" + tab + "'KEYBOARD' => '" + line + "'\n" + tab + "},\n";
file << str;
file.close();
}else return EXIT_FAILURE;
return returncode;
}
string getvalue( string line)
2022-08-16 23:33:14 +02:00
{
2022-08-21 23:11:35 +02:00
string value;
int i;
2022-08-16 23:33:14 +02:00
2022-08-21 23:11:35 +02:00
if ((i = line.find("=")) != string::npos)
{
value = line.substr(i+1, string::npos);
}
return value;
}
2022-08-16 23:33:14 +02:00
2022-08-21 23:11:35 +02:00
bool mklocale(string filename)
{
string localeconf = "/etc/locale.conf";
bool returncode = EXIT_SUCCESS;
int i;
int flag = 0;
int utf8 = 0;
string country;
string lang;
string line;
string str;
string value;
vector<string> languages;
2022-08-16 23:33:14 +02:00
2022-08-21 23:11:35 +02:00
ifstream ifile (localeconf);
ofstream ofile (filename, ofstream::app);
if (ifile.is_open() and ofile.is_open())
{
while (getline(ifile, line))
{
value = getvalue(line);
//cout << "Value ===>" + value << endl;
if((i = line.find("COUNTRY")) != string::npos)
{
country = value;
}else if((i = line.find("LANG=")) != string::npos)
{
lang = value.substr(0, 2);
if (value.find("UTF-8") != string::npos)
{
utf8 = 1;
}
}else if((i = line.find("LANGUAGE")) != string::npos)
{
i = value.find(":");
languages.push_back(value.substr(i+1,string::npos));
}
}
}
ofstream file(filename, ofstream::app);
str = "\t\t'locale' => {\n" + tab + "'IM' => undef,\n" + tab + "'country' => '" + country + "'\n" + tab + "'lang' => '" + lang + "'\n";
str += tab + "'langs' => {\n";
//for(auto & elem : languages)
flag = 0;
for(i=0; i< languages.size(); i++)
{
if (flag == 0)
{
str += tab + "\t\t'" + languages[i] + "' => 1";
}else
{
str += ",\n" + tab + "\t\t'" + languages[i] + "' => 1";
}
}
str += "\n" + tab + "\t\t},\n";
str += tab + "'utf8' => 1\n" + tab + "},\n";
file << str;
file.close();
return returncode;
}
bool miscellaneous(string filename)
{
bool returncode = EXIT_SUCCESS;
int r;
bool numlock = 0;
string str;
r = system("systemctl status numlock");
if (r == 0)
{
numlock = 1;
}
str = "\t\t'miscellaneous' => {\n" + tab + "'numlock' => " + to_string(numlock) + ",\n" + tab + "'HDPARM' => 1,\n\t\t},\n";
ofstream file(filename, ofstream::app);
file << str;
file.close();
return returncode;
}
int mkauto_inst(string pkgsfile)
{
bool returnCode = EXIT_SUCCESS;
bool flag =false;
string dirpath = "auto_inst.files";
string auto_inst_file = "auto_inst.cfg.pl";
string header = "auto_inst.cfg.pl.header";
string users = "auto_inst.cfg.pl.users";
string footer = "auto_inst.cfg.pl.footer";
string cmd = "cp " + dirpath + "/" + header + " ./" + auto_inst_file;
2022-08-16 23:33:14 +02:00
int i = 0;
string line;
2022-08-21 23:11:35 +02:00
cout << "\n\n" + cmd << endl;
returnCode |= system(cmd.c_str());
cout << "copying packages\n";
ifstream ifile(pkgsfile);
ofstream ofile("./" + auto_inst_file, ofstream::app);
if (ifile.is_open() and ofile.is_open())
{
while (getline(ifile, line))
{
if (flag == false)
{
flag = true;
line = tab + line;
ofile << line;
}else
{
line = ",\n" + tab + line;
ofile << line;
}
}
ofile << "\n" + tab + "],\n";
ifile.close();
ofile.close();
}else
{
cout << "error file not open";
return 1;
}
returnCode |= enabledmedias(auto_inst_file);
returnCode |= keyboardlayout(auto_inst_file);
returnCode |= mklocale(auto_inst_file);
returnCode |= miscellaneous(auto_inst_file);
return returnCode;
}
int main(int argc, char** argv)
{
//bool x64 = 0;
2022-08-16 23:33:14 +02:00
string pkgsfile = "pkgsfile.txt";
int opt = 0;
2022-08-21 23:11:35 +02:00
int exitcode = EXIT_SUCCESS;
2022-08-16 23:33:14 +02:00
// listing arguments
if(argc>1)
{
2022-08-21 23:11:35 +02:00
while((opt = getopt(argc, argv, "valmpVfg")) != -1)
2022-08-16 23:33:14 +02:00
{
switch (opt)
{
case 'f':
verif = true;
break;
case 'l':
list = true;
break;
case 'a':
list = true;
verif = true;
2022-08-21 23:11:35 +02:00
generateauto_inst = true;
2022-08-16 23:33:14 +02:00
break;
case 'v':
verbose = true;
break;
2022-08-21 23:11:35 +02:00
case 'g':
generateauto_inst = true;
list = true;
break;
case 'm':
mountpoints = true;
break;
case 'p':
partitions = true;
2022-08-16 23:33:14 +02:00
default:
help(argv[0]);
}
}
}else
{
cout << "no args\n";
help(argv[0]);
}
2022-08-21 23:11:35 +02:00
/*
2022-08-16 23:33:14 +02:00
// getting processeur architecture
if (uname(&buffer) < 0)
{
perror("uname");
exit(EXIT_FAILURE);
}
if ( strcmp(buffer.machine, "x86_64") == 0)
{
x64 = true;
}
if (verbose) printf("architecture is %s\n", buffer.machine);
2022-08-21 23:11:35 +02:00
*/
2022-08-16 23:33:14 +02:00
if (list == true)
{
2022-08-21 23:11:35 +02:00
whatprovides(pkgsfile);
2022-08-16 23:33:14 +02:00
}
if( verif == true)
{
verification();
}
2022-08-21 23:11:35 +02:00
if(generateauto_inst == true)
{
mkauto_inst(pkgsfile);
}
2022-08-16 23:33:14 +02:00
cout << "finished\n";
2022-08-21 23:11:35 +02:00
exit(exitcode);
2022-08-16 23:33:14 +02:00
}