Blame view

Serveur/main.c 8.92 KB
fb2a766e   Antoine Duquenoy   Programme C serve...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
144
145
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
  #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"
  #define FILES_PATH      "/home/antoine/pfe/files/"
  #define TOFLASH_PATH    "./toflash/"
  
  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;
      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);
  
      struct tm tm = { 0 };
      strptime(experiment->date, "%Y-%m-%dT%H:M", &tm);
      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...
208
          sprintf(cmd1, "runuser -l %s -c \'echo \"%s\" | ssh pi@%s \"%s && cat > ~/exp_%d/config_experimentation.txt\"\'", 
fb2a766e   Antoine Duquenoy   Programme C serve...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
              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];
11329db5   Antoine Duquenoy   Ansible terminé e...
227
      sprintf(ansible_command, "runuser -l %s -c \'ansible-playbook /home/antoine/pfe/ansible.yml -i %s -e \"servernames=exp_%d exp_duration=%d binary_dir=%s\"\'",
fb2a766e   Antoine Duquenoy   Programme C serve...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
          USERNAME,
          hosts_filename,
          experiment->exp_id,
          time_sec,
          experiment->dir);
      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,
          experiment->name,
          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);
      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...
278
              if((dirent->d_type == DT_REG) && (strstr(dirent->d_name, ".started") == NULL)) {
fb2a766e   Antoine Duquenoy   Programme C serve...
279
                  char file_name[128];
11329db5   Antoine Duquenoy   Ansible terminé e...
280
  				char new_file_name[128];
fb2a766e   Antoine Duquenoy   Programme C serve...
281
282
283
284
285
286
287
                  strcpy(file_name, TOFLASH_PATH);
                  strcat(file_name, dirent->d_name);
                  Experiment *experiment = read_file(file_name);
  #ifdef DEBUG                
                  print_exp(experiment);
  #endif
                  if(!process_exp(experiment)) {
11329db5   Antoine Duquenoy   Ansible terminé e...
288
289
290
  					strcpy(new_file_name, file_name);
  					strcat(new_file_name, ".started");
                      rename(file_name, new_file_name);
fb2a766e   Antoine Duquenoy   Programme C serve...
291
292
293
294
295
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
                  }
                  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...
326
  }