Cron file - disable access (Google App Engine) - google-app-engine

I'm running a cron file in Google App Engine. It seems to be working fine, except I don't want anyone to be able to access the URL.
Here is my .cron file:
cron:
- description: testing cron
url: /tester
schedule: every 1 minutes
I tried adding: "login: admin" underneath "schedule", but I get:
enter code here
Error parsing yaml file:
Unexpected attribute 'login' for object of type <class 'google.appengine.api.croninfo.CronEntry'>.
So how do I prevent someone from calling the url and running a script that should be automated?
Thanks

You restrict access to URLs in app.yaml. Add a correspondent entry for your cron tester:
application: hello-cron
version: 1
runtime: python
api_version: 1
handlers:
- url: /tester
script: tester.py
login: admin

Related

What's the proper YAML to start a service in Google App Engine?

Using Google App Engine, I have an application myapp as a default service that adds a task to a task queue and launch a background service worker called optimize. Although myapp is running fine, unfortunately I always see a POST 404 error in the myapp log when the AppEngine task queue tries to launch the URL /optimize-dot-myapp.appspot.com/index.php/optimize. Of course because of the 404 error the task queue keeps retrying. My current optimize.yaml file contains the following. Any thoughts?
# optimize.yaml configuration for Google App Engine
# Full details at: https://cloud.google.com/appengine/docs/php/config/appref
runtime: php55
api_version: 1
service: optimize
handlers:
# Serve php scripts.
- url: /index.php/optimize
script: index.php/optimize
The default app.yaml file contains the following:
# app.yaml configuration for Google App Engine
# Full details at: https://cloud.google.com/appengine/docs/php/config/appref
runtime: php55
api_version: 1
handlers:
# Serve php scripts.
- url: /(.+\.php).*
script: \1
- url: /
script: index.php
# All URLs beginning with /assets are treated as paths to
# static files in the assets/ directory.
- url: /assets
static_dir: assets
In case it's useful, the optimize worker is started in the default task queue with the following PHP:
// Start the background worker
// API details: https://cloud.google.com/appengine/docs/php/refdocs/classes/google.appengine.api.taskqueue.PushTask
$url = '/optimize-dot-myapp.appspot.com/index.php/optimize';
$task = new PushTask($url, $param);
$task_name = $task->add();
You have collisions in the URL path patterns. The /index.php/optimize path matches both the /index.php/optimize url pattern from optimize.yaml and the /(.+\.php).* pattern from app.yaml. Probably the request ends up in the default service instead of the optimize one. Easy to confirm: check the app logs, you can select a specific service and you'll see which service got the request.
I would add a dispatch.yaml file to clarify things and eliminate the possibility of ambiguous routing (no need to specify the default module, anything not matching dispatch rules is sent to the default module):
application: my_app
dispatch:
- url: "*/optimize/*"
module: optimize
Then adjust the url patterns accordingly in optimize.yaml (they should all start with /optimize):
- url: /optimize/index.php
script: index.php
Note: the index.php file mentioned above would be in the optimize service dir, not in the default service one. Assuming here that each service has its own dir, as mentioned in Can a default service/module in a Google App Engine app be a sibling of a non-default one in terms of folder structure?
And in the task enqueueing code the url should only contain the request path, not the hostname (which is interpreted as part of the path, thus causing a mismatch with the handler's url pattern). You want:
$url = '/optimize/index.php';
In Google App Engine, applications define task queues in a configuration file called queue.yaml. You can use queue.yaml to configure both push queues and pull queues.
The following a basic example that defines a named queue and overrides the default processing rate:
queue:
- name: my-push-queue
rate: 1/s
The following is a more complex example of a queue.yaml configuration that demonstrates setting up task retries and modifying the default processing rate.
queue:
- name: fooqueue
rate: 1/s
retry_parameters:
task_retry_limit: 7
task_age_limit: 2d
- name: barqueue
rate: 1/s
retry_parameters:
min_backoff_seconds: 10
max_backoff_seconds: 200
max_doublings: 0
- name: bazqueue
rate: 1/s
retry_parameters:
min_backoff_seconds: 10
max_backoff_seconds: 200
max_doublings: 3
Please read this documentation for further details

Issues while creating and deploying an app to google app engine

I am running into this error while deploying my app to google app-engine.
Error:
Error 404: --- begin server output ---
This application does not exist (project_id=u'homework-153002'). To create an App Engine application in this project, run "gcloud beta app create" in your console.
--- end server output ---
When I try to create the app using the above command this is what I see:
ERROR: (gcloud.beta.app.create) You do not have permission to access app [homework] (or it may not exist): Operation not allowed
I have created a project in google developers api website with project_name of "homework" and random id. Here is the url for that:
https://console.developers.google.com/apis/library?project=homework-153002
Any pointers on what I might be doing wrong here?
This is my yaml file:
application: homework-153002
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
Your app URL indicates your app ID is homework-153002, but the
error message indicates you're connecting to an ap called homework - likely not your own, hence the permission issue.
Simply correcting the app ID in your deployment command and/or your app.yaml file should suffice.
As your comment indicates, for your case (gcloud deployment) the solution was:
gcloud config set project homework-153002
gcloud beta app create
appcfg.py update homework/

Google App engine needs periodic restarting

I have a module that works fine when I push it to app engine. When it works it logs stuff nicely and the logs are accessible in the console logs viewer. But then after a while it just stops working and when I try to access any url give me 500 server errors with no info (it just says waiting 30 seconds might be a good idea). When this happens nothing gets logged for requests.
If I restart the module (by pushing my code to app engine) then it works for a little while again.
The module is running a Pyramid app and the configuration file looks a little something like:
application: my_app
module: my_module
version: dev
runtime: python27
api_version: 1
threadsafe: false
instance_class: B2
basic_scaling:
max_instances: 2
idle_timeout: 10m
handlers:
- url: /actions/.*
script: my_module.application
login: admin
- url: /.*
script: my_module.application
builtins:
- appstats: off
libraries:
- name: webob
version: latest
- name: setuptools
version: latest
includes:
- mapreduce/include.yaml
I think what is happening is that it's hitting the idle timeout and shutting down. I need requests to the module to turn it back on again. How do I do that?
Let me know if you need more info, I'm an app engine noob at this stage. Any help would be greatly appreciated.
When a module start, App Engine call the url /_ah/start. You mustn't handle this request.
In you my_module.application you need to add in the handler who match this request :
def get(self):
# Let module start
if "X-Appengine-Cron" in self.request.headers or "X-AppEngine-TaskName" in self.request.headers or "X-Appengine-Failfast" in self.request.headers:
return
Even if you hit the idle timeout, AppEngine will spin a new instance when new request is coming in.
Use Cloud Debugger to inspect the state of your application. The debugger makes it easier to view the application state and understand what happens after your app has been running for while.
Check the docs on startup state https://cloud.google.com/appengine/docs/python/modules/#Python_Instance_states
you current default handler /.* should be able deal with a /_ah/start if youe handler can gracefully deal with a 404.
Thats how I handle startups. Goes through the main handler which can deal with non existent url requests using the default pyramid not found.
I have a config.add_notfound_view(notfound) registered.

Google App Engine and backends: how to configure it on development server?

My configuration of backends.yaml
backends:
- name: mybackend
class: B1
instances: 1
options: dynamic
and app.yaml
handlers:
- url: http://mybackend.myapp.appspot.com
script: mybackend.py
login: admin
Running it localy on development server I get this error:
Unable to assign value 'http://mybackend.myapp.appspot.com' to attribute 'url':
Value 'http://mybackend.myapp.appspot.com' for url does not match expression '^(?!\^)/|.|((.).*(?!\$).$'
How can I test backend on development server?
I believe the url should be the relative url from your site. The script should be the python function that's run, not the filename. So your app.yaml should be.
handlers:
- url: /backend
script: mybackend.myfunction
login: admin
Your backend and frontend instances share the same handlers, there's no way to distinguish between them.

Production server url not there but development server is?

I am running my app on the production server for first time. I have a url with admin logon enabled in the app.yml. The script runs when I browse to its URL when running on the development server. However, after uploading to the production server when I go to the same URL I get the following error:
The requested URL /tasks/ was not found on this server.
Why would this occur? I tried updating again.
The URL and script is the third one in the my app.yml file:
##app.yml file
application: generic_app_name
version: 1
runtime: python
api_version: 1
handlers:
- url: /remote_api
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
login: admin
- url: /stats.*
script: $PYTHON_LIB/google/appengine/ext/appstats/ui.py
- url: /tasks/SR2pop
script: PopulateSR2.py
login: admin
- url: /
script: dbsample.py
Is the capitalization of your script exactly as specified in app.yaml? The production servers are case-sensitive, but if you're developing on Windows, the development server isn't. Check the capitalization of PopulateSR2.py matches the one in app.yaml.
Also, your file is called app.yaml, not app.yml, right?
Your 3rd handler is going to match only the exact string /tasks/SR2pop. None of your handlers will match /tasks/.

Resources