forked from thomasblanc/ocp-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managerRestore.ml
70 lines (59 loc) · 2.22 KB
/
managerRestore.ml
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
open ManagerMisc
open ManagerWrapper
open ManagerInit
let install_prefix = "/usr/"
let libdir = Filename.concat install_prefix "lib/ocp-manager"
let bindir = Filename.concat install_prefix "bin"
let distrib_dir = Filename.concat libdir "distrib"
let list_of_binaries_filename = Filename.concat libdir "binaries.txt"
let manager_binary_filename = Filename.concat libdir basename
let load_binaries () =
ManagerMisc.lines_of_file list_of_binaries_filename
let save_binaries binaries =
let oc = open_out list_of_binaries_filename in
List.iter (fun s -> Printf.fprintf oc "%s\n" s) binaries;
close_out oc
let manage_binary binary =
let binary_filename = Filename.concat bindir binary in
if Sys.file_exists binary_filename then begin
Printf.fprintf stderr "Saving executable %s to %s\n%!"
binary_filename (Filename.concat distrib_dir binary);
Sys.rename binary_filename (Filename.concat distrib_dir binary);
end;
if not (Sys.file_exists binary_filename) then begin
Printf.fprintf stderr "Creating stub executable %s\n%!" binary_filename;
symlink "ocp-manager" binary_filename;
end;
Printf.fprintf stderr "%s is now managed\n%!" binary
let arg_handler () =
if not (Sys.file_exists distrib_dir) then begin
Printf.fprintf stderr "ocp-manager not in control !l\n%!";
exit 2;
end;
let binaries = ManagerMisc.lines_of_file list_of_binaries_filename in
List.iter (fun binary ->
let binary_filename = Filename.concat distrib_dir binary in
let target_filename = Filename.concat bindir binary in
begin
try
let st = MinUnix.lstat target_filename in
match st.MinUnix.st_kind with
MinUnix.S_LNK ->
Printf.fprintf stderr "Removing stub executable %s\n%!" target_filename;
(try
Sys.remove target_filename
with e ->
Printf.fprintf stderr "\tError: %s\n%!" (Printexc.to_string e)
)
| _ -> ()
with _ -> ()
end;
if Sys.file_exists binary_filename then begin
Printf.fprintf stderr "Restoring executable %s to %s\n%!"
binary_filename target_filename;
(try Sys.rename binary_filename target_filename with e ->
Printf.fprintf stderr "\tError: %s\n%!" (Printexc.to_string e)
);
end
) binaries;
MinUnix.rmdir distrib_dir;