private async Task PurgeObjectStorage(string path, int purgeLimitSec) { ObjectStorage o = new ObjectStorage( LogClient.Config.Instance.GetValue(LogClient.Category.Api, LogClient.Key.AccessKey) , LogClient.Config.Instance.GetValue(LogClient.Category.Api, LogClient.Key.SecretKey) , config.GetValue(Category.Backup, Key.ObjectStorageServiceUrl) ); var task = o.List(bucketName, path); var files = await task; foreach (var file in files) { DateTime.TryParseExact(file.LastWriteTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date); string filenameWithExtention = file.Name.Replace(".full", ""); string filename = filenameWithExtention.Replace(".log", ""); string[] splitfilename = filename.Split(new string[] { "__" }, StringSplitOptions.None); if (splitfilename.Count() == 3) { if (date < DateTime.Now.Add(new TimeSpan(0, 0, Math.Abs(purgeLimitSec) * -1))) { var task2 = o.DeleteFileAsync(bucketName, file.Name); await task2; } } } }
async Task IfNotExistsCreateTableInObject(TableName tableName, string filePath = "") { string bucketName = data.GetValue(Category.Backup, Key.BucketName); CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); ObjectStorage o = new ObjectStorage( LogClient.Config.Instance.GetValue(LogClient.Category.Api, LogClient.Key.AccessKey), LogClient.Config.Instance.GetValue(LogClient.Category.Api, LogClient.Key.SecretKey), data.GetValue(Category.Backup, Key.ObjectStorageServiceUrl)); string key = string.Empty; if (filePath.Trim().Length > 0) { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + filePath)) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + filePath); } key = filePath + @"/" + tableName.ToString(); } else { key = tableName.ToString(); } var fileListTask = o.List(bucketName, key + ".txt"); List <ObjectStorageFile> lists = await fileListTask; bool isFileEmpty = false; foreach (var a in lists) { if (a.Length == 0) { isFileEmpty = true; } } if (lists.Count() == 0 || isFileEmpty) { string json = "[]"; File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + key + ".txt"), json); await o.UploadObjectAsync(bucketName, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + key + ".txt"), key + ".txt", cancelTokenSource.Token, 0); } }
async Task IfNotExistsCreateTableInObject(TableName tableName) { string bucketName = data.GetValue(DataManager.Category.ObjectStorage, DataManager.Key.Bucket); CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); ObjectStorage o = new ObjectStorage( LogClient.Config.Instance.GetValue(Category.Api, Key.AccessKey), LogClient.Config.Instance.GetValue(Category.Api, Key.SecretKey), data.GetValue(DataManager.Category.ObjectStorage, DataManager.Key.Endpoint)); var fileListTask = o.List(bucketName, tableName.ToString()); List <ObjectStorageFile> lists = await fileListTask; if (lists.Count() == 0) { string json = "[]"; File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + tableName.ToString() + ".txt"), json); await o.UploadObjectAsync(bucketName, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + tableName.ToString() + ".txt"), tableName.ToString() + ".txt", cancelTokenSource.Token, 0); } }
private void CheckInitScript() { AppendVerifyLog("*. InitScript"); try { string bucketName = dataManager.GetValue(DataManager.Category.ObjectStorage, DataManager.Key.Bucket); string endPoint = dataManager.GetValue(DataManager.Category.ObjectStorage, DataManager.Key.Endpoint); string accessKey = logClientConfig.GetValue(LogClient.Category.Api, LogClient.Key.AccessKey); string secretKey = logClientConfig.GetValue(LogClient.Category.Api, LogClient.Key.SecretKey); string userData = dataManager.GetValue(DataManager.Category.InitScript, DataManager.Key.userDataFinal); string psFileName = dataManager.GetValue(DataManager.Category.InitScript, DataManager.Key.PsFileName); bool isExistsAgent = false; if (userData.Length < 1) { throw new Exception(" Init script not saved."); } WebClient client = new WebClient(); var contents = AsyncHelpers.RunSync <byte[]>(() => client.DownloadDataTaskAsync(string.Format("{0}/{1}/{2}", endPoint, bucketName, psFileName))); string psScript = Encoding.Default.GetString(contents); if (psScript.Length < 1) { new Exception(" Remote powershell script error"); } ObjectStorage o = new ObjectStorage(accessKey, secretKey, endPoint); var lists = AsyncHelpers.RunSync <List <ObjectStorageFile> >(() => o.List(bucketName, "Lazylog64.zip")); foreach (var a in lists) { if (a.Name.Equals("Lazylog64.zip", StringComparison.OrdinalIgnoreCase)) { AppendVerifyLog($" Agent file name : {a.Name}, size : {a.Length}, date : {a.LastWriteTime}"); isExistsAgent = true; } } if (!isExistsAgent) { new Exception(" [ERROR] Agent file does not exist in object storage."); } AppendVerifyLog($" Init Script Check Result : Success"); } catch (Exception ex) { AppendVerifyLog(ex.Message); AppendVerifyLog(" InitScript Help Message..."); AppendVerifyLog(" -----------------------------------------------"); AppendVerifyLog(" 1. Upload the initialization script in SQL Server DBA Tool > Config > Init Script"); AppendVerifyLog(" -----------------------------------------------"); throw new Exception("Init Script Error!"); } }