Is web2py incompatible with 2nd generation Google Cloud SQL ?

109 views
Skip to first unread message

Karoly Kantor

unread,
Apr 25, 2017, 1:54:08 AM4/25/17
to web...@googlegroups.com
Dear Sirs,

I can connect my Google App Engine web2py app to a first generation GAE SQL instance, but not to a second generation one. I was talking to a cloud SQL support person on another forum and he said, I quote:

"Your first generation CloudSQL instance has the app name as an authorized user, the second generation doesn’t. Your logs show connection errors to the instance from a user without password. "

So my question is: Where do I set a database user name and password in web2p to connect to a 2nd Gen Google SQL instance, if it is possible at all? I see this option with other connection strings in the doc, but not for Google.

Thank you.

Massimo Di Pierro

unread,
Apr 26, 2017, 9:55:21 PM4/26/17
to web2py-users
I have not tried it but I have an interest in it. I will try asap and will make sure it works.

Karoly Kantor

unread,
Apr 27, 2017, 1:45:52 AM4/27/17
to web2py-users
Thanks, Massimo.

FYI, some more info that might be helpful in your investigation:

Google docs tell me that my app is granted access to the SQL instance by default, as it is in the same project with the Cloud SQL license, so there's nothing to do there. Also, I set a password for the root user in the data base user management menu. The I created two environment variables in app.yaml as proposed by Google. 

Then I tried a temporary hack in google_adapters.py to send user name and pasword to Google SQL, but the connection was still refused. I hope I did something wrong and you will be able to pinpoint it:

class GoogleSQLAdapter(UseDatabaseStoredFile, MySQLAdapter):
    uploads_in_blob = True

    REGEX_URI = re.compile('^(?P<instance>.*)/(?P<db>.*)$')

    def clear_cache(self):
        ndb.get_context().clear_cache()

    def ignore_cache_for(self, entities = None):
        entities = entities or []
        ndb.get_context().set_cache_policy(lambda key: key.kind() not in entities)

    def __init__(self, db, uri='google:sql://realm:domain/database',
                 pool_size=0, folder=None, db_codec='UTF-8',
                 credential_decoder=IDENTITY, driver_args={},
                 adapter_args={}, do_connect=True, after_connection=None):

        self.db = db
        self.dbengine = "mysql"
        self.uri = uri
        self.pool_size = pool_size
        self.db_codec = db_codec
        self._after_connection = after_connection
        if do_connect: self.find_driver(adapter_args, uri)
        self.folder = folder or pjoin('$HOME',THREAD_LOCAL.folder.split(
                os.sep+'applications'+os.sep,1)[1])
        ruri = uri.split("://")[1]
        m = self.REGEX_URI.match(ruri)
        if not m:
            raise SyntaxError("Invalid URI string in SQLDB: %s" % self.uri)
        instance = credential_decoder(m.group('instance'))
        self.dbstring = db = credential_decoder(m.group('db'))
        driver_args['instance'] = instance
        
        #KK's addition to attempt fix 2nd gen problem with missing user name and password:
        driver_args['user'] = os.environ.get('CLOUDSQL_USER')
        driver_args['password'] = os.environ.get('CLOUDSQL_PASSWORD')
        
        if not 'charset' in driver_args:
            driver_args['charset'] = 'utf8'
        self.createdb = createdb = adapter_args.get('createdb',True)
        if not createdb:
            driver_args['database'] = db
            
        #KK added this for debug:
        print "DEBUG: driver args = " + str(driver_args)    
            
        def connector(driver_args=driver_args):
            return rdbms.connect(**driver_args)
        self.connector = connector
        if do_connect: self.reconnect()

Karoly Kantor

unread,
Apr 28, 2017, 2:49:46 AM4/28/17
to web2py-users
Hello Massimo,

One more piece of information that might be useful:

One Google support person at the "Google App Engine" forum here told me my login attempt was made with password "password", which is NOT the password I set into the environment variable as seen in my attempted hack above, as the App Engine log mentions "password=password" before the connection refusal message. This might mean than my hack was unsuccessful due to insufficient understanding of web2py internals.

Karoly Kantor

unread,
May 23, 2017, 4:22:37 AM5/23/17
to web2py-users
Dear Massimo,

I am only writing again as a reminder because you said last month you would look into this asap. This is very important I think as one of the key offerings of web2py, supporting GAE, seems to be broken. At least for me.

Thanks! 

Karoly Kantor

unread,
May 25, 2017, 2:25:45 AM5/25/17
to web2py-users
I have temporarily solved this problem, as described here:

Reply all
Reply to author
Forward
0 new messages