-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
183 lines (139 loc) · 5.71 KB
/
config.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import os
import sys
import time
import configparser
import ovh
class ConfUpdater():
file_config = 'ovh.conf'
ep = "" # Endpoint
app_key = "" # Application key
app_secret = "" # Application secret
zone_name = ""
subdomain = ""
links = {
1: "https://eu.api.ovh.com/createApp/",
2: "https://ca.api.ovh.com/createApp/",
3: "https://eu.api.soyoustart.com/createApp/",
4: "https://ca.api.soyoustart.com/createApp/",
5: "https://eu.api.kimsufi.com/createApp/",
6: "https://ca.api.kimsufi.com/createApp/",
}
def __init__(self):
pass
def menu(self):
try:
print("""
1. 'ovh-eu' for OVH Europe API
2. 'ovh-ca' for OVH North-America API
3. 'soyoustart-eu' for So you Start Europe API
4. 'soyoustart-ca' for So you Start North America API
5. 'kimsufi-eu' for Kimsufi Europe API
6. 'kimsufi-ca' for Kimsufi North America API
""")
endp = input('Choose your endpoint [1]: ')
if endp == '':
return 1
endp = int(endp)
self.ep = {
1: "ovh-eu",
2: "ovh-ca",
3: "soyoustart-eu",
4: "soyoustart-ca",
5: "kimsufi-eu",
6: "kimsufi-ca"
}[endp]
print("Selected {} endpoint".format(self.ep))
input("Please, put your login credentials, and copy application data on this link {} \n"
"If you have an existent application, put your application data. \n"
"If you do not remember it, access to https://api.ovh.com/ , remove the app, and make another one.\n"
"Press Enter key to continue...".format(
self.links[endp]))
# Set application key
u_key = input("Put your application_key: ")
if u_key == '':
return None
self.app_key = u_key
# Set application secret
u_secret = input("Put your application_secret: ")
if u_secret == '':
return None
self.app_secret = u_secret
return self.ep
except KeyboardInterrupt:
print("\n")
sys.exit(0)
def menu_client(self):
try:
self.zone_name = input('Put your zone_name (example.com): ')
self.subdomain = input('Put your subdomain (mysubdomain): ')
print("""
1. Request Safe RW (GET,PUT,POST) to path /* API access
2. Request RW (GET,PUT,POST,DELETE) to path /* API access
""")
type_request = input('Choose your type requests [1]: ')
if type_request == '':
type_request = 1
return int(type_request)
except KeyboardInterrupt:
print("\n")
sys.exit(0)
def start(self):
endp = self.menu()
if endp != None:
config = configparser.ConfigParser(
allow_no_value=True, default_section="default")
config.set("default", "endpoint", endp)
config.add_section(endp)
config.set(endp, "application_key", self.app_key)
config.set(endp, "application_secret", self.app_secret)
self.main_url = input(
"Main URL endpoint to get your external IP: ")
self.backup_url = input("Backup URL to get your extrenal IP: ")
config.add_section('external-ip')
config.set('external-ip', 'main_url', self.main_url)
config.set('external-ip', 'backup_url', self.backup_url)
with open(self.file_config, 'w') as cf:
config.write(cf)
print("Configuration file was created.")
client = ovh.Client(config_file=self.file_config)
ck = client.new_consumer_key_request()
request_type = self.menu_client()
if(request_type == 1):
# Request Safe RW (GET,PUT,POST) to path /* API access
ck.add_rules(ovh.API_READ_WRITE_SAFE, "/*")
else:
# Request RW (GET,PUT,POST,DELETE) to path /* API access
ck.add_rules(ovh.API_READ_WRITE, "/*")
try:
# Request token
validation = ck.request()
except Exception as e:
print("OVH Exception: ", e)
sys.exit(1)
input("Please visit %s to authenticate and after validation success, press Enter to continue..." %
validation['validationUrl'])
# Print nice welcome message
print("Welcome", client.get('/me')['firstname'])
print("Btw, your 'consumerKey' is '%s'" %
validation['consumerKey'])
print("Writing changes to %s file..." % self.file_config)
config = configparser.ConfigParser()
config.read_file(open(self.file_config))
config.set(endp, "consumer_key", validation['consumerKey'])
config.add_section("updater")
config.set("updater", "zone_name", self.zone_name)
config.set("updater", "subdomain", self.subdomain)
with open(self.file_config, "w") as cf:
config.write(cf)
print("Configuration done!!")
else:
print("Configuration file was not created.")
if __name__ == '__main__':
if (sys.version_info > (3, 0)):
c = ConfUpdater()
c.start()
else:
# Python 2 not supported
print("Please upgrade your python version or use virtualenv")