尝试运行示例 python 应用程序时出错

时间:2021-07-18 05:35:41

标签: python google-cloud-platform grpc

我只想在 1 台虚拟机上运行它以通过 Web 界面 (8080) 访问它: 我所做的事情列表:创建了 vm (debian 9) 默认服务帐户 google。 之后,我按照说明执行了命令 由 Google 提供:

curl -sSO https://dl.google.com/cloudagents/install-logging- agent.sh 
sudo bash install-logging-agent.sh
apt-get update apt-get install -yq git supervisor python python-pip
pip install --upgrade pip
virtualenv useradd -m -d /home/pythonapp pythonapp
export HOME=/root
git clone -b steps https://github.com/GoogleCloudPlatform/getting-started-python.git
/opt/app virtualenv -p python3 /opt/app/gce/env
source /opt/app/gce/env/bin/activate
/opt/app/gce/env/bin/pip install -r /opt/app/gce/requirements.txt 
chown -R pythonapp:pythonapp /opt/app 
# Put supervisor configuration in proper place
cp /opt/app/7-gce/python-app.conf /etc/supervisor/conf.d/python-app.conf
cat >/etc/supervisor/conf.d/python-app.conf << EOF
[program:pythonapp]
directory=/opt/app/7-gce
command=/opt/app/7-gce/env/bin/honcho
start -f ./procfile worker bookshelf autostart=true autorestart=true user=pythonapp environment=VIRTUAL_ENV="/opt/app/7- gce/env",PATH="/opt/app/7-gce/env/bin",\
 HOME="/home/pythonapp",USER="pythonapp" stdout_logfile=syslog stderr_logfile=syslog
EOF
supervisorctl reread
supervisorctl update

我还编辑了文件 config.py:

    import os
    SECRET_KEY = 'secret'
    DATA_BACKEND = 'datastore'
    PROJECT_ID = 'soy-channel-319506'
    CLOUDSQL_USER = 'root'
    CLOUDSQL_PASSWORD = '1111'
    CLOUDSQL_DATABASE = 'bookshelf'(created)
    CLOUDSQL_CONNECTION_NAME = 'soy-channel-319506:us- 
    central1:bookshelfmysql'
    CLOUD_STORAGE_BUCKET = 'bookshelfstorage1'(created)
    MAX_CONTENT_LENGTH = 8 * 1024 * 1024
    ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])`

1 个答案:

答案 0 :(得分:0)

好的,我让它工作了。 Bookshelf 示例使用 Firestore,不使用 Cloud SQL。配置令人困惑 ./gce./bookshelf 等,但以下对我有用。

startup-script.sh

# Install Stackdriver logging agent
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

# Install or update needed software
apt-get update
apt-get install -yq git supervisor python3-pip python3-venv

# Account to own server process
useradd -m -d /home/pythonapp pythonapp

# Fetch source code
export HOME=/root
git clone https://github.com/GoogleCloudPlatform/getting-started-python.git /opt/app

# Python environment setup
python3 -m venv /opt/app/gce/env
source /opt/app/gce/env/bin/activate

# `grpcio` requires upgraded `pip`/`setuptools`
/opt/app/gce/env/bin/python3 -m pip install --upgrade pip
/opt/app/gce/env/bin/python3 -m pip install --upgrade setuptools
/opt/app/gce/env/bin/python3 -m pip install honcho

# Add `requirements.txt` from `bookshelf`
/opt/app/gce/env/bin/python3 -m pip install --requirement /opt/app/bookshelf/requirements.txt

# Move supervisor conf and procfile
cp /opt/app/gce/python-app.conf /opt/app/bookshelf
cp /opt/app/gce/procfile /opt/app/bookshelf

# Edit python-app.conf to reflect `bookshelf` directory
sed --in-place 's|directory=/opt/app/gce|directory=opt/app/bookshelf|g' /opt/app/bookshelf/python-app.conf

# Set ownership to newly created account
chown -R pythonapp:pythonapp /opt/app

# Put supervisor configuration in proper place
cp /opt/app/bookshelf/python-app.conf /etc/supervisor/conf.d/python-app.conf

# Start service via supervisorctl
supervisorctl reread
supervisorctl update

对于 GCP:

BILLING=...
PROJECT=...
REGION=...   # Firestore location
ZONE=...     # VM location
INSTANCE=... # VM instance name
ACCOUNT=...  # Service Account name
FIREWALL=... # Firewall rule name

# Enable services
for SERVICE in "appengine" "compute" "datastore" "firestore"
do
  gcloud services enable ${SERVICE}.googleapis.com \
  --project=${PROJECT}
done

# Or Compute Engine default
gcloud iam service-accounts create ${ACCOUNT} \
--display-name="Python Bookshelf VM" \
--description="Used by Python Bookshelf VM to access DB" \
--project=${PROJECT}

EMAIL=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com

# Minimally logging, datastore.user
for ROLE in "roles/compute.imageUser" "roles/datastore.user" "roles/logging.logWriter"
do
  gcloud projects add-iam-policy-binding ${PROJECT} \
  --member=serviceAccount:${EMAIL} \
  --role=${ROLE}
done

# Create Firestore
gcloud app create \
--region=${REGION} \
--project=${PROJECT}

gcloud firestore databases create \
--region=${REGION} \
--project=${PROJECT}

# Create VM w/ `startup-script.sh`
gcloud beta compute instances create ${INSTANCE} \
--machine-type=f1-micro \
--image-family=debian-10 \
--image-project=debian-cloud \
--metadata-from-file=startup-script=${PWD}/startup-script.sh \
--service-account=${EMAIL} \
--scopes=userinfo-email,cloud-platform \
--tags=http-server \
--zone=${ZONE} \
--project=${PROJECT}

# Check status
# Either
gcloud compute instances get-serial-port-output ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}

# Or
gcloud compute ssh ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}

# And
sudo journalctl \
--unit=google-startup-scripts.service \
--follow

# Configure Firewall
gcloud compute firewall-rules create ${FIREWALL} \
--allow=tcp:8080 \
--description="Allow port 8080 access to VMs tagged http-server" \
--source-ranges=0.0.0.0/0 \
--target-tags=http-server \
--project=${PROJECT}

那么:

IP=$(\
  gcloud compute instances describe ${INSTANCE} \
  --zone=${ZONE} \
  --project=${PROJECT} \
  --format="value(networkInterfaces[0].accessConfigs[0].natIP)")

curl http://${IP}:8080

enter image description here