-
Notifications
You must be signed in to change notification settings - Fork 8
/
user
executable file
·252 lines (201 loc) · 8.24 KB
/
user
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
#!/usr/bin/python3
import os
import yaml
import click
import subprocess
from urllib.request import urlopen
class colors:
reset = '\033[0m'
bold = '\033[01m'
disable = '\033[02m'
underline = '\033[04m'
reverse = '\033[07m'
strikethrough = '\033[09m'
invisible = '\033[08m'
class fg:
black = '\033[30m'
red = '\033[31m'
green = '\033[32m'
orange = '\033[33m'
blue = '\033[34m'
purple = '\033[35m'
cyan = '\033[36m'
lightgrey = '\033[37m'
darkgrey = '\033[90m'
lightred = '\033[91m'
lightgreen = '\033[92m'
yellow = '\033[93m'
lightblue = '\033[94m'
pink = '\033[95m'
lightcyan = '\033[96m'
rainbow = [lightred, orange, yellow,
lightgreen, lightcyan, blue, purple]
seq = 0
def random(self):
if self.seq == 7:
self.seq = 0
self.seq += 1
return self.rainbow[self.seq - 1]
def clear_seq(self):
self.seq = 0
class bg:
black = '\033[40m'
red = '\033[41m'
green = '\033[42m'
orange = '\033[43m'
blue = '\033[44m'
purple = '\033[45m'
cyan = '\033[46m'
lightgrey = '\033[47m'
fg = colors.fg()
def info(msg):
print(colors.bold + fg.cyan + '[INFO] ' +
colors.reset + msg + colors.reset)
def print_list(msg):
print(colors.bold + fg.random() + '[LIST] ' +
colors.reset + msg + colors.reset)
def modrun(msg):
print(colors.bold + fg.green + '[MODRUN] ' +
colors.reset + msg + colors.reset)
def container_msg(msg):
print(colors.bold + fg.purple + '[CONTAINER] ' +
colors.reset + msg + colors.reset)
def association_msg(msg):
print(colors.bold + fg.random() + '[ASSOCIATION] ' +
colors.reset + msg + colors.reset)
def warn(warning):
print(colors.bold + fg.yellow + '[WARNING] ' +
colors.reset + warning + colors.reset)
def error(err):
print(colors.bold + fg.red + '[ERROR] ' +
colors.reset + err + colors.reset)
def proceed():
print(colors.bold + fg.red + '[QUESTION] ' +
colors.reset + 'would you like to proceed?' + colors.reset)
info(f'(press {colors.bold}ENTER{colors.reset} to proceed, or {colors.bold}^C{colors.reset}/{colors.bold}^D{colors.reset} to cancel)')
input()
@click.group("cli")
def cli():
"""Manage user operations using the user utility on blendOS."""
def main():
cli(prog_name="user")
@cli.command("associate")
@click.argument('association')
@click.argument('container')
def associate_binary(association, container):
'''
Create an association (for example, apt -> ubuntu)
'''
if not os.path.exists(os.path.expanduser(f'~/.local/bin/blend_bin/{association}.{container}')):
error(f'{colors.bold}{association}.{container}{colors.reset} does not exist')
exit()
if os.path.isfile(os.path.expanduser('~/.local/bin/blend_bin/.associations')):
subprocess.run(['sed', '-i', f's/^{association}\\x0//g',
os.path.expanduser('~/.local/bin/blend_bin/.associations')])
with open(os.path.expanduser('~/.local/bin/blend_bin/.associations'), 'a+') as f:
f.write(f'{association}\0{container}\n')
_exists = os.path.exists(os.path.expanduser(
f'~/.local/bin/blend_bin/{association}'))
subprocess.run(['ln', '-sf', f'{association}.{container}',
os.path.expanduser(f'~/.local/bin/blend_bin/{association}')])
association_msg(('modified' if _exists else 'created') +
f' {colors.bold}{association} -> {container}{colors.reset}')
@cli.command("dissociate")
@click.argument('association')
def associate_binary(association):
'''
Remove an association
'''
if not os.path.exists(os.path.expanduser(f'~/.local/bin/blend_bin/{association}')):
error(f'{colors.bold}{association}{colors.reset} does not exist')
exit()
if os.path.isfile(os.path.expanduser('~/.local/bin/blend_bin/.associations')):
subprocess.run(['sed', '-i', f's/^{association}\\x0//g',
os.path.expanduser('~/.local/bin/blend_bin/.associations')])
subprocess.run(
['rm', '-f', os.path.expanduser(f'~/.local/bin/blend_bin/{association}')])
association_msg(f'dissociated {colors.bold}{association}')
@cli.command("create-container")
@click.argument('container_name')
@click.argument('distro', default='arch')
def create_container(container_name, distro):
'''
Create a container
'''
if distro not in ('arch', 'almalinux-9', 'crystal-linux', 'debian', 'fedora-38', 'kali-linux', 'neurodebian-bookworm', 'rocky-linux', 'ubuntu-22.04', 'ubuntu-23.04'):
error(
f'distro {colors.bold}{distro}{colors.reset} not supported')
if subprocess.run(['podman', 'container', 'exists', container_name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0:
error(f'container {colors.bold}{container_name}{colors.reset} already exists')
exit(1)
subprocess.run(['blend', 'create-container', '-cn', container_name, '-d', distro])
@cli.command("delete-container")
@click.argument('container')
def delete_container(container):
'''
Delete a container
'''
if subprocess.run(['podman', 'container', 'exists', container], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode != 0:
error(f'container {colors.bold}{container}{colors.reset} does not exist')
exit(1)
subprocess.run(['blend', 'remove-container', container])
@cli.command("shell")
@click.argument('container')
def shell(container):
'''
Enter a shell inside a container
'''
if subprocess.run(['podman', 'container', 'exists', container], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode != 0:
error(f'container {colors.bold}{container}{colors.reset} does not exist')
exit(1)
creation_env = os.environ.copy()
creation_env['BLEND_NO_CHECK'] = 'true'
subprocess.run(['blend', 'enter', '-cn', container], env=creation_env)
@cli.command("exec")
@click.argument('container')
@click.argument('cmds', nargs=-1, required=True)
def exec_c(container, cmds):
'''
Run a command inside a container
'''
if subprocess.run(['podman', 'container', 'exists', container], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode != 0:
error(f'container {colors.bold}{container}{colors.reset} does not exist')
exit(1)
creation_env = os.environ.copy()
creation_env['BLEND_NO_CHECK'] = 'true'
subprocess.run(['blend', 'enter', '-cn', container, '--', *cmds], env=creation_env)
@cli.command("install")
@click.argument('container')
@click.argument('pkgs', nargs=-1, required=True)
def install_c(container, pkgs):
'''
Install a package inside a container
'''
if os.path.isfile(os.path.expanduser(f'~/.local/bin/blend_bin/apt.{container}')):
subprocess.run([f'sudo.{container}', 'apt', 'update'])
subprocess.run([f'sudo.{container}', 'apt', 'install', *pkgs])
elif os.path.isfile(os.path.expanduser(f'~/.local/bin/blend_bin/dnf.{container}')):
subprocess.run([f'sudo.{container}', 'dnf', 'install', *pkgs])
elif os.path.isfile(os.path.expanduser(f'~/.local/bin/blend_bin/pacman.{container}')):
subprocess.run([f'sudo.{container}', 'pacman', '-Syu', *pkgs])
else:
error(f'container {colors.bold}{container}{colors.reset} does not exist')
exit(1)
@cli.command("remove")
@click.argument('container')
@click.argument('pkgs', nargs=-1, required=True)
def remove_c(container, pkgs):
'''
Remove a package inside a container
'''
if os.path.isfile(os.path.expanduser(f'~/.local/bin/blend_bin/apt.{container}')):
subprocess.run([f'sudo.{container}', 'apt', 'purge', *pkgs])
elif os.path.isfile(os.path.expanduser(f'~/.local/bin/blend_bin/dnf.{container}')):
subprocess.run([f'sudo.{container}', 'dnf', 'remove', *pkgs])
elif os.path.isfile(os.path.expanduser(f'~/.local/bin/blend_bin/pacman.{container}')):
subprocess.run([f'sudo.{container}', 'pacman', '-Rcns', *pkgs])
else:
error(f'container {colors.bold}{container}{colors.reset} does not exist')
exit(1)
if __name__ == '__main__':
main()