First version
[fnpdeploy.git] / deploy_setup / __init__.py
1 from pkgutil import get_data
2 from git_remote_run import Remote, setup_repo, upload_hook_command, upload_command
3 from io import BytesIO
4 from subprocess import run, PIPE
5
6
7 def deploy_setup(remote_name):
8     """
9     Setup all needed directories.
10     """
11
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)
15
16     # Create the project directory and bare repository.
17     setup_repo(remote)
18     cmd = []
19
20     # Upload the hook which triggers a deployment.
21     cmd.append(upload_hook_command(
22         BytesIO(get_data('deploy_setup', 'templates/post-update')),
23         'post-update'
24     ))
25
26     # Upload the main Makefile.
27     cmd.append(upload_command(
28         BytesIO(get_data('deploy_setup', 'templates/Makefile')),
29         '$REPO_DIR/../Makefile'
30     ))
31
32     cmd.append("""mkdir -p $REPO_DIR/../etc""")
33
34     cmd.append('''if [ ! -e $REPO_DIR/../etc/deployment.cfg ]; then''')
35
36     # Upload the deployment configuration template.
37     cfg = get_data('deploy_setup', 'templates/deployment.cfg.template').decode('utf-8')
38     cfg = cfg.format(
39         git_dir=git_dir,
40         service_name=service_name,
41     )
42     cfg = cfg.encode('utf-8')
43     cmd.append(upload_command(
44         BytesIO(cfg),
45         '$REPO_DIR/../etc/deployment.cfg'
46     ))
47
48     cmd.append('fi')
49
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'
54     ))
55
56     cmd.append(upload_command(
57         BytesIO(get_data('deploy_setup', 'templates/manage.py')),
58         '$REPO_DIR/../manage.py'
59     ))
60     cmd.append('chmod +x $REPO_DIR/../manage.py')
61
62     res = remote.run("\n\n".join(cmd))
63     print(res['stderr'].decode('utf-8'))