This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocbuilder_proxy.py
executable file
·143 lines (121 loc) · 4.28 KB
/
docbuilder_proxy.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
#!/usr/bin/env python
"""
Executes docbuilder.py on remote ssh machine.
Copyright (C) 2015 Peter Mosmans [Go Forward]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
"""
from __future__ import absolute_import
from __future__ import print_function
import re
import sys
import proxy_vagrant
CONFIG_FILE = 'docbuilder.yml'
def preflight_checks(hostname, rerun=False):
"""
Checks if all tools are there.
Exits with 0 if everything went okilydokily
"""
#pylint: disable=unused-variable
if not rerun:
print('[*] Checking Vagrant... ', end='')
if proxy_vagrant.command_fails(['vagrant', 'version']):
print_error('[-] Could not execute Vagrant', -1)
print('[*] Checking VirtualBox... ', end='')
if proxy_vagrant.command_fails(['vboxmanage', '--version']):
print_error('[-] Could not start VirtualBox', -5)
sys.stdout.flush()
if proxy_vagrant.connect_vagrant(hostname):
print_error('[-] Could not start Vagrant box {0}'.format(hostname), -5)
print('[*] Checking SSH configuration file... ', end='')
if proxy_vagrant.execute_command(hostname, 'id', True):
print_error('[-] Could not execute commands on box {0}'.format(hostname), -5)
print('[+] All checks successful. Ready to rock.')
print(r'''
::::::..
;;; ``;;
[[[,/[['
$$$$$$c
888 "88,
:::::::-. MMM ... "W" .,-:::::
;;, `';, .;;;;;;;. ,;;;'````'
`[[ [[,[[ \[[,[[[
$$, $$$$$, $$$$$$
888_,o8P'"888,_ _,88P`88bo,__,o,
MMMMP"` "YMMMMMP" "YUMMMMMP"
.::::::.
;;;` `
'[==/[[\,
$
88b dP
"YMmMY"
:::::::. ... :::::: ::: :::::::-. .,:::::: :::::::..
;;;'';;' ;; ;;;;;; ;;; ;;, `';,;;;;'' ' ;;;;``;;;;
[[[__[[\.[[' [[[[[[ [[[ `[[ [[ [[cccc [[[,/[[['
$$""""Y$$$$ $$$$$$ $$' $$, $$ $$"\""" $$$$$$c
_88o,,od8P88 .d888888o88oo,._888_,o8P' 888oo,__ 888b "88bo,
""YUMMMP" "YmmMMMM""MMM""""YUMMMMMMMP"` """"YUMMMMMMM "W"v 0.4 [PGCM]''')
sys.exit(0)
def print_error(text, result=False):
"""
Prints error message.
When @result, exits with result.
"""
if len(text):
print_line('[-] ' + text, True)
if result:
sys.exit(result)
def print_line(text, error=False):
"""
Prints text, and flushes stdout and stdin.
When @error, prints text to stderr instead of stdout.
"""
if not error:
print(text)
else:
print(text, file=sys.stderr)
sys.stdout.flush()
sys.stderr.flush()
def print_status(text, options=False):
"""
Prints status message if options array is given and contains 'verbose'.
"""
if options and options['verbose']:
print_line('[*] ' + str(text))
def read_config(filename):
"""
Reads host and command parameters from configuration file.
"""
try:
with open(filename, 'r') as config_file:
contents = config_file.read()
host = re.findall(r'{0}:\s?(.*)'.format('host'), contents)[0]
command = re.findall(r'{0}:\s?(.*)'.format('command'), contents)[0]
except IOError as exception:
print_error('[-] Could not open configuration file {0}: {1}'.
format(filename, exception.strerror))
except IndexError as exception:
print_error('[-] Missing variables in {0}'.format(filename), -1)
return host, command
def main():
"""
Executes COMMAND on BOX.
"""
hostname = 'docbuilder'
options = sys.argv[1:]
if len(options) >= 1 and 'check' in sys.argv[1]:
if len(options) > 1:
hostname = sys.argv[2]
preflight_checks(hostname)
hostname, command = read_config(CONFIG_FILE)
if len(options):
command = '{0} {1}'.format(command, ' '.join(options))
try:
sys.exit(proxy_vagrant.execute_command(hostname, command))
except OSError as exception:
print_error('[-] Could not open file: {0}'.
format(exception.strerror), exception.errno)
if __name__ == "__main__":
main()