public override void DoJob() { if (string.IsNullOrEmpty(Application.Agent)) { Application.Agent = CommonRandom.CrcGuid(); } }
/// <summary> /// Verify folder /// </summary> /// <param name="node">Nodo del cluster di riferimento</param> /// <param name="folderPath">Percorso assoluto del file sul nodo di riferimento</param> /// <returns></returns> public static bool VerifyFolder(ClusterNode node, string folderPath) { var client = new RestClient(node.EntryPoint); var request = new RestRequest(verifyFolderServerPath, Method.GET); request.AddHeader("session-instance-guid", CommonRandom.CrcGuid()); request.AddHeader(HeaderMachineUidKey, CommonRandom.CrcGuid()); request.AddHeader(HeaderFolderPathKey, folderPath); var response = client.Execute(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { ConsoleLogger.Log($"[storage_client] unable to get remote folder: server error"); return(false); } if (string.IsNullOrEmpty(response.Content)) { ConsoleLogger.Log($"[storage_client] unable to get remote folder: empty result"); return(false); } ConsoleLogger.Log($"[storage_client] verify folder '{folderPath}'"); return(true); }
/// <summary> /// Create file /// Equivale a un upload /// </summary> /// <param name="sourcePath">Percorso assoluto del file sul nodo locale</param> /// <param name="node">Nodo del cluster di riferimento</param> /// <param name="destinationPath">Percorso assoluto del file sul nodo di riferimento</param> /// <returns></returns> public static bool CreateFile(ClusterNode node, string sourcePath, string destinationPath) { if (!File.Exists(sourcePath)) { ConsoleLogger.Log($"[storage_client] local file '{sourcePath}' not found"); return(false); } var client = new RestClient(node.EntryPoint); var request = new RestRequest(createFileServerPath, Method.POST); request.AddHeader("session-instance-guid", CommonRandom.CrcGuid()); request.AddHeader(HeaderMachineUidKey, CommonRandom.CrcGuid()); request.AddHeader(HeaderFilePathKey, destinationPath); var fileName = Path.GetFileName(sourcePath); var bytes = File.ReadAllBytes(sourcePath); request.AddFile(fileName, bytes, fileName); var response = client.Execute(request); var result = response.StatusCode; ConsoleLogger.Log($"[storage_client] create file '{destinationPath}'"); return(result == System.Net.HttpStatusCode.OK); }
/// <summary> /// Get file /// Equivale a un download /// </summary> /// <param name="node">Nodo del cluster di riferimento</param> /// <param name="filePath">Percorso assoluto del file sul nodo di riferimento</param> /// <param name="destinationPath">Percorso assoluto del file sul file system locale</param> /// <returns></returns> public static void GetFile(ClusterNode node, string filePath, string destinationPath) { using (var fileStream = new FileStream(filePath, FileMode.Create)) { using (var memoryStream = new MemoryStream()) { var client = new RestClient(node.EntryPoint); var request = new RestRequest(getFileServerPath, Method.GET) { //todo usa rsync ResponseWriter = (responseStream) => responseStream.CopyTo(fileStream) }; request.AddHeader("session-instance-guid", CommonRandom.CrcGuid()); request.AddHeader(HeaderMachineUidKey, CommonRandom.CrcGuid()); request.AddHeader(HeaderFilePathKey, filePath); client.DownloadData(request); int data; while ((data = memoryStream.ReadByte()) != -1) { memoryStream.WriteByte((byte)data); } } } ConsoleLogger.Log($"[storage_client] get file '{filePath}'"); }
/// <summary> /// Move file /// Sposta il file in un altra cartella /// Il nome file rimane lo stesso /// </summary> /// <param name="node">Nodo del cluster di riferimento</param> /// <param name="filePath">Percorso assoluto del file sul nodo di riferimento</param> /// <param name="newFolder">Nuova cartella</param> /// <returns></returns> public static bool MoveFile(ClusterNode node, string filePath, string newFolder) { var client = new RestClient(node.EntryPoint); var request = new RestRequest(moveFileServerPath, Method.PUT); request.AddHeader("session-instance-guid", CommonRandom.CrcGuid()); request.AddHeader(HeaderMachineUidKey, CommonRandom.CrcGuid()); request.AddHeader(HeaderFilePathKey, filePath); request.AddHeader(HeaderFileNewPathKey, newFolder); var response = client.Execute(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { ConsoleLogger.Log($"[storage_client] unable to get remote file: server error"); return(false); } if (string.IsNullOrEmpty(response.Content)) { ConsoleLogger.Log($"[storage_client] unable to get remote file: empty result"); return(false); } ConsoleLogger.Log($"[storage_client] move file '{filePath}' to '{newFolder}'"); return(true); }