Shell to OpenShift Express and Environment Variables

02 Apr 2012
Posted by stylesen

It is possible to shell into the application space of OpenShift Express environment. This is a powerful feature which gives us full control if we are a command line junkie! If we have uploaded the public key from our current machine to http://openshift.redhat.com/ in our account, then we can do a password-less login to the application shell.

Open your application and view details in http://openshift.redhat.com/ which will contain the git repository url which will look like the following:

Copy the above which will act as our ssh string to connect. In order to connect via ssh use the following command which is derived from the above URL:

$ ssh e531c13c8c2341ebafc7f4f182fa1b48@squeez-stylesen.rhcloud.com

    Welcome to OpenShift shell

    This shell will assist you in managing OpenShift applications.

    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!
    Shell access is quite powerful and it is possible for you to
    accidentally damage your application.  Proceed with care!
    If worse comes to worst, destroy your application with 'rhc app destroy'
    and recreate it
    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!

    Type "help" for more info.

[squeez-stylesen.rhcloud.com ~]\>

Thus we get dropped inside the shell environment of our application in OpenShift.

A very useful stuff is to view all the available environment variables in OpenShift and use them at various places such as action hook scripts, in the application, etc. Once you have logged into the application shell environment as describedabove, we can view all the available shell environment variables as follows:

[squeez-stylesen.rhcloud.com ~]\> cd .env/
[squeez-stylesen.rhcloud.com .env]\> ls
OPENSHIFT_APP_DNS     OPENSHIFT_DB_MYSQL_51_DUMP          OPENSHIFT_DB_SOCKET     OPENSHIFT_GEAR_DNS    OPENSHIFT_INTERNAL_PORT  PATH
OPENSHIFT_APP_NAME     OPENSHIFT_DB_MYSQL_51_DUMP_CLEANUP   OPENSHIFT_DB_TYPE         OPENSHIFT_GEAR_NAME    OPENSHIFT_LOG_DIR     USER_VARS
OPENSHIFT_APP_UUID     OPENSHIFT_DB_MYSQL_51_EMBEDDED_TYPE  OPENSHIFT_DB_URL         OPENSHIFT_GEAR_TYPE    OPENSHIFT_REPO_DIR
OPENSHIFT_DATA_DIR     OPENSHIFT_DB_MYSQL_51_RESTORE          OPENSHIFT_DB_USERNAME     OPENSHIFT_GEAR_UUID    OPENSHIFT_RUN_DIR
OPENSHIFT_DB_CTL_SCRIPT  OPENSHIFT_DB_PASSWORD              OPENSHIFT_GEAR_CTL_SCRIPT  OPENSHIFT_HOMEDIR    OPENSHIFT_RUNTIME_DIR
OPENSHIFT_DB_HOST     OPENSHIFT_DB_PORT              OPENSHIFT_GEAR_DIR     OPENSHIFT_INTERNAL_IP    OPENSHIFT_TMP_DIR
[squeez-stylesen.rhcloud.com .env]\> echo $OPENSHIFT_GEAR_DNS
squeez-stylesen.rhcloud.com
[squeez-stylesen.rhcloud.com .env]\>

If we want to use these variables inside python code, we can do it as follows:

[squeez-stylesen.rhcloud.com .env]\> python
Python 2.6.6 (r266:84292, Sep 12 2011, 14:03:14)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['OPENSHIFT_GEAR_DNS']
'squeez-stylesen.rhcloud.com'
>>> exit()
[squeez-stylesen.rhcloud.com .env]\>

If you have a mysql catridge added to your application, then the database could be accessed as follows (this is why, I said it is a very powerful feature to have access to shell environment in OpenShift. A lot can be done with this!):

[squeez-stylesen.rhcloud.com ~]\> /usr/bin/mysql -u $OPENSHIFT_DB_USERNAME -p$OPENSHIFT_DB_PASSWORD -h $OPENSHIFT_DB_HOST $OPENSHIFT_APP_NAME
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.61 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from url_mapper;
+----------+-----------------------+---------------------+---------------------+
| hash     | url                   | created             | expiry              |
+----------+-----------------------+---------------------+---------------------+
| 4b5d02ad | http://www.google.com | 2012-04-01 04:24:54 | 2012-04-04 04:24:54 |
+----------+-----------------------+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> quit;
Bye
[squeez-stylesen.rhcloud.com ~]\>

NOTE: The MySQL dbname is same as app name in OpenShift, ie., $OPENSHIFT_APP_NAME is the database name of MySQL database created for the application.