1# -*- coding: utf-8 -*- #
2# Copyright 2016 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Common utility functions for sql instances."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21import os
22import subprocess
23import time
24
25from apitools.base.py import list_pager
26from googlecloudsdk.api_lib.sql import api_util
27from googlecloudsdk.api_lib.sql import constants
28from googlecloudsdk.api_lib.sql import exceptions as sql_exceptions
29from googlecloudsdk.api_lib.util import apis
30from googlecloudsdk.calliope import exceptions
31from googlecloudsdk.core import config
32from googlecloudsdk.core import execution_utils
33from googlecloudsdk.core import log
34from googlecloudsdk.core import properties
35from googlecloudsdk.core.console import console_io
36from googlecloudsdk.core.util import encoding
37from googlecloudsdk.core.util import files as file_utils
38import six
39
40
41messages = apis.GetMessagesModule('sql', 'v1beta4')
42
43_BASE_CLOUD_SQL_PROXY_ERROR = 'Failed to start the Cloud SQL Proxy'
44
45_MYSQL_DATABASE_VERSION_PREFIX = 'MYSQL'
46_POSTGRES_DATABASE_VERSION_PREFIX = 'POSTGRES'
47_SQLSERVER_DATABASE_VERSION_PREFIX = 'SQLSERVER'
48
49
50class DatabaseInstancePresentation(object):
51  """Represents a DatabaseInstance message that is modified for user visibility."""
52
53  def __init__(self, orig):
54    for field in orig.all_fields():
55      if field.name == 'state':
56        if orig.settings and orig.settings.activationPolicy == messages.Settings.ActivationPolicyValueValuesEnum.NEVER:
57          self.state = 'STOPPED'
58        else:
59          self.state = orig.state
60      else:
61        value = getattr(orig, field.name)
62        if value is not None and not (isinstance(value, list) and not value):
63          if field.name in ['currentDiskSize', 'maxDiskSize']:
64            setattr(self, field.name, six.text_type(value))
65          else:
66            setattr(self, field.name, value)
67
68  def __eq__(self, other):
69    """Overrides the default implementation by checking attribute dicts."""
70    if isinstance(other, DatabaseInstancePresentation):
71      return self.__dict__ == other.__dict__
72    return False
73
74  def __ne__(self, other):
75    """Overrides the default implementation (only needed for Python 2)."""
76    return not self.__eq__(other)
77
78
79def GetRegionFromZone(gce_zone):
80  """Parses and returns the region string from the gce_zone string."""
81  zone_components = gce_zone.split('-')
82  # The region is the first two components of the zone.
83  return '-'.join(zone_components[:2])
84
85
86def _GetCloudSqlProxyPath():
87  """Determines the path to the cloud_sql_proxy binary."""
88  sdk_bin_path = config.Paths().sdk_bin_path
89  if not sdk_bin_path:
90    # Check if cloud_sql_proxy is located on the PATH.
91    proxy_path = file_utils.FindExecutableOnPath('cloud_sql_proxy')
92    if proxy_path:
93      log.debug(
94          'Using cloud_sql_proxy found at [{path}]'.format(path=proxy_path))
95      return proxy_path
96    else:
97      raise exceptions.ToolException(
98          'A Cloud SQL Proxy SDK root could not be found. Please check your '
99          'installation.')
100  return os.path.join(sdk_bin_path, 'cloud_sql_proxy')
101
102
103def _RaiseProxyError(error_msg=None):
104  message = '{}.'.format(_BASE_CLOUD_SQL_PROXY_ERROR)
105  if error_msg:
106    message = '{}: {}'.format(_BASE_CLOUD_SQL_PROXY_ERROR, error_msg)
107  raise sql_exceptions.CloudSqlProxyError(message)
108
109
110def _ReadLineFromStderr(proxy_process):
111  """Reads and returns the next line from the proxy stderr stream."""
112  return encoding.Decode(proxy_process.stderr.readline())
113
114
115def _WaitForProxyToStart(proxy_process, port, seconds_to_timeout):
116  """Wait for the proxy to be ready for connections, then return proxy_process.
117
118  Args:
119    proxy_process: Process, the process corresponding to the Cloud SQL Proxy.
120    port: int, the port that the proxy was started on.
121    seconds_to_timeout: Seconds to wait before timing out.
122
123  Returns:
124    The Process object corresponding to the Cloud SQL Proxy.
125  """
126
127  total_wait_seconds = 0
128  seconds_to_sleep = 0.2
129  while proxy_process.poll() is None:
130    line = _ReadLineFromStderr(proxy_process)
131    while line:
132      log.status.write(line)
133      if constants.PROXY_ADDRESS_IN_USE_ERROR in line:
134        _RaiseProxyError(
135            'Port already in use. Exit the process running on port {} or try '
136            'connecting again on a different port.'.format(port))
137      elif constants.PROXY_READY_FOR_CONNECTIONS_MSG in line:
138        # The proxy is ready to go, so stop polling!
139        return proxy_process
140      line = _ReadLineFromStderr(proxy_process)
141
142    # If we've been waiting past the timeout, throw an error.
143    if total_wait_seconds >= seconds_to_timeout:
144      _RaiseProxyError('Timed out.')
145
146    # Keep polling on the proxy output until relevant lines are found.
147    total_wait_seconds += seconds_to_sleep
148    time.sleep(seconds_to_sleep)
149
150  # If we've reached this point, the proxy process exited unexpectedly.
151  _RaiseProxyError()
152
153
154def StartCloudSqlProxy(instance, port, seconds_to_timeout=10):
155  """Starts the Cloud SQL Proxy for instance on the given port.
156
157  Args:
158    instance: The instance to start the proxy for.
159    port: The port to bind the proxy to.
160    seconds_to_timeout: Seconds to wait before timing out.
161
162  Returns:
163    The Process object corresponding to the Cloud SQL Proxy.
164
165  Raises:
166    CloudSqlProxyError: An error starting the Cloud SQL Proxy.
167    ToolException: An error finding a Cloud SQL Proxy installation.
168  """
169  command_path = _GetCloudSqlProxyPath()
170
171  # Specify the instance and port to connect with.
172  args = ['-instances', '{}=tcp:{}'.format(instance.connectionName, port)]
173  # Specify the credentials.
174  account = properties.VALUES.core.account.Get(required=True)
175  args += ['-credential_file', config.Paths().LegacyCredentialsAdcPath(account)]
176  proxy_args = execution_utils.ArgsForExecutableTool(command_path, *args)
177  log.status.write(
178      'Starting Cloud SQL Proxy: [{args}]]\n'.format(args=' '.join(proxy_args)))
179
180  proxy_process = subprocess.Popen(
181      proxy_args,
182      stdout=subprocess.PIPE,
183      stdin=subprocess.PIPE,
184      stderr=subprocess.PIPE)
185  return _WaitForProxyToStart(proxy_process, port, seconds_to_timeout)
186
187
188def IsInstanceV1(sql_messages, instance):
189  """Returns a boolean indicating if the database instance is first gen."""
190  return (instance.backendType ==
191          sql_messages.DatabaseInstance.BackendTypeValueValuesEnum.FIRST_GEN or
192          (instance.settings and instance.settings.tier and
193           instance.settings.tier.startswith('D')))
194
195
196def IsInstanceV2(sql_messages, instance):
197  """Returns a boolean indicating if the database instance is second gen."""
198  return instance.backendType == sql_messages.DatabaseInstance.BackendTypeValueValuesEnum.SECOND_GEN
199
200
201# TODO(b/73648377): Factor out static methods into module-level functions.
202class _BaseInstances(object):
203  """Common utility functions for sql instances."""
204
205  @staticmethod
206  def GetDatabaseInstances(limit=None, batch_size=None):
207    """Gets SQL instances in a given project.
208
209    Modifies current state of an individual instance to 'STOPPED' if
210    activationPolicy is 'NEVER'.
211
212    Args:
213      limit: int, The maximum number of records to yield. None if all available
214          records should be yielded.
215      batch_size: int, The number of items to retrieve per request.
216
217    Returns:
218      List of yielded DatabaseInstancePresentation instances.
219    """
220
221    client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
222    sql_client = client.sql_client
223    sql_messages = client.sql_messages
224    project_id = properties.VALUES.core.project.Get(required=True)
225
226    params = {}
227    if limit is not None:
228      params['limit'] = limit
229    if batch_size is not None:
230      params['batch_size'] = batch_size
231
232    yielded = list_pager.YieldFromList(
233        sql_client.instances,
234        sql_messages.SqlInstancesListRequest(project=project_id), **params)
235
236    def YieldInstancesWithAModifiedState():
237      for result in yielded:
238        yield DatabaseInstancePresentation(result)
239
240    return YieldInstancesWithAModifiedState()
241
242  @staticmethod
243  def PrintAndConfirmAuthorizedNetworksOverwrite():
244    console_io.PromptContinue(
245        message='When adding a new IP address to authorized networks, '
246        'make sure to also include any IP addresses that have already been '
247        'authorized. Otherwise, they will be overwritten and de-authorized.',
248        default=True,
249        cancel_on_no=True)
250
251  @staticmethod
252  def IsMysqlDatabaseVersion(database_version):
253    """Returns a boolean indicating if the database version is MySQL."""
254    return database_version.name.startswith(_MYSQL_DATABASE_VERSION_PREFIX)
255
256  @staticmethod
257  def IsPostgresDatabaseVersion(database_version):
258    """Returns a boolean indicating if the database version is Postgres."""
259    return database_version.name.startswith(_POSTGRES_DATABASE_VERSION_PREFIX)
260
261  @staticmethod
262  def IsSqlServerDatabaseVersion(database_version):
263    """Returns a boolean indicating if the database version is SQL Server."""
264    return database_version.name.startswith(_SQLSERVER_DATABASE_VERSION_PREFIX)
265
266
267class InstancesV1Beta4(_BaseInstances):
268  """Common utility functions for sql instances V1Beta4."""
269
270  @staticmethod
271  def SetProjectAndInstanceFromRef(instance_resource, instance_ref):
272    instance_resource.project = instance_ref.project
273    instance_resource.name = instance_ref.instance
274
275  @staticmethod
276  def AddBackupConfigToSettings(settings, backup_config):
277    settings.backupConfiguration = backup_config
278