diff options
Diffstat (limited to 'app/services/google_cloud')
5 files changed, 178 insertions, 19 deletions
diff --git a/app/services/google_cloud/base_service.rb b/app/services/google_cloud/base_service.rb index 016ab15408f..01aee2231c9 100644 --- a/app/services/google_cloud/base_service.rb +++ b/app/services/google_cloud/base_service.rb @@ -22,7 +22,7 @@ module GoogleCloud def unique_gcp_project_ids filter_params = { key: 'GCP_PROJECT_ID' } - ::Ci::VariablesFinder.new(project, filter_params).execute.map(&:value).uniq + @unique_gcp_project_ids ||= ::Ci::VariablesFinder.new(project, filter_params).execute.map(&:value).uniq end def group_vars_by_environment(keys) diff --git a/app/services/google_cloud/create_cloudsql_instance_service.rb b/app/services/google_cloud/create_cloudsql_instance_service.rb new file mode 100644 index 00000000000..f7fca277c52 --- /dev/null +++ b/app/services/google_cloud/create_cloudsql_instance_service.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +module GoogleCloud + DEFAULT_REGION = 'us-east1' + + class CreateCloudsqlInstanceService < ::GoogleCloud::BaseService + WORKER_INTERVAL = 30.seconds + + def execute + create_cloud_instance + trigger_instance_setup_worker + success + rescue Google::Apis::Error => err + error(err.to_json) + end + + private + + def create_cloud_instance + google_api_client.create_cloudsql_instance(gcp_project_id, + instance_name, + root_password, + database_version, + region, + tier) + end + + def trigger_instance_setup_worker + GoogleCloud::CreateCloudsqlInstanceWorker.perform_in(WORKER_INTERVAL, + current_user.id, + project.id, + { + 'google_oauth2_token': google_oauth2_token, + 'gcp_project_id': gcp_project_id, + 'instance_name': instance_name, + 'database_version': database_version, + 'environment_name': environment_name, + 'is_protected': protected? + }) + end + + def protected? + project.protected_for?(environment_name) + end + + def instance_name + # Generates an `instance_name` for the to-be-created Cloud SQL instance + # Example: `gitlab-34647-postgres-14-staging` + environment_alias = environment_name == '*' ? 'ALL' : environment_name + name = "gitlab-#{project.id}-#{database_version}-#{environment_alias}" + name.tr("_", "-").downcase + end + + def root_password + SecureRandom.hex(16) + end + + def database_version + params[:database_version] + end + + def region + region = ::Ci::VariablesFinder + .new(project, { key: Projects::GoogleCloud::GcpRegionsController::GCP_REGION_CI_VAR_KEY, + environment_scope: environment_name }) + .execute.first + region&.value || DEFAULT_REGION + end + + def tier + params[:tier] + end + end +end diff --git a/app/services/google_cloud/enable_cloudsql_service.rb b/app/services/google_cloud/enable_cloudsql_service.rb new file mode 100644 index 00000000000..a466b2f3696 --- /dev/null +++ b/app/services/google_cloud/enable_cloudsql_service.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module GoogleCloud + class EnableCloudsqlService < ::GoogleCloud::BaseService + def execute + return no_projects_error if unique_gcp_project_ids.empty? + + unique_gcp_project_ids.each do |gcp_project_id| + google_api_client.enable_cloud_sql_admin(gcp_project_id) + google_api_client.enable_compute(gcp_project_id) + google_api_client.enable_service_networking(gcp_project_id) + end + + success({ gcp_project_ids: unique_gcp_project_ids }) + end + + private + + def no_projects_error + error("No GCP projects found. Configure a service account or GCP_PROJECT_ID CI variable.") + end + end +end diff --git a/app/services/google_cloud/get_cloudsql_instances_service.rb b/app/services/google_cloud/get_cloudsql_instances_service.rb new file mode 100644 index 00000000000..701e83d556d --- /dev/null +++ b/app/services/google_cloud/get_cloudsql_instances_service.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module GoogleCloud + class GetCloudsqlInstancesService < ::GoogleCloud::BaseService + CLOUDSQL_KEYS = %w[GCP_PROJECT_ID GCP_CLOUDSQL_INSTANCE_NAME GCP_CLOUDSQL_VERSION].freeze + + def execute + group_vars_by_environment(CLOUDSQL_KEYS).map do |environment_scope, value| + { + ref: environment_scope, + gcp_project: value['GCP_PROJECT_ID'], + instance_name: value['GCP_CLOUDSQL_INSTANCE_NAME'], + version: value['GCP_CLOUDSQL_VERSION'] + } + end + end + end +end diff --git a/app/services/google_cloud/setup_cloudsql_instance_service.rb b/app/services/google_cloud/setup_cloudsql_instance_service.rb index 73650ee752f..10237f83b37 100644 --- a/app/services/google_cloud/setup_cloudsql_instance_service.rb +++ b/app/services/google_cloud/setup_cloudsql_instance_service.rb @@ -16,29 +16,29 @@ module GoogleCloud return error("CloudSQL instance not RUNNABLE: #{get_instance_response.to_json}") end - database_response = google_api_client.create_cloudsql_database(gcp_project_id, instance_name, database_name) + save_instance_ci_vars(get_instance_response) - if database_response.status != OPERATION_STATE_DONE - return error("Database creation failed: #{database_response.to_json}") - end + list_database_response = google_api_client.list_cloudsql_databases(gcp_project_id, instance_name) + list_user_response = google_api_client.list_cloudsql_users(gcp_project_id, instance_name) - user_response = google_api_client.create_cloudsql_user(gcp_project_id, instance_name, username, password) + existing_database = list_database_response.items.find { |database| database.name == database_name } + existing_user = list_user_response.items.find { |user| user.name == username } - if user_response.status != OPERATION_STATE_DONE - return error("User creation failed: #{user_response.to_json}") + if existing_database && existing_user + save_database_ci_vars + save_user_ci_vars(existing_user) + return success end - primary_ip_address = get_instance_response.ip_addresses.first.ip_address - connection_name = get_instance_response.connection_name + database_response = execute_database_setup(existing_database) + return database_response if database_response[:status] == :error - save_ci_var('GCP_PROJECT_ID', gcp_project_id) - save_ci_var('GCP_CLOUDSQL_INSTANCE_NAME', instance_name) - save_ci_var('GCP_CLOUDSQL_CONNECTION_NAME', connection_name) - save_ci_var('GCP_CLOUDSQL_PRIMARY_IP_ADDRESS', primary_ip_address) - save_ci_var('GCP_CLOUDSQL_VERSION', database_version) - save_ci_var('GCP_CLOUDSQL_DATABASE_NAME', database_name) - save_ci_var('GCP_CLOUDSQL_DATABASE_USER', username) - save_ci_var('GCP_CLOUDSQL_DATABASE_PASS', password, true) + save_database_ci_vars + + user_response = execute_user_setup(existing_user) + return user_response if user_response[:status] == :error + + save_user_ci_vars(existing_user) success rescue Google::Apis::Error => err @@ -64,11 +64,55 @@ module GoogleCloud end def password - SecureRandom.hex(16) + @password ||= SecureRandom.hex(16) end def save_ci_var(key, value, is_masked = false) create_or_replace_project_vars(environment_name, key, value, @params[:is_protected], is_masked) end + + def save_instance_ci_vars(cloudsql_instance) + primary_ip_address = cloudsql_instance.ip_addresses.first.ip_address + connection_name = cloudsql_instance.connection_name + + save_ci_var('GCP_PROJECT_ID', gcp_project_id) + save_ci_var('GCP_CLOUDSQL_INSTANCE_NAME', instance_name) + save_ci_var('GCP_CLOUDSQL_CONNECTION_NAME', connection_name) + save_ci_var('GCP_CLOUDSQL_PRIMARY_IP_ADDRESS', primary_ip_address) + save_ci_var('GCP_CLOUDSQL_VERSION', database_version) + end + + def save_database_ci_vars + save_ci_var('GCP_CLOUDSQL_DATABASE_NAME', database_name) + end + + def save_user_ci_vars(user_exists) + save_ci_var('GCP_CLOUDSQL_DATABASE_USER', username) + save_ci_var('GCP_CLOUDSQL_DATABASE_PASS', user_exists ? user_exists.password : password, true) + end + + def execute_database_setup(database_exists) + return success if database_exists + + database_response = google_api_client.create_cloudsql_database(gcp_project_id, instance_name, database_name) + + if database_response.status != OPERATION_STATE_DONE + return error("Database creation failed: #{database_response.to_json}") + end + + success + end + + def execute_user_setup(existing_user) + return success if existing_user + + user_response = google_api_client.create_cloudsql_user(gcp_project_id, instance_name, username, password) + + if user_response.status != OPERATION_STATE_DONE + return error("User creation failed: #{user_response.to_json}") + end + + success + end end end |