【GCP】【Python】gcloudを使用する時に発生したエラーとその対処療法
この記事では、gcloudを使おうとした時にいくつかのエラーでかなり時間を浪費したので、その時の対処方法について書いていきます。
タイトルにもあるように、対処療法なのでなぜ発生しているのか深堀りしていませんし、正しい対応なのかについては保証していません。あくまでも動けばいいぐらいの前提です。
はじめに
背景として、Kaggle用にGCEをたててgcloudでポートフォワーディングしようと思ったのですが
- gcloudを実行するとエラーがでてしまい使えない
- 使えるようになったけどコマンドによってはまだエラーがでる
といった状態だったため、まずはgcloudを実行してもエラーが出ない状態にすることを目的として対応しました。
環境
本題
基本、下記コマンドを実行した時に発生するメッセージを対象としています。
gcloud auth login
問題① ERROR:root:code for hash md5 was not found.
エラーはもっと長いので、上記だけが出ているわけではないのですが、一番最初にでるのでこのメッセージで検索する人が多いかなと思い載せました。
エラーメッセージ
chittai:google-cloud-sdk chittai$ gcloud auth login ERROR:root:code for hash md5 was not found. Traceback (most recent call last): File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ValueError: unsupported hash type md5 ERROR:root:code for hash sha1 was not found. Traceback (most recent call last): File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ValueError: unsupported hash type sha1 ERROR:root:code for hash sha224 was not found. Traceback (most recent call last): File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ValueError: unsupported hash type sha224 ERROR:root:code for hash sha256 was not found. Traceback (most recent call last): File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ValueError: unsupported hash type sha256 ERROR:root:code for hash sha384 was not found. Traceback (most recent call last): File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ValueError: unsupported hash type sha384 ERROR:root:code for hash sha512 was not found. Traceback (most recent call last): File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/Users/chittai/.pyenv/versions/2.7.10/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ValueError: unsupported hash type sha512 ERROR: gcloud failed to load: 'module' object has no attribute 'sha256' gcloud_main = _import_gcloud_main() import googlecloudsdk.gcloud_main from googlecloudsdk.calliope import base from googlecloudsdk.calliope import arg_parsers from googlecloudsdk.core import log from googlecloudsdk.core import properties from googlecloudsdk.core import config from googlecloudsdk.core.util import files as file_utils class Checksum(object): def __init__(self, algorithm=hashlib.sha256): This usually indicates corruption in your gcloud installation or problems with your Python interpreter. Please verify that the following is the path to a working Python 2.7 executable: /Users/chittai/.pyenv/versions/2.7.10/bin/python2.7 If it is not, please set the CLOUDSDK_PYTHON environment variable to point to a working Python 2.7 executable. If you are still experiencing problems, please reinstall the Cloud SDK using the instructions here: https://cloud.google.com/sdk/
対処法
このメッセージはgcloud auth login に限らず出てきました。pipコマンドでもでています。調べて見たところ、openssl関連っぽかったのです。このエラーはPythonのバージョンを変更したら治りました。
pyenv install 2.7.16
問題② ERROR: gcloud crashed (SSLHandshakeError): [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
エラーメッセージ
gcloud crashed (SSLHandshakeError): [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
対処法
下記リンクに2つの対応方法について書かれています。 gcloudコマンドのSSLのチェックを無効化するという方法をとっていきます。
gcloud config set auth/disable_ssl_validation True
問題③ gcloud crashed (OperationalError): attempt to write a readonly database
エラーメッセージ
gcloud crashed (OperationalError): attempt to write a readonly database
対処法
読んで字のごとく、databaseへの書き込みができないのでデータベースファイルの権限を変更指定いきます。
対象ディレクトリ
~/.config/gcloud/
下記のファイルの権限に書き込み権限を付けます。
chmod 776 ~/.config/gcloud/access_tokens.db chmod 776 ~/.config/gcloud/credentials.db
感想
一旦、以上です。完全に自分用のメモですが、とりあえず動かしたいという時に使用できるようようにメモとして残しました。