Blame view

Serveur/main.c 9.12 KB
fb2a766e   Antoine Duquenoy   Programme C serve...
1
2
3
4
5
6
7
8
9
10
11
12
  #include <stdio.h>
  #include <time.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <string.h>
  #include <dirent.h>
  #include <sys/types.h>
  
  #define MAX_NODES       128
  #define MAX_SENSORS     8
  #define MAX_BUFFER      1024
  #define USERNAME        "antoine"
76cabcde   Antoine Duquenoy   Correction Ansibl...
13
14
15
  #define FILES_PATH      "/home/antoine/PFE_DEMO/files/"
  #define TOFLASH_PATH    "/home/antoine/PFE_DEMO/toflash/"
  #define PLAYBOOK_PATH	"/home/antoine/PFE_DEMO/ansible.yml"
fb2a766e   Antoine Duquenoy   Programme C serve...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
  typedef struct node {
      char *ip;
      char *sensors[MAX_SENSORS];
      int n_sensors;
  } Node;
  
  typedef struct experiment {
      int exp_id;
      int n_nodes;
      char *date;
      char *file;
      char *duration;
      char *name;
      char *arch;
      char *dir;
76cabcde   Antoine Duquenoy   Correction Ansibl...
32
  	char *exp_name;
fb2a766e   Antoine Duquenoy   Programme C serve...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      Node nodes[MAX_NODES];
  } Experiment;
  
  void process_line(char *line, Experiment *experiment) {
      char *key;
      char *value;
      char *save_ptr;
      key = strtok_r(line, ":", &save_ptr);
      if(!strcmp(key, "date")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->date = strdup(value + 1);
      }
      else if(!strcmp(key, "name")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->name = strdup(value + 1);       
      }
      else if(!strcmp(key, "dir")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->dir = strdup(value + 1);       
      }
      else if(!strcmp(key, "arch")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->arch = strdup(value + 1);       
      }
      else if(!strcmp(key, "exp_id")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->exp_id = atoi(value + 1);   
      }
      else if(!strcmp(key, "time")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->duration = strdup(value + 1);
      }        
      else if(!strcmp(key, "file")) {
          value = strtok_r(NULL, "\n", &save_ptr);
          experiment->file = strdup(value + 1);
      }
      else if(!strcmp(key, "nodes")) {
          value = strtok_r(NULL, "\n", &save_ptr) + 2;
          char *nodes_list = strtok_r(value, "]", &save_ptr);
          char *node;
          char *save_ptr_node;
          int present = 0;
  
          node = strtok_r(nodes_list, ",", &save_ptr);
  
          while(node != NULL) {
              char *sensor = strtok_r(node, "@", &save_ptr_node);
              char *ip = strtok_r(NULL, "@", &save_ptr_node);
  
              if(experiment->n_nodes == 0) {
                  Node n;
                  n.n_sensors = 1;
                  n.ip = strdup(ip);
                  n.sensors[0] = strdup(sensor);
                  experiment->nodes[0] = n;
                  experiment->n_nodes++;
              }
  
              else {
                  for(int i = 0; i < experiment->n_nodes; i++) {
                      if(!strcmp(experiment->nodes[i].ip, ip)) {
                          int nb = experiment->nodes[i].n_sensors;
                          experiment->nodes[i].sensors[nb] = strdup(sensor);
                          experiment->nodes[i].n_sensors++;
                          present = 1;
                      }
                  }
                  if(!present) {
                      Node n;
                      n.n_sensors = 1;
                      n.ip = strdup(ip);
                      n.sensors[0] = strdup(sensor);
                      experiment->nodes[experiment->n_nodes] = n;
                      experiment->n_nodes++;
                  }
                  present = 0;
              }
  
              node = strtok_r(NULL, ",", &save_ptr);
              if(node != NULL && *node == ' ') {
                  node++;
              }
          }
      }
  }
  
  Experiment *read_file(char *file_name) {
      char ch;
      char buffer[MAX_BUFFER];
      FILE *fp;
      Experiment *experiment = (Experiment*) malloc(sizeof(Experiment));
      experiment->n_nodes = 0;
  
      fp = fopen(file_name, "r");
  
      if(fp == NULL) {
          perror("Error while opening the file.\n");
          exit(EXIT_FAILURE);
      }
  
      while(fgets(buffer, MAX_BUFFER, fp) != NULL) {
          process_line(buffer, experiment);
      }
  
      fclose(fp);
      return experiment;
  }
  
  int check_date(Experiment *experiment) {
      time_t now;
      time(&now);
fb2a766e   Antoine Duquenoy   Programme C serve...
144
      struct tm tm = { 0 };
76cabcde   Antoine Duquenoy   Correction Ansibl...
145
      strptime(experiment->date, "%Y-%m-%dT%H:%M", &tm);
fb2a766e   Antoine Duquenoy   Programme C serve...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
      time_t t = mktime(&tm);
  
      if(t <= now) {
          printf("Starting deployment of \"[%d] %s\" ...\n", experiment->exp_id, experiment->name);
          return 0;
      }
      return 1;
  }
  
  int process_time(Experiment *experiment) {
      char duration[8];
      strcpy(duration, experiment->duration);
      char *hour;
      char *min;
      hour = strtok(duration, ":");
      min = strtok(NULL, "\0");
      return atoi(hour) * 3600 + atoi(min) * 60;
  }
  
  int binary_not_present(Experiment *experiment) {
      char path[512];
      strcpy(path, FILES_PATH);
      strcat(path, experiment->dir);
      strcat(path, "/out.bin");
      FILE *file;
      if(file = fopen(path, "r")) {
          fclose(file);
          return 0;
      }
      return 1;
  }
  
  int process_exp(Experiment *experiment) {
      if(check_date(experiment)) {
          return 1;
      }
  
      char docker_cmd[1024];
  
      if(!strcmp(experiment->arch, "arm")) {
          if(binary_not_present(experiment)) {
              sprintf(docker_cmd, "docker run --rm -v %s:/exp -e SRC_NAME=\"%s\" -e SRC_DIR=\"%s\" arm_compiler",
                  FILES_PATH,
                  experiment->file,
                  experiment->dir
              );
              exec_prog(docker_cmd);
          }
      }
  
      int time_sec = process_time(experiment);
      char cmd1[1024];
      char cmd2[1024];
      char sensors_list[512];
      strcpy(sensors_list, "");
      for(int i = 0; i < experiment->n_nodes; i++) {
          for(int j = 0; j < experiment->nodes[i].n_sensors; j++) {
              strcat(sensors_list, experiment->nodes[i].sensors[j]);
              if(j < experiment->nodes[i].n_sensors - 1) {
                  strcat(sensors_list, "\n");
              }
          }
          sprintf(cmd2, "mkdir ~/exp_%d", experiment->exp_id);
11329db5   Antoine Duquenoy   Ansible terminé e...
209
          sprintf(cmd1, "runuser -l %s -c \'echo \"%s\" | ssh pi@%s \"%s && cat > ~/exp_%d/config_experimentation.txt\"\'", 
fb2a766e   Antoine Duquenoy   Programme C serve...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
              USERNAME,
              sensors_list, 
              experiment->nodes[i].ip, 
              cmd2, 
              experiment->exp_id);
          exec_prog(cmd1);
      }
  
      char hosts_filename[64];
      sprintf(hosts_filename, "/etc/ansible/exp_%d.hosts", experiment->exp_id);
      FILE *hosts_fp = fopen(hosts_filename, "w");
      fprintf(hosts_fp, "[exp_%d]\n", experiment->exp_id);
      for(int i = 0; i < experiment->n_nodes; i++) {
          fprintf(hosts_fp, "%s ansible_user=pi\n", experiment->nodes[i].ip);
      }
      fclose(hosts_fp);
  
      char ansible_command[512];
76cabcde   Antoine Duquenoy   Correction Ansibl...
228
      sprintf(ansible_command, "runuser -l %s -c \'ansible-playbook %s -i %s -e \"servernames=exp_%d exp_duration=%d binary_dir=%s exp_name=%s\"\'",
fb2a766e   Antoine Duquenoy   Programme C serve...
229
          USERNAME,
76cabcde   Antoine Duquenoy   Correction Ansibl...
230
  	PLAYBOOK_PATH,
fb2a766e   Antoine Duquenoy   Programme C serve...
231
232
233
          hosts_filename,
          experiment->exp_id,
          time_sec,
76cabcde   Antoine Duquenoy   Correction Ansibl...
234
235
          experiment->dir,
  		experiment->exp_name);
fb2a766e   Antoine Duquenoy   Programme C serve...
236
237
238
239
240
241
242
243
      exec_prog_background(ansible_command);
  
      return 0;
  }
  
  void print_exp(Experiment *experiment) {
      printf("== EXP[%d] : %s ==\n%s during %s with the file %s (%s) on the %d nodes :\n",
          experiment->exp_id,
76cabcde   Antoine Duquenoy   Correction Ansibl...
244
          experiment->exp_name,
fb2a766e   Antoine Duquenoy   Programme C serve...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
          experiment->date,
          experiment->duration,
          experiment->file,
          experiment->arch,
          experiment->n_nodes);
      for(int i = 0; i < experiment->n_nodes; i++) {
          printf("* %s\n", experiment->nodes[i].ip);
          for(int j = 0; j < experiment->nodes[i].n_sensors; j++) {
              printf("    - %s\n", experiment->nodes[i].sensors[j]);
          }
      }
  }
  
  void free_exp(Experiment *experiment) {
      free(experiment->date);
      free(experiment->duration);
      free(experiment->file);
      free(experiment->arch);
      free(experiment->dir);
      free(experiment->name);
76cabcde   Antoine Duquenoy   Correction Ansibl...
265
  	free(experiment->exp_name);
fb2a766e   Antoine Duquenoy   Programme C serve...
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
      for(int i = 0; i < experiment->n_nodes; i++) {
          for(int j = 0; j < experiment->nodes[i].n_sensors; j++) {
              free(experiment->nodes[i].sensors[j]);
          }
          free(experiment->nodes[i].ip);
      }
      free(experiment);
  }
  
  void check_files() {
      DIR *dir;
      struct dirent *dirent;
      dir = opendir(TOFLASH_PATH);
  
      if(dir != NULL) {
          while(dirent = readdir(dir)) {
11329db5   Antoine Duquenoy   Ansible terminé e...
282
              if((dirent->d_type == DT_REG) && (strstr(dirent->d_name, ".started") == NULL)) {
fb2a766e   Antoine Duquenoy   Programme C serve...
283
                  char file_name[128];
11329db5   Antoine Duquenoy   Ansible terminé e...
284
  				char new_file_name[128];
fb2a766e   Antoine Duquenoy   Programme C serve...
285
286
287
                  strcpy(file_name, TOFLASH_PATH);
                  strcat(file_name, dirent->d_name);
                  Experiment *experiment = read_file(file_name);
76cabcde   Antoine Duquenoy   Correction Ansibl...
288
  				experiment->exp_name = strdup(dirent->d_name);
fb2a766e   Antoine Duquenoy   Programme C serve...
289
290
291
292
  #ifdef DEBUG                
                  print_exp(experiment);
  #endif
                  if(!process_exp(experiment)) {
11329db5   Antoine Duquenoy   Ansible terminé e...
293
294
295
  					strcpy(new_file_name, file_name);
  					strcat(new_file_name, ".started");
                      rename(file_name, new_file_name);
fb2a766e   Antoine Duquenoy   Programme C serve...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
                  }
                  free_exp(experiment);
              }
          }
          closedir(dir);
      }
      else {
          perror("Dossier toflash non trouvable");
          exit(EXIT_FAILURE);
      }
  
      return;
  }
  
  int exec_prog(char *cmd) {
      if(system(cmd) == -1) {
          return 1;
      }
      return 0;
  }
  
  int exec_prog_background(char *cmd) {
      if(!fork()) {
          system(cmd);
          return 0;
      }
      return 1;
  }
  
  int main(int argc, char **argv) {
      while(1) {
          check_files();
          sleep(10);
      }
      return 0;
11329db5   Antoine Duquenoy   Ansible terminé e...
331
  }