| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | """ |
| 2 | # |
| 3 | # Copyright 2016 RIFT.IO Inc |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | @file utils.py |
| 19 | @author Varun Prasad(varun.prasad@riftio.com) |
| 20 | @date 2016-06-14 |
| 21 | """ |
| 22 | |
| 23 | import logging |
| 24 | import os |
| 25 | import sys |
| 26 | |
| 27 | import gi |
| 28 | gi.require_version('RwcalYang', '1.0') |
| 29 | gi.require_version('RwLog', '1.0') |
| 30 | |
| 31 | from gi.repository import RwcalYang |
| 32 | import rift.rwcal.cloudsim.net as net |
| 33 | import rwlogger |
| 34 | import rw_peas |
| 35 | |
| 36 | |
| 37 | class Logger(): |
| 38 | """A wrapper to hold all logging related configuration. """ |
| 39 | LOG_FILE = "/var/log/rift/cloudsim_server.log" |
| 40 | FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| 41 | |
| 42 | def __init__(self, daemon_mode=True, log_name=__name__, log_level=logging.DEBUG): |
| 43 | """ |
| 44 | Args: |
| 45 | daemon_mode (bool, optional): If set, then logs are pushed to the |
| 46 | file. |
| 47 | log_name (str, optional): Logger name |
| 48 | log_level (<Log level>, optional): INFO, DEBUG .. |
| 49 | """ |
| 50 | self.logger = logging.getLogger(log_name) |
| 51 | logging.basicConfig(level=log_level, format=self.FORMAT) |
| 52 | |
| 53 | if daemon_mode: |
| 54 | handler = logging.FileHandler(self.LOG_FILE) |
| 55 | handler.setFormatter(logging.Formatter(self.FORMAT)) |
| 56 | self.logger.addHandler(handler) |
| 57 | |
| 58 | |
| 59 | |
| 60 | class CloudSimCalMixin(object): |
| 61 | """Mixin class to provide cal plugin and account access to classes. |
| 62 | """ |
| 63 | |
| 64 | def __init__(self): |
| 65 | self._cal, self._account = None, None |
| 66 | |
| 67 | @property |
| 68 | def cal(self): |
| 69 | if not self._cal: |
| 70 | self.load_plugin() |
| 71 | return self._cal |
| 72 | |
| 73 | @property |
| 74 | def account(self): |
| 75 | if not self._account: |
| 76 | self.load_plugin() |
| 77 | return self._account |
| 78 | |
| 79 | def load_plugin(self): |
| 80 | """Load the cal plugin and account |
| 81 | |
| 82 | Returns: |
| 83 | Tuple (Cal, Account) |
| 84 | """ |
| 85 | plugin = rw_peas.PeasPlugin('rwcal_cloudsimproxy', 'RwCal-1.0') |
| 86 | engine, info, extension = plugin() |
| 87 | |
| 88 | rwloggerctx = rwlogger.RwLog.Ctx.new("Cal-Log") |
| 89 | cal = plugin.get_interface("Cloud") |
| 90 | rc = cal.init(rwloggerctx) |
| 91 | |
| 92 | account = RwcalYang.CloudAccount() |
| 93 | account.account_type = "cloudsim_proxy" |
| 94 | account.cloudsim_proxy.host = "192.168.122.1" |
| 95 | |
| 96 | self._cal, self._account = cal, account |
| 97 | |
| 98 | |
| 99 | def check_and_create_bridge(func): |
| 100 | """Decorator that checks if a bridge is available in the VM, if not checks |
| 101 | for permission and tries to create one. |
| 102 | """ |
| 103 | |
| 104 | def func_wrapper(*args, **kwargs): |
| 105 | logging.debug("Checking if bridge exists") |
| 106 | |
| 107 | if net.bridge_exists('virbr0'): |
| 108 | logging.debug("Bridge exists, can proceed with further operations.") |
| 109 | else: |
| 110 | logging.warning("No Bridge exists, trying to create one.") |
| 111 | |
| 112 | if os.geteuid() != 0: |
| 113 | logging.error("No bridge exists and cannot create one due to " |
| 114 | "insufficient privileges. Please create it manually using " |
| 115 | "'virsh net-start default' or re-run the same command as root.") |
| 116 | sys.exit(1) |
| 117 | |
| 118 | net.virsh_initialize_default() |
| 119 | |
| 120 | return func(*args, **kwargs) |
| 121 | |
| 122 | return func_wrapper |
| 123 | |