Can't update website built on Google Cloud Platform - python

I have set up a Google Cloud website using a simple hello world tutorial in Python. I have then edited my code (just changed Hello world to put out another text), and I want to redeploy\update the changes from the cloud shell bash, but I cant figure out what the command is. I have tried
gcloud deployment-manager deployments update euphoric-graph-242609 \
I get no errors, but nothing happens. And the website still shows Hello World instead of the edited code.
Here are the steps I used to create the website, following the App Engine tutorial steps - starting with
git clone
https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/standard_python37/hello_world
virtualenv --python python3 ~/envs/hello_world
source ~/envs/hello_world/bin/activate
pip install -r requirements.txt
python main.py
gcloud app create
gcloud app deploy app.yaml --project euphoric-graph-242609
and I just now realised running the last code again would redeploy the app. But is this the correct way of just updating the app? Or should I use some update command like I tried first?

Running gcloud app deploy app.yaml --project euphoric-graph-242609 again would re-deploy any changes.

Related

Why does running `gcloud run deploy`, exactly according to the tutorial, give the error "image must be specified"

I'm following the Google Cloud Run quickstart for Python, but it ain't working. I am running Python 3.7, have gcloud installed, and have all the necessary permissions on my project.
Here's the tutorial I followed: https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-python-service
Here's evidence I copied their four files into the right place locally:
******#penguin:~/helloworld$ ls -a
. .. Dockerfile .dockerignore main.py requirements.txt
And yet here's what I see when I run the final command, gcloud run deploy:
******#penguin:~/helloworld$ gcloud run deploy
ERROR: (gcloud.run.deploy) argument --image: Must be specified.
According to the error, I need to specify an image (presumably existing), yet the tutorial is explicit that running this command from the source directory will automatically build an image. Here's their line: "Deploy from source automatically builds a container image from source code and deploys it."
Thanks for any tips.
Supposing you are in the folder where the files are, you can create a docker image:
gcloud builds submit --tag gcr.io/your_project_id/image_name .
Then
gcloud run deploy service-name --image gcr.io/your_project_id/image_name
Ok, update your gcloud SDK. the Current version is 379, you are 62 versions behind, more than 1 year, and 1year ago, the gcloud run deploy to upload sources, build a container and deploy it on Cloud Run didn't exist

Is there a way to run an already-built python API from google cloud?

I built a functioning python API that runs from my local machine. I'd like to run this API from Google Cloud SDK, but after looking through the documentation and googling every variation of "run local python API from google cloud SDK" I had no luck finding anything that wouldn't involve me restructuring the script heavily. I have a hunch that "google run" or "API endpoint" might be what I'm looking for, but as a complete newbie to everything other than Firestore (which I would rather not convert my entire api into if I don't have to), I want to ask if there's a straightforward way to do this.
tl;dr The API runs successfully when I simply type "python apiscript.py" into local console, is there a way I can transfer it to Google Cloud without adjusting the script itself too much?
IMO, the easiest solution for portable app is to use Container. And to host the container in serverless mode, you can use Cloud Run.
In the getting started guide, you have python example. The main task for you is to create a Dockerfile
FROM python:3.9-slim
ENV PYTHONUNBUFFERED True
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# Install production dependencies.
RUN pip install -r requirements.txt
CMD python apiscript.py
I adapted the script to your description, and I assumed that you have a requirements.txt file for the dependencies.
Now, build your container
gcloud builds submit --tag gcr.io/<PROJECT_ID>/apiscript
Replace the PROJECT_ID by your project ID, not the name of the project (even if sometimes it's the same, it's a common mistake for the newcomers)
Deploy on Cloud Run
gcloud run deploy --region=us-central1 --image=gcr.io/<PROJECT_ID>/apiscript --allow-unauthenticated --platform=managed apiscript
I assume that your API is served on the port 8080. else you need to add a --port parameter to override this.
That should be enough
Here it's a getting started example, you can change the region, the security mode (here no security) the name and the project.
In addition, for this deployment, the Compute Engine default service account is used. You can use another service account if you want, but, in any cases, you need to grant the used service account the permission to access to the Firestore database.

How to upload and deploy django cookiecutter project, with docker, to heroku?

I am developing an app with django cookiecutter (with docker and heroku setup) and have come so far as to deploying it. This is my first ever project, so no prior experience with django, docker or heroku. I've read up on it at cookiecutter docs, heroku and a little on the docker website but I still don't how to deploy.
I have downloaded the heroki cli , have set up app on heroku with my own domain and a postgres db and I am planning on getting hobby tier to get the automated certificate. All environment variables are set in the .env file and are set in the heroku config vars in my app at heoku. So everything should be alright as far as code and settings. I am also using git as version control.
Am I supposed to upload the whole project (code, settings, docker files etc) to heroku with git or by some other means? I saw there was an option to deploy the project with docker deploys aswell at herokus website. What option is the correct one?
I was thinking initially that I would just upload the project through git and run docker-compose -f production.yml up (in the heroku bash)... or something like that and that. I dont know, please help.
If some info is missing or is unclear I will try edit it as best as I can.
It is better to deploy the project to Heroku using git.
$ heroku login
$ heroku create your_custom_app_name
$ git add --a
$ git commit -m "My custom app deployment to heroku"
$ git push heroku master
and then once it is deployed.
$ heroku python manage.py migrate

App directory doesn't show up on google cloud shell

Probably a stupid question.
I'm new to google app engine. So I followed the tutorial and successfully deployed their HelloWorld app, where the final steps are done on the cloud shell.
Then I built my own app in flask on my local machine, tested it (pushed the repo to the project's cloud repo) and deployed it from the command line (gcloud app deploy) and it works fine, anyone can use the app on their browser and I can also see the source code in the console website.
But I don't see any directories when I use the cloud shell. I get the prompt username#project-id:~$ but when I ls, there's just one README file and no other directories, therefore I can't use the devapp_sever.py, gcloud app deploy or any other shell function on this project.
But when I choose the hello world project that was created initially, the shell shows an src directory which contains the app's code and I can use the shell and deploy the app from there.
What's happening here and what am I supposed to do.?
Think of your Cloud Shell as just another workstation with local disk similar to your local machine. To deploy code to an app engine app, Google will create a Cloud Source Repository. Having said that, this is not related to your Cloud Shell. You can of course git clone any Git repo into your Cloud Shell.
Dan also wrote a nice explanation here -
https://stackoverflow.com/a/42123320/7947020
Hope this clarifies it!

Running simple python script continuously on Heroku

I have simple python script which I would like to host on Heroku and run it every 10 minutes using Heroku scheduler. So can someone explain me what I should type on the rake command at the scheduler and how I should change the Procfile of Heroku?
Sure, you need to do a few things:
Define a requirements.txt file in the root of your project that lists your dependencies. This is what Heroku will use to 'detect' you're using a Python app.
In the Heroku scheduler addon, just define the command you need to run to launch your python script. It will likely be something like python myscript.py.
Finally, you need to have some sort of web server that will listen on the proper Heroku PORT -- otherwise, Heroku will think your app isn't working and it will be in the 'crashed' state -- which isn't what you want. To satisfy this Heroku requirement, you can run a really simple Flask web server like this...
Code (server.py):
from os import environ
from flask import Flask
app = Flask(__name__)
app.run(environ.get('PORT'))
Then, in your Procfile, just say: web: python server.py.
And that should just about do it =)
If you use free account [unverified*] on Heroku (so you cannot install addons), instead of using "Heroku scheduler", use time.sleep(n). You don't need Flask or any server in this case, just place script, say, inside folder Scripts (in default app/project by Heroku) and add to Procfile: worker: python script.py. Of course you replace script.py with Path to your script, including name, ex. worker: python Scripts/my_script.py
Note: If your script uses third-party modules, say bs4 or requests, you need to install them in pipenv install MODULE_NAME or create requirements.txt and place it where manage.py, Procfile, Pipfile, (etc) are. Next place in that requirements.txt:
requirements.txt:
MODULE_NAME==MODULE_VERSION
You can check them in pip freeze | grep MODULE_NAME
Finally deploy to Heroku server using git and run following command:
heroku ps:scale worker=1
That's it! Bot/Script is running, check it in logs:
heroku logs --tail
Source: https://github.com/michaelkrukov/heroku-python-script
unverified* - "To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you will be directed to visit the verification site." It redirects to Credit Card info. However you can still have Free Acc, but you will not be able to use certain options for free users, such as installing addons:https://devcenter.heroku.com/articles/getting-started-with-python#provision-add-ons

Categories