LaaS Base functionality
[pharos-tools.git] / laas-fog / source / deployment_manager.py
1 """
2 #############################################################################
3 #Copyright 2017 Parker Berberian and others                                 #
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
19 import logging
20 from api.libvirt_api import Libvirt
21
22
23 class Deployment_Manager:
24     """
25     This class manages the deployment of OPNFV on a booked host
26     if it was requested. If no OPNFV installer was requested, this class will
27     create the virtual machines and networks in the config files and exit.
28     """
29     def __init__(self, installerType, scenario, utility):
30         """
31         init function
32         """
33         # installerType will either be the constructor for an installer or None
34         self.installer = installerType
35         self.virt = Libvirt(
36                 utility.host,
37                 net_conf=utility.conf['hypervisor_config']['networks'],
38                 dom_conf=utility.conf['hypervisor_config']['vms']
39                 )
40         self.host = utility.host
41         self.util = utility
42
43     def getIso(self):
44         """
45         checks if any of the domains expect an ISO file to exist
46         and retrieves it.
47         """
48         isoDom = None
49         for dom in self.doms:
50             if dom.iso['used']:
51                 isoDom = dom
52                 break
53         if isoDom:
54             path = isoDom.iso['location']
55             url = isoDom.iso['URL']
56             self.util.sshExec(['wget', '-q', '-O', path, url])
57
58     def getDomMacs(self):
59         """
60         assigns the 'macs' instance variable to the domains
61         so that they know the mac addresses of their interfaces.
62         """
63         for dom in self.doms:
64             dom.macs = self.virt.getMacs(dom.name)
65
66     def makeDisks(self):
67         """
68         Creates the qcow2 disk files the domains expect on the remote host.
69         """
70         disks = []
71         for dom in self.doms:
72             disks.append(dom.disk)
73         self.util.execRemoteScript("mkDisks.sh", disks)
74
75     def go(self):
76         """
77         'main' function.
78         creates virtual machines/networks and either passes control to the
79         OPNFV installer, or finishes up if an installer was not requested.
80         """
81         log = logging.getLogger(self.util.hostname)
82         self.virt.setLogger(log)
83         log.info("%s", "Connecting to the host hypervisor")
84         self.virt.openConnection()
85         domains, networks = self.virt.go()
86         log.info("%s", "Created all networks and VM's on host")
87         self.doms = domains
88         self.nets = networks
89         if self.installer is None:
90             log.warning("%s", "No installer requested. Finishing deployment")
91             self.util.finishDeployment()
92             return
93         log.info("%s", "retrieving ISO")
94         self.getIso()
95         self.getDomMacs()
96         self.util.copyScripts()
97         self.makeDisks()
98         log.info("%s", "Beginning installation of OPNFV")
99         try:
100             installer = self.installer(
101                     self.doms,
102                     self.nets,
103                     self.virt,
104                     self.util
105                     )
106             installer.go()
107         except Exception:
108             log.exception('%s', "failed to install OPNFV")