Skip to content

Instantly share code, notes, and snippets.

@jayd3e
Created June 4, 2012 18:30
Show Gist options
  • Save jayd3e/2870070 to your computer and use it in GitHub Desktop.
Save jayd3e/2870070 to your computer and use it in GitHub Desktop.
Subprocess error
import os
import subprocess
from pyramid.view import view_config
from retools.queue import QueueManager
from thewall.repository import Repository
qm = QueueManager()
@view_config(route_name='home', renderer='home.html')
def home(request):
# we need the current tags from git
# sorted tag list
web_app_dir = request.registry.settings['web_app_dir']
repository = Repository(web_app_dir)
tags = repository.tags
last_tag = getCurrentlyDeployedVersion(request)
return {'last_tag': last_tag.rjust(8, '0'),
'next_tag': str(int(tags[-1]['number']) + 1).rjust(8, '0'),
'tags': reversed(tags)}
def getCurrentlyDeployedVersion(request):
path = os.path.join(request.registry.settings['web_app_dir'], 'global/version.txt')
if os.path.exists(path):
with (open(path)) as f:
return f.readline()
else:
return "1"
@view_config(route_name='deploy_tag', renderer='json')
def tag(request):
if (request.method == 'POST'):
web_app_dir = request.registry.settings['web_app_dir']
repository = Repository(web_app_dir)
try:
web_app_dir = request.registry.settings['web_app_dir']
repository = Repository(web_app_dir)
repository.tag('deploy_%s' % request.matchdict['id'], 'deploying from website')
return {'result': 'success'}
except Exception, e:
return {'result': 'exception: %s' % e}
else:
return {'result': 'you can\'t use a GET to add the tag'}
@view_config(route_name='deploy_do', renderer='json')
def deploy_do(request):
id = request.params['version']
log = get_log_path(request, id)
print 'writing to %s' % log
qm.enqueue('thewall.views:do', args=get_args(request, id), log_path=log)
return {'push_id': id}
def do(args=[], log_path=None):
with open(log_path, 'w+') as log_file:
p = subprocess.Popen(
args,
stdin=None,
stdout=log_file,
stderr=subprocess.STDOUT
)
p.wait()
log_file.flush()
def get_args(request, id):
args = [
get_script_path(request),
'-s', request.params['environment'],
'-t', "deploy_%s" % request.params['version']]
if request.params.get('restartMemcache'):
args.append('-m')
if request.params.get('restartApache'):
args.append('-a')
return args
def get_script_path(request):
if request.registry.settings['deploy_script_path'] == 'cwd':
return os.path.join(os.getcwd(), 'deploy.sh')
else:
return request.registry.settings['deploy_script_path']
def get_log_path(request, id):
if request.registry.settings['log_path'] == 'cwd':
return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logs/log_%s.log' % str(id))
else:
return os.path.join(request.registry.settings['log_path'], "d_{}.log".format(id))
@view_config(route_name='deploy_read', renderer='json')
def deploy_read(request):
id = request.matchdict['id']
print 'reading from %s' % get_log_path(request, id)
test_log = open(get_log_path(request, id), 'w+')
return {'content': test_log.read()}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment