-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector-pkg.py
More file actions
847 lines (709 loc) · 32.3 KB
/
vector-pkg.py
File metadata and controls
847 lines (709 loc) · 32.3 KB
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
#!/usr/bin/env python
# RCM 2020-8-22 Modified from https://github.com/kurianinc/opkg
# Note Vector runs python 2.7, and the rest of the world is on 3
from __future__ import print_function
"""
Vector openpkg package management engine.
"""
__author__ = "Randall Maas <randym@randym.name>"
# The original used yaml manifest, we use a windows .ini style, which is
# compatible with what is already installed on Vector
import re
import os
import subprocess
import ConfigParser
import time
import hashlib
import sys
import shutil
'''This file will be looked up under OPKG_DIR/conf'''
OPKG_CONF_FILE='/etc/vpkg/conf/vpkg.env'
META_FILE_PREVIOUS='Previous.meta'
META_FILE_LATEST='Latest.meta'
EXTRA_PARAM_DELIM=','
EXTRA_PARAM_KEY_VAL_SEP='='
'''Logs an error'''
def loge(msg):
"Send error message to stderr"
print(msg, file=sys.stderr)
sys.stderr.flush()
'''A helper to create all of the directories along a path'''
def makedirs(path):
if not os.path.exists(path):
os.makedirs(path)
'''A helper to remove a directory tree that is no longer needed'''
def rmtree(path):
if os.path.exists(path):
shutil.rmtree(path, ignore_errors=True)
'''This reads the .ini configuration file.
This modifies it to preserve case of the filesystem'''
class PkgConfigParser(ConfigParser.ConfigParser):
def optionxform(self, optionstr):
return optionstr
''' Classes '''
'''Tracks local env configuration'''
class EnvConfig():
def __init__(self):
self.config_file=OPKG_CONF_FILE
self.conf=None
def setConfigFile(self,config_file):
self.config_file=config_file
def loadConfigFile(self):
self.conf = PkgConfigParser()
if os.path.isfile(self.config_file) and os.access(self.config_file, os.R_OK):
self.conf.read(self.config_file)
def updateConfigItem(self,section,key,val):
try:
self.conf.set(section,key,val)
except:
print ("Warning: Cannot locate config item "+key+" in "+self.config_file)
return False
return True
def getConfigItem(self,section,item):
return self.conf.get(section,item)
'''Reads the manifest config parsed from INI file'''
def get_manifest(manifest_file):
"Returns config parsed from INI file in filelike object"
config = None
try:
config = PkgConfigParser();
config.read(manifest_file);
except Exception as exc:
loge ("Error: Problem loading manifest file "+manifest_file)
print(exc)
return
if not config.has_option("META", 'rel_num'):
loge ("Error: rel_num not found in "+manifest_file)
return
return config
'''Class for core Open Pkg'''
class Pkg():
def __init__(self,name):
if re.search("[^\w\-]", name) is not None:
loge ("Error: Illegal character in package name (" + name + ")")
return
self.name=name
self.rel_num=None
self.rel_ts=None
self.manifest_file = name + '.ini'
self.tarball_name = name + '.vpkg'
self.md5=None #md5 of package being installed.
self.manifest=None
self.build_root = os.getcwd()
self.manifest_path=self.build_root+'/'+self.manifest_file
'''stage_dir - where files to create a tarball are staged and
files from a tarball are extracted for installation.
By default, the stage_dir is set for action create pkg.
'''
self.stage_dir = self.build_root + '/.pkg/' + name
self.deploy_dir=self.stage_dir+'/.install'
self.env_conf=None
self.install_meta=None #meta data of existing installation
self.install_md5=None #md5 of currently installed version
@staticmethod
def parseName(pkg_label):
'''pkg can be specified in following ways:
- /path/to/mypkg.vpkg -- Vector package (tarball) available locally
- /path/to/mypkg-rel_num.vpkg -- Vector package (tarball) available locally
- /path/to/mypkg.tgz -- tarball available locally
- /path/to/mypkg-rel_num.tgz -- tarball available locally
- mypkg
- mypkg-rel_num
'''
pkg_name_rel_num = os.path.basename(pkg_label)
pkg_name_rel_num = pkg_name_rel_num.replace('.tgz', '')
pkg_name_rel_num = pkg_name_rel_num.replace('.vpkg', '')
tarball_name = pkg_name_rel_num + '.vpkg'
m = re.search('^(.+)-([\\d.]+|[\\d.]+[\\d.]+)$', pkg_name_rel_num)
if m:
pkg_name = m.group(1)
else:
pkg_name = pkg_name_rel_num
return pkg_name,pkg_name_rel_num,tarball_name
@staticmethod
def parseTarballName(tarball_name):
rel_num, rel_ts = 'dev', None
'''The dev version will not have any rel_num or rel_ts
The parsing is based on the assumption that the tarball names can have only 2 formats:
name.vpkg - dev
name.tgz - dev
name-rel_num-rel_ts.vpkg - release
name-rel_num-rel_ts.tgz - release
'''
m = re.search('.+?-([\\d.]+?)-([\\d.]+).(tgz|vpkg)', tarball_name)
if m:
rel_num = m.group(1)
rel_ts = m.group(2)
else:
m = re.search('.+?-([\\d.]+?).(tgz|vpkg)', tarball_name)
if m:
rel_num = m.group(1)
return rel_num,rel_ts
def setRelNum(self,rel_num):
self.rel_num=rel_num
def setRelTs(self,rel_ts):
self.rel_ts=rel_ts
'''Meta file has this syntax: pkg_name,rel_num,rel_ts,pkg_md5,deploy_ts'''
def loadMeta(self):
self.install_meta=dict()
meta_dir=self.env_conf['basic']['opkg_dir'] + '/meta/' + self.name
meta_path = meta_dir + "/" + META_FILE_LATEST
self.install_meta['dir']=meta_dir
self.install_meta['latest_install']=self.loadMetaFile(meta_path)
if not self.install_meta['latest_install']:
print ("Info: No active installation of "+self.name+" found at "+self.env_conf['basic']['opkg_dir'])
meta_path = meta_dir + "/" + META_FILE_PREVIOUS
self.install_meta['previous_install'] = self.loadMetaFile(meta_path)
if not self.install_meta['previous_install']:
print ("Info: No previous installation of "+self.name+" found.")
if self.install_meta['latest_install']:
self.install_md5 = self.install_meta['latest_install']['pkg_md5']
def getMeta(self):
return self.install_meta
'''Load .meta files that keep track of deployments and verifies the data in those.
The meta data on package deployment is a single line with attrs delimited by , in the following order:
pkg_name,pkg_rel_num,pkg_ts,pkg_md5,deploy_ts
'''
def loadMetaFile(self,file_path):
if not os.path.isfile(file_path): return None
str = loadFile(file_path)
install_info = str.strip().split(',')
if len(install_info) < 5: return None
meta=dict()
meta['pkg_name']=install_info[0]
meta['pkg_rel_num'] = install_info[1]
meta['pkg_ts'] = install_info[2]
meta['pkg_md5'] = install_info[3]
meta['deploy_ts'] = install_info[4]
meta['undo_package'] = install_info[5]
return meta
'''Reset install meta files upon successful installation of a package.'''
def registerInstall(self,deploy_inst, uninstall):
meta_dir=deploy_inst.opkg_dir + "/meta/" + self.name
meta_file_previous = meta_dir + "/" + META_FILE_PREVIOUS
meta_file_latest = meta_dir + "/" + META_FILE_LATEST
makedirs(meta_dir)
# move the uninstall package to the folder
if not execOSCommand("mv -f " + uninstall + " " + meta_dir):
print ("Problem moving " + uninstall + " to uninstall directory")
return False
if os.path.exists(meta_file_latest):
if not execOSCommand("mv -f " + meta_file_latest + " " + meta_file_previous):
print ("Problem moving " + meta_file_latest + " as " + meta_file_previous)
return False
'''Meta file has this syntax: pkg_name,rel_num,rel_ts,pkg_md5,deploy_dir,undo-pkg'''
rel_num=''
if self.rel_num: rel_num=self.rel_num
rel_ts=0
if self.rel_ts: rel_ts=self.rel_ts
strx = self.name+','+rel_num+','+str(rel_ts)+','+self.pkg_md5+','+deploy_inst.deploy_ts+','+os.path.basename(uninstall)
cmd = "echo " + strx + ">" + meta_file_latest
if not execOSCommand(cmd):
loge ("Error: Couldn't record the package installation.")
return False
self.loadMeta()
return True
def setEnvConfig(self,env_conf):
self.env_conf=env_conf
def create(self):
#the default manifest points to that in build dir
self.manifest = get_manifest(self.manifest_path)
rmtree(self.stage_dir)
makedirs(self.stage_dir)
os.chdir(self.stage_dir)
'''Copy manifest to the deploy folder in archive'''
makedirs(self.deploy_dir)
try:
shutil.copy(self.manifest_path, self.deploy_dir)
except Exception as err:
loge ("Error: Problem copying package manifest. " + str(err))
return False
'''Stage files content for archiving'''
if self.manifest.has_section('files'):
for tgt in self.manifest.options('files'):
src = self.manifest.get('files',tgt)
if not self.stageContent(os.path.join(self.build_root,src),tgt.strip('/')):
loge ("Error: Cannot copy content at "+src+" for archiving.")
return False
'''Make tarball and clean up the staging area'''
if self.manifest.has_option("META", 'rel_num'):
rel_num=self.manifest.get("META", 'rel_num')
self.tarball_name = self.name + '-' + rel_num + '.vpkg'
os.chdir(self.stage_dir)
rc = runCmd("tar czf " + self.tarball_name + ' * '+os.path.basename(self.deploy_dir))
if rc != 0:
loge ("Error: Couldn't create package " + self.tarball_name)
return False
os.chdir(self.build_root)
try:
shutil.move(self.stage_dir + '/' + self.tarball_name, './')
rmtree(self.stage_dir)
print ("Package " + self.tarball_name + " has been created.")
except Exception as err:
loge ("Error: Package " + self.tarball_name + " couldn't be created. "+str(err))
return False
'''Creates an archive snapshotting the current state. This is used to to
later undoan installation.'''
def createUndoManifest(self,undoManifestPath):
undoConfig = PkgConfigParser()
#copy the key pieces from the manifest
self.manifest = get_manifest(self.manifest_path)
# copy the manifest information
if self.manifest.has_section('META'):
undoConfig.add_section('META')
for tgt in self.manifest.options('META'):
undoConfig.set('META', tgt, self.manifest.get('META', tgt))
# copy the list of files -- just the ones that will be modified
# not the ones from the pkg
undoConfig.add_section('files')
if self.manifest.has_section('files'):
for tgt in self.manifest.options('files'):
target = '/' + tgt.strip('/')
undoConfig.set('files',target,target)
if self.manifest.has_section('templates'):
for index in self.manifest.options('templates'):
target = '/' + self.manifest.get('templates',index)+ tgt.strip('/')
undoConfig.set('files',target,target)
# copy and reverse the string flipper
# todo: this maybe should be save that file?
# - the pattern could be a regular expression so that won'y work easily
if self.manifest.has_section('replaces'):
undoConfig.add_section('replaces')
for replaces_file,token in self.manifest.items('replaces'):
'''Each entry for replacement in the replaces_file is a dict as replacement entries are delimited with :
We'll reverse those'''
pattern, replace = re.split(Tmpl.TMPL_KEY_VAL_DELIM,token)
undoConfig.set('replaces', replaces_file, replace+Tmpl.TMPL_KEY_VAL_DELIM+pattern)
# TODO: could fix up the symlinks, permissions but that isn't clear enough how to
# write it out
cfgfile = open(undoManifestPath, 'w+')
undoConfig.write(cfgfile)
cfgfile.close()
# How to launch a making of the archive?
def stageContent(self,src,tgt):
os.chdir(self.stage_dir)
if os.path.isdir(src):
'''skip build folder silently, but individual files can still be added.'''
try:
makedirs(os.path.dirname(tgt))
shutil.copytree(src, tgt)
except:
return False
else:
tgt_dir = os.path.dirname(tgt)
if tgt_dir != '':
try:
makedirs(tgt_dir)
except:
return False
if os.path.exists(src):
try:
shutil.copy(src, tgt)
except Exception as err:
return False
return True
'''Execute the deploy playbook for a package specified in the manifest'''
def install(self,tarball_path,deploy_inst,pkg_name):
deploy_inst.logHistory("Installing package "+self.name+" using "+tarball_path)
''' Track the md5 of package being installed '''
self.pkg_md5=getFileMD5(tarball_path)
'''Extract the tarball in stage_dir, to prepare for deploy playbook to execute steps'''
stage_dir=os.path.join(deploy_inst.stage_dir,self.name,deploy_inst.deploy_ts)
try:
makedirs(stage_dir)
except:
return
os.chdir(stage_dir)
if not execOSCommand('tar xzf ' + tarball_path):
loge ("Error: Problem extracting " + tarball_path + " in " + stage_dir)
return False
'''Resolve manifest, and files defined under templates and replaces with actual values
defined for this specific installation.'''
self.manifest_path=os.path.join(os.getcwd(),os.path.join(".install",self.manifest_file))
tmpl_inst=Tmpl(self.manifest_path)
if not tmpl_inst.resolveVars(deploy_inst.getVars()):
loge ("Error: Problem resolving "+self.manifest_file)
return False
pkg_manifest=get_manifest(self.manifest_path)
'''Snapshot the files that will be changed to allow undoing'''
if not deploy_inst.uninstall:
deploy_inst.logHistory("Making backup")
undo_manifest_path=os.path.join(deploy_inst.stage_dir, pkg_name+"_undo.ini")
self.createUndoManifest(undo_manifest_path)
os.chdir(deploy_inst.stage_dir)
undo_pkg=Pkg(pkg_name+"_undo")
undo_pkg.create()
runCmd("rm " + undo_manifest_path)
os.chdir(stage_dir)
'''copy targets entries to install_root'''
# Only uses well-defined folders to prevent too much damage
for base_path in ['anki','etc','home', 'usr', 'var']:
source_path = os.path.join(stage_dir, base_path)
# Did the archive include this top-level folder?
if not os.path.exists(source_path): continue
# Command to copy the folder onto the main system
target_path = '/' + base_path
cmd='cp -r '+source_path+'/* '+target_path+'/'
if not execOSCommand(cmd):
loge ("Error: Problem copying from " + stage_dir + ". command: " + cmd)
return False
'''Generate installation template files with actual values, variables are marked as {{ var }} '''
if pkg_manifest.has_section('templates'):
for index in pkg_manifest.options('templates'):
tmpl = pkg_manifest.get('templates',index)
tmpl_path=tmpl
#if not re.match("^\/", tmpl): tmpl_path = deploy_dir + "/" + tmpl
tmpl_inst=Tmpl(tmpl_path)
if not tmpl_inst.resolveVars(deploy_inst.getVars()):
loge ("Error: Couldn't install resolved files for those marked as templates, with real values.")
return False
'''Replaces tokens in files flagged for that, tokens are unmarked like PORT=80 etc'''
if pkg_manifest.has_section('replaces'):
for replaces_file,pattern in pkg_manifest.items('replaces'):
'''Each entry for replacement in the replaces_file is a dict as replacement entries are delimited with :'''
replaces_path=replaces_file
#if not re.match("^\/", replaces_file): replaces_path = deploy_dir + "/" + replaces_file
tmpl_inst=Tmpl(replaces_path)
if not tmpl_inst.replaceTokens(pattern):
loge ("Error: Couldn't install resolved files for those marked with having tokens in the 'replaces' section, with real values.")
return False
'''Symlinks'''
if pkg_manifest.has_section('symlinks'):
for tgt_path in pkg_manifest.options('symlinks'):
src_path = pkg_manifest.get('symlinks',tgt_path)
#if not re.match("^\/", tgt_path): tgt_path = deploy_dir + "/" + tgt_path
#if not re.match("^\/", src_path): src_path = deploy_dir + "/" + src_path
cmd = "ln -sfn " + src_path + " " + tgt_path
if not execOSCommand(cmd):
loge ("Error: Problem creating symlink " + cmd)
return False
'''Permissions
The list items will be returned in the format, dir=owner:group mod; eg: 'apps=root:root 0444'
Parse each line accordingly.
'''
if pkg_manifest.has_section('permissions'):
for fpath in pkg_manifest.options('permissions'):
perm_opt= pkg_manifest.get('permissions',fpath)
chown_opt,chmod_opt = perm_opt.split(' ')
#if not re.match("^\/", fpath): fpath = deploy_dir + "/" + fpath
cmd="chown -R "+chown_opt+" "+fpath+';chmod -R '+chmod_opt+' '+fpath
if not execOSCommand(cmd):
loge ("Error: Problem setting permissions on " + fpath+'. Command: '+cmd)
return False
''' Register the installation and the uninstall package'''
if not deploy_inst.uninstall:
self.registerInstall(deploy_inst, os.path.join(deploy_inst.stage_dir, undo_pkg.tarball_name))
'''delete the stage_dir upon successful installation of the package'''
os.chdir("/tmp") # a workaround to avoid system warning when curr dir stage_dir is deleted.
print ("deleting " + stage_dir)
if not execOSCommand('rm -r ' + stage_dir):
print ("Warning: Couldn't delete " + stage_dir)
if not deploy_inst.uninstall:
print ("Info: Package "+self.name+" has been installed")
else:
print ("Info: Package has been uninstalled.")
return True
def isInstalled(self,tarball_path):
if not self.getMeta()['latest_install']:
return False
md5_local = self.install_meta['latest_install']['pkg_md5']
return (getFileMD5(tarball_path) == md5_local)
'''Class to process the main opkg actions'''
class opkg():
ACTIONS=['create','list','install']
'''action specific required configs'''
ACTION_CONFIGS={
'install': ['install_root'],
'list':['install_root'],
}
OPKG_LABEL='vector-pkg'
OPKG_VERSION='0.1.0'
def __init__(self,params):
self.arg_dict=dict()
self.arg_dict['opkg_cmd']=params[0]
self.action=None #This will be available in the env as OPKG_ACTION
self.extra_vars=dict()
self.conf_file=None
self.configs=dict()
self.pkgs=None
self.opkg_dir=None
if len(params) < 2:
self.printHelp()
Exit(0)
'''action is positional'''
self.action=params[1]
'''The args can be in these formats: argx,--opt_x,--opt_y=opt_val'''
for argx in params[1:]:
if re.match("^--", argx) is not None:
m = re.split('--', argx)
n = re.match("^(.+?)=(.+)", m[1])
if n is not None:
self.arg_dict[n.group(1)] = n.group(2)
else:
self.arg_dict[m[1]] = ''
else:
self.arg_dict[argx] = ''
'''Set extra-vars dict'''
if 'extra-vars' in self.arg_dict:
extra_vars = re.split(EXTRA_PARAM_DELIM,self.arg_dict['extra-vars'])
for extra_var in extra_vars:
k, v = re.split(EXTRA_PARAM_KEY_VAL_SEP,extra_var)
self.extra_vars[k] = v
if 'help' in self.arg_dict:
self.printHelp()
Exit(0)
elif 'version' in self.arg_dict:
self.printVersion()
Exit(0)
'''Check if config file is specified, if it exists load and initialize configs from it.
Note, the config items are grouped under sections in config file, but,
from command-line there is no option to qualify an item with section and so it should be unique across sections.
'''
opkg_conf_file=OPKG_CONF_FILE
if 'opkg_dir' in self.arg_dict: opkg_conf_file=self.arg_dict['opkg_dir']+'/conf/opkg.env'
self.conf_file=opkg_conf_file
self.loadConfigFile()
self.opkg_dir=self.configs['basic']['opkg_dir']
'''Override config items specified in config file with those from command-line'''
for section in self.configs:
for item in self.configs[section]:
if item in self.arg_dict: self.configs[section][item]=self.arg_dict[item]
'''Parse out common options such as pkg'''
if 'pkg' in self.arg_dict:
self.pkgs=re.split(',',self.arg_dict['pkg'])
return
'''Loads configs from opkg.env as a dictionary'''
def loadConfigFile(self):
self.configs['basic']={'opkg_dir': '/etc/vpkg','stage_dir':'/tmp/vpkg-staging',
'deploy_history_file':'/var/log/deploy_history.log',
'install_root': '/tmp/vpkg'};
Config = PkgConfigParser()
Config.read(self.conf_file)
sections=Config.sections()
for section in sections:
self.configs[section]=dict()
for item in Config.options(section):
self.configs[section][item]=Config.get(section,item)
return
def printVersion(self):
print (opkg.OPKG_LABEL + " v" + opkg.OPKG_VERSION)
return True
def printHelp(self):
self.printVersion()
script = os.path.basename(self.arg_dict['opkg_cmd'])
print ("Usages:")
print (script + " --version")
print (script + " --help")
print (script + " list [--pkg=pkg1,pkg2,...]")
print (script + " create --pkg=pkg1,pkg2,... [--release]")
print (script + " install --pkg=pkg1,pkg2[-REL_NUM|dev],... [--install_root=/path/to/install]")
print (script + " uninstall [--pkg=pkg1,pkg2,...]")
return True
'''Execute the action'''
def main(self):
if self.action=='create':
self.extra_vars['OPKG_ACTION'] = 'create'
for pkg in self.pkgs:
pkg_inst=Pkg(pkg)
pkg_inst.create()
elif self.action=='list':
self.extra_vars['OPKG_ACTION'] = 'list'
if None == self.pkgs:
path = os.path.join(self.configs['basic']['opkg_dir'], 'meta')
self.pkgs = []
if os.path.exists(path):
self.pkgs = os.listdir(path)
for pkg in self.pkgs:
pkg_name, pkg_name_rel_num, tarball_name = Pkg.parseName(pkg)
pkg_inst=Pkg(pkg_name)
pkg_inst.setEnvConfig(self.configs)
pkg_inst.loadMeta()
pkg_meta=pkg_inst.getMeta()
if not pkg_meta: continue
print (pkg_name+'-'+pkg_meta['latest_install']['pkg_rel_num'])
elif self.action=='install':
self.extra_vars['OPKG_ACTION'] = 'install'
deploy_inst=Deploy(self.configs,self.arg_dict,self.extra_vars)
if None == self.pkgs:
self.pkgs = []
loge("Error: no packages were given to install")
for pkg in self.pkgs:
pkg_name,pkg_name_rel_num,tarball_name =Pkg.parseName(pkg)
print("installing "+pkg_name)
'''Start installation of the package once the tarball is copied to staging location.'''
deploy_inst.installPackage(pkg_name,tarball_name,pkg_name_rel_num,os.path.join(os.getcwd(),pkg))
elif self.action=='uninstall':
self.extra_vars['OPKG_ACTION'] = 'uninstall'
self.arg_dict['uninstall']=''
deploy_inst=Deploy(self.configs,self.arg_dict,self.extra_vars)
if self.pkgs is None:
self.pkgs = []
for pkg in self.pkgs:
# First, look up the package to undo it
pkg_name,pkg_name_rel_num,tarball_name =Pkg.parseName(pkg)
pkg_inst=Pkg(pkg_name)
pkg_inst.setEnvConfig(self.configs)
pkg_inst.loadMeta()
pkg_meta=pkg_inst.getMeta()
if not pkg_meta: continue
undo_package_name = pkg_meta['latest_install']['undo_package'];
pkg_name,pkg_name_rel_num,tarball_name =Pkg.parseName(undo_package_name)
'''Start uninstallation of the package.'''
deploy_inst.installPackage(pkg_name,os.path.join(pkg_meta['dir'],tarball_name),pkg_name_rel_num,os.path.join(pkg_meta['dir'],undo_package_name))
# finally nuke the old folder
rmtree(pkg_meta['dir'])
else:
print ("Unsupported action: "+self.action)
'''Class for installation specific methods'''
class Deploy():
def __init__(self,env_conf,deploy_options,extra_vars=None):
self.deploy_ts=str(int(time.time()))
self.env_conf=env_conf
self.install_root=self.env_conf['basic']['install_root']
self.deploy_root=self.install_root+'/installs/'+self.deploy_ts
self.opkg_dir=self.env_conf['basic']['opkg_dir']
self.stage_dir=self.env_conf['basic']['stage_dir']
self.history_dir=os.path.join(self.opkg_dir,'history')
self.extra_vars=extra_vars
self.deploy_force=False
self.uninstall=False
if 'force' in deploy_options: self.deploy_force=True
if 'uninstall' in deploy_options:
self.deploy_force=True
self.uninstall=True
if not self.extra_vars: self.extra_vars=dict()
self.extra_vars['OPKG_NAME'] = None
self.extra_vars['OPKG_REL_NUM'] = None
self.extra_vars['OPKG_TS'] = None
self.extra_vars['OPKG_ACTION'] = None
try:
makedirs(self.history_dir)
except Exception as err:
loge("Error: could not create history directory "+str(err))
return
#if not execOSCommand('mkdir -p ' + self.history_dir): return
'''Returns the extra-vars specified from commandline and the OPKG_ vars'''
def getVars(self):
return self.extra_vars
def logHistory(self,log_entry):
history_log=self.deploy_ts+": "+log_entry
history_file=self.env_conf['basic']['deploy_history_file']
with open(os.path.join(self.history_dir, history_file), "a+") as hf: hf.write(history_log+"\n")
return True
'''The tarball is downloaded/copied to download_dir'''
def installPackage(self,pkg_name,tarball_name,pkg_name_rel_num,tarball_path):
rel_num,rel_ts=Pkg.parseTarballName(tarball_name)
self.extra_vars['OPKG_NAME'] = pkg_name
self.extra_vars['OPKG_REL_NUM'] = rel_num
self.extra_vars['OPKG_TS'] = rel_ts
pkg=Pkg(pkg_name)
pkg.setRelNum(rel_num)
pkg.setRelTs(rel_ts)
pkg.setEnvConfig(self.env_conf)
pkg.loadMeta()
if self.deploy_force or not pkg.isInstalled(tarball_path):
if not pkg.install(tarball_path,self,pkg_name):
loge ("Error installing")
else:
print ("Info: This revision of package "+pkg_name+" is already installed at "+self.install_root+'/installs/'+pkg.getMeta()['latest_install']['deploy_ts']+'/'+pkg_name)
print ("Info: Use --force option to override.")
return True
'''Utility classes '''
'''Utility class to do template related tasks'''
class Tmpl():
TMPL_KEY_VAL_DELIM=':'
def __init__(self,tmpl_path):
self.tmpl_path=tmpl_path
self.is_dir=False
if not os.path.exists(tmpl_path):
loge ("Error: " + tmpl_path + " doesn't exist.")
return
if os.path.isdir(tmpl_path): self.is_dir = True
'''Recreates files under tmpl_path with values from vars_dict
The template vars are searched for using pattern {{ var }}
tmpl_path could be single file or a directory,
in the latter case all files in the dir will be checked for recursively.
'''
def resolveVars(self,vars_dict,backup=False):
if self.is_dir:
files=os.listdir(self.tmpl_path)
for f in files:
ftmpl=Tmpl(self.tmpl_path+'/'+f)
ftmpl.resolveVars(vars_dict,backup)
else:
if not self.resolveVarsFile(self.tmpl_path,vars_dict,backup):
loge ("Error: Failed to resolve template "+self.tmpl_path)
return False
return True
'''Recreates files under tmpl_path with values from vars_list
vars_list contains a search/replace pair SEARCH-STR:REPLACE-STR, the file is updated by replacing all SEARCH-STR with REPLACE-STR
tmpl_path could be single file or a directory,
in the latter case all files in the dir will be checked for recursively.
'''
def replaceTokens (self,tokens_list,backup=False):
if self.is_dir:
files = os.listdir(self.tmpl_path)
for f in files:
fpath = os.path.join(self.tmpl_path, f)
ftmpl = Tmpl(fpath)
ftmpl.replaceTokens(tokens_list,backup)
else:
if not self.replaceTokensFile(self.tmpl_path,tokens_list, backup):
loge ("Error: Failed to resolve template " + self.tmpl_path)
return False
return True
def resolveVarsFile(self,file_path, vars_dict, backup=False):
str = loadFile(file_path)
for var in vars_dict:
if var not in vars_dict or not vars_dict[var]: continue
str = re.sub('{{ ' + var + ' }}', vars_dict[var], str)
if backup:
if not execOSCommand("mv " + file_path + " " + file_path + '.' + str(int(time.time()))):
loge ("Error: Couldn't backup " + file_path)
return False
try:
with open(file_path, "w") as f:
f.write(str)
except EnvironmentError:
loge ("Error: Cannot save updated " + file_path)
return False
return True
def replaceTokensFile(self,file_path, token, backup=False):
str = loadFile(file_path)
pattern, replace = re.split(Tmpl.TMPL_KEY_VAL_DELIM,token)
str = re.sub(pattern, replace, str)
if backup:
if not execOSCommand("mv " + file_path + " " + file_path + '.' + str(int(time.time()))):
loge ("Error: Couldn't backup " + file_path)
return False
try:
with open(file_path, "w") as f:
f.write(str)
except EnvironmentError:
loge ("Error: Cannot save updated " + file_path)
return False
return True
''' Utility Functions '''
'''returns the status code after executing cmd in the shell'''
def runCmd(cmd):
return subprocess.call(cmd,shell=True)
'''process return status from a command execution '''
def execOSCommand(cmd):
rc=runCmd(cmd)
if rc!=0:
loge ("Error executing "+cmd)
return False
return True
'''returns the file content as a string.'''
def loadFile(file_path):
s = open(file_path)
str = s.read()
s.close()
return str
def Exit(rc):
sys.exit(rc)
def getFileMD5(file_path):
return hashlib.md5(open(file_path, 'rb').read()).hexdigest()
''' main '''
opkg_cmd=opkg(sys.argv)
opkg_cmd.main()