A R Karthick | ea8bfce | 2016-10-13 16:32:07 -0700 | [diff] [blame] | 1 | import os,time |
| 2 | from CordContainer import Container |
| 3 | |
| 4 | class XosBase(object): |
| 5 | workspace = '/tmp/xos_scratch_workspace' |
| 6 | image = 'xosproject/xos' |
| 7 | tag = 'latest' |
| 8 | |
| 9 | @classmethod |
| 10 | def clone(cls, update = False): |
| 11 | fetch_cmd = 'mkdir -p {} && cd {} && \ |
| 12 | git clone http://gerrit.opencord.org/xos'.format(cls.workspace, cls.workspace) |
| 13 | fetch = True |
| 14 | if os.access(cls.workspace, os.F_OK): |
| 15 | fetch = update |
| 16 | if update is True: |
| 17 | os.system('rm -rf {}'.format(cls.workspace)) |
| 18 | if fetch is True: |
| 19 | ##fetch the xos |
| 20 | os.system(fetch_cmd) |
| 21 | |
| 22 | @classmethod |
| 23 | def build_images(cls): |
| 24 | images = ( ['xos', ('base', 'build',),], |
| 25 | ['postgresql', ('build',),], |
| 26 | ['synchronizer', ('build',),], |
| 27 | ['onboarding_synchronizer', ('build',),], |
| 28 | ['syndicate-ms', ('build',),], |
| 29 | ) |
| 30 | |
| 31 | for cnt, targets in images: |
| 32 | for target in targets: |
| 33 | xos_dir = 'cd {}/xos/containers/{} && make {}'.format(cls.workspace, cnt, target) |
| 34 | os.system(xos_dir) |
| 35 | |
| 36 | class XosServiceProfile(XosBase): |
| 37 | |
| 38 | def __init__(self, profile = 'cord-pod', update = False): |
| 39 | self.workspace = XosBase.workspace |
| 40 | self.profile = profile |
| 41 | self.service_dir = '{}/service-profile'.format(self.workspace) |
| 42 | self.profile_dir = '{}/{}'.format(self.service_dir, profile) |
| 43 | XosBase.clone(update = update) |
| 44 | self.__clone(update = update) |
| 45 | |
| 46 | def __clone(self, update = False): |
| 47 | fetch_cmd = 'cd {} && git clone http://gerrit.opencord.org/service-profile'.format(self.workspace) |
| 48 | fetch = True |
| 49 | if os.access(self.service_dir, os.F_OK): |
| 50 | fetch = update |
| 51 | if update is True: |
| 52 | os.system('rm -rf {}'.format(self.service_dir)) |
| 53 | if fetch: |
| 54 | os.system(fetch_cmd) |
| 55 | |
| 56 | def __ssh_key_check(self): |
| 57 | id_rsa = '{}/.ssh/id_rsa'.format(os.getenv('HOME')) |
| 58 | if not os.access(id_rsa, os.F_OK): |
| 59 | return False |
| 60 | return True |
| 61 | |
| 62 | def __ssh_copy_keys(self, dest): |
| 63 | cmd = 'cp -v {}/.ssh/id_rsa* {}'.format(os.getenv('HOME'), dest) |
| 64 | return os.system(cmd) |
| 65 | |
| 66 | def build_images(self, force = False): |
| 67 | if force is True or not Container.image_exists('{}:{}'.format(XosBase.image, XosBase.tag)): |
| 68 | XosBase.build_images() |
| 69 | |
| 70 | def start_services(self): |
| 71 | if not self.__ssh_key_check(): |
| 72 | log.info('SSH keys need to be generated before building XOS service containers') |
| 73 | log.info('Use the following commands to generate ssh keys') |
| 74 | log.info('ssh-keygen -t rsa -q -N ""') |
| 75 | log.info('ssh-copy-id -i $HOME/.ssh/id_rsa ubuntu@localhost') |
| 76 | return False |
| 77 | if not os.access(self.profile_dir, os.F_OK): |
| 78 | log.error('Profile directory %s does not exist' %self.profile_dir) |
| 79 | return False |
| 80 | self.build_images() |
| 81 | ##copy the keys to the profile dir |
| 82 | self.__ssh_copy_keys(self.profile_dir) |
| 83 | service_cmd = 'cd {} && make dirs download_services bootstrap onboarding'.format(self.profile_dir) |
| 84 | return os.system(service_cmd) |
| 85 | |
| 86 | def stop_services(self, rm = False): |
| 87 | if os.access(self.profile_dir, os.F_OK): |
| 88 | cmds = ['cd {}'.format(self.profile_dir), 'make stop'] |
| 89 | if rm is True: |
| 90 | cmds += ['make rm'] |
| 91 | cmd = ' && '.join(cmds) |
| 92 | return os.system(cmd) == 0 |
| 93 | return False |