Skip to content

Instantly share code, notes, and snippets.

@kyptin
Last active October 16, 2023 06:22
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kyptin/e5da270a54abafac2fbfcd9b52cafb61 to your computer and use it in GitHub Desktop.
Save kyptin/e5da270a54abafac2fbfcd9b52cafb61 to your computer and use it in GitHub Desktop.
Accessing a rails console in Google App Engine (flexible)

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.

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

  2. 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.

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

    2. 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.

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

  4. 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
    
  5. Note the container name of your app (gaeapp in my case), and run container_exec <container_name> bash.

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

  7. cd /app to get to your application code.

  8. 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.

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

@TravisSperry
Copy link

Thanks for posting this. I came across this looking for help and think I may have found an easier solution.

Once you're in the SSH session after step three I found these steps to be a little easier:

Once you have the container ID you can view logs by running:

$ sudo docker logs [CONTAINER-ID]

Or start a shell in the container that is running your code:

$ docker exec -it [CONTAINER-ID] /bin/bash

Once you've started a shell in the container that is running your code you can run bundle exec rails c

@will3216
Copy link

@kyptin & @TravisSperry thanks for this, its super helpful! I don't suppose either of you have found a more automated approach to this have you? If not, I'll probably look into it in the next week or so and post it here if I figure something out.

@will3216
Copy link

This isn't exactly pretty yet, but in case I forget to get back to this, I've gotten it down to 2 steps that don't require going into the web UI:

  1. SSH to your first running app instance:
gcloud --project $PROJECT_NAME app instances ssh $(gcloud app instances list --service=$SERVICE_NAME --filter=VM_STATUS:RUNNING --format=json | jq -r '.[0].id') --version $(gcloud app instances list --service=$SERVICE_NAME --filter=VM_STATUS:RUNNING --format=json | jq -r '.[0].version') --service=$SERVICE_NAME
  1. Start your console once you've SSH'd:
docker exec -it gaeapp /bin/bash -c "bundle exec rails c"

Note, this assumes yo have jq installed locally, and that your container has the default gaeapp container name.

Still working on seeing if I can get this down to a one-liner, but its one step closer!

@dennislysenko
Copy link

dennislysenko commented Jun 23, 2020

If you're still trying to get this down to a one-liner, you can pass --container=gaeapp to the gcloud command to ssh straight into the docker container, and then append the command you need, e.g. -- bundle exec rails c.

gcloud --project $PROJECT_NAME app instances ssh $(gcloud app instances list --service=$SERVICE_NAME --filter=VM_STATUS:RUNNING --format=json | jq -r '.[0].id') --version $(gcloud app instances list --service=$SERVICE_NAME --filter=VM_STATUS:RUNNING --format=json | jq -r '.[0].version') --service=$SERVICE_NAME --container=gaeapp -- /bin/bash -c "bundle exec rails c"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment