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
|
#
# Collective Knowledge (indexing through ElasticSearch)
#
# See CK LICENSE.txt for licensing details
# See CK COPYRIGHT.txt for copyright details
#
# Developer: Grigori Fursin
#
cfg={} # Will be updated by CK (meta description of this module)
work={} # Will be updated by CK (temporal data)
ck=None # Will be updated by CK (initialized CK kernel)
# Local settings
##############################################################################
# Initialize module
def init(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
return {'return':0}
##############################################################################
# turn indexing on
def on(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
o=i.get('out','')
i['status']='yes'
r=status(i)
if r['return']>0: return r
if o=='con':
ck.out('Indexing is on')
return {'return':0}
##############################################################################
# turn indexing off
def off(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
o=i.get('out','')
i['status']='no'
r=status(i)
if r['return']>0: return r
if o=='con':
ck.out('Indexing is off')
return {'return':0}
##############################################################################
# show indexing status
def show(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
o=i.get('out','')
i['status']=''
r=status(i)
if r['return']>0: return r
s=r['status']
if s=='yes': sx='on'
else: sx='off'
if o=='con':
ck.out('Indexing status: '+sx)
return {'return':0}
##############################################################################
# check indexing status
def status(i):
"""
Input: {
status - if 'yes', turn it on
if 'no', turn it off
if '', return status
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
status
}
"""
# Get current configuration
cfg={}
r=ck.access({'action':'load',
'repo_uoa':ck.cfg['repo_name_default'],
'module_uoa':ck.cfg['subdir_kernel'],
'data_uoa':ck.cfg['subdir_kernel_default']})
if r['return']==0:
cfg.update(r['dict'])
r=ck.access({'action':'load',
'repo_uoa':ck.cfg['repo_name_local'],
'module_uoa':ck.cfg['subdir_kernel'],
'data_uoa':ck.cfg['subdir_kernel_default']})
if r['return']>0:
if r['return']!=16: return r
ck.out('')
ck.out('We strongly suggest you to setup local repository first!')
return {'return':0}
cfg.update(r['dict'])
# Turn on indexing
st=i.get('status','')
s=cfg.get('use_indexing',ck.cfg.get('use_indexing',''))
if st!='':
cfg['use_indexing']=st
s=st
r=ck.access({'action':'update',
'repo_uoa':ck.cfg['repo_name_local'],
'module_uoa':ck.cfg['subdir_kernel'],
'data_uoa':ck.cfg['subdir_kernel_default'],
'dict':cfg,
'substitute':'yes',
'ignore_update':'yes'})
if r['return']>0: return r
return {'return':0, 'status':s}
##############################################################################
# check indexing status
def test(i):
"""
Input: {
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
o=i.get('out','')
r=ck.access_index_server({'request':'TEST', 'dict':{}})
if r['return']>0: return r
dd=r['dict']
status=dd.get('status',0)
if status!=200:
return {'return':1, 'error':'returned status is not 200'}
version=dd.get('version',{}).get('number','')
if o=='con':
ck.out('Indexing server is working. Version = '+version+'.')
return r
##############################################################################
# clean whole index
def clean(i):
"""
Input: {
(force) - if 'yes', force cleaning
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
o=i.get('out','')
to_delete=True
if o=='con' and i.get('force','')!='yes':
r=ck.inp({'text':'Are you sure to clean the whole index (y/N): '})
c=r['string'].lower()
if c!='y' and c!='yes': to_delete=False
if to_delete:
r=ck.access_index_server({'request':'DELETE', 'path':'/_all', 'dict':{}})
if r['return']>0: return r
dd=r['dict']
status=dd.get('status',0)
err=dd.get('error','')
if err!='': r={'return':1, 'error':err}
return r
|