[PYTHON] Deploy the management page to production to make maintenance easier.

Ohashi is impatient with two Advent calendars on the same day.

I've been touching GAE / Py for the last two years at work. Until now, I've touched GAE / J and GAE / Go, and since it's GAE / Py that returns to the origin, I have a lot of things to think about, but after all GAE is wonderful, I think I'm not using SQL. , I think I shouldn't have forced this application into a Datastore every day.

By the way, if you create and operate a service with GAE, you will often want to play with the Datastore directly. How are you doing in such a case?

I think there are roughly the following methods.

  1. Modify with Datastore Viewer of Cloud Console
  2. Hit from the client via Remote API
  3. Deploy and hit the fix batch

None of these methods are bad, but each has its own problems and can be painful.

This time I would like to write about google.appengine.ext.admin.application which cleared problems 1, 2 and 3.

The pain of modifying data in a data store

The following is just writing various grudges, so I think you don't have to read it.

When modifying with Datastore Viewer of Cloud Console

The Cloud Console Datastore Viewer has been significantly updated to allow you to update your data in quite a few cases. However, if you are using ndb with GAE / Py, or if you are using your own library, Goon, etc., if you update directly with Datastore Viewer, there is a problem that ** memcache data is not updated **. ..

When you run into this problem, it's hard to notice, and it often creates a difference between memcache and Datastore, which can lead to failures.

In some cases it is necessary to clear all memcache, which is quite annoying.

Hit from the client via Remote API

By using App Engine remote API, you can directly execute the code below on your own PC.

You can also use the Remote API Shell to interact with App Engine like iPython.

 >>> from google.appengine.ext import ndb
 >>>
 >>> # Fetch 10 keys from the datastore
 >>> ndb.Query().fetch(10, keys_only=True)

This is pretty handy, but when writing a process that requires frequent data exchange, all RPC calls, such as getting and writing data, are HTTP communication from the client to the App Engine server, which is very slow. There is a problem.

Also, it's annoying that an HTTP request is sent once per RPC call on the log.

Deploy and hit the fix batch

When adding properties to large-scale model data, you may create an appropriate TaskQueue and process it all at once.

To be honest, deploying is too troublesome, so I don't want to do it if I do it in operation.

So google.appengine.ext.admin.application

Local Amdmin Server

When launching dev_appserver.py locally


INFO     2016-12-02 08:30:49,546 admin_server.py:116] Starting admin server at: http://localhost:8000

Admin_server.py has been launched for, and you can use the administration page by accessing it. On the administration page, you can simulate the status of local Datastore, Memcache, Task Queue, and email reception.

There is something called "Interactive Console" in it, which allows you to write Python code directly and send and execute the code to the server.

Kobito.FlIaob.png

You can easily execute the code without writing the code in the file in the local environment, and it can be used for maintenance and debugging.

Run Admin page in production

google.appengine.ext.admin.application makes this local Admin Server available in production and is included within Python's App Engine SDK.

To use google.appengine.ext.admin.application, add the routing in app.yaml.

- url: /adminapplication/.*
  script: google.appengine.ext.admin.application
  login: admin

Be sure to add login: admin. If you forget to add it, anyone will be able to execute your favorite code on your GAE. Please do it at your own risk.

If you deploy the application in this state, you will be able to display the Admin page with the assigned path.

Kobito.3NQ9jC.png

The reason why it looks a little different is probably because the application on this Admin page is old and has not been updated, only the Admin server side has been updated.

There is also an "Intractive Console" on this page, where you can write and execute code directly as you would in the local version.

Kobito.ZGst87.png

Of course, all the modules and libraries used in my application can be used, and ndb etc. will work without problems. Maintenance will be very easy!

Summary and notes

Using google.appengine.ext.admin.application will make maintenance of your production service much easier. Also, when you are asked to output a little data with csv, if you use a print statement etc., it will be displayed in the text area on the left side, so you can easily extract from here.

However, there are some caveats. First, since this mechanism is the same as a normal handler, it receives the normal 60-second limit and various timeouts in the same way. Be careful when updating a large amount of data.

I wrote the second one above, but you need to be careful about security because you can execute any code. Be sure to specify login: admin

The third is obvious, but if you write a sloppy code, all the data will be lost, which is a pain. It's always a good idea to have a mechanism that allows you to try it locally, then execute the code, point to it, and recover.

When I do it, my brain seems to disappear for a moment.

Please note that we cannot take responsibility for anything, so please use at your own risk.

I hope the GAE operator can return as soon as possible.

Recommended Posts

Deploy the management page to production to make maintenance easier.
Make the display of Python module exceptions easier to understand
Deploy the Django tutorial to IIS ①
The easiest way to make Flask
Make SikuliX's click function easier to use
Tips to make Python here-documents easier to read
[Wagtail] Add a login page to the Wagtail project