You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
9.1KB

  1. #' Set the domain
  2. #'
  3. #' Set the domain id and domain name of the instance
  4. #'
  5. #' Set the domain id and domain name in the databases
  6. #' and set the domain name in the webclient'
  7. #'
  8. #' @param domain_id The desired domain_id
  9. #' @param domain_name The desired domain_name
  10. #' @param host The host to connect to
  11. #' @param admin Name of the database admin account
  12. #' @param pass Password of the database admin account
  13. #' @export
  14. set_domain <- function(domain_id, domain_name, host = "", admin = "", pass = "")
  15. {
  16. # Connect to the db
  17. hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2hive", user = admin, password = pass)
  18. pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2pm", user = admin, password = pass)
  19. # Set the domain id to all cells in i2b2hive
  20. c("crc", "im", "ont", "work") %>%
  21. purrr::walk(~RPostgreSQL::dbGetQuery(hive, stringr::str_c("UPDATE ", .x, "_db_lookup SET c_domain_id = '", domain_id, "';")))
  22. # Set the domain id and name in pm_hive_data
  23. RPostgreSQL::dbGetQuery(pm, stringr::str_c("UPDATE pm_hive_data SET domain_id = '", domain_id, "', domain_name = '", domain_id, "';"))
  24. # Set the domain name for the webclient
  25. "/var/www/html/webclient/i2b2_config_data.js" %>%
  26. readLines %>%
  27. stringr::str_c(collapse = "\n") %>%
  28. stringr::str_replace("domain: *\"[^\"]+\"", stringr::str_c("domain: \"", domain_id, "\"")) %>%
  29. stringr::str_replace("name: *\"[^\"]+\"", stringr::str_c("name: \"", domain_name, "\"")) %>%
  30. write(file = "/var/www/html/webclient/i2b2_config_data.js")
  31. # Disconnect the db
  32. RPostgreSQL::dbDisconnect(hive)
  33. RPostgreSQL::dbDisconnect(pm)
  34. }
  35. #' Get the domain
  36. #'
  37. #' Get the domain id of the instance
  38. #'
  39. #' @param host The host to connect to
  40. #' @param admin Name of the database admin account
  41. #' @param pass Password of the database admin account
  42. #' @return The domain id and name
  43. #' @export
  44. get_domain <- function(host = "", admin = "", pass = "")
  45. {
  46. dplyr::src_postgres("i2b2pm", host = host, user = admin, password = pass) %>%
  47. dplyr::tbl("pm_hive_data") %>%
  48. dplyr::collect()
  49. }
  50. #' Add a project
  51. #'
  52. #' Add a project with its id, path and name in the hive
  53. #'
  54. #' Also create a specific database for this project
  55. #'
  56. #' @param project_id The desired project id
  57. #' @param project_name The desired project name
  58. #' @param host Address of the host, defaults to 127.0.0.1
  59. #' @param admin Name of the database admin account
  60. #' @param pass Password of the database admin account
  61. #' @export
  62. add_project <- function(project_id, project_name, host = "", admin = "", pass = "")
  63. {
  64. # Get the current domain id
  65. domain_id <- get_domain(host, admin, pass)$domain_id
  66. # Connect to the db
  67. hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host, dbname = "i2b2hive", user = admin, password = pass)
  68. pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host, dbname = "i2b2pm", user = admin, password = pass)
  69. # Set the project id to all cells in i2b2hive
  70. RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO im_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '", project_id, "/', '@', 'public', 'java:/IMDemoDS', 'POSTGRESQL', 'IM');"))
  71. RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO ont_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '", project_id, "/', '@', 'public', 'java:/OntologyDemoDS', 'POSTGRESQL', 'Metadata');"))
  72. RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO work_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '", project_id, "/', '@', 'public', 'java:/WorkplaceDemoDS', 'POSTGRESQL', 'Workplace');"))
  73. RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO crc_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '/", project_id, "/', '@', 'public', 'java:/QueryTool", project_id, "DS', 'POSTGRESQL', '", project_name,"');"))
  74. # Set the project id and name in pm_hive_data
  75. RPostgreSQL::dbGetQuery(pm, stringr::str_c("INSERT INTO pm_project_data (project_id, project_name, project_wiki, project_path, status_cd) VALUES ('", project_id, "', '", project_name, "', 'http://www.i2b2.org', '/", project_id, "', 'A');"))
  76. RPostgreSQL::dbGetQuery(hive, stringr::str_c("CREATE DATABASE i2b2", project_id, "data WITH TEMPLATE i2b2demodata owner i2b2demodata;"))
  77. # Add the AGG_SERVICE_ACCOUNT user to the project
  78. RPostgreSQL::dbGetQuery(pm, stringr::str_c("INSERT INTO pm_project_user_roles (project_id, user_id, user_role_cd, status_cd) VALUES ('", project_id,"', 'AGG_SERVICE_ACCOUNT', 'USER', 'A'), ('", project_id,"', 'AGG_SERVICE_ACCOUNT', 'MANAGER', 'A'), ('", project_id,"', 'AGG_SERVICE_ACCOUNT', 'DATA_OBFSC', 'A'), ('", project_id,"', 'AGG_SERVICE_ACCOUNT', 'DATA_AGG', 'A');"))
  79. # Disconnect the db
  80. RPostgreSQL::dbDisconnect(hive)
  81. RPostgreSQL::dbDisconnect(pm)
  82. # crc-ds.xml
  83. xml2::read_html("/opt/wildfly-10.0.0.Final/standalone/deployments/crc-ds.xml") %>%
  84. rvest::xml_nodes("datasource") %>%
  85. xml2::as_list() -> crc
  86. datasource <- list()
  87. datasource$datasource$`connection-url` <- list(stringr::str_c("jdbc:postgresql://localhost:5432/i2b2", stringr::str_to_lower(project_id), "data"))
  88. datasource$datasource$`driver-class` <- list("org.postgresql.Driver")
  89. datasource$datasource$driver <- list("postgresql-9.2-1002.jdbc4.jar")
  90. datasource$datasource$security$`user-name` <- list("i2b2demodata")
  91. datasource$datasource$security$password <- list("demouser")
  92. datasource$datasource$validation$`validate-on-match` <- list("false")
  93. datasource$datasource$validation$`background-validation` <- list("false")
  94. datasource$datasource$statement$`share-prepared-statements` <- list("false")
  95. attr(datasource$datasource, "jta") <- "false"
  96. attr(datasource$datasource, "jndi-name") <- stringr::str_c("java:/QueryTool", project_id, "DS")
  97. attr(datasource$datasource, "pool-name") <- stringr::str_c("QueryTool", project_id, "DS")
  98. attr(datasource$datasource, "enabled") <- "true"
  99. attr(datasource$datasource, "use-ccm") <- "false"
  100. c(crc, datasource) %>%
  101. stats::setNames(rep("datasource", length(.))) -> crc
  102. new <- list(datasources = crc)
  103. attr(new$datasources, "xmlns") <- "http://www.jboss.org/ironjacamar/schema"
  104. xml2::write_xml(new %>% xml2::as_xml_document(), "/opt/wildfly-10.0.0.Final/standalone/deployments/crc-ds.xml")
  105. # Restart wildfly
  106. service("jboss", "restart")
  107. }
  108. #' List projects
  109. #'
  110. #' List available projects in the hive
  111. #'
  112. #' @param host Address of the hostdefaults to 127.0.0.1
  113. #' @param admin Name of the database admin account
  114. #' @param pass Password of the database admin account
  115. #' @return The list of projects
  116. #' @export
  117. list_projects <- function(host = "", admin = "", pass = "")
  118. {
  119. dplyr::src_postgres(dbname = "i2b2pm", host = host, user = admin, password = pass) %>%
  120. dplyr::tbl("pm_project_data") %>%
  121. dplyr::collect()
  122. }
  123. #' Delete a project
  124. #'
  125. #' Delete a project from the hive
  126. #'
  127. #' @param project_id The project to delete
  128. #' @param host Address of the host, defaults to 127.0.0.1
  129. #' @param admin Name of the database admin account
  130. #' @param pass Password of the database admin account
  131. #' @export
  132. delete_project <- function(project_id, host = "", admin = "", pass = "")
  133. {
  134. # Restart postgresql
  135. service("pg", "restart")
  136. # Connect to the db
  137. hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2hive", user = admin, password = pass)
  138. pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host, dbname = "i2b2pm", user = admin, password = pass)
  139. # Delete the connection lookups in i2b2hive
  140. c("im", "ont", "work") %>%
  141. purrr::walk(~RPostgreSQL::dbGetQuery(hive, stringr::str_c("DELETE FROM ", .x, "_db_lookup WHERE c_project_path = '", project_id, "/';")))
  142. RPostgreSQL::dbGetQuery(hive, stringr::str_c("DELETE FROM crc_db_lookup WHERE c_project_path = '/", project_id, "/';"))
  143. # Delete the project reference in pm_project_data
  144. RPostgreSQL::dbGetQuery(pm, stringr::str_c("DELETE FROM pm_project_data WHERE project_id = '", project_id, "';"))
  145. # Delete the crc database
  146. RPostgreSQL::dbGetQuery(pm, stringr::str_c("DROP DATABASE i2b2", project_id, "data;"))
  147. # Delete user roles for this project
  148. RPostgreSQL::dbGetQuery(pm, stringr::str_c("DELETE FROM pm_project_user_roles WHERE project_id = '", project_id, "';"))
  149. # Disconnect the db
  150. RPostgreSQL::dbDisconnect(hive)
  151. RPostgreSQL::dbDisconnect(pm)
  152. # Change crc-ds.xml
  153. xml2::read_html("/opt/wildfly-10.0.0.Final/standalone/deployments/crc-ds.xml") %>%
  154. rvest::xml_nodes(stringr::str_c("datasource[jndi-name!='java:/QueryTool", project_id, "DS']")) %>%
  155. xml2::as_list() %>%
  156. stats::setNames(rep("datasource", length(.))) ->
  157. datasources
  158. new <- list(datasources = datasources)
  159. attr(new$datasources, "xmlns") <- "http://www.jboss.org/ironjacamar/schema"
  160. xml2::write_xml(new %>% xml2::as_xml_document(), "/opt/wildfly-10.0.0.Final/standalone/deployments/crc-ds.xml")
  161. # Restart wildfly
  162. service("jboss", "restart")
  163. }