2014-01-24 44 views
2

我想將一個django應用程序部署到遠程服務器。在軌道他們capistrano,處理安裝的依賴,寶石更新,更新的git,shell命令等什麼是部署django應用程序的最簡單最完整的解決方案?

是否有django建什麼,是完整的,那麼容易,因爲capistrano使用?

注:我想你也可以使用capistranodjango,但有什麼特別建成pythondjango

關閉?:許多人提出的解決方案是2010年問題。除非你們對解決方案有絕對的信心,否則請不要關閉這個問題。軟件不斷變化,始終存在創新。自2010年以來有沒有新的/增加的解決方案?

+0

@gawel yea,我聽說過面料。想知道自2010年以來是否還有其他問題出現,當時提出這個問題。 – Derek

+1

ansible&salt但織物仍然是最容易使用的 – gawel

+0

+1。例如,關閉的第二個部署將部署到Heroku或OpenShift,很難打敗「git push heroku master」部署代碼,但我想這取決於你部署的位置。 – iandouglas

回答

0

使用Fabric將django應用程序部署到遠程服務器。 Fabric是一個Python庫和命令行工具,用於簡化應用程序部署或系統管理任務的SSH使用。您可以使用與Capistrano寶石相同的面料。看看這個實時代碼。

from fabric.api import * 


def dev(): 
    env.user = "example" 
    env.hosts = ["example.com", ] 
    env.dev = True 
    env.prod = False 


def prod(): 
    env.user = "example" 
    env.hosts = ["192.68.1.23", ] 
    env.dev = False 
    env.prod = True 


def start_virtualenv(): 
    local("workon django_test") 

# Local developent 
def start_dev_server(): 
    local("python manage.py runserver_plus --settings django_test.settings.dev") 

def start_dev_server_z(): 
    local("python manage.py runserver_plus --settings django_test.settings.dev 0.0.0.0:9000") 

def start_dev_shell(): 
    local("python manage.py shell --settings django_test.settings.dev") 

def start_dev_dbshell(): 
    local("python manage.py dbshell --settings django_test.settings.dev") 

def run_dev_command(command_name=""): 
    """Run a command with the settings thing already setup""" 
    local("python manage.py %s --settings django_test.settings.dev" % command_name) 

# Remote serving 
def run_prod_command(command_name=""): 
    """ Just run this command on remote server """ 
    with cd("/srv/www/django_test/app/"): 
     with prefix("source /home/user/.virtualenvs/agn/bin/activate"): 
      run("python manage.py %s --settings django_test.settings.prod" % command_name) 

def restart_prod_server(): 
    """ Start a gunicorn instance using the supervisor daemon from the server """ 
    run("sudo supervisorctl restart django_test") 

# Deploy and shit 
def deploy(commit="true"): 
    """ 
    TODO: there is sure a better way to set that prefix thing 
    """ 
    if commit == "true": 
     local("git add .") 
     local("git commit -a") 
     local("git push") 

    with cd("/srv/www/agn/app"): 
     run("git pull") 

    if env.dev: 
     account_name = 'exampledev' 
    else: 
     account_name = 'user' 
    prefix_string = 'source /home/%s/.virtualenvs/django_test/bin/activate' % account_name 

    with cd("/srv/www/django_test/app/requirements"): 
     with prefix(prefix_string): 
      run("pip install -r prod.txt") 

    with cd("/srv/www/django_test/app"): 
     with prefix(prefix_string): 
      run("python manage.py migrate --settings django_test.settings.prod") 
      run("python manage.py collectstatic --settings django_test.settings.prod --noinput") 

    restart_prod_server() 
相關問題