2012-05-19 58 views
0

我有一個小型項目,我想部署到Heroku和OpenShift,在兩個平臺上嘗試。我使用Python/Django的所以我想利用動態導入功能的優勢來定義每一環境設置,保持這些平臺我要部署到,像,設置模塊:如何可靠地確定哪個PaaS是我的項目運行?

my_project/ 
    settings/ 
     __init__.py <-- This is what's being imported during app init, here is where I want to detect which platform specific settings module to load 
     dev_settings.py 
     heroku_settings.py 
     openshift_settings.py 

到目前爲止我用此代碼:

ENVIRONMENT = the_function_or_code_that_returns_the_environment() # Defaults to "dev" 
from django.utils.importlib import import_module 

try: 
    # Import any symbols that begin with A-Z. Append to lists any symbols that 
    # begin with "EXTRA_". 
    local_settings_module_name = '%s_settings' % ENVIRONMENT 
    local_settings = import_module(local_settings_module_name) 
    import re 
    for attr in dir(local_settings): 
     match = re.search('^EXTRA_(\w+)', attr) 
     if match: 
      name = match.group(1) 
      value = getattr(local_settings, attr) 
      try: 
       globals()[name] += value 
      except KeyError: 
       globals()[name] = value 
     elif re.search('^[A-Z]', attr): 
      globals()[attr] = getattr(local_settings, attr) 
except ImportError: 
    pass 

這將優選通過默認(未由我/開發人員設置)環境變量檢測此。 Heroku的暴露只是幾個:

DATABASE_URL 
ENVIRONMENT 
LANG 
LD_LIBRARY_PATH 
LIBRARY_PATH 
PATH 
PYTHONHASHSEED 
PYTHONHOME 
PYTHONPATH 
PYTHONUNBUFFERED 
SHARED_DATABASE_URL 

而且OpenShift的:

OPENSHIFT_DB_HOST 
OPENSHIFT_LOG_DIR 
OPENSHIFT_TMP_DIR 
OPENSHIFT_DB_CTL_SCRIPT 
OPENSHIFT_REPO_DIR 
OPENSHIFT_HOMEDIR 
OPENSHIFT_GEAR_NAME 
OPENSHIFT_PHPMYADMIN_IP 
OPENSHIFT_RUNTIME_DIR 
OPENSHIFT_INTERNAL_PORT 
OPENSHIFT_PHPMYADMIN_CTL_SCRIPT 
OPENSHIFT_DB_MYSQL_51_RESTORE 
OPENSHIFT_DB_MYSQL_51_DUMP 
OPENSHIFT_DB_PASSWORD 
OPENSHIFT_DB_USERNAME 
OPENSHIFT_PHPMYADMIN_GEAR_DIR 
OPENSHIFT_RUN_DIR 
OPENSHIFT_INTERNAL_IP 
OPENSHIFT_GEAR_DIR 
OPENSHIFT_GEAR_CTL_SCRIPT 
OPENSHIFT_APP_DNS 
OPENSHIFT_GEAR_TYPE 
OPENSHIFT_GEAR_DNS 
OPENSHIFT_DB_MYSQL_51_EMBEDDED_TYPE 
OPENSHIFT_DB_URL 
OPENSHIFT_APP_NAME 
OPENSHIFT_DATA_DIR 
OPENSHIFT_DB_SOCKET 
OPENSHIFT_GEAR_UUID 
OPENSHIFT_APP_UUID 
OPENSHIFT_DB_TYPE 
OPENSHIFT_DB_MYSQL_51_DUMP_CLEANUP 
OPENSHIFT_DB_PORT 

OpenShift變量與OPENSHIFT_前綴,以便有一個開始,但是這不是與Heroku的情況。

如何可靠地使用環境變量檢測平臺?

回答

1

@Kevin,我張貼我的評論將其作爲答案,讓人們可以快速查看(或改進它):

if len([k for k in os.environ.keys() if k.startswith('OPENSHIFT_')]): 
    ENVIRONMENT = 'openshift' 
elif len([k for k in os.environ.values() if k.find('heroku') > -1]): 
    ENVIRONMENT = 'heroku' 

local_settings_module_name = '%s_settings' % ENVIRONMENT 
local_settings = import_module(local_settings_module_name) 
# do the magic with the `local_settings` module 
2

我建議設置我自己的ENV配置設置。這可以使用heroku輕鬆完成。

heroku config:add MY_DEPLOYMENT_PLATFORM=heroku 

然後,在你初始化(或任何你需要的),您可以訪問值,像這樣:

if ENV["MY_DEPLOYMENT_PLATFORM"] == "heroku" 
// perform heroku config 
else 
// perform other PAAS config 
end 
+0

OpenShift不允許您設置自定義環境變量(還),這就是爲什麼我要尋找一種「啓發式」方法。 –

+0

您仍然可以檢查變量的存在(或缺失)。 –

+1

'len('k for k in os.environ.keys()如果k.startswith('OPENSHIFT_')])> 0#測試OpenShift','len([k for k in os.environ.values ()如果k.find('heroku')> -1])> 0#測試Heroku'? –

1

這就是我如何檢測當前PaaS我的應用程序。 overide_settings方法不是我的。

def override_settings(dottedpath): 
    """Imports uppercase modules from an string based module. 
    Example: 
     override_settings('my.module.settings') 
    """ 
    try: 
     _m = import_module(dottedpath) 
    except ImportError: 
     warnings.warn("Failed to import %s" % dottedpath) # <-- will show up in your error log 
    else: 
     _thismodule = sys.modules[__name__] 
     for _k in dir(_m): # <-- moved the block inside else 
      if _k.isupper() and not _k.startswith('__'): setattr(_thismodule, 
       _k, getattr(_m, _k)) 

ON_OPENSHIFT = False 
if os.environ.has_key('OPENSHIFT_REPO_DIR'): 
    ON_OPENSHIFT = True 

ON_HEROKU = False 
if os.environ.get('LD_LIBRARY_PATH', None) == '/app/.heroku/vendor/lib': 
    ON_HEROKU = True 

if ON_OPENSHIFT: 
    override_settings('settings.openshift') 

if ON_HEROKU: 
    override_settings('settings.heroku') 
相關問題