googlecodelabs / feedback

Provide feedback to our codelabs by filing an issue here
18 stars 22 forks source link

[cloud-run-wagtail]: ValueError, "No frame marked with %s." #1264

Closed songproducer closed 1 year ago

songproducer commented 1 year ago

At step 7 when running the migrate command

python manage.py migrate && python manage.py collectstatic --noinput --clear

I get this:

Traceback (most recent call last):
  File "/home/leotreasure/wagtail-cloudrun/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/dist-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/dist-packages/django/core/management/__init__.py", line 382, in execute
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.9/dist-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.9/dist-packages/django/conf/__init__.py", line 89, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.9/dist-packages/django/conf/__init__.py", line 217, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/leotreasure/wagtail-cloudrun/myproject/settings.py", line 5, in <module>
    import environ
  File "/home/leotreasure/.local/lib/python3.9/site-packages/environ.py", line 114
    raise ValueError, "No frame marked with %s." % fname

I checked which python returns: /usr/bin/python and python --version: Python 3.9.2

glasnt commented 1 year ago

Hi @songproducer.

You're getting an error after running import environ, which presumably should be from django-environ, but that ValueError is not present in the django-environ==0.10.0 code. It does however match line 114 in the source dist of environ==1.0.0, which is a different package.

Please confirm your requirements.txt and virtualenv from step 6 that you are installing django-environ, not environ.

songproducer commented 1 year ago

Hi @glasnt I checked the requirements file and it's definitely django-environ==0.10.0

Here's the full wagtail-cloudrun/requirements.txt

Django>=4.2,<4.3
wagtail>=5.1,<5.2
gunicorn==20.1.0
psycopg2-binary==2.9.6
django-storages[google]==1.13.2
django-environ==0.10.0

There was an empty requirements.txt file in the parent directory so I copied the requirements from the wagtail-cloudrun/requirements.txt but it didn't help.

I'm sorry I'm not sure how to check the virtualenv.

glasnt commented 1 year ago

While in your virtualenv, run pip freeze to get a list of all the packages

songproducer commented 1 year ago

Although the instructions before told me to delete the venv, I recreated and activated it and installed pip -r requirements and pip freeze but got django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

Then I tried the command again:

gcloud secrets add-iam-policy-binding django_superuser_password \
  --member serviceAccount:${SERVICE_ACCOUNT} \
  --role roles/secretmanager.secretAccessor

but get

ERROR: Policy modification failed. For a binding with condition, run "gcloud alpha iam policies lint-condition" to identify issues in condition.
ERROR: (gcloud.secrets.add-iam-policy-binding) INVALID_ARGUMENT: Invalid service account ().

Here's a list of pip freeze:

absl-py==1.4.0
anyascii==0.3.2
anyio==3.7.1
asgiref==3.7.2
astroid==2.15.6
asttokens==2.2.1
astunparse==1.6.3
attrs==23.1.0
autopep8==1.5.5
backcall==0.2.0
beautifulsoup4==4.11.2
bleach==6.0.0
blinker==1.6.2
cachetools==5.3.1
certifi==2023.5.7
cffi==1.15.1
charset-normalizer==3.2.0
click==8.1.5
cryptography==41.0.2
dbus-python==1.2.16
decorator==5.1.1
defusedxml==0.7.1
dill==0.3.6
distlib==0.3.6
Django==4.2.3
django-environ==0.10.0
django-filter==23.2
django-modelcluster==6.0
django-permissionedforms==0.1
django-storages==1.13.2
django-taggit==4.0.0
django-treebeard==4.7
djangorestframework==3.14.0
dnspython==2.4.0
draftjs-exporter==2.1.7
environ==1.0
et-xmlfile==1.1.0
exceptiongroup==1.1.2
executing==1.2.0
filelock==3.12.2
filetype==1.2.0
flake8==3.8.4
Flask==2.3.2
Flask-PyMongo==2.3.0
flatbuffers==23.5.26
gast==0.4.0
gce-v2v @ https://storage.googleapis.com/velostrata-release/gce-v2v/gce-v2v.tar.gz
google-api-core==2.11.1
google-api-python-client==2.93.0
google-auth==2.22.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==1.0.0
google-cloud-appengine-logging==1.3.1
google-cloud-audit-log==0.2.5
google-cloud-bigquery==3.11.3
google-cloud-core==2.3.3
google-cloud-datastore==2.16.1
google-cloud-language==2.10.1
google-cloud-logging==3.6.0
google-cloud-spanner==3.36.0
google-cloud-storage==2.10.0
google-cloud-translate==3.11.2
google-cloud-videointelligence==2.11.3
google-cloud-vision==3.4.4
google-crc32c==1.5.0
google-pasta==0.2.0
google-resumable-media==2.5.0
googleapis-common-protos==1.59.1
grpc-google-iam-v1==0.12.6
grpcio==1.56.0
grpcio-status==1.56.0
gunicorn==20.1.0
h11==0.14.0
h5py==3.9.0
html5lib==1.1
httpcore==0.17.3
httplib2==0.22.0
idna==3.4
importlib-metadata==6.8.0
ipython==8.14.0
isort==5.12.0
itsdangerous==2.1.2
jaraco.classes==3.3.0
jedi==0.17.0
jeepney==0.8.0
Jinja2==3.1.2
jsonschema==4.18.3
jsonschema-specifications==2023.6.1
keras==2.13.1
keyring==24.2.0
keyrings.google-artifactregistry-auth==1.1.2
l18n==2021.3
lazy-object-proxy==1.6.0
libclang==16.0.6
Markdown==3.4.3
MarkupSafe==2.1.3
matplotlib-inline==0.1.6
mccabe==0.6.1
mercurial==5.6.1
more-itertools==9.1.0
mypy==1.4.1
mypy-extensions==1.0.0
numpy==1.24.3
oauth2client==4.1.3
oauthlib==3.2.2
openpyxl==3.1.2
opt-einsum==3.3.0
packaging==23.1
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==10.0.0
pillow-heif==0.12.0
platformdirs==3.9.1
pluggy==1.2.0
prompt-toolkit==3.0.39
proto-plus==1.22.3
protobuf==4.23.4
psycopg2==2.9.6
psycopg2-binary==2.9.6
ptyprocess==0.7.0
pure-eval==0.2.2
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycodestyle==2.6.0
pycparser==2.21
pycurl==7.43.0.6
pydocstyle==6.3.0
pyflakes==2.2.0
Pygments==2.15.1
PyGObject==3.38.0
pylint==2.17.4
pyls-mypy==0.1.8
pymongo==4.4.1
pyparsing==3.1.0
python-apt==2.2.1
python-dateutil==2.8.2
python-jsonrpc-server==0.4.0
python-language-server==0.35.1
pytoolconfig==1.2.5
pytz==2023.3
PyYAML==6.0
referencing==0.29.1
requests==2.31.0
requests-oauthlib==1.3.1
rope==1.9.0
rpds-py==0.8.10
rsa==4.9
SecretStorage==3.3.3
six==1.16.0
sniffio==1.3.0
snowballstemmer==2.2.0
soupsieve==2.4.1
sqlparse==0.4.4
stack-data==0.6.2
supervisor==4.2.2
telepath==0.3.1
tensorboard==2.13.0
tensorboard-data-server==0.7.1
tensorflow==2.13.0
tensorflow-estimator==2.13.0
tensorflow-io-gcs-filesystem==0.32.0
termcolor==2.3.0
toml==0.10.2
tomli==2.0.1
tomlkit==0.11.8
traitlets==5.9.0
typing-extensions==4.5.0
ujson==5.8.0
uritemplate==4.1.1
urllib3==1.26.16
virtualenv==20.24.0
wagtail==5.1
wcwidth==0.2.6
webencodings==0.5.1
Werkzeug==2.3.6
willow==1.6
wrapt==1.15.0
yapf==0.40.1
zipp==3.16.2
glasnt commented 1 year ago

At the very least you've lost the value in the $SERVICE_ACCOUNT variable. I'd suggest you restart the codelab with a fresh virtualenv. My apologies for the inconvenience.

songproducer commented 1 year ago

I tried restarting, reinstalling wagtail, recreating venv and was able to get to this error:

django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

I guess because in the settings it refers to SECRET_KEY = "env("SECRET_KEY"")

but in the previous step removed the .env file (end of Step 5).

I tried hardcoding the SECRET_KEY in the settings file but then got an error about setting the Database URL

django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

glasnt commented 1 year ago

You set your environment variables when you create the .env file at the end of step 5. You do remove the env file, but only after you upload to secret manager. If you're running this in a local environment, you'll need to copy the .env back down, as mentioned in step 10. Hope this helps

songproducer commented 1 year ago

I was unable to get it working:

gcloud run jobs create migrate   --region $REGION   --image gcr.io/${PROJECT_ID}/myimage   --set-cloudsql-instances ${PROJECT_ID}:${REGION}:myinstance   --set-secrets APPLICATION_SETTINGS=application_settings:latest   --service-account $SERVICE_ACCOUNT   --command migrate

returned:

Creating Cloud Run job [migrate] in project [wagtail-394813] region [us-central1]
X  Creating job...                                                                                                                                                                                                
Job failed to deploy                                                                                                                                                                                              
ERROR: (gcloud.run.jobs.create) INVALID_ARGUMENT: spec.template.metadata.annotations: Invalid cloud sql instance names: [':us-central1:myinstance']. Instance names must be 97 characters or less, matching [a-zA-Z0-9-]{1,}:[a-zA-Z0-9-]{1,}:[a-z][a-z0-9-]{0,}, and cannot end in an hyphen
- '@type': type.googleapis.com/google.rpc.BadRequest
  fieldViolations:
  - description: "Invalid cloud sql instance names: [':us-central1:myinstance']. Instance\
      \ names must be 97 characters or less, matching [a-zA-Z0-9-]{1,}:[a-zA-Z0-9-]{1,}:[a-z][a-z0-9-]{0,},\
      \ and cannot end in an hyphen"
    field: spec.template.metadata.annotations
glasnt commented 1 year ago

Whenever you have commands that use variables like this, it's a good idea to echo out the value to confirm that all the variables have values set. In this case, you can see the error is about the Cloud SQL instance, which is set in your command with ${PROJECT_ID}:${REGION}:myinstance, but the error message says:us-central1:myinstance, so you can see that there's evidence that the PROJECT_ID variable hasn't been set. This was part of step 5.