forked from opensource-expert/ovh-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmk_cred.py
executable file
·139 lines (112 loc) · 3.44 KB
/
mk_cred.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
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
# vim: set et ts=2 sw=2 sts=2:
#
# Usage: python mk_cred.py < copy_paste_credential.txt
# copy_paste_credential.txt is the text ouputed by:
# https://eu.api.ovh.com/createApp/
#
from __future__ import print_function
import sys
import re
import os
import fileinput
import ovh
# pip install --user Jinja2
from jinja2 import Environment, FileSystemLoader
re.UNICODE
re.LOCALE
# Request full API access
access_rules = [
{'method': 'GET', 'path': '/*'},
{'method': 'POST', 'path': '/*'},
{'method': 'PUT', 'path': '/*'},
{'method': 'DELETE', 'path': '/*'}
]
def parse_input():
d = {
'application_key' : None,
'application_secret' : None,
'consumer_key' : None
}
# parse input
# lines are all stored in a buffer for forward reading
lines = sys.stdin.readlines()
i = 0
n = len(lines)
while i < n:
i += 1
l = lines[i-1].rstrip()
if l == '':
# skip empty line
next
# TODO: make it DRY
if re.search(r'Application Key', l):
# key is on the next line
d['application_key'] = lines[i+1].rstrip()
i += 2
if re.search(r'Application Secret', l):
# key is on the next line
d['application_secret'] = lines[i+1].rstrip()
i += 2
if re.search(r'Consumer Key', l):
# key is on the next line
d['consumer_key'] = lines[i+1].rstrip()
i += 2
return d
def update_consumer_key(conffile, consumer_key):
for line in fileinput.FileInput(conffile, inplace=1):
if re.match(r'consumer_key=', line):
line = "consumer_key=%s\n" % consumer_key
print(line, end='')
print("updated in '%s'"%conffile)
def init_app():
url = "https://eu.api.ovh.com/createApp/"
print("go to '%s' and register your app, then copy/paste text here + CTRL-D" % url)
d = parse_input()
client = ovh.Client(
endpoint='ovh-eu',
application_key=d['application_key'],
application_secret=d['application_secret'],
consumer_key=None
)
# Request token
d['consumer_key'] = generate_consumer_key(client)
env = Environment(loader=FileSystemLoader('./templates'))
template = env.get_template('ovh_t.conf')
tmp = 'ovh_conf.tmp'
f = open(tmp, 'w')
f.write(template.render(d))
f.close()
print("file written '%s', you have to copy or rename to ovh.conf" % tmp)
def generate_consumer_key(client = None):
if client == None:
# log with current ovh.conf
client = ovh.Client()
# Request token
try:
validation = client.request_consumerkey(access_rules)
except ovh.exceptions.APIError as e:
print(e)
return None
print("Please visit %s to authenticate" % validation['validationUrl'])
raw_input("and press Enter to continue...")
# Print nice welcome message
print("Welcome", client.get('/me')['firstname'])
print("Here is your Consumer Key: '%s'" % validation['consumerKey'])
return validation['consumerKey']
if __name__ == '__main__':
if len(sys.argv) == 1:
print("no arg, can be: update_key | update | new")
elif sys.argv[1] == 'update_key':
# simply write a key in local ovh.conf
consumer_key = sys.argv[2]
update_consumer_key('ovh.conf', consumer_key)
elif sys.argv[1] == 'new' or sys.argv[1] == 'init':
init_app()
elif sys.argv[1] == 'update':
consumer_key = generate_consumer_key()
if consumer_key != None:
update_consumer_key('ovh.conf', consumer_key)
else:
print('some error with application key, nothing changed')