1 from pkgutil import get_data
2 from git_remote_run import Remote, setup_repo, upload_hook_command, upload_command
4 from subprocess import run, PIPE
7 def deploy_setup(remote_name):
9 Setup all needed directories.
12 remote = Remote(remote_name)
13 url = run(['git', 'remote', 'get-url', remote_name], stdout=PIPE).stdout.decode('latin1')
14 _, service_name, git_dir = url.rstrip().rsplit('/', 2)
16 # Create the project directory and bare repository.
20 # Upload the hook which triggers a deployment.
21 cmd.append(upload_hook_command(
22 BytesIO(get_data('deploy_setup', 'templates/post-update')),
26 # Upload the main Makefile.
27 cmd.append(upload_command(
28 BytesIO(get_data('deploy_setup', 'templates/Makefile')),
29 '$REPO_DIR/../Makefile'
32 cmd.append("""mkdir -p $REPO_DIR/../etc""")
34 cmd.append('''if [ ! -e $REPO_DIR/../etc/deployment.cfg ]; then''')
36 # Upload the deployment configuration template.
37 cfg = get_data('deploy_setup', 'templates/deployment.cfg.template').decode('utf-8')
40 service_name=service_name,
42 cfg = cfg.encode('utf-8')
43 cmd.append(upload_command(
45 '$REPO_DIR/../etc/deployment.cfg'
50 # Upload the make script for copying configuration.
51 cmd.append(upload_command(
52 BytesIO(get_data('deploy_setup', 'templates/supply-configuration.mk')),
53 '$REPO_DIR/../etc/.supply-configuration.mk'
56 cmd.append(upload_command(
57 BytesIO(get_data('deploy_setup', 'templates/manage.py')),
58 '$REPO_DIR/../manage.py'
60 cmd.append('chmod +x $REPO_DIR/../manage.py')
62 res = remote.run("\n\n".join(cmd))
63 print(res['stderr'].decode('utf-8'))