let sync_lock = Mutex.create ()
let sync_host ~__context host =
- Mutex.execute sync_lock (fun () ->
- try
- let localhost = host = !Xapi_globs.localhost_ref in
- let host_has_storage = not (List.mem_assoc Xapi_globs.host_no_local_storage (Db.Host.get_other_config ~__context ~self:host)) in
- if (not localhost) && host_has_storage then begin
- let address = Db.Host.get_address ~__context ~self:host in
- debug "Beginning sync with host at address: %s" address;
- let localpath = Printf.sprintf "%s/" Xapi_globs.xapi_blob_location in
- let remotepath = Printf.sprintf "%s:%s" address Xapi_globs.xapi_blob_location in
- let session = Xapi_session.slave_login ~__context ~host:(Helpers.get_localhost ~__context) ~psecret:!Xapi_globs.pool_secret in
- Unix.putenv "XSH_SESSION" (Ref.string_of session);
- let env = Unix.environment () in
- let output,log = Forkhelpers.execute_command_get_output ~env "/usr/bin/rsync" ["--delete";"-avz";localpath;remotepath;"-e";"/opt/xensource/bin/xsh"] in
- debug "sync output: '%s' log: '%s'" output log;
- (* Store the last blob sync time in the Host.other_config *)
- (try Db.Host.remove_from_other_config ~__context ~self:host ~key:Xapi_globs.last_blob_sync_time with _ -> ());
- Db.Host.add_to_other_config ~__context ~self:host ~key:Xapi_globs.last_blob_sync_time ~value:(string_of_float (Unix.gettimeofday ()));
- end else begin
- debug "Ignoring host synchronise: localhost=%b host_has_storage=%b" localhost host_has_storage
- end;
- with Forkhelpers.Spawn_internal_error(log,output,status) ->
- (* Do we think the host is supposed to be online? *)
- let online = try let m = Db.Host.get_metrics ~__context ~self:host in Db.Host_metrics.get_live ~__context ~self:m with _ -> false in
- (* In rolling upgrade mode we would also expect a failure *)
- let rolling_upgrade = Helpers.rolling_upgrade_in_progress ~__context in
- if online && not rolling_upgrade
- then error "Unexpected failure synchronising blobs to host %s; log='%s'; output='%s'" (Ref.string_of host) log output;
- )
+ Mutex.execute sync_lock (fun () ->
+ try
+ let localhost = host = !Xapi_globs.localhost_ref
+ and host_has_storage = not (List.mem_assoc Xapi_globs.host_no_local_storage (Db.Host.get_other_config ~__context ~self:host)) in
-let do_sync () =
- Server_helpers.exec_with_new_task "blob sync" (fun __context ->
- let hosts = Db.Host.get_all ~__context in
- List.iter (sync_host ~__context) hosts)
+ if (not localhost) && host_has_storage then begin
+ let address = Db.Host.get_address ~__context ~self:host in
+ debug "Beginning sync with host at address: %s" address;
+
+ let localpath = Printf.sprintf "%s/" Xapi_globs.xapi_blob_location
+ and remotepath = Printf.sprintf "%s:%s" address Xapi_globs.xapi_blob_location
+ and session = Xapi_session.slave_login ~__context ~host:(Helpers.get_localhost ~__context) ~psecret:!Xapi_globs.pool_secret in
+ Unix.putenv "XSH_SESSION" (Ref.string_of session);
+
+ let env = Unix.environment () in
+ let output,log = Forkhelpers.execute_command_get_output ~env "/usr/bin/rsync" ["--delete";"-avz";localpath;remotepath;"-e";"/opt/xensource/bin/xsh"] in
+ debug "sync output: '%s' log: '%s'" output log;
+
+ (* Store the last blob sync time in the Host.other_config *)
+ (try Db.Host.remove_from_other_config ~__context ~self:host ~key:Xapi_globs.last_blob_sync_time with _ -> ());
+ Db.Host.add_to_other_config ~__context ~self:host ~key:Xapi_globs.last_blob_sync_time ~value:(string_of_float (Unix.gettimeofday ()));
+ end
+ else begin
+ debug "Ignoring host synchronise: localhost=%b host_has_storage=%b" localhost host_has_storage
+ end;
+
+ with Forkhelpers.Spawn_internal_error(log,output,status) ->
+ (* Do we think the host is supposed to be online? *)
+ let online =
+ try
+ let m = Db.Host.get_metrics ~__context ~self:host in
+ Db.Host_metrics.get_live ~__context ~self:m
+ with _ -> false in
+
+ (* In rolling upgrade mode we would also expect a failure *)
+ let rolling_upgrade = Helpers.rolling_upgrade_in_progress ~__context in
+ if online && not rolling_upgrade
+ then error "Unexpected failure synchronising blobs to host %s; log='%s'; output='%s'" (Ref.string_of host) log output;
+ )
+
+let do_sync () =
+ Server_helpers.exec_with_new_task "blob sync" (fun __context ->
+ let hosts = Db.Host.get_all ~__context in
+ List.iter (sync_host ~__context) hosts)