运行 GCloud 实例上的 `rails c` 与应用程序引擎 gem

Run `rails c` on GCloud instace with appengine gem

我有一个部署到 GCP 的 Rails 6.0.0.rc1 应用程序(安装了 appengine gem)。有没有办法在运行应用程序的实例上登录远程 rails 控制台?我试过这个:

bundle exec rake appengine:exec -- bundle exec rails c

给出以下输出:

...
---------- EXECUTE COMMAND ----------
bundle exec rails c
Loading production environment (Rails 6.0.0.rc1)
Switch to inspect mode.
...

显然它执行了命令,但随后立即关闭了连接。

有没有简单的方法来做到这一点?

作为参考:在 Heroku 上,这将是:

heroku run rails c --app my-application

涉及几个步骤:

https://gist.github.com/kyptin/e5da270a54abafac2fbfcd9b52cafb61

If you're running a Rails app in Google App Engine's flexible environment, it takes a bit of setup to get to a rails console attached to your deployed environment. I wanted to document the steps for my own reference and also as an aid to others.

Open the Google App Engine -> instances section of the Google Cloud Platform (GCP) console.

Select the "SSH" drop-down for a running instance. (Which instance? Both of my instances are in the same cluster, and both are running Rails, so it didn't matter for me. YMMV.) You have a choice about how to connect via ssh.

Choose "Open in browser window" to open a web-based SSH session, which is convenient but potentially awkward.

Choose "View gcloud command" to view and copy a gcloud command that you can use from a terminal, which lets you use your favorite terminal app but may require the extra steps of installing the gcloud command and authenticating the gcloud command with GCP.

When you're in the SSH session of your choice, run sudo docker ps to see what docker containers are presently running.

Identify the container of your app. Here's what my output looked like (abbreviated for easier reading). My app's container was the first one.

jeff@aef-default-425eaf...hvj:~$ sudo docker ps CONTAINER ID IMAGE COMMAND NAMES 38e......552 us.gcr.io/my-project/appengine/default... "/bin/sh -c 'exec bun" gaeapp 8c0......0ab gcr.io/google_appengine/cloud-sql-proxy "/cloud_sql_proxy -di" focused_lalande 855......f92 gcr.io/google_appengine/api-proxy "/proxy" api 7ce......0ce gcr.io/google_appengine/nginx-proxy "/var/lib/nginx/bin/s" nginx_proxy 25f......bb8 gcr.io/google_appengine/fluentd-logger "/opt/google-fluentd/" fluentd_logger Note the container name of your app (gaeapp in my case), and run container_exec bash.

Add ruby and node to your environment: export PATH=$PATH:/rbenv/versions/2.3.4/bin:/rbenv/bin:/nodejs/bin

cd /app to get to your application code.

Add any necessary environment variables that your Rails application expects to your environment. For example: export DATABASE_URL='...'

If you don't know what your app needs, you can view the full environment of the app with cat app.yaml.

bin/rails console production to start a Rails console in the Rails production environment.