private static void ValidateDeploymentConfig(DeploymentConfig config) { if (string.IsNullOrEmpty(config.DeploymentPath)) { throw new ArgumentException("DeploymentPath must not be empty in deployment config."); } System.IO.FileInfo fi = null; try { fi = new System.IO.FileInfo(config.DeploymentPath); } catch (ArgumentException) { } catch (System.IO.PathTooLongException) { } catch (NotSupportedException) { } if (ReferenceEquals(fi, null)) { throw new ArgumentException("DeploymentPath is not a legal path. Did u use double '\\'?"); } else { } if (config.IsUpdate) { return; } if (string.IsNullOrEmpty(config.Username)) { throw new ArgumentException("Username must not be empty in deployment config."); } if (string.IsNullOrEmpty(config.Password)) { throw new ArgumentException("Password must not be empty in deployment config."); } }
public void DeployWebApi(ITaskContext context) { DeploymentConfig config = null; var json = File.ReadAllText("DeploymentConfig.json"); config = JsonConvert.DeserializeObject <DeploymentConfig>(json); ValidateDeploymentConfig(config); string connectionString; if (!string.IsNullOrWhiteSpace(config.LiteDbConnectionString)) { connectionString = config.LiteDbConnectionString; } else { var liteDbPassword = GenerateRandomSecureString(15); connectionString = $"FileName=database.db; Password={liteDbPassword}"; } bool createDb = false; var dbFileName = Files.GetFileNameFromConnectionString(connectionString); var isPathRooted = Path.IsPathRooted(dbFileName); if (!config.IsUpdate) { if (config.RecreateDatabase) { createDb = true; } else { if (isPathRooted) { if (!File.Exists(dbFileName)) { createDb = true; } } else { var dbPath = Path.Combine(config.DeploymentPath, dbFileName); if (!File.Exists(dbPath)) { createDb = true; } } } if (createDb) { File.Delete(dbFileName); using (var db = new LiteRepository(connectionString)) { IUserRepository repository = new UserRepository(db); var hashService = new HashService(); repository.AddUser(new User { Username = config.Username, Password = hashService.Hash(config.Password), }); } if (!isPathRooted) { var outputDbFilePath = Path.Combine("FlubuCore.WebApi", dbFileName); context.Tasks().CopyFileTask(dbFileName, outputDbFilePath, true) .Execute(context); } } else { ////Delete old db file if it exists so it doesnt rewrite database at deployed location. var outputDbFilePath = Path.Combine("FlubuCore.WebApi", dbFileName); if (File.Exists(outputDbFilePath)) { File.Delete(outputDbFilePath); } } context.Tasks().UpdateJsonFileTask(@".\FlubuCore.WebApi\appsettings.json") .Update(new KeyValuePair <string, JValue>("FlubuConnectionStrings.LiteDbConnectionString", new JValue(connectionString))).Execute(context); context.Tasks().UpdateJsonFileTask(@".\FlubuCore.WebApi\appsettings.json") .Update(new KeyValuePair <string, JValue>("WebApiSettings.AllowScriptUpload", new JValue(config.AllowScriptUpload))).Execute(context); context.Tasks().UpdateJsonFileTask(@".\FlubuCore.WebApi\appsettings.json") .Update("JwtOptions.SecretKey", GenerateRandomString(30)).Execute(context); context.Tasks().CreateDirectoryTask(config.DeploymentPath + "\\Packages", false).Execute(context); context.Tasks().CreateDirectoryTask(config.DeploymentPath + "\\Scripts", false).Execute(context); } else { ////Delete old db file if it exists so it doesnt rewrite database at deployed location. var outputDbFilePath = Path.Combine("FlubuCore.WebApi", dbFileName); if (File.Exists(outputDbFilePath)) { File.Delete(outputDbFilePath); } if (File.Exists("FlubuCore.WebApi/appsettings.json")) { File.Delete("FlubuCore.WebApi/appsettings.json"); } if (File.Exists("FlubuCore.WebApi/web.config")) { File.Delete("FlubuCore.WebApi/web.config"); } } context.Tasks().CopyDirectoryStructureTask("FlubuCore.Webapi", config.DeploymentPath, true) .Execute(context); }