Google Cloud Functions: error deploying, main.py doesn't exist - python

Deploying Google Cloud Functions from the local system fails with the following error:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: File main.py that is expected to define function doesn't exist
I've used the console UI to check the contents of the package that was uploaded in the failed deployment and the file is present. The package was created using the gcloud CLI:
gcloud functions deploy <redacted> \
--trigger-http \
--runtime=python37 \
--region=europe-west1 \
--project=<redacted> \
--entry-point=<redacted>
For context, the same project was deployed successfully multiple times by several people but started failing for everyone new that checked it out after a specific date.

In our case, this was because of invalid credentials, i.e. the JSON file passed to ServiceAccountCredentials.from_json_keyfile_name was missing. Confusingly, the error doesn't mention anything about security, credentials or that missing file.
Secrets are not in version control and the shared vault had a stale file that did not match the path provided in the python script. This was fixed once the credentials were corrected.
We managed to isolate the problem to the authenticated call by successfully deploying 'dumb' functions (return a string) and then incrementally adding back functionality until it broke.

I solved this problem in my application in Node.js, basically I identified that the .gcloudignore file it invokes the .gitignore of the project, so if your function is in a skipped folder it simply does not find, then just remove the line #! Include : .gitignore file .gcloudignore

Related

Django, Webpack and Heroku: Error reading /app/webpack-stats.json

I'm trying to use Django with Webpack on Heroku via django-webpack-loader. After pushing the files from the repository to Heroku, I run webpack to generate the static files in /dist as well as webpack-stats.json:
npx webpack --config webpack.config.js
This works fine: both the /dist files and the webpack-stats.json file are correctly created (both are placed within /app/). However, when I try to load the page I get an error:
OSError at /
Error reading /app/webpack-stats.json. Are you sure webpack has generated the file and the path is correct?
The error doensn't say much other than the file /app/webpack-stats.json is missing; however, the file exists, its content is well-formatted and it's in the right location.

How to fix 'No such file or directory: '/home/jenkins/.kube/config'' when using load_incluster_config in python openshift rest client

I wrote a script that checks some secrets within an OpenShift cluster. I used the python rest-client library for Openshift and the script is executed within the cluster. But i always get IOError: [Errno 2] No such file or directory: '/home/jenkins/.kube/config'
I know that I don't have a kube config in the pod and therefore I tried to use the kubernetes.config.load_incluster_config() method to enable the in cluster config.
from kubernetes import client, config
from openshift.dynamic import DynamicClient
config.load_incluster_config()
k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)
I would assume that it is no longer necessary to provide a kube config with the load_incluster_config call. Did someone solve the problem with the rest client and openshift in cluster execution with a service account?
I appreciate any help, thanks.
I mean, you've probably already checked this, but are sure that you're in the right directory? Because running a file from the wrong directory can cause the error of "No such file or directory".
I solved it with the following:
if os.getenv('KUBERNETES_SERVICE_HOST'):
config.load_incluster_config()
else:
config.load_kube_config()
dyn_client = DynamicClient(ApiClient())
ApiClient is using the default configuration.

Flask app on Cloud Foundry - Command "python setup.py egg_info" failed with error code 1

I am trying to push a Python (3.6.5) app via Flask to Cloud Foundry (cf version 6.36.1+e3799ad7e.2018-04-04). The application takes a POST request (text file), does some text transformation, saves the new file, and returns a confirmation message. It works locally (tested via Postman). However, when attempting to push it to CF, it gives the following error -
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ygzuah5g/logging/
Could you please let me know how I can go about solving this issue? Thanks in advance. The entire files together are ~ 350 MB. I am using a manifest.yml
---
applications:
- name: textsum
memory: 512M
command: python server.py
buildpack: https://github.com/cloudfoundry/buildpack-python.git
PS - Not sure if this is helpful, I do have import queue in one of my files. If I change it to import Queue (Py 2x compatible) and use Py 2.7.15 runtime, the cf push is successful, but it throws runtime errors
ERROR in app: Exception on / [POST]
File "/home/vcap/deps/0/python/lib/python2.7/subprocess.py", line 1047, in _execute_child
ERR raise child_exception
ERR OSError: [Errno 2] No such file or directory
ERR 10.0.65.11 - - [12/Jun/2018 20:56:16] "POST / HTTP/1.1" 500 -
First. Don't do this in your manifest.yml:
buildpack: https://github.com/cloudfoundry/buildpack-python.git
This is telling Cloud Foundry to use the master branch of the buildpack which can change quite frequently and there are no guarantees of it's fitness (i.e. it could even be broken).
Instead, make sure that you are using a release. Releases are tested and the code won't change out from under you.
You can do this by using the platform supplied Python buildpack, simply remove this line or put python_buildpack as the name, or you can point to buildpack: https://github.com/cloudfoundry/buildpack-python.git#v1.6.17 where v1.6.17 is a release from here.
https://github.com/cloudfoundry/python-buildpack/releases
Second, it seems like you need to tell Cloud Foundry & the buildpack which version of Python you want. If you need Python 3, then you should tell it that. See here for instructions on doing that.
https://stackoverflow.com/a/50837696/1585136
Third, this error:
ERR OSError: [Errno 2] No such file or directory
Seems like you're trying to write to a location that does not exist. Where are you trying to write your file? The paths will be different on your local machine vs running on Cloud Foundry. Typically on Cloud Foundry, you would write temporary files to /home/vcap/tmp or /app/tmp (/app is a symlink to /home/vcap) or $HOME/tmp. They all point to the same place.
I'm specifically mentioning temporary files here because you don't want to write permanent files to the local file system on Cloud Foundry. The local file system is ephemeral and your data will not persist for very long. See here for details.
https://docs.cloudfoundry.org/devguide/deploy-apps/prepare-to-deploy.html#filesystem
Hope that helps!

Unable to run dev_appserver.py with gcloud

I'm attempting to use the command dev_appserver.py ./ from my application directory, but it's throwing the following error.
ERROR 2016-01-04 04:47:51,421 wsgi.py:263]
Traceback (most recent call last):
File "/Users/Joshua/Scripts/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/Joshua/Scripts/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Users/Joshua/Scripts/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/Joshua/Projects/inkren/skritter-api/main.py", line 2, in <module>
from gcloud import datastore
ImportError: No module named gcloud
I'm running a virtualenv that has gcloud installed and also have the gcloud sdk installed with the proper python component. The gcloud library is found when I run python ./main.py.
# main.py
import webapp2
from gcloud import datastore
class MainPage(webapp2.RequestHandler):
def get(self):
client = datastore.Client(dataset_id='my-project')
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
What I'd like to do is be able to run my code locally in the browser. Is there a way to get dev_appserver.py to recognize the gcloud library?
Is using the python development server from gcloud still a problem? I'm successfully using dev_appserver.py from the terminal. See reference.
Perhaps the install and setup instructions have improved since a year ago. I believe the installation puts the file, dev_appserver.py, in your system path, even on Windows, so it is accessible from the terminal.
I did a bit of research on this, so... Okay, here's what you've got to do.
takes a breath
Head over to this gentleman's GitHub page, and download the package as a zip. https://github.com/dhermes/test-gcloud-on-gae
Now, you need to add some folders/files from his folder into yours to get gcloud working.
1) Open the application folder and put the vendor folder into your root directory. This folder contains the gcloud package, as well as many others. You can delete the others if you want.
2) Add the appengine_config.py into your root directory as well. This file tells app engine where your third party packages are.
3) Add darth.py to your root directory as well. This is just a help file.
Actually, that should do it. This gentleman wrote some scripts for using third party apps with app engine, and they're quite useful. Let me know if your import works after doing that!
I tested it locally and it works perfectly. Basically, any third party python packages work with app engine, as long as there are no other languages within them such as C.

File not accessible error (setuptools) in logs

When I run my app, my app engine logs give me this error:
WARNING 2012-03-01 23:27:31,089 py_zipimport.py:139] Can't open
zipfile/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/setuptools-0.6c11-py2.7.egg:
IOError: [Errno 13] file not accessible: '/Library/Frameworks/Python.framework/
Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg'
What does this mean?
Errno 13 is EACCES. It means "permission denied". So the access permissions do not allow you to access that file. Check the permissions with ls -l /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
A list of error names and explanations is usually found in the manual page for errno, or the C include file errno.h.
This may happen after building a new library. For example, it happened to me after building lxml. Remake your symlinks from the appengine and it should fix your issue.
The answer you have marked as accepted isn't helpful. An annoying bug in the App Engine SDK actually throws these errors when running the development server with Python 2.7. Here's how I fixed it:
Edit the file <local path to app engine>/google/appengine/tools/dev_appserver_import_hook.py
On most systems the local path to App Engine is /usr/local/google_appengine
Search for py27_optional=False (around line 477) and replace it with py27_optional=True
Every time you update your local App Engine SDK, you will need to redo this patch.
Credit Carl D'Halluin

Categories