forked from freifunk-mwu/backend-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap_git_all.py
executable file
·90 lines (75 loc) · 2.51 KB
/
bootstrap_git_all.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
from common import pinit
photon, settings = pinit('bootstrap_git', verbose=True)
DESC = 'Make sure this key is associated with the "freifunkmwu" user on github'
SSH_TPL = '''\n
# freifunk-mwu github access
## ${desc}
Host ${gh_ident}
\tUser git
\tHostname github.com
\tPreferredAuthentications publickey
\tIdentityFile ~/.ssh/${prv_s}
\n'''
def mkprv_ssh():
return photon.m(
'generating new private ssh keypair',
cmdd=dict(
cmd='ssh-keygen -t rsa -b 4096 -N "" -f %s' %(settings['crypt']['ssh']['prv']),
cwd=settings['crypt']['ssh']['folder'], verbose=True
)
)
def getpub_ssh():
from photon.util.locations import search_location
from photon.util.files import write_file, read_file
if not search_location(settings['crypt']['ssh']['prv']):
mkprv_ssh()
pub = photon.m(
'generating new public ssh key from private',
cmdd=dict(
cmd='ssh-keygen -f %s -y' %(settings['crypt']['ssh']['prv']),
cwd=settings['crypt']['ssh']['folder']
)
)
if not pub.get('returncode') == 0:
photon.m('Error creating public ssh key', state=False)
if read_file(settings['crypt']['ssh']['pub']) != pub.get('out'):
write_file(settings['crypt']['ssh']['pub'], pub.get('out'))
photon.m('wrote public ssh key', more=pub)
return pub.get('out')
def bootstrap_git():
pub = getpub_ssh()
if not pub:
photon.m('Can not bootstrap git configuration', state=True)
conf_t = photon.template_handler(SSH_TPL)
conf_t.sub = dict(
desc=DESC,
gh_ident=settings['common']['gh_ident'],
prv_s=settings['crypt']['ssh']['prv_s']
)
conf_t.write(settings['crypt']['ssh']['conf'])
photon.m(
'setting git user.name',
cmdd=dict(
cmd='git config --global --replace-all user.name "%s"' %(settings['common']['hostname'])
)
)
photon.m(
'setting git user.email',
cmdd=dict(
cmd='git config --global --replace-all user.email "%s@%s"' %(settings['common']['hostname'], settings['common']['domain'])
)
)
# display public-key on shell
shell_t = photon.template_handler(
'${br}${desc}${br}\n${pub} ${local}\n${br}',
fields=dict(
desc=DESC,
pub=pub,
local=settings['common']['mailto']['local'],
br='\n%s\n' %('~'*8)
)
)
photon.m(shell_t.sub, verbose=True)
if __name__ == '__main__':
bootstrap_git()