/
jail.py
1130 lines (927 loc) · 37.6 KB
/
jail.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import os
import subprocess as su
import iocage_lib.iocage as ioc
import iocage_lib.ioc_exceptions as ioc_exceptions
import libzfs
import requests
import itertools
import pathlib
import json
import sqlite3
import errno
from iocage_lib.ioc_check import IOCCheck
from iocage_lib.ioc_clean import IOCClean
from iocage_lib.ioc_image import IOCImage
from iocage_lib.ioc_json import IOCJson
# iocage's imports are per command, these are just general facilities
from iocage_lib.ioc_list import IOCList
from middlewared.schema import Bool, Dict, Int, List, Str, accepts
from middlewared.service import CRUDService, job, private
from middlewared.service_exception import CallError, ValidationErrors
from middlewared.utils import filter_list
from middlewared.validators import IpInUse, MACAddr
from collections import deque, Iterable
SHUTDOWN_LOCK = asyncio.Lock()
class JailService(CRUDService):
class Config:
process_pool = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# We want debug for jails starting/stopping
os.environ['IOCAGE_DEBUG'] = 'TRUE'
# FIXME: foreign schemas cannot be referenced when
# using `process_pool`
# @filterable
@accepts(
List('query-filters', default=[]),
Dict('query-options', additional_attrs=True),
)
def query(self, filters=None, options=None):
options = options or {}
jail_identifier = None
jails = []
if filters and len(filters) == 1 and list(
filters[0][:2]) == ['host_hostuuid', '=']:
jail_identifier = filters[0][2]
recursive = False if jail_identifier == 'default' else True
try:
jail_dicts = ioc.IOCage(
jail=jail_identifier).get('all', recursive=recursive)
if jail_identifier == 'default':
jail_dicts['host_hostuuid'] = 'default'
jails.append(jail_dicts)
else:
for jail in jail_dicts:
jail = list(jail.values())[0]
jail['id'] = jail['host_hostuuid']
if jail['dhcp'] == 'on':
uuid = jail['host_hostuuid']
if jail['state'] == 'up':
interface = jail['interfaces'].split(',')[0].split(
':')[0]
if interface == 'vnet0':
# Inside jails they are epair0b
interface = 'epair0b'
ip4_cmd = ['jexec', f'ioc-{uuid}', 'ifconfig',
interface, 'inet']
try:
out = su.check_output(ip4_cmd)
out = out.splitlines()[2].split()[1].decode()
jail['ip4_addr'] = f'{interface}|{out}'
except (su.CalledProcessError, IndexError):
jail['ip4_addr'] = f'{interface}|ERROR'
else:
jail['ip4_addr'] = 'DHCP (not running)'
jails.append(jail)
except ioc_exceptions.JailMisconfigured as e:
self.logger.error(e, exc_info=True)
except BaseException:
# Brandon is working on fixing this generic except, till then I
# am not going to make the perfect the enemy of the good enough!
self.logger.debug('Failed to get list of jails', exc_info=True)
return filter_list(jails, filters, options)
query._fiterable = True
@accepts(
Dict(
"options",
Str("release", required=True),
Str("template"),
Str("pkglist"),
Str("uuid", required=True),
Bool("basejail", default=False),
Bool("empty", default=False),
Bool("short", default=False),
List("props", default=[])
)
)
async def do_create(self, options):
"""Creates a jail."""
# Typically one would return the created jail's id in this
# create call BUT since jail creation may or may not involve
# fetching a release, which in turn could be time consuming
# and could then block for a long time. This dictates that we
# make it a job, but that violates the principle that CRUD methods
# are not jobs as yet, so I settle on making this a wrapper around
# the main job that calls this and return said job's id instead of
# the created jail's id
return await self.middleware.call('jail.create_job', options)
@private
@job(lock=lambda args: f'jail_create:{args[-1]["uuid"]}')
def create_job(self, job, options):
verrors = ValidationErrors()
uuid = options["uuid"]
job.set_progress(0, f'Creating: {uuid}')
try:
self.check_jail_existence(uuid, skip=False)
verrors.add(
'uuid',
f'A jail with name {uuid} already exists'
)
raise verrors
except CallError:
# A jail does not exist with the provided name, we can create one
# now
verrors = self.common_validation(verrors, options)
if verrors:
raise verrors
job.set_progress(20, 'Initial validation complete')
if not any('resolver' in p for p in options['props']):
dc = self.middleware.call_sync(
'service.query', [('service', '=', 'domaincontroller')]
)[0]
dc_config = self.middleware.call_sync('domaincontroller.config')
if dc['enable'] and (
dc_config['dns_forwarder'] and
dc_config['dns_backend'] == 'SAMBA_INTERNAL'
):
options['props'].append(
f'resolver=nameserver {dc_config["dns_forwarder"]}'
)
iocage = ioc.IOCage(skip_jails=True)
release = options["release"]
template = options.get("template", False)
pkglist = options.get("pkglist", None)
basejail = options["basejail"]
empty = options["empty"]
short = options["short"]
props = options["props"]
pool = IOCJson().json_get_value("pool")
iocroot = IOCJson(pool).json_get_value("iocroot")
if template:
release = template
if (
not os.path.isdir(f'{iocroot}/releases/{release}') and
not template and
not empty
):
job.set_progress(50, f'{release} missing, calling fetch')
self.middleware.call_sync(
'jail.fetch', {"release": release}, job=True
)
err, msg = iocage.create(
release,
props,
0,
pkglist,
template=template,
short=short,
_uuid=uuid,
basejail=basejail,
empty=empty
)
if err:
raise CallError(msg)
job.set_progress(100, f'Created: {uuid}')
return True
@private
def validate_ips(self, verrors, options, schema='options.props', exclude=None):
for item in options['props']:
for f in ('ip4_addr', 'ip6_addr'):
# valid ip values can be
# "none" "interface|accept_rtadv" "interface|ip/subnet" "interface|ip"
# we explicitly check these
if f in item:
for ip in [
ip.split('|')[1].split('/')[0] if '|' in ip else ip.split('/')[0]
for ip in item.split('=')[1].split(',')
if ip != 'none' and (ip.count('|') and ip.split('|')[1] != 'accept_rtadv')
]:
try:
IpInUse(self.middleware, exclude)(
ip
)
except ValueError as e:
verrors.add(
f'{schema}.{f}',
str(e)
)
@accepts(Str("jail"), Dict(
"options",
Bool("plugin", default=False),
additional_attrs=True,
))
def do_update(self, jail, options):
"""Sets a jail property."""
plugin = options.pop("plugin")
_, _, iocage = self.check_jail_existence(jail)
name = options.pop("name", None)
verrors = ValidationErrors()
jail = self.query([['id', '=', jail]], {'get': True})
verrors = self.common_validation(verrors, options, True, jail)
if name is not None and plugin:
verrors.add('options.plugin',
'Cannot be true while trying to rename')
if verrors:
raise verrors
for prop, val in options.items():
p = f"{prop}={val}"
try:
iocage.set(p, plugin)
except RuntimeError as err:
raise CallError(err)
if name:
iocage.rename(name)
return True
@private
def common_validation(self, verrors, options, update=False, jail=None):
if not update:
# Ensure that api call conforms to format set by iocage for props
# Example 'key=value'
for value in options['props']:
if '=' not in value:
verrors.add(
'options.props',
'Please follow the format specified by iocage for api calls'
'e.g "key=value"'
)
break
if verrors:
raise verrors
# normalise vnet mac address
# expected format here is 'vnet0_mac=00-D0-56-F2-B5-12,00-D0-56-F2-B5-13'
vnet_macs = {
f.split('=')[0]: f.split('=')[1] for f in options['props']
if any(f'vnet{i}_mac' in f.split('=')[0] for i in range(0, 4))
}
self.validate_ips(verrors, options)
else:
vnet_macs = {
key: value for key, value in options.items()
if any(f'vnet{i}_mac' in key for i in range(0, 4))
}
exclude_ips = [
ip.split('|')[1].split('/')[0] if '|' in ip else ip.split('/')[0]
for f in ('ip4_addr', 'ip6_addr') for ip in jail[f].split(',')
if ip not in ('none', 'DHCP (not running)')
]
self.validate_ips(
verrors, {'props': [f'{k}={v}' for k, v in options.items()]},
'options', exclude_ips
)
# validate vnetX_mac addresses
for key, value in vnet_macs.items():
if value and value != 'none':
value = value.replace(',', ' ')
try:
for mac in value.split():
MACAddr()(mac)
if (
len(value.split()) != 2 or
any(value.split().count(v) > 1 for v in value.split())
):
raise ValueError('Exception')
except ValueError:
verrors.add(
key,
'Please Enter two valid and different '
f'space/comma-delimited MAC addresses for {key}.'
)
return verrors
@accepts(Str("jail"))
def do_delete(self, jail):
"""Takes a jail and destroys it."""
_, _, iocage = self.check_jail_existence(jail)
# TODO: Port children checking, release destroying.
iocage.destroy_jail()
return True
@private
def check_dataset_existence(self):
try:
IOCCheck()
except ioc_exceptions.PoolNotActivated as e:
raise CallError(e, errno=errno.ENOENT)
@private
def check_jail_existence(self, jail, skip=True, callback=None):
"""Wrapper for iocage's API, as a few commands aren't ported to it"""
try:
if callback is not None:
iocage = ioc.IOCage(callback=callback,
skip_jails=skip, jail=jail)
else:
iocage = ioc.IOCage(skip_jails=skip, jail=jail)
jail, path = iocage.__check_jail_existence__()
except (SystemExit, RuntimeError):
raise CallError(f"jail '{jail}' not found!")
return jail, path, iocage
@accepts()
def get_activated_pool(self):
"""Returns the activated pool if there is one, or None"""
try:
pool = ioc.IOCage(skip_jails=True).get('', pool=True)
except RuntimeError as e:
raise CallError(f'Error occurred getting activated pool: {e}')
except (ioc_exceptions.PoolNotActivated, FileNotFoundError):
self.check_dataset_existence()
try:
pool = ioc.IOCage(skip_jails=True).get('', pool=True)
except ioc_exceptions.PoolNotActivated:
pool = None
return pool
@accepts(
Dict(
"options",
Str("release"),
Str("server", default="download.freebsd.org"),
Str("user", default="anonymous"),
Str("password", default="anonymous@"),
Str("name", default=None, null=True),
Bool("accept", default=True),
List("props", default=[]),
List(
"files",
default=["MANIFEST", "base.txz", "lib32.txz", "doc.txz"]
),
Str("branch", default=None, null=True)
)
)
@job(lock=lambda args: f"jail_fetch:{args[-1]}")
def fetch(self, job, options):
"""Fetches a release or plugin."""
fetch_output = {'install_notes': []}
release = options.get('release', None)
post_install = False
verrors = ValidationErrors()
self.validate_ips(verrors, options)
if verrors:
raise verrors
def progress_callback(content, exception):
msg = content['message'].strip('\r\n')
rel_up = f'* Updating {release} to the latest patch level... '
nonlocal post_install
if options['name'] is None:
if 'Downloading : base.txz' in msg and '100%' in msg:
job.set_progress(5, msg)
elif 'Downloading : lib32.txz' in msg and '100%' in msg:
job.set_progress(10, msg)
elif 'Downloading : doc.txz' in msg and '100%' in msg:
job.set_progress(15, msg)
elif 'Downloading : src.txz' in msg and '100%' in msg:
job.set_progress(20, msg)
if 'Extracting: base.txz' in msg:
job.set_progress(25, msg)
elif 'Extracting: lib32.txz' in msg:
job.set_progress(50, msg)
elif 'Extracting: doc.txz' in msg:
job.set_progress(75, msg)
elif 'Extracting: src.txz' in msg:
job.set_progress(90, msg)
elif rel_up in msg:
job.set_progress(95, msg)
else:
job.set_progress(None, msg)
else:
if ' These pkgs will be installed:' in msg:
job.set_progress(50, msg)
elif 'Installing plugin packages:' in msg:
job.set_progress(75, msg)
elif 'Command output:' in msg:
job.set_progress(90, msg)
# Sets each message going forward as important to the user
post_install = True
else:
job.set_progress(None, msg)
if post_install:
for split_msg in msg.split('\n'):
fetch_output['install_notes'].append(split_msg)
self.check_dataset_existence() # Make sure our datasets exist.
start_msg = f'{release} being fetched'
final_msg = f'{release} fetched'
iocage = ioc.IOCage(callback=progress_callback, silent=False)
if options["name"] is not None:
pool = IOCJson().json_get_value('pool')
iocroot = IOCJson(pool).json_get_value('iocroot')
plugin_index = pathlib.Path(
f'{iocroot}/.plugin_index'
)
if not plugin_index.is_dir():
# WORKAROUND until rewritten for #39653
# We want the plugins to not prompt interactively
try:
iocage.fetch(plugin_file=True, _list=True, **options)
except Exception:
# Expected, this is to avoid it later
pass
options["plugin_file"] = True
start_msg = 'Starting plugin install'
final_msg = f"Plugin: {options['name']} installed"
options["accept"] = True
job.set_progress(0, start_msg)
iocage.fetch(**options)
if post_install and options['name'] is not None:
plugin_manifest = pathlib.Path(
f'{iocroot}/.plugin_index/{options["name"]}.json'
)
plugin_json = json.loads(plugin_manifest.read_text())
schema_version = plugin_json.get('plugin_schema', '1')
if schema_version.isdigit() and int(schema_version) >= 2:
plugin_output = pathlib.Path(
f'{iocroot}/jails/{options["name"]}/root/root/PLUGIN_INFO'
)
if plugin_output.is_file():
# Otherwise it will be the verbose output from the
# post_install script
fetch_output['install_notes'] = [
x for x in plugin_output.read_text().split('\n') if x
]
# This is to get the admin URL and such
fetch_output['install_notes'] += job.progress[
'description'].split('\n')
job.set_progress(100, final_msg)
return fetch_output
@accepts(Str("resource", enum=["RELEASE", "TEMPLATE", "PLUGIN"]),
Bool("remote", default=False))
def list_resource(self, resource, remote):
"""Returns a JSON list of the supplied resource on the host"""
self.check_dataset_existence() # Make sure our datasets exist.
iocage = ioc.IOCage(skip_jails=True)
resource = "base" if resource == "RELEASE" else resource.lower()
if resource == "plugin":
if remote:
try:
resource_list = self.middleware.call_sync(
'cache.get', 'iocage_remote_plugins')
return resource_list
except KeyError:
pass
resource_list = iocage.fetch(list=True, plugins=True,
header=False)
else:
resource_list = iocage.list("all", plugin=True)
pool = IOCJson().json_get_value("pool")
iocroot = IOCJson(pool).json_get_value("iocroot")
index_path = f'{iocroot}/.plugin_index/INDEX'
if not pathlib.Path(index_path).is_file():
index_json = None
for plugin in resource_list:
plugin += ['N/A', 'N/A']
return resource_list
else:
index_fd = open(index_path, 'r')
index_json = json.load(index_fd)
for plugin in resource_list:
for i, elem in enumerate(plugin):
# iocage returns - for None
plugin[i] = elem if elem != "-" else None
if remote:
pv = self.get_plugin_version(plugin[2])
else:
pv = self.get_local_plugin_version(
plugin[1], index_json, iocroot)
resource_list[resource_list.index(plugin)] = plugin + pv
if remote:
self.middleware.call_sync(
'cache.put', 'iocage_remote_plugins', resource_list,
86400
)
else:
index_fd.close()
elif resource == "base":
try:
if remote:
resource_list = self.middleware.call_sync(
'cache.get', 'iocage_remote_releases')
return resource_list
except KeyError:
pass
resource_list = iocage.fetch(list=True, remote=remote, http=True)
if remote:
self.middleware.call_sync(
'cache.put', 'iocage_remote_releases', resource_list,
86400
)
else:
resource_list = iocage.list(resource)
return resource_list
@accepts(Str("action", enum=["START", "STOP", "RESTART"]))
def rc_action(self, action):
"""Does specified action on rc enabled (boot=on) jails"""
iocage = ioc.IOCage(rc=True)
if action == "START":
iocage.start()
elif action == "STOP":
iocage.stop()
else:
iocage.restart()
return True
@accepts(Str("jail"))
def start(self, jail):
"""Takes a jail and starts it."""
uuid, _, iocage = self.check_jail_existence(jail)
status, _ = IOCList.list_get_jid(uuid)
if not status:
iocage.start()
return True
@accepts(Str("jail"), Bool('force', default=False))
def stop(self, jail, force):
"""Takes a jail and stops it."""
uuid, _, iocage = self.check_jail_existence(jail)
status, _ = IOCList.list_get_jid(uuid)
if status:
iocage.stop(force=force)
return True
@accepts(Str('jail'))
def restart(self, jail):
"""Takes a jail and restarts it."""
uuid, _, iocage = self.check_jail_existence(jail)
status, _ = IOCList.list_get_jid(uuid)
if status:
iocage.stop()
iocage.start()
return True
@private
def get_iocroot(self):
pool = IOCJson().json_get_value("pool")
return IOCJson(pool).json_get_value("iocroot")
@accepts(
Str("jail"),
Dict(
"options",
Str("action", enum=["ADD", "EDIT", "REMOVE", "REPLACE", "LIST"], required=True),
Str("source"),
Str("destination"),
Str("fstype", default='nullfs'),
Str("fsoptions", default='ro'),
Str("dump", default='0'),
Str("pass", default='0'),
Int("index", default=None),
))
def fstab(self, jail, options):
"""Adds an fstab mount to the jail"""
uuid, _, iocage = self.check_jail_existence(jail, skip=False)
status, jid = IOCList.list_get_jid(uuid)
action = options['action'].lower()
if status and action != 'list':
raise CallError(
f'{jail} should not be running when adding a mountpoint')
verrors = ValidationErrors()
source = options.get('source')
if source:
if not os.path.exists(source):
verrors.add(
'options.source',
'Provided path for source does not exist'
)
source = source.replace(' ', r'\040') # fstab hates spaces ;)
destination = options.get('destination')
if destination:
destination = f'/{destination}' if destination[0] != '/' else destination
dst = f'{self.get_iocroot()}/jails/{jail}/root'
if dst not in destination:
destination = f'{dst}{destination}'
if os.path.exists(destination):
if not os.path.isdir(destination):
verrors.add(
'options.destination',
'Destination is not a directory, please provide a valid destination'
)
elif os.listdir(destination):
verrors.add(
'options.destination',
'Destination directory should be empty'
)
else:
os.makedirs(destination)
# fstab hates spaces ;)
destination = destination.replace(' ', r'\040')
if action != 'list':
for f in options:
if not options.get(f) and f not in ('index',):
verrors.add(
f'options.{f}',
'This field is required'
)
fstype = options.get('fstype')
fsoptions = options.get('fsoptions')
dump = options.get('dump')
_pass = options.get('pass')
index = options.get('index')
if action == 'replace' and index is None:
verrors.add(
'options.index',
'Index must not be None when replacing fstab entry'
)
if verrors:
raise verrors
try:
_list = iocage.fstab(
action, source, destination, fstype, fsoptions,
dump, _pass, index=index
)
except ioc_exceptions.ValidationFailed as e:
# CallError uses strings, the exception message may not always be a
# list.
if not isinstance(e.message, str) and isinstance(
e.message,
Iterable
):
e.message = '\n'.join(e.message)
self.logger.error(f'{e!r}')
raise CallError(e.message)
if action == "list":
split_list = {}
system_mounts = (
'/root/bin', '/root/boot', '/root/lib', '/root/libexec',
'/root/rescue', '/root/sbin', '/root/usr/bin',
'/root/usr/include', '/root/usr/lib', '/root/usr/libexec',
'/root/usr/sbin', '/root/usr/share', '/root/usr/libdata',
'/root/usr/lib32'
)
for i in _list:
fstab_entry = i[1].split()
_fstab_type = 'SYSTEM' if fstab_entry[0].endswith(
system_mounts) else 'USER'
split_list[i[0]] = {'entry': fstab_entry, 'type': _fstab_type}
return split_list
return True
@accepts(Str("pool"))
def activate(self, pool):
"""Activates a pool for iocage usage, and deactivates the rest."""
zfs = libzfs.ZFS(history=True, history_prefix="<iocage>")
pools = zfs.pools
prop = "org.freebsd.ioc:active"
activated = False
for _pool in pools:
if _pool.name == pool:
ds = zfs.get_dataset(_pool.name)
ds.properties[prop] = libzfs.ZFSUserProperty("yes")
activated = True
else:
ds = zfs.get_dataset(_pool.name)
ds.properties[prop] = libzfs.ZFSUserProperty("no")
return activated
@accepts(Str("ds_type", enum=["ALL", "JAIL", "TEMPLATE", "RELEASE"]))
def clean(self, ds_type):
"""Cleans all iocage datasets of ds_type"""
if ds_type == "JAIL":
IOCClean().clean_jails()
elif ds_type == "ALL":
IOCClean().clean_all()
elif ds_type == "TEMPLATE":
IOCClean().clean_templates()
return True
@accepts(
Str("jail"),
List("command", required=True),
Dict("options", Str("host_user", default="root"), Str("jail_user")))
def exec(self, jail, command, options):
"""Issues a command inside a jail."""
_, _, iocage = self.check_jail_existence(jail, skip=False)
host_user = options["host_user"]
jail_user = options.get("jail_user", None)
if isinstance(command[0], list):
# iocage wants a flat list, not a list inside a list
command = list(itertools.chain.from_iterable(command))
# We may be getting ';', '&&' and so forth. Adding the shell for
# safety.
if len(command) == 1:
command = ["/bin/sh", "-c"] + command
host_user = "" if jail_user and host_user == "root" else host_user
try:
msg = iocage.exec(
command, host_user, jail_user, start_jail=True, msg_return=True
)
except RuntimeError as e:
raise CallError(str(e))
return '\n'.join(msg)
@accepts(Str("jail"))
@job(lock=lambda args: f"jail_update:{args[-1]}")
def update_to_latest_patch(self, job, jail):
"""Updates specified jail to latest patch level."""
job.set_progress(0, f'Updating {jail}')
msg_queue = deque(maxlen=10)
def progress_callback(content, exception):
msg = content['message'].strip('\n')
msg_queue.append(msg)
final_msg = '\n'.join(msg_queue)
if 'Inspecting system... done' in msg:
job.set_progress(20)
elif 'Preparing to download files... done.' in msg:
job.set_progress(50)
elif 'Applying patches... done.' in msg:
job.set_progress(75)
elif 'Installing updates... done.' in msg:
job.set_progress(90)
elif f'{jail} has been updated successfully' in msg:
job.set_progress(100)
job.set_progress(None, description=final_msg)
_, _, iocage = self.check_jail_existence(
jail,
callback=progress_callback
)
iocage.update()
return True
@accepts(
Str("jail"),
Dict("options",
Str("release", required=False),
Bool("plugin", default=False))
)
@job(lock=lambda args: f"jail_upgrade:{args[-1]}")
def upgrade(self, job, jail, options):
"""Upgrades specified jail to specified RELEASE."""
verrors = ValidationErrors()
release = options.get('release', None)
plugin = options['plugin']
if release is None and not plugin:
verrors.add(
'options.release',
'Must not be None if options.plugin is False.'
)
raise verrors
job.set_progress(0, f'Upgrading {jail}')
msg_queue = deque(maxlen=10)
def progress_callback(content, exception):
msg = content['message'].strip('\n')
msg_queue.append(msg)
final_msg = '\n'.join(msg_queue)
if plugin:
plugin_progress(job, msg)
else:
jail_progress(job, msg)
job.set_progress(None, description=final_msg)
def plugin_progress(job, msg):
if 'Snapshotting' in msg:
job.set_progress(20)
elif 'Updating plugin INDEX' in msg:
job.set_progress(40)
elif 'Running upgrade' in msg:
job.set_progress(70)
elif 'Installing plugin packages' in msg:
job.set_progress(90)
elif f'{jail} successfully upgraded' in msg:
job.set_progress(100)
def jail_progress(job, msg):
if 'Inspecting system' in msg:
job.set_progress(20)
elif 'Preparing to download files' in msg:
job.set_progress(50)
elif 'Applying patches' in msg:
job.set_progress(75)
elif 'Installing updates' in msg:
job.set_progress(90)
elif f'{jail} successfully upgraded' in msg:
job.set_progress(100)
_, _, iocage = self.check_jail_existence(
jail,
callback=progress_callback
)
iocage.upgrade(release=release)
return True
@accepts(Str("jail"))
@job(lock=lambda args: f"jail_export:{args[-1]}")
def export(self, job, jail):
"""Exports jail to zip file"""
uuid, path, _ = self.check_jail_existence(jail)
status, jid = IOCList.list_get_jid(uuid)
started = False
if status:
self.stop(jail)
started = True
IOCImage().export_jail(uuid, path)
if started:
self.start(jail)
return True
@accepts(Str("jail"))
@job(lock=lambda args: f"jail_import:{args[-1]}")
def _import(self, job, jail):
"""Imports jail from zip file"""
IOCImage().import_jail(jail)
return True
@private
def get_plugin_version(self, pkg):
"""
Fetches a list of pkg's from the http://pkg.cdn.trueos.org/iocage/
repo and returns a list with the pkg version and plugin revision
"""
try:
pkg_dict = self.middleware.call_sync('cache.get',
'iocage_rpkgdict')
r_plugins = self.middleware.call_sync('cache.get',
'iocage_rplugins')
except KeyError:
branch = self.get_version()
r_pkgs = requests.get(
f'http://pkg.cdn.trueos.org/iocage/{branch}/All')
r_pkgs.raise_for_status()
pkg_dict = {}
for i in r_pkgs.iter_lines():
i = i.decode().split('"')
try:
pkg, version = i[1].rsplit('-', 1)
pkg_dict[pkg] = version
except (ValueError, IndexError):
continue # It's not a pkg
self.middleware.call_sync(
'cache.put', 'iocage_rpkgdict', pkg_dict,
86400
)
r_plugins = requests.get(
'https://raw.githubusercontent.com/freenas/'
f'iocage-ix-plugins/{branch}/INDEX'
)
r_plugins.raise_for_status()