0

I'm very new to the GCP as a whole, and I need to deploy a Flask app for a project with a client. Deploying an app is simple enough given all of the docs Google has provided, and since using the flexible app engine seems like the easiest way to do it, that's what I'm trying to use.

The issue I'm having though is in trying to connect to an MSSQL database that was setup on a Compute Engine. So far, I've connected to the database locally using pyodbc with some help from Connect to MSSQL Database using Flask-SQLAlchemy.

I was certain running gcloud app deploy would not work, and sure enough it wasn't able to install the pyodbc module. I figured that that wouldn't be the way to go anyway, and based on this page of the docs, it seems like I should be able to connect to the compute engine via its internal IP address.

I don't know how to proceed from here though, because everything in the docs wants me to use a Cloud SQL instance, but given that this data was provided by a client and I'm working on their GCP project, I'm a bit limited to the scenario I've described above.

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
  • Connecting to the database should be fine as long as the host is reachable and the network is configured properly to access that host. I am not sure why you are not able to install pyodbc though – mad_ Jul 24 '18 at 16:45
  • I think you're right, and the issue actually lies in installing pyodbc. When deploying I get this error: `src/pyodbc.h:56:17: fatal error: sql.h: No such file or directory`. So after some digging I found that running `sudo apt-get install unixodbc unixodbc-dev` should fix the problem. I'm able to install `pyodbc` when I ssh in now, but it still fails when I run `gcloud app deploy` for some reason, and I'm not sure why. – Brooks Lybrand Jul 24 '18 at 21:15
  • What error do you get? – mad_ Jul 24 '18 at 21:16
  • Check if you import pyodbc lib properly following this - https://cloud.google.com/appengine/docs/flexible/python/using-python-libraries – J.L Valtueña Jul 25 '18 at 10:55
  • @mad_ I posted the error above, but to give you the fuller context this is what `glcoud app deploy` ends with: `In file included from src/cnxninfo.cpp:7:0: src/pyodbc.h:56:17: fatal error: sql.h: No such file or directory compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1` @J.LValtueña, thanks for sharing the link. I am already using a requirements.txt file and a virtual environment as specified though. Everything loads in properly without the pyodbc line in my requirements file, but the app does not run without it. – Brooks Lybrand Jul 25 '18 at 13:14
  • Are you including the pyodbc line in the requirements file or not? I don't understand this part of your comment. – J.L Valtueña Jul 27 '18 at 14:38
  • pyodbc was in the requirements folder, that's why I was receiving an error in pyodbc as I pointed out above. This has actually been resolved, I just added the solution I came to. – Brooks Lybrand Jul 27 '18 at 15:03

1 Answers1

2

This has since been resolved.

The issue was that there were no ODBC drivers downloaded on the server, so I needed to create a custom runtime with a Dockerfile in order to first install the drivers.

My solution was greatly aided by this solution: Connect docker python to SQL server with pyodbc

The steps are as follows:

Run gcloud beta app gen-config --custom in your flask app's directory.

Inside of the now created Dockerfile, add these lines before installing the pip requirements.

#Install FreeTDS and dependencies for PyODBC
RUN apt-get update
RUN apt-get install -y tdsodbc unixodbc-dev
RUN apt install unixodbc-bin -y
RUN apt-get clean -y
ADD odbcinst.ini /etc/odbcinst.ini

The file odbcinst.ini should contain the following lines:

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

After that gcloud app deploy should work just fine.