3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 @author Varun Prasad(varun.prasad@riftio.com)
28 gi
.require_version('RwcalYang', '1.0')
29 gi
.require_version('RwLog', '1.0')
31 from gi
.repository
import RwcalYang
32 import rift
.rwcal
.cloudsim
.net
as net
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'
42 def __init__(self
, daemon_mode
=True, log_name
=__name__
, log_level
=logging
.DEBUG
):
45 daemon_mode (bool, optional): If set, then logs are pushed to the
47 log_name (str, optional): Logger name
48 log_level (<Log level>, optional): INFO, DEBUG ..
50 self
.logger
= logging
.getLogger(log_name
)
51 logging
.basicConfig(level
=log_level
, format
=self
.FORMAT
)
54 handler
= logging
.FileHandler(self
.LOG_FILE
)
55 handler
.setFormatter(logging
.Formatter(self
.FORMAT
))
56 self
.logger
.addHandler(handler
)
60 class CloudSimCalMixin(object):
61 """Mixin class to provide cal plugin and account access to classes.
65 self
._cal
, self
._account
= None, None
79 def load_plugin(self
):
80 """Load the cal plugin and account
85 plugin
= rw_peas
.PeasPlugin('rwcal_cloudsimproxy', 'RwCal-1.0')
86 engine
, info
, extension
= plugin()
88 rwloggerctx
= rwlogger
.RwLog
.Ctx
.new("Cal-Log")
89 cal
= plugin
.get_interface("Cloud")
90 rc
= cal
.init(rwloggerctx
)
92 account
= RwcalYang
.CloudAccount()
93 account
.account_type
= "cloudsim_proxy"
94 account
.cloudsim_proxy
.host
= "192.168.122.1"
96 self
._cal
, self
._account
= cal
, account
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.
104 def func_wrapper(*args
, **kwargs
):
105 logging
.debug("Checking if bridge exists")
107 if net
.bridge_exists('virbr0'):
108 logging
.debug("Bridge exists, can proceed with further operations.")
110 logging
.warning("No Bridge exists, trying to create one.")
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.")
118 net
.virsh_initialize_default()
120 return func(*args
, **kwargs
)