def cn_sls_pillar(target, ttl=api.RUNNER_CACHE_TIME, output=False): '''limited cloud pillar to expose to a compute node''' func_name = 'mc_cloud_lxc.cn_sls_pillar {0}'.format(target) __salt__['mc_api.time_log']('start {0}'.format(func_name)) def _do(target): pillar = {} imgSettings = cli('mc_cloud_images.settings') lxcSettings = cli('mc_cloud_lxc.settings') imgSettingsData = {} lxcSettingsData = {} for name, imageData in imgSettings['lxc']['images'].items(): imgSettingsData[name] = { 'lxc_tarball': imageData['lxc_tarball'], 'lxc_tarball_md5': imageData['lxc_tarball_md5'], 'lxc_tarball_name': imageData['lxc_tarball_name'], 'lxc_tarball_ver': imageData['lxc_tarball_ver']} for v in ['use_bridge', 'bridge', 'gateway', 'netmask_full', 'network', 'netmask']: lxcSettingsData[v] = lxcSettings['defaults'][v] # imgSettingsData = api.json_dump(imgSettingsData) # lxcSettingsData = api.json_dump(lxcSettingsData) pillar.update({'lxcSettings': lxcSettingsData, 'imgSettings': imgSettingsData}) return pillar cache_key = 'mc_cloud_lxc.cn_sls_pillar_{0}'.format(target) ret = memoize_cache(_do, [target], {}, cache_key, ttl) cret = result() cret['result'] = ret salt_output(cret, __opts__, output=output) __salt__['mc_api.time_log']('end {0}'.format(func_name)) return ret
def vm_settings_for(vm, ttl=60): ''' VM cloud related settings THIS IS USED ON THE COMPUTE NODE SIDE ! ''' def _do(vm): return vm_settings('_' + vm) cache_key = ('mc_cloud_vm.vm_settings_for' '{0}').format(vm) return memoize_cache(_do, [vm], {}, cache_key, ttl)
def vm_sls_pillar(compute_node, vm, ttl=api.RUNNER_CACHE_TIME): '''limited cloud pillar to expose to a vm This will be stored locally inside a local registry compute_node compute node to gather pillar from vm vm to gather pillar from ''' func_name = 'mc_cloud_vm.vm_sls_pillar {0} {1}'.format( compute_node, vm) __salt__['mc_api.time_log']('start {0}'.format(func_name)) def _do(compute_node, vm): compute_node = __salt__['mc_cloud_vm.get_compute_node'](vm, compute_node) cloudSettings = cli('mc_cloud.settings') cloudSettingsData = {} vmSettingsData = {} cnSettingsData = {} cloudSettingsData['all_sls_dir'] = cloudSettings['all_sls_dir'] cloudSettingsData[ 'compute_node_sls_dir'] = cloudSettings['compute_node_sls_dir'] cloudSettingsData['root'] = cloudSettings['root'] cloudSettingsData['prefix'] = cloudSettings['prefix'] cnsettings = cli('mc_cloud_compute_node.settings') targets = cnsettings.get('targets', {}) cnSettingsData['virt_types'] = targets.get( compute_node, {}).get('virt_types', []) vmSettingsData['vm_name'] = vm vt = targets.get(compute_node, {}).get('vms', {}).get(vm, None) vmSettingsData['vm_vt'] = vt # vmSettingsData = api.json_dump(vmSettingsData) # cloudSettingsData = api.json_dump(cloudSettingsData) # cnSettingsData = api.json_dump(cnSettingsData) pillar = {'cloudSettings': cloudSettingsData, 'mccloud_vmname': vm, 'mccloud_vm_ssh_port': cli( 'mc_cloud_compute_node.get_ssh_port', vm, target=compute_node), 'mccloud_targetname': compute_node, 'vmSettings': vmSettingsData, 'isdevhost': cli('mc_nodetypes.registry')['is']['devhost'], 'cnSettings': cnSettingsData} if vt in ['lxc']: vtVmData = cli( 'mc_cloud_{0}.get_settings_for_vm'.format(vt), compute_node, vm, full=False) pillar['vtVmData'] = vtVmData return pillar cache_key = 'mc_cloud_vm.vm_sls_pillar_{0}_{1}'.format( compute_node, vm) ret = memoize_cache(_do, [compute_node, vm], {}, cache_key, ttl) __salt__['mc_api.time_log']('end {0}'.format(func_name)) return ret
def cli(*args, **kwargs): '''Correctly forward salt globals to a regular python module ''' def _do(args, kwargs): if not kwargs: kwargs = {} kwargs.update({ 'salt_cfgdir': __opts__.get('config_dir', None), 'salt_cfg': __opts__.get('conf_file', None), 'salt___opts__': __opts__, }) return client(*args, **kwargs) fun_ = args[0] defaults_ttls = {} # 'mc_cloud_compute_node.target_for_vm': 60, # 'mc_cloud_compute_node.vt_for_vm': 60, # 'mc_cloud_compute_node.get_reverse_proxies_for_target': 60, # 'mc_cloud_compute_node.get_settings_for_target': 60, # 'mc_cloud_saltify.settings_for_target': 60, #} ttl = kwargs.pop('cache_ttl', defaults_ttls.get(fun_, api.RUNNER_CACHE_TIME)) cache_key = '' if ( fun_ in [ 'mc_cloud_compute_node.target_for_vm', 'mc_cloud_compute_node.vt_for_vm', 'mc_cloud_compute_node.get_reverse_proxies_for_target', 'mc_cloud_compute_node.get_settings_for_target', 'mc_cloud_saltify.settings_for_target', ] ) or ( fun_.endswith('.get_settings_for_vm') ): cache_key = ' '.join(['{0}'.format(a) for a in args]) if ( fun_ in [ 'mc_nodetypes.registry' ] ) or ( fun_.endswith('.settings') and len(args) == 1 and not kwargs ): cache_key = fun_ if cache_key: __salt__['mc_api.time_log'](cache_key, categ='CLOUD_CACHE') return memoize_cache(_do, [args, kwargs], {}, cache_key, ttl) else: return _do(args, kwargs)
def cn_settings(ttl=60): ''' compute node related settings THIS IS USED ON THE COMPUTE NODE SIDE ! ''' def _do(): reg = __salt__['mc_macros.get_local_registry']( 'cloud_compute_node_settings', registry_format='pack') if 'cnSettings' not in reg: raise ValueError( 'Registry not yet configured') return reg cache_key = 'mc_cloud_compute_node.cn_settings' return memoize_cache(_do, [], {}, cache_key, ttl)
def lazy_register_configuration(cn, ttl=5*60, *args, **kwargs): ''' Wrapper to register_configuration at the exception that only one shared call can de done in a five minutes row. This can be used as a decorator in orchestrations functions to ensure configuration has been dropped on target tenants. ''' cache_key = ('mc_cloud_compute_node.lazy_' 'register_configuration_{0}').format(cn) def _do(cn, *args, **kwargs): return __salt__['mc_cloud_compute_node.register_configuration']( cn, *args, **kwargs) ret = memoize_cache(_do, [cn] + list(args), kwargs, cache_key, ttl) return ret
def vm_settings(suf='', ttl=60): ''' VM cloud related settings THIS IS USED ON THE VM SIDE ! ''' def _do(suf): reg = __salt__['mc_macros.get_local_registry']( 'cloud_vm_settings{0}'.format(suf), registry_format='pack') if 'vmSettings' not in reg: raise ValueError( 'Registry not yet configured {0}'.format( suf)) return reg cache_key = 'mc_cloud_vm.vm_settings{0}'.format(suf) return memoize_cache(_do, [suf], {}, cache_key, ttl)
def lazy_register_configuration(vm, *args, **kwargs): ''' Wrapper to register_configuration at the exception that only one shared call can de done in a five minutes row. This can be used as a decorator in orchestrations functions to ensure configuration has been dropped on target tenants and is enoughtly up to date. ''' ttl = kwargs.get('ttl', 5 * 60) salt_target = kwargs.get('salt_target', '') cache_key = 'mc_cloud_vm.lazy_register_configuration_{0}_{1}'.format( vm, salt_target) def _do(vm, *args, **kwargs): return __salt__['mc_cloud_vm.register_configuration']( vm, *args, **kwargs) ret = memoize_cache(_do, [vm] + list(args), kwargs, cache_key, ttl) return ret
def cn_sls_pillar(target, ttl=api.RUNNER_CACHE_TIME): '''limited cloud pillar to expose to a compute node This will be stored locally inside a local registry''' func_name = 'mc_cloud_compute_node.cn_sls_pillar {0}'.format( target) __salt__['mc_api.time_log']('end {0}'.format(func_name)) def _do(target): cloudSettings = cli('mc_cloud.settings') cloudSettingsData = {} cnSettingsData = {} cnSettingsData['cn'] = cli( 'mc_cloud_compute_node.get_settings_for_target', target) vt_data = cnSettingsData['cn'].get('virt_types', {}) vts = [] for vt, enabled in vt_data.items(): if enabled: vts.append(vt) cnSettingsData['rp'] = cli( 'mc_cloud_compute_node.get_reverse_proxies_for_target', target) cloudSettingsData['all_sls_dir'] = cloudSettings['all_sls_dir'] cloudSettingsData[ 'compute_node_sls_dir'] = cloudSettings['compute_node_sls_dir'] cloudSettingsData['prefix'] = cloudSettings['prefix'] cloudSettingsData = cloudSettingsData cnSettingsData = cnSettingsData pillar = {'cloudSettings': cloudSettingsData, 'cnSettings': cnSettingsData} # add to the compute node pillar all the VT specific pillars for vt in vts: cid = 'mc_cloud_{0}.cn_sls_pillar'.format(vt) if cid in __salt__: pillar.update(__salt__[cid](target)) # add to the compute node pillar all the VM pillars return pillar cache_key = 'mc_cloud_compute_node.cn_sls_pillar_{0}'.format(target) ret = memoize_cache(_do, [target], {}, cache_key, ttl) __salt__['mc_api.time_log']('end {0}'.format(func_name)) return ret