|
1
|
|
|
import os |
|
2
|
|
|
import sys |
|
3
|
|
|
import subprocess |
|
4
|
|
|
|
|
5
|
|
|
from clint.textui import colored, puts, columns |
|
6
|
|
|
from lib import docker |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class Lamp(): |
|
10
|
|
|
def __init__(self, base_dir: str): |
|
11
|
|
|
self.lamp_base_dir = base_dir |
|
12
|
|
|
self.current_dir = os.getcwd() |
|
13
|
|
|
# Make sure we are in the right directory |
|
14
|
|
|
self.current_dir_relative = self.current_dir[len(self.lamp_base_dir):].lstrip('/') |
|
15
|
|
|
os.chdir(self.lamp_base_dir) |
|
16
|
|
|
|
|
17
|
|
|
from lib.configreader import Config |
|
18
|
|
|
self.default_config_main = Config('conf/compose.ini.tpl').read()['main'] |
|
19
|
|
|
self.user_config_main = Config('conf/compose.ini').read()['main'] |
|
20
|
|
|
|
|
21
|
|
|
self.vms = docker.get_vms() |
|
22
|
|
|
|
|
23
|
|
|
self.running_vms = 0 |
|
24
|
|
|
for vm_id, vm_data in self.vms.items(): |
|
25
|
|
|
if vm_data['running'] is False: |
|
26
|
|
|
continue |
|
27
|
|
|
|
|
28
|
|
|
self.running_vms += 1 |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def run_services_post_scripts(self): |
|
32
|
|
|
services = [service for service in self.user_config_main.get('services', '').split(',') if service != ''] |
|
33
|
|
|
for service in services: |
|
34
|
|
|
service_script = 'services/' + service + '.sh' |
|
35
|
|
|
vm_name = self.get_vm_item(service, 'name') |
|
36
|
|
|
if os.path.isfile(service_script) is False: |
|
37
|
|
|
continue |
|
38
|
|
|
|
|
39
|
|
|
subprocess.call(['bash', service_script, vm_name]) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def display_services_ports(self): |
|
43
|
|
|
puts('To access the {} use : http://{}\n'.format(colored.yellow('web server'), self.get_vm_item('apache', 'ip'))) |
|
44
|
|
|
|
|
45
|
|
|
mailcatcher_ip = self.get_vm_item('mailcatcher', 'ip') |
|
46
|
|
|
if mailcatcher_ip != '': |
|
47
|
|
|
puts('For {} use : http://{}'.format(colored.yellow('mailcatcher'), mailcatcher_ip)) |
|
48
|
|
|
puts(' '*16 + 'and in your VM use the server "{}" with the port 25\n'.format(colored.yellow('mailcatcher'))) |
|
49
|
|
|
|
|
50
|
|
|
maildev_ip = self.get_vm_item('maildev', 'ip') |
|
51
|
|
|
if maildev_ip != '': |
|
52
|
|
|
puts('For {} use : http://{}'.format(colored.yellow('maildev'), maildev_ip)) |
|
53
|
|
|
puts(' '*12 + 'and in your VM use the server "{}" with the port 25\n'.format(colored.yellow('maildev'))) |
|
54
|
|
|
|
|
55
|
|
|
mongoclient_ip = self.get_vm_item('mongoclient', 'ip') |
|
56
|
|
|
if mongoclient_ip != '': |
|
57
|
|
|
puts('For {} use : http://{}:3000\n'.format(colored.yellow('mongoclient'), mongoclient_ip)) |
|
58
|
|
|
|
|
59
|
|
|
pma_ip = self.get_vm_item('phpmyadmin', 'ip') |
|
60
|
|
|
if pma_ip != '': |
|
61
|
|
|
puts('For {} use : http://{}\n'.format(colored.yellow('phpMyAdmin'), pma_ip)) |
|
62
|
|
|
|
|
63
|
|
|
xhgui_ip = self.get_vm_item('xhgui', 'ip') |
|
64
|
|
|
if xhgui_ip != '': |
|
65
|
|
|
puts('For {} use : http://{}\n'.format(colored.yellow('xhgui'), xhgui_ip)) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def start(self, pull: bool): |
|
69
|
|
|
if pull is True: |
|
70
|
|
|
subprocess.call(['bin/compose', 'pull']) |
|
71
|
|
|
|
|
72
|
|
|
subprocess.call(['bin/compose', 'up', '-d', '--remove-orphans']) |
|
73
|
|
|
self.vms = docker.get_vms() |
|
74
|
|
|
self.run_services_post_scripts() |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def stop(self): |
|
78
|
|
|
self.check_vms_are_running() |
|
79
|
|
|
subprocess.call(['bin/compose', 'stop']) |
|
80
|
|
|
|
|
81
|
|
|
def destroy(self): |
|
82
|
|
|
subprocess.call(['bin/compose', 'down']) |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def restart(self): |
|
86
|
|
|
self.stop() |
|
87
|
|
|
self.start() |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def status(self): |
|
91
|
|
|
self.check_vms_are_running() |
|
92
|
|
|
puts(columns( |
|
93
|
|
|
[(colored.green('VM')), 20], |
|
94
|
|
|
[(colored.green('IP')), 15], |
|
95
|
|
|
[(colored.green('Ports')), 30], |
|
96
|
|
|
[(colored.green('Image')), 30], |
|
97
|
|
|
[(colored.green('Docker ID')), 15], |
|
98
|
|
|
[(colored.green('Docker Name')), 25] |
|
99
|
|
|
)) |
|
100
|
|
|
puts(columns( |
|
101
|
|
|
['-'*20, 20], |
|
102
|
|
|
['-'*15, 15], |
|
103
|
|
|
['-'*30, 30], |
|
104
|
|
|
['-'*30, 30], |
|
105
|
|
|
['-'*15, 15], |
|
106
|
|
|
['-'*25, 25] |
|
107
|
|
|
)) |
|
108
|
|
|
for vm_id, vm_data in self.vms.items(): |
|
109
|
|
|
if vm_data['ip'] == '': |
|
110
|
|
|
continue |
|
111
|
|
|
|
|
112
|
|
|
puts(columns( |
|
113
|
|
|
[vm_data['compose_name'], 20], |
|
114
|
|
|
[vm_data['ip'], 15], |
|
115
|
|
|
[', '.join(vm_data['ports']), 30], |
|
116
|
|
|
[vm_data['image'], 30], |
|
117
|
|
|
[vm_id[:12], 15], |
|
118
|
|
|
[vm_data['name'], 25] |
|
119
|
|
|
)) |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
def fullstart(self): |
|
123
|
|
|
subprocess.call(['bin/compose', 'build']) |
|
124
|
|
|
self.start() |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def console(self, vm: str, user: str): |
|
128
|
|
|
self.check_vms_are_running() |
|
129
|
|
|
|
|
130
|
|
|
vm_name = self.get_vm_item(vm, 'name') |
|
131
|
|
|
if vm_name == '': |
|
132
|
|
|
raise Exception('{} does not seem to be in your services or has crashed'.format(vm)) |
|
133
|
|
|
|
|
134
|
|
|
tty = 't' if sys.stdin.isatty() else '' |
|
135
|
|
|
subprocess.call(['docker', 'exec', '-u', user, '-i' + tty, vm_name, 'env', 'TERM=xterm', 'bash']) |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
def run_php(self, user: str, args: str): |
|
139
|
|
|
self.check_vms_are_running() |
|
140
|
|
|
|
|
141
|
|
|
tty = 't' if sys.stdin.isatty() else '' |
|
142
|
|
|
cmd = ['docker', 'exec', '-u', user, '-i' + tty, self.get_vm_item('php', 'name'), 'bash', '-c', '--'] |
|
143
|
|
|
cmd += ['cd /var/' + self.current_dir_relative + '; exec /usr/bin/php ' + args] |
|
144
|
|
|
subprocess.call(cmd, stdin=sys.stdin) |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
def run_mysql(self, args: str): |
|
148
|
|
|
self.check_vms_are_running() |
|
149
|
|
|
|
|
150
|
|
|
vm_name = self.get_vm_item('mysql', 'name') |
|
151
|
|
|
if vm_name == '': |
|
152
|
|
|
raise Exception('mysql does not seem to be in your services or has crashed') |
|
153
|
|
|
|
|
154
|
|
|
tty = 't' if sys.stdin.isatty() else '' |
|
155
|
|
|
password = self.user_config_main.get( |
|
156
|
|
|
'mysql.root_password', |
|
157
|
|
|
self.default_config_main.get('mysql.root_password') |
|
158
|
|
|
) |
|
159
|
|
|
cmd = ['docker', 'exec', '-u', 'root', '-i' + tty, vm_name] |
|
160
|
|
|
cmd += ['mysql', '-u', 'root', '-p' + password, args] |
|
161
|
|
|
subprocess.call(cmd, stdin=sys.stdin) |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
def get_vm_item(self, compose_name: str, item_name: str): |
|
165
|
|
|
for vm_id, vm_data in self.vms.items(): |
|
166
|
|
|
if vm_data['compose_name'] == compose_name: |
|
167
|
|
|
return vm_data[item_name] |
|
168
|
|
|
|
|
169
|
|
|
return '' |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
def check_vms_are_running(self): |
|
173
|
|
|
if self.running_vms == 0: |
|
174
|
|
|
raise Exception('Have you started your server with the start or fullstart action ?') |
|
175
|
|
|
|