-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_borgbase_repo.py
154 lines (130 loc) · 3.49 KB
/
create_borgbase_repo.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/ansible_launchpad/bin/python
import requests
import subprocess
import argparse
import sys
import os.path
# Define the parser
parser = argparse.ArgumentParser(description='Borgbase Repo Creator')
parser.add_argument('-token', action="store", dest='token', default=0)
parser.add_argument('-hostname', action="store", dest='hostname', default=0)
args = parser.parse_args()
if len(sys.argv) < 4:
sys.stderr.write("Required Arguments: -token xx.yy.zz -hostname localhost")
sys.exit(1)
REPO_DETAILS = '''
query repoList {
repoList {
id
name
quota
quotaEnabled
lastModified
currentUsage
}
}
'''
SSH_ADD = '''
mutation sshAdd(
$name: String!
$keyData: String!
) {
sshAdd(
name: $name
keyData: $keyData
) {
keyAdded {
id
name
hashMd5
keyType
bits
}
}
}
'''
REPO_ADD = '''
mutation repoAdd(
$name: String
$quota: Int
$quotaEnabled: Boolean
$appendOnlyKeys: [String]
$fullAccessKeys: [String]
$alertDays: Int
$region: String
$borgVersion: String
) {
repoAdd(
name: $name
quota: $quota
quotaEnabled: $quotaEnabled
appendOnlyKeys: $appendOnlyKeys
fullAccessKeys: $fullAccessKeys
alertDays: $alertDays
region: $region
borgVersion: $borgVersion
) {
repoAdded {
id
name
region
repoPath
}
}
}
'''
class GraphQLClient:
def __init__(self, token, endpoint='https://api.borgbase.com/graphql'):
self.endpoint = endpoint
self.token = token
def execute(self, query, variables=None):
return self._send(query, variables)
def _send(self, query, variables):
data = {'query': query,
'variables': variables}
headers = {'Accept': 'application/json',
'Content-Type': 'application/json',
"Authorization": "Bearer " + self.token,
}
request = self.session.post(self.endpoint, json=data, headers=headers)
if request.status_code != 200:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
return request.json()
# get short hostname
hostname = args.hostname
client = GraphQLClient(args.token)
# check if repo already exists at borgbase with name of hostname
res = client.execute(REPO_DETAILS)
for repo in res['data']['repoList']:
if repo['name'] == hostname:
sys.stderr.write("Repo already exists\n")
sys.exit(0)
# lets create an ssh key if it does not exist
if (not os.path.exists('/root/.ssh/id_ed25519')):
subprocess.call('ssh-keygen -t ed25519 -N "" -f /root/.ssh/id_ed25519 >/dev/null', shell=True)
f = open("/root/.ssh/id_ed25519.pub")
borg_pub = f.read()
new_key_vars = {
'name': hostname,
'keyData': borg_pub
}
# add host ssh key to borgbase
res = client.execute(SSH_ADD, new_key_vars)
new_key_id = res['data']['sshAdd']['keyAdded']['id']
new_repo_vars = {
'name': hostname,
'quotaEnabled': False,
'fullAccessKeys': [new_key_id],
'region': 'us',
'alertDays': 2
}
# create repo
res = client.execute(REPO_ADD, new_repo_vars)
new_repo = res['data']['repoAdd']['repoAdded']['repoPath']
# ensure facts directory exists and create if it does not
subprocess.call('mkdir -p /etc/ansible/facts.d', shell=True)
# lets create a fact with the repo info
f = open("/etc/ansible/facts.d/borgbase_repo.fact", "w")
f.write('"{}"'.format(new_repo))
f.close()
print '{}'.format(new_repo)