Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

############################################################################### 

# (c) Copyright 2016 CERN # 

# # 

# This software is distributed under the terms of the GNU General Public # 

# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # 

# # 

# In applying this licence, CERN does not waive the privileges and immunities # 

# granted to it by virtue of its status as an Intergovernmental Organization # 

# or submit itself to any jurisdiction. # 

############################################################################### 

''' 

CVMFS nightlies installer 

@author: Stefan-Gabriel CHITIC 

''' 

import logging 

import subprocess 

import os 

import hashlib 

 

from lbCVMFSTools.TaskHandlerInterface import TaskHandlerInterface 

 

FREQ = 10 * 60 

 

class GitMirrorTask(TaskHandlerInterface, object): 

 

def __init__(self, installOnCvmfs=True, 

repodir='/cvmfs/lhcbdev.cern.ch/git-mirrors', 

*args, **kwargs): 

if kwargs.get('FREQ', None): 

global FREQ 

FREQ = kwargs.get('FREQ') 

super(GitMirrorTask, self).__init__(FREQ, *args, **kwargs) 

self.log_prefix = '' 

if self.dry_run: 

self.log_prefix = 'IN DRY-RUN MODE: ' 

self.installOnCvmfs = installOnCvmfs 

self.repoDir = repodir 

logging.getLogger().setLevel(logging.INFO) 

 

def _md5(self, fname): 

hash_md5 = hashlib.md5() 

with open(fname, "rb") as f: 

for chunk in iter(lambda: f.read(4096), b""): 

hash_md5.update(chunk) 

return hash_md5.hexdigest() 

 

def get_list_of_tasks(self): 

allrepos = os.listdir(self.repoDir) 

todo = [] 

for r in allrepos: 

path = os.path.join(self.repoDir, r) 

path_head = os.path.join(path, 'FETCH_HEAD') 

md5 = self._md5(path_head) 

todo.append({ 

'path': path, 

'md5': md5 

}) 

return [todo] 

 

def perform_task(self, tasks): 

if not isinstance(tasks, list): 

tasks = [tasks] 

# BY default we consider all is there 

needPublish = False 

# Now installing and checking the difference 

for task in tasks: 

repo = task['path'] 

try: 

logging.info("%sUpdating git repo: %s" % (self.log_prefix, 

repo)) 

subprocess.call( 

["git", "--git-dir=%s" % repo, "remote", "update", 

"--prune"]) 

path_head = os.path.join(repo, 'FETCH_HEAD') 

md5 = self._md5(path_head) 

if md5 != task['md5']: 

logging.info("%sGit repo %s has changed" % (self.log_prefix, 

repo)) 

needPublish = True 

except Exception as e: 

logging.info( 

"%Git update problem. Ignoring to avoid transaction" 

"rollback" % self.log_prefix) 

pass 

 

if not needPublish and self.installOnCvmfs: 

logging.info( 

"%sNo new files to install - aborting transaction" % 

self.log_prefix) 

raise UserWarning("No new files, aborting transaction")