def GetInstanceIP(instance_name, project): """This function gets a SQL instance's IP by running the gcloud sql describe. Args: instance_name: name of the instance project: the project name in which the instance is in Returns: Returns a string which is the IP Address of the `instance_name`. """ status = subprocess.check_output( sh_utils.GetGcloud(["instances", "describe", instance_name], project=project, service="sql")) ip = re.search(r"ipAddress:\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", status) return ip.group(1)
def CheckStatus(args): """This function tries to check the status of the VM Instance. Args: args: all the arguments to the program Returns: Returns None, if everything is fine Otherwise, return the error message and status parsed """ status = subprocess.check_output(sh_utils.GetGcloud([ "instances", "describe", args.vm_name, "--zone", args.zone], project=args.project, service="compute")) cur_status = re.search(r"status:\s+([A-Z]+)", status) if cur_status.group(1) == "RUNNING": print("Instance is running successfully.") return None else: return ("Instance is not running" ". Current status: {}").format(cur_status.group(1))
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--db_instance_name", help="the name of the gcloud instance", default="envoy-db-instance") parser.add_argument("--tier", help="the tier of GCloud SQL service", default="db-n1-standard-2") parser.add_argument("--username", help="username on the host DB", default="root") parser.add_argument("--database", help="name of the database", default="envoy_stat_db") parser.add_argument("--json_result", help="the JSON result file", default="result.json") parser.add_argument("--table_name", help=("the table which stores " "the benchmarking data"), default="envoy_stat") parser.add_argument("--project", help="the project name in Google Cloud.", default="envoy-ci") parser.add_argument("--logfile", help="the local log file for this script. New log will be" "appended to this file.", default="benchmark.log") parser.add_argument("--ownip", help=("the machine's IP from where" " the script is being run"), default="127.0.0.1") parser.add_argument("--envoy_hash", help="the hash of envoy version", required=True) parser.add_argument("--runid", help="the run id of this benchmark", default="0") utils.CreateBooleanArgument(parser, "create_instance", ("turn on if you want to create" " a Google Cloud SQL instance"), create_instance=True) utils.CreateBooleanArgument(parser, "create_db", ("turn on if you want" " to create the DB"), create_db=True) utils.CreateBooleanArgument(parser, "delete_db", ("turn on if you want" " to delete the DB"), delete_db=True) args = parser.parse_args() logfile = open(args.logfile, "ab") if args.create_instance: sh_utils.RunGCloudService(["instances", "create", args.db_instance_name, "--tier", args.tier], args.project, "sql", logfile) print("Google Cloud SQL Instance {} is created.".format( args.db_instance_name)) else: print("DB Instance creation is skipped due to --no-create_instance.") # generating a random password to be used for the DB password = utils.GetRandomPassword() sh_utils.RunGCloudService(["users", "set-password", args.username, "%", "--instance", args.db_instance_name, "--password", password], project=args.project, service="sql", logfile=logfile) print("DB Usernames and passwords are set.") auth_ip_command = sh_utils.GetGcloud( ["instances", "patch", args.db_instance_name, "--authorized-networks", args.ownip], project=args.project, service="sql") auth_ip_command = " ".join(auth_ip_command) pexpect.run(auth_ip_command, events={"Do you want to continue (Y/n)?": "Y\n"}, logfile=logfile, timeout=None) print(("The machine is configured to use the Google" " Cloud SQL Instance {}.").format(args.db_instance_name)) hostname = db_utils.GetInstanceIP(args.db_instance_name, args.project) if args.create_db: connection = MySQLdb.connect(host=hostname, user=args.username, passwd=password) print("Connection successful.") db_utils.ExecuteAndReturnResult(connection, "CREATE DATABASE {};".format( args.database)) connection.select_db(args.database) print("Created DB.") else: connection = MySQLdb.connect(host=hostname, user=args.username, passwd=password, db=args.database) print("Connection to DB {} is successful.".format(args.database)) # table will only be created if not exists CreateTable(connection, args.table_name) with open(args.json_result, "r") as f: InsertIntoTable(connection, args.table_name, f, args.runid, args.envoy_hash) connection.commit() print("Data is inserted from JSON file to Database.") if args.delete_db: db_utils.ExecuteAndReturnResult(connection, "DROP DATABASE {};".format(args.database)) print("Database deletion is successful.") else: print("Database deletion is skipped due to --no-delete_db.") connection.close() print("DB Connection closed.")