Skip to content

Commit

Permalink
Added log delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenrong-wang committed Oct 16, 2024
1 parent a92defd commit b56b1b6
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 3 deletions.
50 changes: 50 additions & 0 deletions hpcopr/cluster_general_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4853,6 +4853,56 @@ int tf_test(char* cluster_name, char* cloud_flag, char* cloud_ak, char* cloud_sk
return 0;
}

int64_t delete_logs(char* cluster_name){
int64_t file_size=0;
if(cluster_name==NULL||strlen(cluster_name)==0){
char logfile[FILENAME_LENGTH]="";
snprintf(logfile,FILENAME_LENGTH,"%s%slog_trashbin.txt",NOW_LOG_DIR,PATH_SLASH);
file_size=get_filesize_byte_byname(logfile);
if(file_size<0){
return -5;
}
rm_file_or_dir(logfile);
return (file_exist_or_not(logfile))?file_size:(-1);
}
else{
char workdir[DIR_LENGTH]="";
char logdir[DIR_LENGTH]="";
char logfile_dbg[FILENAME_LENGTH]="";
char logfile_err[FILENAME_LENGTH]="";
char logfile_std[FILENAME_LENGTH]="";
if(cluster_name_check(cluster_name)!=-7){
return -3; // Invalid cluster name.
}
get_nworkdir(workdir,DIR_LENGTH,cluster_name);
create_and_get_subdir(workdir,"log",logdir,DIR_LENGTH);
snprintf(logfile_dbg,FILENAME_LENGTH,"%s%stf_dbg.log.archive",logdir,PATH_SLASH);
snprintf(logfile_std,FILENAME_LENGTH,"%s%stf_prep.log.archive",logdir,PATH_SLASH);
snprintf(logfile_err,FILENAME_LENGTH,"%s%stf_prep.err.log.archive",logdir,PATH_SLASH);

int64_t size_dbg=get_filesize_byte_byname(logfile_dbg);
int64_t size_std=get_filesize_byte_byname(logfile_std);
int64_t size_err=get_filesize_byte_byname(logfile_err);

if(size_dbg<0&&size_std<0&&size_err<0){
return -5;
}
if(size_dbg>=0){
file_size+=size_dbg;
rm_file_or_dir(logfile_dbg);
}
if(size_std>=0){
file_size+=size_std;
rm_file_or_dir(logfile_std);
}
if(size_err>=0){
file_size+=size_err;
rm_file_or_dir(logfile_err);
}
return (file_exist_or_not(logfile_dbg)&file_exist_or_not(logfile_err)&file_exist_or_not(logfile_std))?file_size:(-1);
}
}

/* return 1 - running; return 0 - stopped */
/*
int check_volce_ecs_state(char* node_name, char* stackdir){
Expand Down
1 change: 1 addition & 0 deletions hpcopr/cluster_general_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ int create_cluster_lock(char* workdir);
ssize_t check_cluster_lock(char* workdir);

int tf_test(char* cluster_name, char* cloud_flag, char* cloud_ak, char* cloud_sk, char* az_subscription, char* az_tenant, tf_exec_config* tf_run, char* force_flag);
int64_t delete_logs(char* cluster_name);

/*int check_volce_ecs_state(char* node_name, char* stackdir);
int generate_volce_ecs_state(char* node_name, char* stackdir, int target_state);*/
Expand Down
10 changes: 10 additions & 0 deletions hpcopr/general_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,16 @@ int64_t get_filesize_byte(FILE* file_p){
return length;
}

int64_t get_filesize_byte_byname(char* filename){
FILE *file_p=fopen(filename,"rb");
if(file_p){
int64_t size=get_filesize_byte(file_p);
fclose(file_p);
return size;
}
return NULL_PTR_ARG;
}

/*
* This function is to replace the delete_file_or_dir function
*/
Expand Down
1 change: 1 addition & 0 deletions hpcopr/general_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ int delete_file_or_dir(char* file_or_dir);

/* Get the size of a given file. */
int64_t get_filesize_byte(FILE* file_p);
int64_t get_filesize_byte_byname(char* filename);

/* Remove a given file or directory. Return 0 if successfully. */
int rm_file_or_dir(char* file_or_dir);
Expand Down
3 changes: 3 additions & 0 deletions hpcopr/general_print_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ void print_help(char* cmd_name){
printf("| --print(Default) ~ Print out the usage data.\n");
printf("| -d DEST_PATH ~ Export the usage data to a destination file.\n");
}
if(strcmp(cmd_name,"del-logs")==0||strcmp(cmd_name,"all")==0){
printf("| " HIGH_GREEN_BOLD "del-logs" RESET_DISPLAY " :~ Delete archived logs or log trashbin.\n");
}
if(strcmp(cmd_name,"ssh")==0||strcmp(cmd_name,"all")==0){
printf("| " HIGH_GREEN_BOLD "ssh" RESET_DISPLAY " :~ SSH to the master node of a cluster.\n");
printf("| -u USER_NAME ~ SSH to the cluster as a valid user.\n");
Expand Down
36 changes: 36 additions & 0 deletions hpcopr/hpcopr_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ char commands[COMMAND_NUM][COMMAND_STRING_LENGTH_MAX]={
"usage,gen,NULL",
"history,gen,NULL",
"syserr,gen,NULL",
"del-logs,gen,NULL",
"ssh,gen,UNAME",
"rdp,gen,UNAME",
"set-tf,gen,NULL",
Expand Down Expand Up @@ -240,6 +241,7 @@ char jobman_commands[3][SUBCMD_STRING_LENGTH_MAX]={
47 GRAPH_NOT_UPDATED
48 RDP_CONNECTION_FAILED
49 CLUSTER_EMPTY
50 FAILED_TO_DELETE_LOGS
51 CLUSTER_NOT_EMPTY
53 PROCESS_LOCKED
55 NO_CONF_FILE
Expand Down Expand Up @@ -908,6 +910,40 @@ int main(int argc, char* argv[]){
return 0;
}

if(strcmp(final_command,"del-logs")==0){
cmd_keyword_ncheck(argc,argv,"-c",cluster_name,32);
int64_t delete_flag=delete_logs(cluster_name);
if(delete_flag==-3){
printf(FATAL_RED_BOLD "[ FATAL: ] The specified cluster name %s is not in the registry." RESET_DISPLAY "\n",cluster_name);
write_operation_log(cluster_name,operation_log,argc,argv,"NOT_IN_THE_CLUSTER_REGISTRY",39);
check_and_cleanup(workdir);
return 39;
}
else if(delete_flag==-1){
printf(FATAL_RED_BOLD "[ FATAL: ] Failed to delete the log(s)." RESET_DISPLAY "\n");
write_operation_log(cluster_name,operation_log,argc,argv,"FAILED_TO_DELETE_LOGS",50);
check_and_cleanup(workdir);
return 50;
}
else if(delete_flag==-5){
printf(WARN_YELLO_BOLD "[ -WARN- ] Log file(s) not exist, there is nothing to delete." RESET_DISPLAY "\n");
write_operation_log(cluster_name,operation_log,argc,argv,"NO_LOGS_TO_DELETE",50);
check_and_cleanup(workdir);
return 50;
}
if(strlen(cluster_name)==0){
printf(GENERAL_BOLD "\n[ -INFO- ]" RESET_DISPLAY " Deleted the log trashbin under %s.\n", NOW_LOG_DIR);
printf("[ **** ] %ld bytes of disk storage freed.\n", delete_flag);
}
else{
printf(GENERAL_BOLD "\n[ -INFO- ]" RESET_DISPLAY " Deleted the log archives of %s.\n", cluster_name);
printf("[ **** ] %ld bytes of disk storage freed.\n", delete_flag);
}
write_operation_log(cluster_name,operation_log,argc,argv,"SUCCEEDED",0);
check_and_cleanup(workdir);
return 0;
}

if(strcmp(final_command,"import")==0){
cmd_keyword_ncheck(argc,argv,"-s",import_source,512);
cmd_keyword_ncheck(argc,argv,"-p",pass_word,128);
Expand Down
4 changes: 2 additions & 2 deletions hpcopr/now_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef NOW_MACROS_H
#define NOW_MACROS_H

#define CORE_VERSION_CODE "0.3.1.0145"
#define CORE_VERSION_CODE "0.3.1.0147"

#define NULL_PTR_ARG -127

Expand Down Expand Up @@ -356,7 +356,7 @@ typedef struct{

#define AKSK_LENGTH 256
#define CONF_STRING_LENTH 64
#define COMMAND_NUM 55
#define COMMAND_NUM 56
#define DATAMAN_COMMAND_NUM 17
#define COMMAND_STRING_LENGTH_MAX 64
#define SUBCMD_STRING_LENGTH_MAX 32
Expand Down
2 changes: 1 addition & 1 deletion installer/installer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef INSTALLER_H
#define INSTALLER_H

#define INSTALLER_VERSION_CODE "0.3.1.0145"
#define INSTALLER_VERSION_CODE "0.3.1.0147"
#define DEFAULT_URL_HPCOPR_LATEST "https://hpc-now-1308065454.cos.accelerate.myqcloud.com/hpcopr-0.3.x/"
#define URL_MSRDP_FOR_MAC "https://hpc-now-1308065454.cos.accelerate.myqcloud.com/packages/rdp_for_mac.zip"

Expand Down

0 comments on commit b56b1b6

Please sign in to comment.