def AuthorizeMachineAndConnectDB(db_instance_name, project, username, database,
logfile):
"""This function authorizes a machine to connect to a DB.
Args:
db_instance_name: name of the DB instance.
project: name of the project in which the db_instance belongs to.
username: username that will be used to log in to the DB
database: name of the database to connect to
logfile: logfile for the gcloud command in shell
Returns:
Returns the connection after being connected to the DB.
"""
hostname = GetInstanceIP(db_instance_name, project)
password = utils.GetRandomPassword()
sh_utils.RunGCloudService(["users", "set-password", username, "%",
"--instance", db_instance_name, "--password",
password], project=project,
service="sql", logfile=logfile)
print "DB Usernames and passwords are set."
connection = MySQLdb.connect(host=hostname, user=username,
passwd=password, db=database)
return connection
评论列表
文章目录