All Classes Functions
nginx.py
1 ##
2 #
3 # Check the documentation in __init__.py for more information on the methods in
4 # this module.
5 #
6 
7 from vnfs_operations import VNFSOperations
8 import os
9 import logging
10 import getpass
11 import errno
12 import errors
13 ##
14 #
15 # special_files is a list of files specific to this VNF that requires special
16 # handling while reading. For example, if a read system call is issued for
17 # rx_bytes, then the VNF instance needs to be queried for the number of bytes it
18 # has received and that is returned as the read data.
19 #
20 # action_files contains a list of files that are used to represent different
21 # action on a VNF. For example, writing 'stop' to 'action' will stop a VNF
22 # instance.
23 #
24 
25 special_files = { 'rx_bytes':'rx_bytes',
26  'tx_bytes':'tx_bytes',
27  'pkt_drops':'pkt_drops',
28  'status':'status',
29  'vm.ip' : 'vm_ip',
30  'action': 'action'}
31 
32 logger = logging.getLogger(__name__)
33 
34 def full_path(root, partial_path):
35  if partial_path.startswith("/"):
36  partial_path = partial_path[1:]
37  return os.path.join(root, partial_path)
38 
39 def get_nf_config(vnfs_ops, full_nf_path):
40  # nf_path for calling vnfs_op function
41  tokens = full_nf_path.encode('ascii').split('/')
42  last_index_to_keep = tokens.index('nf-types') + 3
43  nf_path = "/".join(tokens[0:last_index_to_keep])
44 
45  # get info about nf
46  nf_instance_name, nf_type, host, nf_image_name = vnfs_ops.vnfs_get_instance_configuration(nf_path)
47  try:
48  nf_id = vnfs_ops._hypervisor.get_id(host, getpass.getuser(),
49  nf_instance_name)
51  nf_id = None
52  return {'nf_id':nf_id,
53  'nf_image_name':nf_image_name,
54  'nf_instance_name':nf_instance_name,
55  'host':host,
56  'username':getpass.getuser()
57  }
58 
59 
60 def _mkdir(root, path, mode):
61  vnfs_ops = VNFSOperations(root)
62  result = vnfs_ops.vnfs_create_vnf_instance(path, mode)
63  return result
64 
65 
66 def _getattr(root, path, fh=None):
67  vnfs_ops = VNFSOperations(root)
68  f_path = full_path(root, path)
69  st = os.lstat(f_path)
70  file_name = vnfs_ops.vnfs_get_file_name(f_path)
71  return_dictionary = dict()
72  return_dictionary['st_atime'] = st.st_atime
73  return_dictionary['st_ctime'] = st.st_ctime
74  return_dictionary['st_gid'] = st.st_gid
75  return_dictionary['st_mode'] = st.st_mode
76  return_dictionary['st_mtime'] = st.st_mtime
77  return_dictionary['st_nlink'] = st.st_nlink
78  return_dictionary['st_size'] = st.st_size
79  return_dictionary['st_uid'] = st.st_uid
80  if file_name in special_files:
81  return_dictionary['st_size'] = 1000
82  return return_dictionary
83 
84 
85 def _read(root, path, length, offset, fh):
86  f_path = full_path(root, path)
87  vnfs_ops = VNFSOperations(root)
88  file_name = vnfs_ops.vnfs_get_file_name(f_path)
89  nf_path = ''
90  ret_str = ''
91  if file_name in special_files and special_files[file_name]+'_read' in globals():
92 
93  try:
94  nf_config = get_nf_config(vnfs_ops, f_path)
95  # call the custom read function
96  logger.info('Reading ' + file_name + ' from ' +
97  nf_config['nf_instance_name'] + '@' + nf_config['host'])
98  ret_str = globals()[special_files[file_name]+'_read'](vnfs_ops._hypervisor,
99  nf_config)
100  except errors.HypervisorError, ex:
101  logger.debug('raised OSErro ' + str(ex.errno))
102  raise OSError(ex.errno, os.strerror(ex.errno))
103  logger.info('Successfully read ' + file_name +
104  ' from ' + nf_config['nf_instance_name'] + '@' + nf_config['host'])
105  if offset >= len(ret_str):
106  ret_str = ''
107  #if file_name == "rx_bytes":
108  # ret_str = vnfs_ops.vnfs_get_rx_bytes(nf_path)
109  # if offset >= len(ret_str):
110  # ret_str = ''
111  #elif file_name == 'tx_bytes':
112  # ret_str = vnfs_ops.vnfs_get_tx_bytes(nf_path)
113  # if offset >= len(ret_str):
114  # ret_str = ''
115  #elif file_name == 'pkt_drops':
116  # ret_str = vnfs_ops.vnfs_get_pkt_drops(nf_path)
117  # if offset >= len(ret_str):
118  # ret_str = ''
119  #elif file_name == 'status':
120  # ret_str = vnfs_ops.vnfs_get_status(nf_path)
121  # if offset >= len(ret_str):
122  # ret_str = ''
123  #elif file_name == 'vm.ip':
124  # ret_str = vnfs_ops.vnfs_get_ip(nf_path)
125  # logger.debug('vm.ip ' + ret_str)
126  # if offset >= len(ret_str):
127  # ret_str = ''
128  else:
129  os.lseek(fh, offset, os.SEEK_SET)
130  ret_str = os.read(fh, length)
131  return ret_str
132 
133 
134 def _write(root, path, buf, offset, fh):
135  f_path = full_path(root, path)
136  vnfs_ops = VNFSOperations(root)
137  file_name = vnfs_ops.vnfs_get_file_name(f_path)
138  #if file_name == "action":
139  if file_name in special_files and special_files[file_name]+'_write' in globals():
140  try:
141  nf_config = get_nf_config(vnfs_ops, f_path)
142  # call the custom write function
143  logger.info('Writing to ' + file_name + ' in ' +
144  nf_config['nf_instance_name'] + '@' + nf_config['host'])
145  ret_str = globals()[special_files[file_name]+'_write'](vnfs_ops._hypervisor,
146  nf_config, buf.rstrip("\n"))
147  except errors.HypervisorError, ex:
148  logger.debug('raised OSErro ' + str(ex.errno))
149  raise OSError(ex.errno, os.strerror(ex.errno))
150  logger.info('Successfully wrote ' + file_name +
151  ' in ' + nf_config['nf_instance_name'] + '@' + nf_config['host'])
152 
153  #if buf.rstrip("\n") == "activate":
154  # try:
155  # vnfs_ops.vnfs_deploy_nf(nf_path)
156  # except errors.VNFCreateError:
157  # #raise OSError(errno.EBUSY, os.strerror(errno.EBUSY))
158  # raise OSError(747, 'Cannot create VNF')
159  #elif buf.rstrip("\n") == "stop":
160  # vnfs_ops.vnfs_stop_vnf(nf_path)
161  #elif buf.rstrip("\n") == "start":
162  # vnfs_ops.vnfs_start_vnf(nf_path)
163  #elif buf.rstrip("\n") == "destroy":
164  # vnfs_ops.vnfs_destroy_vnf(nf_path)
165  os.lseek(fh, offset, os.SEEK_SET)
166  os.write(fh, buf.rstrip("\n"))
167  return len(buf)
168  else:
169  os.lseek(fh, offset, os.SEEK_SET)
170  return os.write(fh, buf)
171 
172 def rx_bytes_read(hypervisor_driver, nf_config):
173  command = "ifconfig eth0 | grep -Eo 'RX bytes:[0-9]+' | cut -d':' -f 2"
174  return hypervisor_driver.execute_in_guest(nf_config['host'],
175  nf_config['nf_id'], command)
176 
177 def tx_bytes_read(hypervisor_driver, nf_config):
178  command = "ifconfig eth0 | grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2"
179  return hypervisor_driver.execute_in_guest(nf_config['host'],
180  nf_config['nf_id'], command)
181 
182 def pkt_drops_read(hypervisor_driver, nf_config):
183  command = "ifconfig eth0 | grep -Eo 'RX .* dropped:[0-9]+' | cut -d':' -f 4"
184  return hypervisor_driver.execute_in_guest(nf_config['host'],
185  nf_config['nf_id'], command)
186 
187 """
188 nginx specific
189 
190 def _start(hypervisor_driver, nf_config):
191  command = "/user/bin nginx"
192  return hypervisor_driver.execute_in_guest(nf_config['host'],
193  nf_config['nf_id'], command)
194 
195 
196 def _nginx_signal(hypervisor_driver, nf_config, signal):
197  if signal not in ["stop", "quit", "reload", 'reopen']:
198  logger.info("invalid signal")
199  else:
200  command = "nginx -s %s" % signal
201  return hypervisor_driver.execute_in_guest(nf_config['host'],
202  nf_config['nf_id'], command)
203 
204 
205 """
206 
207 
208 def status_read(hypervisor_driver, nf_config):
209  return hypervisor_driver.guest_status(nf_config['host'],
210  nf_config['nf_id'])
211 
212 def vm_ip_read(hypervisor_driver, nf_config):
213  return hypervisor_driver.get_ip(nf_config['host'],
214  nf_config['nf_id'])
215 
216 def action_write(hypervisor_driver, nf_config, data):
217  if data == "activate":
218  logger.info('Deploying new VNF instance ' + nf_config['nf_instance_name'] +
219  ' @ ' + nf_config['host'])
220  nf_id = hypervisor_driver.deploy(nf_config['host'], nf_config['username'],
221  nf_config['nf_image_name'], nf_config['nf_instance_name'])
222  logger.info('VNF deployed, now starting VNF instance ' +
223  nf_config['nf_instance_name'] + ' @ ' + nf_config['host'])
224  try:
225  hypervisor_driver.start(nf_config['host'], nf_id)
226  logger.info('Successfully started VNF instance ' +
227  nf_config['nf_instance_name'] + ' @ ' + nf_config['host'])
228  except errors.VNFStartError:
229  logger.error('Attempt to start ' + nf_config['nf_instance_name'] +
230  '@' + nf_config['host'] + ' failed. Destroying depoyed VNF.')
231  try:
232  hypervisor_driver.destroy(nf_config['host'], nf_id)
233  logger.info('Successfully destroyed ' +
234  nf_config['nf_instance_name'] + '@' + nf_config['host'])
235  # the VNF was deployed, but failed to start so...
236  raise errors.VNFDeploymentError
237  except errors.VNFDestroyError:
238  logger.error('Failed to destroy partially activated VNF instance ' +
239  nf_config['nf_instance_name'] + '@' + nf_config['host'] +
240  '. VNF is in inconsistent state.')
242  elif data == "stop":
243  logger.info('Stopping VNF instance ' + nf_config['nf_instance_name'] +
244  '@' + nf_config['host'])
245  hypervisor_driver.stop(nf_config['host'], nf_config['nf_id'])
246  logger.info(nf_config['nf_instance_name'] + '@' + nf_config['host'] +
247  ' successfully stopped')
248  elif data == "start":
249  logger.info('Starting VNF instance ' + nf_config['nf_instance_name'] +
250  '@' + nf_config['host'])
251  hypervisor_driver.start(nf_config['host'], nf_config['nf_id'])
252  logger.info(nf_config['nf_instance_name'] + '@' + nf_config['host'] +
253  ' successfully started')
254  elif data == "destroy":
255  logger.info('Destroying VNF instance ' + nf_config['nf_instance_name'] +
256  '@' + nf_config['host'])
257  hypervisor_driver.destroy(nf_config['host'], nf_config['nf_id'])
258  logger.info(nf_config['nf_instance_name'] + '@' + nf_config['host'] +
259  ' successfully destroyed')
260 
261 
262