Error using Google App Engine cloudshell to upload Python app - python

I'm trying to use appcfg to upload my helloworld app on GCloud using GAE. According to Google's own quickstarter site, the syntax is:
appcfg.py -A YOUR_PROJECT_ID_ -V v1 update helloworld/
So I did:
appcfg.py -A project_name_123456 -V v1 update c:/users/user/helloworld/
Which makes this error:
"appcfg.py: error: Not a directory: C:/users/user/helloworld"
What am I doing wrong? I have the app.yaml file in C:/users/user/helloworld, and I've tried lots of different things. Thanks

Related

How to run a "hello world" python script with Google Cloud Run

Forgive my ignorance..
I'm trying to learn how to schedule python scripts with Google Cloud. After a bit of research, I've seen many people suggest Docker + Google Cloud Run + Cloud Scheduler. I've attempted to get a "hello world" example working, to no avail.
Code
hello.py
print("hello world")
Dockerfile
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "hello.py"]
Steps
Create a repo with Google Cloud Artifact Registry
gcloud artifacts repositories create test-repo --repository-format=docker \
--location=us-central1 --description="My test repo"
Build the image
docker image build --pull --file Dockerfile --tag 'testdocker:latest' .
Configure auth
gcloud auth configure-docker us-central1-docker.pkg.dev
Tag the image with a registry name
docker tag testdocker:latest \
us-central1-docker.pkg.dev/gormanalysis/test-repo/testdocker:latest
Push the image to Artifact Registry
docker push us-central1-docker.pkg.dev/gormanalysis/test-repo/testdocker:latest
Deploy to Google Cloud Run
Error
At this point, I get the error
The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable.
I've seen posts like this which say to add
app.run(port=int(os.environ.get("PORT", 8080)),host='0.0.0.0',debug=True)
but this looks like a flask thing, and my script doesn't use flask. I feel like i have a fundamental misunderstanding of how this is supposed to work. Any help would be appreciated it.
UPDATE
I've documented my problem and solution in much more detail here ยป
I had been trying to deploy my script as a Cloud Run Service. I should've tried deploying it as a Cloud Run Job. The difference is that cloud run services require your script to listen for a port. jobs do not.
Confusingly, you cannot deploy a cloud run job directly from Artifact Registry. You have to start from the cloud run dashboard.
Your Flask application should be something like below:
import os
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
See this official documentation for step by step instruction: Deploy a Python service to Cloud Run
There is a plugin called: Cloud Code IDE plugin which makes the test and deployment easy. I am using it for VS code, once the initial setups and permissions are taken care, few clicks, you will be able to run locally, debug and deploy Cloud run services from your local instance.

Setting flask environment variables in a shell script

all
As flask-script document said they are no longer to maintain it,
thus I am trying to use flask cli (Command Line Interface) to activate my flask app.
When I run the following flask cli commands one by one at my terminal, they worked fine for me.
$ export PYTHONPATH=$PWD:flask_api/
$ export FLASK_APP=flask_api/app
$ export FLASK_RUN_PORT=8000
$ flask run
The problem is that it seems not that efficient every time I tried to start my flask app.
so I came up with writing them into a shell scripts (that said run_flask.sh)
the content in run_flask.sh is:
export PYTHONPATH=$PWD:flask_api/
export FLASK_APP=flask_api/app
export FLASK_RUN_PORT=8000
flask run
and I simply run source run_flask.sh
and the result showed and did not start to run flask:
"* Serving Flask app "flask_api/app
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
".ror: Could not import "src.flask_api.app
I am not sure what's happening,
could any one tell me why flask could not import my flask app while I tried to run it with shell scripts? Great Thanks!
ps. I declare flask app instance in flask_api/base_api/init.py,
and app.py is in flask_api/ folder, app.py would import the app instance from base_api
(I used CentOS7 as the OS)

Changing a file on Google App Engine

I have a Google App Engine Python project that I haven't touched since 2014, when you worked with a desktop app to upload and deploy it. Google App engine changed a lot in the meantime. According to the information the deploying guide I copy
the file(s) like this:
gsutil cp main.py gs://myproject.appspot.com/
I converted my app.yaml file to a json file using their utility. When I initially deployed the app using this page it failed because there was no deployment information, so I added this:
"deployment": {
"files": {
"main": {
"sourceUrl": "https://storage.googleapis.com/myproject/main.py"
},
}
},
However when I deployed again I got an error because it couldn't find the file with that URL. The gsutil has to use a bucket with gs://something and the file has to exist at https:// something. How can I upload the file to the project and specify the location in the app.json file?
You were probably using what is now called App Engine Standard environment. Deploys are handled with the gcloud app deploy command.
Example:
gcloud app deploy --version [YOUR_VERSION_ID] --no-promote --project [YOUR_PROJECT_ID]
Here is more info for Deploying a Python App
The deploy command uses app.yaml file for configuration info.

PyCharm cannot find app.yaml when pushing to GAE

I can't deploy to app engine.
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/appcfg.py -e my#gmail.com --passin --no_cookies update .
Usage: appcfg.py [options] update <directory> | [file, ...]
appcfg.py: error: Directory does not contain an app.yaml configuration file
Process finished with exit code 2
The project dir contains an app.yaml, which gae ca find when running the devserver.
Is this just another hopeless reinstall everything kind of problem?

Google App Engine: "[path]" does not refer to a valid SDK directory"

I'm running Ubuntu 13.10. Just downloaded the Python SDK for Google App Engine. I type
python appcfg.py udaproject319
and it says
ValueError: GAE_SDK_ROOT '/home/username/app/new_project_template' does not refer to a valid SDK directory
I have no idea what this means. What makes a directory an SDK directory? Here are the files I have in the folder:
appcfg.py favicon.ico main.py main.pyc
index.yaml udaproject319.yaml
After you download and unzip the SDK, you have a folder google_appengine somewhere. The sdk directory is something like /home/username/google_appengine
when you want deploy or start dev_appserver you type:
python [GAE_SDK_ROOT]/appcfg.py update [YOUR_APP_ROOT]
for example:
python /home/username/google_appengine/appcfg.py update /home/username/udaproject319

Categories