在dokku app部署上运行shell脚本

时间:2015-04-21 20:44:38

标签: shell deployment docker dokku dokku-alt

我一直在寻找一种运行一次脚本的方法,该脚本将数据加载到我们的数据库中。目前我们正在为我们的开发环境使用 dokku-alt ,我们有一个python脚本,用于更新我们的应用程序可用的架构,数据和函数。我面临的问题是试图通过dokku-alt找到在应用程序部署上运行脚本的方法。

我冒险使用一名工人,但工人们自己并没有按照我的期望去做。从我注意到的是,一旦工人完成,工人将终止每个流程。这是我们需要的东西。我们需要运行一次脚本来加载我们的数据和模式并优雅地关闭。我们仍然希望我们的Web进程继续工作,因此子进程向另一个进程发送一个kill信号。

所以我的问题是,有没有办法在部署时只运行一次脚本而无需编写自定义插件?

05:23:07 schema.1   | started with pid 15
05:23:07 function.1 | started with pid 17
05:23:07 data.1     | started with pid 19
05:23:07 web.1      | started with pid 21
05:23:07 web.1      | Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8 -Djava.rmi.server.useCodebaseOnly=true
05:23:12 function.1 | Begin dbupdater
05:23:12 function.1 | pq://user:UcW3P587Eki8Fqrr@postgresql:5432/db
05:23:13 schema.1   | Begin dbupdater
05:23:13 data.1     | Begin dbupdater
05:23:13 data.1     | pq://user:UcW3P587Eki8Fqrr@postgresql:5432/db
05:23:13 schema.1   | pq://user:UcW3P587Eki8Fqrr@postgresql:5432/db
05:23:13 schema.1   | do (AccountCrosstabKey_create.sql)
05:23:13 schema.1   | Done
05:23:13 data.1     | do (Accountinfo_data.sql)
05:23:13 function.1 | do (Connectby_create.sql)
05:23:13 function.1 | Done
05:23:13 data.1     | Done
05:23:13 schema.1   | exited with code 0
05:23:13 system     | sending SIGTERM to all processes
05:23:13 function.1 | terminated by SIGTERM
05:23:13 data.1     | terminated by SIGTERM
05:23:13 web.1      | terminated by SIGTERM

Python脚本:

#!/usr/bin/python
import os
import sys
import glob
import shlex
import subprocess
import postgresql
import postgresql.driver as pg_driver

try:
  print('Begin dbupdater')
  dbhost = os.environ.get('DATABASE_URL','localhost').replace('postgres://', 'pq://')
  print(dbhost)
  targetDir = sys.argv[1]

  db = postgresql.open(dbhost)
  os.chdir(targetDir)
  currDir = os.getcwd()
  for file in glob.glob("*.sql"):
    sqlCmd = ''
    with open(file,'r') as myfile:
      sqlCmd = myfile.read().replace('DO', '').replace('$do$', '')
    db.do('plpgsql',sqlCmd)
    print('do (' + file + ')')
  db.close()
  print('Done')

except (ValueError, KeyError, TypeError) as error:
  print (error)

0 个答案:

没有答案
相关问题