-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
425 lines (379 loc) · 16.5 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import SlideTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.config import Config
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.utils import get_color_from_hex
from kivy.properties import (
ListProperty, StringProperty,
NumericProperty)
from config import PROJECT_PATH, DB, run_syscall
import os
import gitlab
def findparent(curclass, targetclass):
"""find wanted widget from selected or current one"""
reqclass = curclass
if type(targetclass) in [unicode, str]:
targetclass_name = targetclass
else:
targetclass_name = str(targetclass().__class__).\
split('.')[1].replace("'>", "")
while True:
cls = str(reqclass.__class__).split('.')[1].replace("'>", "")
if cls == targetclass_name:
break
elif cls == 'core':
reqclass = None
break
reqclass = reqclass.parent
return reqclass
class ActionServerItem(BoxLayout):
name = StringProperty("")
url = StringProperty("")
cmd = StringProperty("")
def pressed_but(self):
images = self.checkbox.children
if images:
self.checkbox.remove_widget(images[0])
self.input_box.name_input.background_color = (1, 1, 1, 1)
self.input_box.url_input.background_color = (1, 1, 1, 1)
self.input_box.cmd_input.background_color = (1, 1, 1, 1)
else:
image = Image(source="assets/tick.png",
pos=self.checkbox.pos,
size=self.checkbox.size)
self.checkbox.add_widget(image)
name_input = self.input_box.name_input
url_input = self.input_box.url_input
cmd_input = self.input_box.cmd_input
name_input.background_color = get_color_from_hex('eeeeee')
url_input.background_color = get_color_from_hex('eeeeee')
cmd_input.background_color = get_color_from_hex('eeeeee')
class SettingsServerItem(BoxLayout):
name = StringProperty("")
url = StringProperty("")
repo = StringProperty("")
cmd = StringProperty("")
def add_server(self, *args):
name = self.input_box.name_input.text.strip()
url = self.input_box.url_input.text.strip()
repo = self.input_box.repo_input.text.strip()
cmd = self.input_box.cmd_input.text.strip()
if name and url and cmd and repo:
servers = DB.store_get('servers')
servers.append({'name': name, 'url': url,
'cmd': cmd, 'repo': repo})
DB.store_put('servers', servers)
DB.store_sync()
root = findparent(self, Deployment)
root.load_servers()
root.servers_correction_screenbased('settings')
def delete_server(self, *args):
name = self.input_box.name_input.text.strip()
url = self.input_box.url_input.text.strip()
repo = self.input_box.repo_input.text.strip()
cmd = self.input_box.cmd_input.text.strip()
tmp = {'name': name, 'url': url, 'cmd': cmd, 'repo': repo}
servers = DB.store_get('servers')
if filter(lambda x: x == tmp, servers):
servers.pop(servers.index(tmp))
DB.store_put('servers', servers)
DB.store_sync()
root = findparent(self, Deployment)
root.load_servers()
root.servers_correction_screenbased('settings')
class Deployment(ScreenManager):
servers = ListProperty([])
username = StringProperty("")
password = StringProperty("")
branch = StringProperty("")
checked_servers = ListProperty([])
progress = NumericProperty(0)
unit_progress = NumericProperty(0)
def __init__(self, *args, **kwargs):
super(Deployment, self).__init__(*args, **kwargs)
self.load_servers()
self.servers_correction_screenbased('action')
self.username = DB.store_get('username')
self.password = DB.store_get('password')
def load_servers(self):
server_datas = DB.store_get('servers')
self.servers = []
for server in server_datas:
self.servers.append({
'name': server.get('name', ''), 'url': server.get('url', ''),
'cmd': server.get('cmd', ''), 'repo': server.get('repo', '')})
self.servers.append({'name': '', 'url': '', 'cmd': '', 'repo': ''})
def switch_screen(self, screen, side='up'):
self.transition = SlideTransition(direction=side)
self.current = screen
def fast_switch_screen(self, obj, screen='action_screen', side='up'):
self.switch_screen(screen, side)
def uname_pword_update(self):
username = self.current_screen.username_input.text.strip()
password = self.current_screen.password_input.text.strip()
if username and password:
DB.store_put('username', username)
DB.store_put('password', password)
DB.store_sync()
self.username = username
self.password = password
def servers_correction_screenbased(self, screen):
emptyslot = filter(
lambda x: (x['name'].strip() == '' or
x['url'].strip() == '' or
x['cmd'].strip() == '' or
x['repo'].strip() == ''),
self.servers)
if emptyslot:
for slot in emptyslot:
self.servers.pop(self.servers.index(slot))
if screen == 'action':
pass
if screen == 'settings':
self.servers.append({'name': "", 'url': "", 'cmd': "", 'repo': ""})
def servers_to_items(self, raw_index, item):
return {
'name': item['name'],
'url': item['url'],
'cmd': item['cmd'],
'repo': item['repo']
}
def authentication_check(self, name, server):
def inner_authentication_check():
result = message = ""
sys_call = ('fab authentication_check:%(username)s,'
'%(password)s,%(server)s')
os.chdir('%(path)s' % {'path': PROJECT_PATH})
out, err = run_syscall(
sys_call % {
'username': self.username,
'password': self.password,
'server': server})
try:
result = out.split('DEPLOYMENT:')
result = result[1].split(":")[0].strip()
result = True and result == "True" or False
except:
result = False
if not result:
message = u"%s authentication failed" % name
return result, message
return inner_authentication_check
def branch_check(self, name, branch, repo):
def inner_branch_check():
try:
git = gitlab.Gitlab("http://gitlab.markafoni.net")
git.login(self.username, self.password)
try:
origin_repo_id = filter(
lambda x: (
x['web_url'] ==
"http://gitlab.markafoni.net/%s" %
repo.strip('/')),
git.getprojects(per_page=1000))[0].get('id', 0)
except:
raise Exception(u'Repository could not found.')
branches = filter(lambda x: x.get('name', '') == branch,
git.getbranches(origin_repo_id))
result = True
message = ""
if not branches:
result = False
message = u"Branch not found for %s" % name
return result, message
except Exception, e:
return False, e.message
return inner_branch_check
def command_check(self, server, command):
def inner_command_check():
sys_call = ('fab command_check:%(username)s,%(password)s,'
'%(server)s,%(command)s')
out, err = run_syscall(sys_call % {
'username': self.username, 'password': self.password,
'server': server, 'command': command})
if err.strip():
return False, '%s command not found on server' % command
else:
out = out.replace('which %s' % command, ' ')
if out.find(command) != -1:
return True, ""
else:
return False, '%s command not found on server' % command
return inner_command_check
def display_message(self, screen, message):
def inner_display_message():
screen.info.text = message
return True, ""
return inner_display_message
def deploy_server(self, username, password, name, url, command, branch):
def inner_deploy_server():
sys_call = ("fab start_deployment:%(branch)s,%(username)s,"
"%(password)s,%(server)s,%(call)s")
out, err = run_syscall(
sys_call % {
'call': command,
'branch': branch,
'username': username,
'password': password,
'server': url
}
)
if not err:
self.display_message(
self.current_screen, u'%s --> %s' % (branch, name))()
return True, ""
else:
print err
return False, ("%s could not be deployed "
"to %s server" % (branch, name.capitalize()))
return inner_deploy_server
def reset_progess(self):
def inner_reset_progress(process_count):
self.unit_progress = 440 / process_count
self.progress = 0
return inner_reset_progress
def deploymentComplition(self, requests, output=False, call_count=1):
if requests:
if not output:
call = requests[0]
call()
Clock.schedule_once(
lambda dt: self.deploymentComplition(requests[1:]), 1)
else:
call = requests[0]
result, message = call()
if not result:
self.display_message(self.current_screen, message)()
self.display_error_buts_deployment()
return False
else:
Clock.schedule_once(
lambda dt: self.deploymentComplition(
requests[1:], output=True,
call_count=call_count + 1), 1)
anim = Animation(
progress=self.unit_progress * call_count,
t='linear', duration=.5)
anim.start(self)
else:
ok_but = Button(
size=(100, 30), pos=(200, 150), text="[b]COMPLETE[/b]",
background_color=get_color_from_hex('97BE0D'),
switch_screen='action_screen')
ok_but.bind(on_release=self.fast_switch_screen)
self.current_screen.add_widget(ok_but)
anim = Animation(progress=440, t='linear', duration=.5)
anim.start(self)
return True
def collect_deploy_data(self):
screen = self.current_screen
servers = screen.server_items.children[0].children[0].children
self.branch = screen.branch.text.strip()
self.checked_servers = filter(lambda x: x.checkbox.children, servers)
def reset_screen(self):
if self.current == 'deploy_screen':
children = self.current_screen.children
buttons = []
for child in children:
if str(child.__class__).find('Button') != -1:
buttons.append(child)
for button in buttons:
for child in button.children:
button.remove_widget(child)
self.current_screen.remove_widget(button)
def display_error_buts_deployment(self):
cross_img = Image(
source="assets/cross2.png", size=(20, 20), pos=(470, 370))
cross_but = Button(
size=(20, 20), pos=(470, 370), switch_screen='action_screen',
background_color=get_color_from_hex('404248'))
cross_but.add_widget(cross_img)
cross_but.bind(on_release=self.fast_switch_screen)
tryagain_but = Button(
size=(100, 30), pos=(200, 150), text="[b]TRY AGAIN[/b]",
background_color=get_color_from_hex('FFCC00'),
switch_screen='action_screen',)
tryagain_but.bind(on_release=self.fast_switch_screen)
self.current_screen.add_widget(cross_but)
self.current_screen.add_widget(tryagain_but)
def deploy(self):
if self.current == "deploy_screen":
self.reset_screen()
screen = self.current_screen
if not self.branch:
self.display_message(screen, u"'branch' is empty!")()
self.display_error_buts_deployment()
else:
if not self.checked_servers:
self.display_message(
screen, u"at least check one server please!")()
self.display_error_buts_deployment()
else:
requests = []
for server in self.checked_servers:
name = server.input_box.name_input.text.strip()
url = server.input_box.url_input.text.strip()
cmd = server.input_box.cmd_input.text.strip()
server_datas = DB.store_get('servers')
repo = filter(
lambda x: x['name'] == name, server_datas)[0].get(
'repo')
tmp = [
self.display_message(
screen, '%s authentication check' % name),
self.authentication_check(name, url),
self.display_message(
screen, '%s branch check' % self.branch),
self.branch_check(name, self.branch, repo),
self.display_message(
screen, '%s command check' % cmd),
self.command_check(url, cmd)
]
requests.extend(tmp)
for server in self.checked_servers:
name = server.input_box.name_input.text.strip()
url = server.input_box.url_input.text.strip()
cmd = server.input_box.cmd_input.text.strip()
repo = filter(
lambda x: x['name'] == name, server_datas)[0].get(
'repo')
requests.append(self.display_message(
screen, '%s deployment...' % name))
tmp_call = self.deploy_server(self.username,
self.password,
name, url, cmd,
self.branch)
requests.append(tmp_call)
requests.append(self.display_message(
screen,
"Deployment complete for branch; '%s'" % self.branch))
self.reset_progess()(len(requests))
self.deploymentComplition(requests, output=True)
class DeploymentApp(App):
def __init__(self, *args, **kwargs):
super(DeploymentApp, self).__init__(*args, **kwargs)
Builder.load_file('%s/assets/style.kv' % PROJECT_PATH)
self.title = 'Deploy App'
self.icon = 'assets/deployapp.png'
def build(self):
layout = Deployment()
return layout
if __name__ == "__main__":
"""
Window sizes and wanted skills are set, then app calls
"""
Window.size = (500, 400)
Window.clearcolor = (get_color_from_hex("1C1D20"))
Window.borderless = False
Config.set('kivy', 'desktop', 1)
Config.set('kivy', 'exit_on_escape', 0)
Config.set('graphics', 'fullscreen', 0)
Config.set('graphics', 'resizable', 0)
DeploymentApp().run()