Total Pageviews

Showing posts with label openshift. Show all posts
Showing posts with label openshift. Show all posts

Monday, 21 April 2014

安装Openshift的开发工具RHC

OpenShift是红帽公司推出的一个云计算服务平台,开发人员可以用它来构建和发布web应用。Openshift广泛支持多种编程语言和框 架,如Java,Ruby和PHP等。另外它还提供了多种集成开发工具如Eclipse integration,JBoss Developer Studio和 Jenkins等。OpenShift 基于一个开源生态系统为移动应用,数据库服务等,提供支持。
Ubuntu
  1. 使用sudo apt-get install ruby-full rubygems git-core安装ruby rubygems git
  2. 使用sudo gem install rhc安装rhc工具
  3. 使用rhc setup配置rhc即可
Fedora
  1. 使用sudo yum install rubygem-rhc安装RHC,这个命令会自动安装相关组件包括git,ruby,gem等。
  2. 使用rhc setup配置rhc工具
Linux Mint
  1. 使用sudo apt-get install ruby-full rubygems git-core安装ruby rubygems git
  2. 使用sudo gem install rubygems-update安装gem更新工具,再使用sudo update_rubygems更新gem
  3. 使用sudo gem install rhc安装rhc工具
  4. 使用rhc setup配置rhc即可
备注:如果需要升级rhc,执行sudo gem update rhc即可。

Friday, 6 December 2013

openshift.com/Python

Free Python Hosting:
openshift.com/Python
Run Your Python Apps on OpenShift。

Wednesday, 27 November 2013

OpenShift的客户端rhc使用指南

A quickstart to the OpenShift command line client tools (rhc)

OpenShift is a PaaS from Red Hat. It allows for deployment (and scaling) of apps written using Java, PHP, Ruby, Python, Perl including a number of frameworks and with support for MySQL, MongoDB, SQLite, PostgreSQL and even provides continuous integration tools (Jenkins), it’s definitely an interesting range of tools.
The deployments can be controlled via a REST api or the rhc command line tool. This short guide aims at helping the newcomers to get started quickly with the rhc command.

Installing the tools

There is a YUM repo available for Fedora and RHEL systems, but rhc is a Ruby gem and it means you can install it with gem once your gems build environment is setup properly.
$ gem install rhc

Creating a domain

You should have created already an account on OpenShift, if you haven’t please do, then you’ll be able to chose you domain name. This is a namespace binded to your username and will affect the apps url, which will look like $APPNAME-$DOMAINNAME.rhcloud.com
$ rhc domain create $NAMESPACE -l $USERNAME

Creating your first app

Very simple step needed to create an app inside the namespace defining some of its peculiarities.
$ rhc app create $APPNAME -l $USERNAME -t ruby-1.9
This will create an app of type ruby-1.9 called $APPNAME, but other types can be used (as of January 21st 2013):
diy-0.1             Do-It-Yourself
jbossas-7           JBoss Application Server 7.1
jbosseap-6.0        JBoss Enterprise Application Platform 6.0
jenkins-1.4         Jenkins Server 1.4
nodejs-0.6          Node.js 0.6
perl-5.10           Perl 5.10
php-5.3             PHP 5.3
python-2.6          Python 2.6
ruby-1.8            Ruby 1.8
ruby-1.9            Ruby 1.9
jbossews-1.0        Tomcat 6 (JBoss EWS 1.0)
zend-5.6            Zend Server 5.6
Here is a complete list of commands you can use to control your app
create             Create an application and adds it to a domain
git-clone          Clone and configure an application's repository locally
delete             Delete an application from the server
start              Start the application
stop               Stop the application
force-stop         Stops all application processes
restart            Restart the application
reload             Reload the application's configuration
tidy               Clean out the application's logs and tmp directories and tidy up the git repo on the server
show               Show information about an application
status             Show status of an application's gears

Adding cartridges (eg. a database or cron)

This is needed to add support for a specific feature to your app. We are adding here the mysql-5.1 cartridge.
$ rhc cartridge add mysql-5.1 -a $APPNAME -l $USERNAME
Here is a list of available cartridges (as of January 21st 2013)
10gen-mms-agent-0.1 10gen Mongo Monitoring Service Agent 0.1
cron-1.4            Cron 1.4
haproxy-1.4         HAProxy 1.4
jenkins-client-1.4  Jenkins Client 1.4
mongodb-2.2         MongoDB NoSQL Database 2.2
mysql-5.1           MySQL Database 5.1
metrics-0.1         OpenShift Metrics 0.1
phpmyadmin-3.4      phpMyAdmin 3.4
postgresql-8.4      PostgreSQL Database 8.4
rockmongo-1.1       RockMongo 1.1
switchyard-0.6      SwitchYard 0.6

Using rhc from another (additional) client

A very common need is to access the same account from different workstations, in this case you’ll have to add the user’s public SSH keys to the account.
$ rhc sshkey add $KEYNAME /path/to/key.pub -l $USERNAME
$NAME is a general identifier. You can customize which is the default SSH key to be used by rhc in its config file.
$ cat ~/.openshift/express.conf 
# SSH key file
#ssh_key_file = 'libra_id_rsa'
# Default rhlogin to use if none is specified
#default_rhlogin=$USERNAME

Troubleshooting your app

Two very powerful commands allow you to tail log files and to create local copies of the entire workspace where your app is running into remotely.
$ rhc tail $APPNAME -l $USERNAME
This executes the regular tail command on the target host, copying back the output. You can use any other tail option passing the -o argument.
Finally you can get a snapshot of the working environment to look, for example, at custom files or even the execution environment variables.
$ rhc snapshot save $APPNAME -l $USERNAME

$ tar xzf $APPNAME.tar.gz

$ ls $APPID/.env/
OPENSHIFT_APP_CTL_SCRIPT
OPENSHIFT_APP_NAME
OPENSHIFT_CONTAINER_UUID
OPENSHIFT_INTERNAL_IP
OPENSHIFT_REPO_DIR
PATH
OPENSHIFT_APP_DIR
OPENSHIFT_APP_TYPE
OPENSHIFT_DATA_DIR
OPENSHIFT_INTERNAL_PORT
OPENSHIFT_RUN_DIR
OPENSHIFT_APP_DNS
OPENSHIFT_APP_UUID
OPENSHIFT_HOMEDIR
OPENSHIFT_LOG_DIR
OPENSHIFT_TMP_DIR

$ cat $APPID/.env/OPENSHIFT_DATA_DIR 
export OPENSHIFT_DATA_DIR='/var/lib/libra/0f5c99f17db74cf3a93edcf330401208/hellotornado/data/

Notes

The tool is quite easy to manage and can be updated easily as it is a gem. There are also a lot of other interesting features you may want to look at, like the port forwarding but this was just a quickstart intended to provide a short reference. One last note before closing: you can SSH into your remote system:
$ rhc domain show
  hellotornado @ http://hellotornado-gfidente.rhcloud.com/
  ========================================================
    Application Info
    ================
      Created   = Jan 19  5:24 PM
      UUID      = ca5b02c422f04691959d6235de18ff81
      Gear Size = small
      Git URL   = ssh://ca5b02c422f04691959d6235de18ff81@hellotornado-gfidente.rhcloud.com/~/git/hellotornado.git/
      SSH URL   = ssh://ca5b02c422f04691959d6235de18ff81@hellotornado-gfidente.rhcloud.com
    Cartridges
    ==========
      diy-0.1

$ ssh ca5b02c422f04691959d6235de18ff81@hellotornado-gfidente.rhcloud.com

    Welcome to OpenShift shell

    [...] cut

[hellotornado-opinoid.rhcloud.com ~]> help
Help menu: The following commands are available to help control your openshift
application and environment.

ctl_app         control your application (start, stop, restart, etc)
ctl_all         control application and deps like mysql in one command
tail_all        tail all log files
export          list available environment variables
rm              remove files / directories
ls              list files / directories
ps              list running applications
kill            kill running applications
mongo           interactive MongoDB shell
Lot of fun in there. There is also a shortcut for the interactive mongo shell. There doesn’t seem to be any for mysql or postgres but you can use the standard command line tools or access to them via port forward as per this blog entry.

from https://github.com/giulivo/openshift-rhc-quickstart

在Openshift 上建立一个Python tornado的服务环境

官方这里有简单详细的教程,我照着做一次,顺便记录一下过程及遇到的问题。
在git bash 面板建立一个应用


1
rhc app create py27 diy-0.1
或者在Openshift 网站上建立一个DIY 的环境 然后用PuTTY 登录到你刚才建的环境

安装Python-2.7

依次敲入下面命令


1
2
3
4
5
6
7
cd $OPENSHIFT_TMP_DIR
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar jxf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure --prefix=$OPENSHIFT_DATA_DIR
make install
$OPENSHIFT_DATA_DIR/bin/python -V
会显示 Python 2.7.3

安装Setuptools

官方的示例装不了,用下面的方法


1
2
3
cd $OPENSHIFT_TMP_DIR
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
$OPENSHIFT_DATA_DIR/bin/python ez_setup.py
看到$OPENSHIFT_DATA_DIR 下有个setuptools-1.1.6.tar.gz


1
2
3
tar zxf setuptools-1.1.6.tar.gz
cd setuptools-1.1.6
$OPENSHIFT_DATA_DIR/bin/python setup.py install

安装pip

这里有个较简单的方法


1
2
3
cd $OPENSHIFT_TMP_DIR
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$OPENSHIFT_DATA_DIR/bin/python get-pip.py
接下来就可以用pip 安装各种东西了。注意,调用的路径是$OPENSHIFT_DATA_DIR/bin/pip,python 的路径是$OPENSHIFT_DATA_DIR/bin/python,如果单敲python -V 会看到 Python 2.6.6

安装Tornado

可通过pip 安装


1
$OPENSHIFT_DATA_DIR/bin/pip install tornado
我还要安装:


1
2
$OPENSHIFT_DATA_DIR/bin/pip install markdown
$OPENSHIFT_DATA_DIR/bin/easy_install Pygments
参考 https://github.com/giulivo/openshift-hellotornado
----------------------------------------------------------------------

Enabling Python 2.7 on a PaaS with the OpenShift DIY App Type


Preface

I have been using OpenShift for a few months now and have to say that I still love it. I've sampled most of the other PaaS solutions and feel most comfortable with OpenShift. One of the features I enjoy most in the service is the "do-it-yourself" application. With this, you can run just about anything in a "minimal-hassle", scalable environment.
I created a quickstart a few weeks ago that had Django running on Python 2.7 in "DIY" mode. This will use some of that, but be a more "step-by-step" approach for newcomers. This example will use the Flask micro-framework as well as the uWSGI application container for a true, production ready environment.

Diving In

The first thing you will need to do is get an OpenShift account (it's free).

Client Tools

Install the OpenShift client tools:
gem install rhc
** See here if you have trouble getting the tools installed.

Application Setup

Once you have the toolchain setup, we will create the application environment in OpenShift. For this example, we will name our application "py27".
rhc app create py27 diy-0.1
You will see something similar to:
Creating application: py27 in ehazlett
Now your new domain name is being propagated worldwide (this might take a minute)...
Confirming application 'py27' is available:  Success!

py27 published:  http://py27-ehazlett.rhcloud.com/
git url:  ssh://uuid1234567890@py27-ehazlett.rhcloud.com/~/git/py27.git/
Disclaimer: This is an experimental cartridge that provides a way to try unsupported languages, frameworks, and middleware on Openshift.
You can view the bootstrapped application at http://py27-[namepsace].rhcloud.com

Python 2.7

Now that the application environment is created, we will build and install the latest Python 2.7.x release. To do that, we will first need to get the application SSH credentials.
Run the following to show your application config (enter your account password when prompted):
rhc app show -a py27
You should see something similar to this:
Application Info
================
py27
    Framework: diy-0.1
     Creation: 2012-05-15T22:54:09-04:00
         UUID: 1qaz2wsx3edc4rfv
      Git URL: ssh://1qaz2wsx3edc4rfv@py27-ehazlett.rhcloud.com/~/git/py27.git/
   Public URL: http://py27-ehazlett.rhcloud.com/

 Embedded:
      None
Log into your OpenShift application using the SSH credentials from above (in the Git URL line):
ssh 1qaz2wsx3edc4rfv@py27-[username].rhcloud.com
We will build everything in the OpenShift application tmp directory.
Navigate into the "tmp" directory:
cd $OPENSHIFT_TMP_DIR
Download the latest Python 2.7.x release:
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
Extract:
tar jxf Python-2.7.3.tar.bz2
Configure (to put the custom Python into the OpenShift runtime dir):
cd Python-2.7.3
./configure --prefix=$OPENSHIFT_DATA_DIR
Make and install:
make install
You can now check that Python was successfully installed:
$OPENSHIFT_DATA_DIR/bin/python -V
You should get
Python 2.7.3

Supporting Tools

We now have Python installed, but we can't do much. Part of the common Python toolchain is PIP for Python package management.

Setuptools

Change into the OpenShift "tmp" directory:
cd $OPENSHIFT_TMP_DIR
Download and install setuptools:
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar zxf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
Install:
$OPENSHIFT_DATA_DIR/bin/python setup.py install

PIP

Change into the OpenShift "tmp" directory:
cd $OPENSHIFT_TMP_DIR
Download and install setuptools:
wget http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz
tar zxf pip-1.1.tar.gz
cd pip-1.1
Install:
$OPENSHIFT_DATA_DIR/bin/python setup.py install

Application

Okay, so now we have all of the parts needed to run the app. We now need to create the Flask application and configure OpenShift so that it knows how to start and stop it.
First we will create the Flask application. Logout of your OpenShift environment and navigate into the Git repository that was created for your app.
Create a file named application.py in the repo, for example - in the diy folder:
    ??? README
    ??? diy
    ?   ??? index.html
    ?   ??? testrubyserver.rb
    ?   ??? application.py
    ??? misc
BTW, the index.html and testrubyserver.rb are not longer necessary and can be removed anytime.
Edit application.py with the following:
from flask import Flask
import platform
application = Flask(__name__)
 
@application.route("/")
def index():
    return 'Hello from Flask'
 
@application.route("/info")
def info():
    return platform.python_version()
Create a requirements.txt in the root application directory with the following:
uWSGI==1.2.3
Flask==0.8
Directory structure should look like:
??? README
??? diy
?   ??? application.py
?   ??? index.html
?   ??? testrubyserver.rb
??? misc
??? requirements.txt

Hooks

We will now create the hooks that are needed for deployment as well as for starting and stopping the application. In your application Git repository, there is a hidden directory called ".openshift" that contains stubs for the action hooks. We will be editing these.
Edit .openshift/action_hooks/build to have the following:
$OPENSHIFT_DATA_DIR/bin/pip install --use-mirrors -r $OPENSHIFT_REPO_DIR/requirements.txt
Edit .openshift/action_hooks/start to have the following:
$OPENSHIFT_DATA_DIR/bin/uwsgi -s $OPENSHIFT_DIY_IP:$OPENSHIFT_DIY_PORT --socket-protocol http --pp $OPENSHIFT_REPO_DIR/diy --module application -d $OPENSHIFT_DIY_LOG_DIR/app.log --pidfile $OPENSHIFT_TMP_DIR/uwsgi.pid
Edit .openshift/action_hooks/stop to the following:
kill `cat $OPENSHIFT_TMP_DIR/uwsgi.pid`

Deploy

Commit all of the changes to the repository:
git add .
git commit -am "initial commit"
Deploy to OpenShift:
git push
You should now be able to browse and see your application running:
curl http://py27-[namespace].rhcloud.com
Should return:
Hello from Flask
And to verify that your application is using Python 2.7:
curl http://py27-[namespace].rhcloud.com/info
Should return:
2.7.3
 
from https://www.openshift.com/blogs/enabling-python-27-on-a-paas-with-the-openshift-diy-app-type 

Openshift DIY python环境的常用变量


Openshift DIY 确实很方便,记录一些常用环境变量,以备日后查询。

在 os.environ 里面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
OPENSHIFT_MYSQL_DIR: /var/lib/openshift/526b927d4382ece9020000a8/mysql/
OPENSHIFT_MYSQL_IDENT: redhat:mysql:5.1:0.2.4
TMP_DIR: /tmp/
OPENSHIFT_CLOUD_DOMAIN: rhcloud.com
OPENSHIFT_PYPI_MIRROR_URL: http://mirror1.ops.rhcloud.com/mirror/python/web/simple
OPENSHIFT_APP_DNS: yourname-youdomain.rhcloud.com
OPENSHIFT_SECRET_TOKEN: zcDe8pAt_xxxxxx_rTB2W_MxdXMLLF
PATH: /bin:/usr/bin:/usr/sbin
OPENSHIFT_PRIMARY_CARTRIDGE_DIR: /var/lib/openshift/526xxxxxxx382ece9020000a8/diy/
OPENSHIFT_TMP_DIR: /tmp/
OPENSHIFT_MYSQL_DB_PORT: 3306
OPENSHIFT_BROKER_HOST: openshift.redhat.com
OPENSHIFT_DIY_DIR: /var/lib/openshift/526b927d4382ece9020000a8/diy/
OPENSHIFT_MYSQL_DB_URL: mysql://xxxxxx:xxxxxxxx@127.2.184.2:3306/
OPENSHIFT_DIY_IDENT: redhat:diy:0.1:0.0.4
SHLVL: 3
OPENSHIFT_MYSQL_DB_HOST: 127.2.184.2
OPENSHIFT_GEAR_UUID: 526b927d4382ece9020000a8
TMP: /tmp/
OPENSHIFT_DIY_PORT: 8080
_: /usr/bin/nohup
OPENSHIFT_DIY_LOG_DIR: /var/lib/openshift/526b927d4382ece9020000a8/diy//logs/
OPENSHIFT_HOMEDIR: /var/lib/openshift/526b927d4382ece9020000a8/
HOME: /var/lib/openshift/526b927d4382ece9020000a8/
OPENSHIFT_APP_NAME: dixx
OPENSHIFT_DATA_DIR: /var/lib/openshift/526b927d4382ece9020000a8/app-root/data/
OPENSHIFT_NAMESPACE: xxcn
OPENSHIFT_MYSQL_DB_LOG_DIR: /var/lib/openshift/526b927d4382ece9020000a8/mysql//log/
OPENSHIFT_DIY_IP: 127.2.184.1
OPENSHIFT_APP_UUID: 526b927d4382ece9020000a8
OPENSHIFT_MYSQL_DB_SOCKET: /var/lib/openshift/526b927d4382ece9020000a8/mysql//socket/mysql.sock
OPENSHIFT_REPO_DIR: /var/lib/openshift/526b927d4382ece9020000a8/app-root/runtime/repo/
OPENSHIFT_CARTRIDGE_SDK_BASH: /usr/lib/openshift/cartridge_sdk/bash/sdk
OPENSHIFT_APP_SSH_PUBLIC_KEY: /var/lib/openshift/526b927d4382ece9020000a8/.openshift_ssh/id_rsa.pub
TMPDIR: /tmp/
OPENSHIFT_GEAR_DNS: xxxx.rhcloud.com
OPENSHIFT_MYSQL_DB_PASSWORD: Uxxxx1k
OPENSHIFT_APP_SSH_KEY: /var/lib/openshift/526b927d4382ece9020000a8/.openshift_ssh/id_rsa
OPENSHIFT_GEAR_NAME: diy
OPENSHIFT_MYSQL_DB_USERNAME: xxxxxxC
PWD: /var/lib/openshift/526b927d4382ece9020000a8/diy
GEM_HOME: /var/lib/openshift/526b927d4382ece9020000a8/.gem