hollyさんのwiki


これは古いのでもう使わない

なんとなくopenshiftを試した。https://openshift.redhat.com
アカウントを先に作成すること。expressにはDBが提供されるのか不明

install

openshiftを使えるようにするためのツールを準備する。rubyが必要。https://openshift.redhat.com/app/getting_started/e...をみればだいたいわかると思う。

ちなみに深い意味はないが環境はFreeBSD8.2でしている

gems/iconv/keychain

先に必要なのでインストールしておく。keychainはgitでdeployする時にssh秘密鍵のパスフレーズを聞かれるのがめんどくさいので、いれておくことにする
sudo portinstall devel/ruby-gems converters/iconv security/keychain

rhc

自分のhome directoryにインストールする
export RB_USER_INSTALL=true
export PATH=$PATH:/home/holly/.gem/ruby/1.8/bin

gem install --user-install  --source http://gems.rubyforge.org --source https://openshift.redhat.com/app/repo/ rhc
# json-1.5.1
# parseconfig-0.5.2
# rhc-0.69.3
gem install --user-install iconv -- --with-opt-dir=/usr/local
余談だがcentosの場合
openshiftのサイトに書いているopenshift.repoからのインストールはだめだったので、ruby最新版をインストールするところからした。https://github.com/imeyer/ruby-1.9.2-rpm

必要なpackageをインストール
yum install -y rpm-build readline-devel ncurses-devel gdbm-devel tcl-devel openssl-devel db4-devel byacc
1.9 rpm作成。まんまぱくる
cd /usr/src/redhat/SOURCES
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz
cd /usr/src/redhat/SPECS
curl https://github.com/robduncan/ruby-1.9.2-rpm/raw/master/ruby19.spec > ruby19.spec
rpmbuild -bb ruby19.spec
rpm -Uvh ../RPMS/i386/ruby-1.9.2p180-1.ruby-1.9.2p180-1.i386.rpm
結局これもだめだったのでcheckinstallでやることにした。詳細はrpmねた参照
tar xvfz ruby-1.9.2-p180.tar.gz
./configure --prefix=/usr
checkinstall --fstrans=no
rpm -ivh /usr/src/redhat/RPMS/x86_64/ruby-1.9.2-p180-1.x86_64.rpm

※これはgemが古い場合の対応なのでいらない
su - holly
cat <<EOL >~/.gemrc
verbose:
   true
sources:
   http://gems.rubyforge.org/
   https://openshift.redhat.com/app/repo/
EOL
gem install --user-install rhc
とした

rhc cli

domain作成

rhc-create-domain
rhc-create-domain -n holly -l <login email>
を実行すると、$HOMEに
.ssh/libra_id_rsa
.ssh/libra_id_rsa.pub
.openshift/express.conf
が生成される

express.conf

以下のようになっている。
default_rhlogin=<signup mailaddress>

<signup mailaddress>=<system account>

keychain実行

のちのち便利なので、keyが生成された時点で準備しておく
# execute ssh-agent and add secret key
keychain ~/.ssh/libra_id_rsa

# for login
# vi ~/.tcshrc で以下を追記
if ( -f $HOME/.keychain/$hostname-csh ) then
        source $HOME/.keychain/$hostname-csh
endif
option
  -n|--namespace   namespace   Opensift Express namespace (alphanumeric - max 16 chars) (required)
  -l|--rhlogin     rhlogin     Red Hat login (required)
  -p|--password    password    RHLogin password (optional, will prompt)
  -a|--alter                   Alter namespace (will change urls) and/or ssh key
  -d|--debug                   Print Debug info
  -h|--help                    Show Usage info

アプリケーション作成

今のところは(php-5.3.2, wsgi-3.2.1, jbossas-7.0.0, rack-1.1.0)が対応しているようなので、とりあえずwsgi(python)で試してみる
rhc-create-app -a hollyapp -t wsgi-3.2.1
hollyapp repositoryが生成される
Found a bug? Post to the forum and we'll get right on it.
    IRC: #openshift on freenode
    Forums: https://www.redhat.com/openshift/forums

Creating remote application space: hollyapp
Contacting https://openshift.redhat.com

RESULT:
Successfully created application: hollyapp

Checking ~/.ssh/config
Contacting https://openshift.redhat.com
Found rhcloud.com in ~/.ssh/config... No need to adjust
Now your new domain name is being populated worldwide (this might take a minute)                       ...
Pulling new repo down
Enter passphrase for key '/home/holly/.ssh/libra_id_rsa':
Confirming application hollyapp is available
  Attempt # 1

Success!  Your application is now published here:

      http://hollyapp-holly.rhcloud.com/

The remote repository is located here:

    ssh://$systemaccount@hollyapp-holly.rhcloud.com/~/git/hollyapp.git/

To make changes to your application, commit to hollyapp/.
Then run 'git push' to update your Openshift Express space
$app-$namesppace.rhcloud.com でサブドメインが生成される。
option
  -a|--app   application     Application name  (alphanumeric - max 16 chars) (required)
  -t|--type  type            Type of app to create (php-5.3.2, wsgi-3.2.1, jbossas-7.0.0, rack-1.1.0) (required)
  -l|--rhlogin  rhlogin      RHCloud rhlogin (Default: emperor.kurt@gmail.com)
  -p|--password  password    RHLogin password  (optional, will prompt)
  -r|--repo  path            Git Repo path (defaults to ./$app_name)
  -n|--nogit                 Only create remote space, don't pull it locally
  -d|--debug                 Print Debug info
  -h|--help                  Show Usage info

アプリケーション情報

rhc-user-info
Contacting https://openshift.redhat.com
Application Info
================
hollyapp
  Framework: wsgi-3.2.1
   Creation: 2011-05-15T11:55:11-04:00
    Git URL: ssh://$systemaccount@hollyapp-holly.rhcloud.com/~/git/hollyapp.git/
 Public URL: http://hollyapp-holly.rhcloud.com/

snapshot

rhc-snapshot
rhc-snapshot -a hollyapp
$app.tar.gzとしてバックアップを取得することができる
option
  -l|--rhlogin   rhlogin    RHCloud rhlogin (Default: emperor.kurt@gmail.com)
  -a|--app                  Target application to snapshot (required)
  -s|--save                 Local path to save tarball (default: ./$APPNAME.tar.gz)
  -p|--password  password   RHLogin password (optional, will prompt)
  -d|--debug                Print Debug info
  -h|--help                 Show Usage info

アプリ環境操作

rhc-ctl-app サービスを停止/再開/削除など。-cで各種操作可能
rhc-ctl-app -a hollyapp -pxxxxxx -c restart
option
Usage: /home/holly/.gem/ruby/1.8/bin/rhc-ctl-app
Control an OpenShift express app

  -a|--app   application   Application name  (alphanumeric) (required)
  -l|--rhlogin rhlogin     Red Hat login (Default: emperor.kurt@gmail.com)
  -p|--password password   RHLogin password  (optional, will prompt)
  -c|--command command     (start|stop|restart|reload|status|destroy)
  -b|--bypass              Bypass warnings
  -d|--debug               Print Debug info
  -h|--help                Show Usage info
stopを指定した場合はService Temporarily Unavailableが返される。destroyの場合は確認メッセージが表示されるので
!!!! WARNING !!!! WARNING !!!! WARNING !!!!
You are about to destroy the hollyapp application.

This is NOT reversable, all remote data for this application will be removed.
Do you want to destroy this application (y/n):
yを入力すれば削除完了する。削除後は使用できなくなるので、rhc-snapshotでバックアップをとっておくこと

flask deploy

turbogears2 deployの例があったので、flaskでできるかを行った https://www.redhat.com/openshift/blogs/deploying-t...
事前にrhc-create-appでアプリケーションをwsgiで作成しておくこと

virtualenv

なければinstallすること
pip install virtualenv

WORKON_DIR作成

rhc-create-appで作成されたリポジトリのdata領域に作成する。pythonは2.6であることが必要(openshiftのpythonが2.6のため)
cd ~/hollyapp/data
virtualenv --no-site-packages --distribute -p /path/to/python2.6 env
間違えて2.6以外のバージョンでvirtualenvをしたり、2.6が無い場合はvirtualenv実行時に-p はなしで実行後
cd env/lib/
# 自分の環境は2.7だった
ln -s python2.7 python2.6
のようにsymlinkを作成すること

activate

source env/bin/activate.sh

flaskインストール

せっかくなので、最新版(dev)をインストールする
cd
git clone http://github.com/mitsuhiko/flask.git
cd flask
python setup.py install
extension install
あわせて何個かインストールする。これは必要と思うものだけインストールしておけばいい
pip install Flask-Uploads Flask-WTF

アプリケーション作成

flask アプリケーション/ディレクトリ作成
libsに作成したアプリケーションをいれる
cd ~/hollyapp/libs
mkdir application
mkdir application/static
mkdir application/templates
ファイル一覧
./application/templates/base.html
./application/templates/index.html
./application/config.py
./application/__init__.py
各ソース
あとは今までのflaskの作り方とかわらない

application/init.py
# vim:fileencoding=utf-8

import os
import os.path
import sys
import pprint
from flask import Flask, Markup, url_for, abort, redirect, render_template, session, request, make_response, send_from_directory, jsonify, g, flash, escape
from werkzeug import secure_filename

__version__ = 1.0

app = Flask(__name__)
app_env = os.environ["FLASK_APP_ENV"].lower() if "FLASK_APP_ENV" in os.environ else "product"

app.config.from_object("application.config.%sConfig" % (app_env.capitalize()))

@app.route("/")
def index():
    return render_template("index.html")

application/config.py
# vim:fileencoding=utf-8

class Config(object):
    DEBUG = False
    TESTING = False
    SECRET_KEY = "/S97ojSkTNB2C@2DR5qVXKgKGNyaHvz6evbGwk-X.grmR9LE"
    SESSION_COOKIE_NAME = "_sid"

class ProductConfig(Config):
    pass

class DevelopConfig(Config):
    DEBUG = True

application/templates/base.html
<html>
        <head>
                <title>holly application</title>
        </head>
        <body>
                <h1>hello openshift app!!</h1>
                <div id="content">{% block content %}{% endblock %}</div>
        </body>
</html>

application/templates/index.html
{% extends "base.html" %}
{% block content %}
index
{% endblock %}

flask wsgi作成
wsgiは$repo/wsgi/applicationがwsgi scriptとなるので、これを編集する。libsにpathを通す設定とactivateさえ気をつければよい
cd ~/hollyapp/wsgi
applicationを以下のように作成する
#!/usr/bin/env python
# vim: fileencoding=utf-8

import os
import sys

here = os.path.dirname(os.path.abspath(__file__))

#libs     = os.path.join(os.path.expanduser("~"), "wsgi", "hollyapp", "repo/libs")
flaskapp = os.path.join(here, "../libs")
activate = os.path.join(here, "../data/env/bin/activate_this.py")
pythoneggs = os.path.join(here, "../data/python-eggs")

sys.path.insert(0, flaskapp)
#sys.path.insert(0, libs)
execfile(activate, dict(__file__=activate))
os.environ["PYTHON_EGG_CACHE"] = pythoneggs

#print(sys.path)

from application import app as application

sys.stdout = sys.stderr

#
# Below for testing only
#
if __name__ == '__main__':
    application.run()

動作確認

localで動くか確認
python application
とでもして
curl http://localhost:5000/
としてinit.pyのroutingごとの出力が返ってくれば問題ない

.gitignore

.pycなファイルは管理対象に含めないようにする
# その他のは好みで
cat <<EOL >.gitignore
*.pyc
*.org
*.bak
*.old
*.sw[po]
EOL

deploy

gitでする
git add .
git commit -m "first flask app"
git push origin master
push後、http://hollyapp-holly.rhcloud.com/ にアクセスして、localhostの結果と同じならdeployは完了。

Wiki内検索

Menu

ここは自由に編集できるエリアです。

管理人/副管理人のみ編集できます