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 Austin Cormier(austin.cormier@riftio.com)
20 @author Varun Prasad(varun.prasad@riftio.com)
31 import tornado
.httpserver
33 import tornado
.platform
.asyncio
36 gi
.require_version('RwcalYang', '1.0')
37 gi
.require_version('RwCal', '1.0')
38 gi
.require_version('RwLog', '1.0')
39 gi
.require_version('RwTypes', '1.0')
40 from gi
.repository
import (
47 import rift
.rwcal
.cloudsim
.net
48 import rift
.rwcal
.cloudsim
.lvm
as lvm
49 import rift
.rwcal
.cloudsim
.lxc
as lxc
50 import rift
.rwcal
.cloudsim
.shell
as shell
54 logger
= logging
.getLogger(__name__
)
56 if sys
.version_info
< (3, 4, 4):
57 asyncio
.ensure_future
= asyncio
.async
65 def verify_requirements(log
):
67 Check if all the requirements are met
68 1. bridgeutils should be installed
69 2. The user should be root
72 shell
.command('/usr/sbin/brctl show')
73 except shell
.ProcessError
:
74 log
.exception('/usr/sbin/brctl command not found, please install '
75 'bridge-utils (yum install bridge-utils)')
79 log
.error("User should be root to start the server.")
82 def __init__(self
, logging_level
=logging
.DEBUG
):
85 self
.log_hdl
= RwLog
.Ctx
.new("a")
87 self
.log
.setLevel(logging_level
)
89 def get_cal_interface(self
):
90 self
.log
.debug("Creating CAL interface.")
91 if CalServer
.cal_interface
is None:
92 plugin
= rw_peas
.PeasPlugin('rwcal_cloudsim', 'RwCal-1.0')
93 engine
, info
, extension
= plugin()
95 CalServer
.cal_interface
= plugin
.get_interface("Cloud")
96 CalServer
.cal_interface
.init(self
.log_hdl
)
98 return CalServer
.cal_interface
101 self
.log
.info("Cleaning up resources and backing store.")
102 for container
in lxc
.containers():
103 self
.log
.debug("Stopping {}".format(container
))
106 for container
in lxc
.containers():
107 lxc
.destroy(container
)
113 """Start the server."""
115 cal
= self
.get_cal_interface()
116 account
= RwcalYang
.CloudAccount(account_type
="cloudsim")
118 tornado
.platform
.asyncio
.AsyncIOMainLoop().install()
119 loop
= asyncio
.get_event_loop()
121 self
.app
= app
.CalProxyApp(self
.log
, loop
, cal
, account
)
122 self
.server
= tornado
.httpserver
.HTTPServer(self
.app
)
124 self
.log
.info("Starting Cal Proxy Http Server on port %s",
126 self
.server
.listen(CalServer
.HTTP_PORT
)
129 self
.log
.info("Creating a default network")
130 rift
.rwcal
.cloudsim
.net
.virsh_initialize_default()
131 self
.log
.info("Creating backing store")
134 loop
.add_signal_handler(signal
.SIGHUP
, self
.cleanup
)
135 loop
.add_signal_handler(signal
.SIGTERM
, self
.cleanup
)
138 loop
.run_in_executor(None, startup
)
140 except KeyboardInterrupt:
142 except Exception as exc
:
143 self
.log
.exception(exc
)
150 self
.log
.exception("Caught Exception in LP stop:", sys
.exc_info()[0])