OSDN Git Service

adds MySQL support.
[metasearch/grid-chef-repo.git] / cookbooks / screwdriver / recipes / docker-compose.rb
index 22df17a..dcf1195 100644 (file)
@@ -111,7 +111,6 @@ else
     api_port = (elms.size == 2 ? elms[0] : elms[1]) if elms.last == api_in_port
   }
 end
-api_vols.push("#{data_dir}:/sd-data:rw")
 
 override_api_config['executor'] = default_executor if override_api_config['executor'].empty?
 
@@ -197,7 +196,7 @@ node['screwdriver']['api']['scms_vault_items'].each {|scm, props|
   }
 }
 =begin
-# DEPRECATED!!
+# **DEPRECATED!!**
 oauth_client_id = nil
 oauth_client_id_vault_item = node['screwdriver']['docker-compose']['oauth_client_id_vault_item']
 unless oauth_client_id_vault_item.empty?
@@ -220,6 +219,60 @@ unless webhook_github_secret_vault_item.empty?
 end
 =end
 
+db_username = nil
+db_username_vault_item = node['screwdriver']['db_username_vault_item']
+unless db_username_vault_item.empty?
+  db_username = get_vault_item_value(db_username_vault_item)
+  api_envs['DATASTORE_SEQUELIZE_USERNAME'] = '${DB_USERNAME}'
+end
+
+db_password = nil
+db_password_vault_item = node['screwdriver']['db_password_vault_item']
+unless db_password_vault_item.empty?
+  db_password = get_vault_item_value(db_password_vault_item)
+  api_envs['DATASTORE_SEQUELIZE_PASSWORD'] = '${DB_PASSWORD}'
+end
+
+db_root_password = nil
+db_root_password_vault_item = node['screwdriver']['db_root_password_vault_item']
+unless db_root_password_vault_item.empty?
+  db_root_password = get_vault_item_value(db_root_password_vault_item)
+end
+
+db_dialect = api_envs_org['DATASTORE_SEQUELIZE_DIALECT']
+case db_dialect
+when 'sqlite'
+  api_vols.push("#{data_dir}:/sd-data:rw")
+  api_envs['DATASTORE_SEQUELIZE_STORAGE'] = '/sd-data/storage.db'
+when 'mysql'
+  override_config_srvs['api']['links'] = ['db']
+  api_envs['DATASTORE_SEQUELIZE_HOST'] = 'db'
+end
+
+# db
+if db_dialect != 'sqlite'
+  #db_envs_org = config_srvs['db']['environment']
+  db_envs = {}
+  db_vols = config_srvs['db']['volumes'].to_a
+
+  case db_dialect
+  when 'mysql'
+    mysql_data_dir = "#{data_dir}/mysql"
+    resources(directory: mysql_data_dir) rescue directory mysql_data_dir do
+      owner 'root'
+      group 'root'
+      mode '0755'
+      recursive true
+    end
+
+    db_envs['MYSQL_DATABASE'] = api_envs_org['DATASTORE_SEQUELIZE_DATABASE']
+    db_envs['MYSQL_USER'] = '${DB_USERNAME}' unless db_username.nil?
+    db_envs['MYSQL_PASSWORD'] = '${DB_PASSWORD}' unless db_password.nil?
+    db_envs['MYSQL_ROOT_PASSWORD'] = '${DB_ROOT_PASSWORD}' unless db_root_password.nil?
+    db_vols.push("#{mysql_data_dir}:/var/lib/mysql:rw")
+  end
+end
+
 # ui
 #ui_envs_org = config_srvs['ui']['environment']
 ui_envs = {}
@@ -397,10 +450,16 @@ end
 force_override_config_srvs['api']['environment'] = api_envs unless api_envs.empty?
 force_override_config_srvs['ui']['environment'] = ui_envs unless ui_envs.empty?
 force_override_config_srvs['store']['environment'] = store_envs unless store_envs.empty?
+if db_dialect != 'sqlite'
+  force_override_config_srvs['db']['environment'] = db_envs unless db_envs.empty?
+end
 # reset vlumes array.
 override_config_srvs['api']['volumes'] = api_vols unless api_vols.empty?
 override_config_srvs['ui']['volumes'] = ui_vols unless ui_vols.empty?
 override_config_srvs['store']['volumes'] = store_vols unless store_vols.empty?
+if db_dialect != 'sqlite'
+  override_config_srvs['db']['volumes'] = db_vols unless db_vols.empty?
+end
 
 template env_file do
   source 'opt/docker-compose/app/screwdriver/.env'
@@ -411,11 +470,15 @@ template env_file do
   # prevent Chef from logging password attribute value.
   variables(
     # secrets
+    cookie_password: cookie_password,
+    password: password,
+    db_username: db_username,
+    db_password: db_password,
+    db_root_password: db_root_password,
+    # **DEPRECATED!!**
     # JWT keys setting -> /config/local.yaml
     #jwt_private_key: jwt_private_key,
     #jwt_public_key: jwt_public_key,
-    cookie_password: cookie_password,
-    password: password,
     # SCM secrets setting -> /config/local.yaml
     #oauth_client_id: oauth_client_id,
     #oauth_client_secret: oauth_client_secret,