Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / foreman_objects / hosts.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15 # Authors:
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
18
19 from opensteak.foreman_objects.objects import ForemanObjects
20 from opensteak.foreman_objects.itemHost import ItemHost
21 import time
22
23
24 class Hosts(ForemanObjects):
25     """
26     Host sclass
27     """
28     objName = 'hosts'
29     payloadObj = 'host'
30
31     def list(self):
32         """ Function list
33         list the hosts
34
35         @return RETURN: List of ItemHost objects
36         """
37         return list(map(lambda x: ItemHost(self.api, x['id'], x),
38                         self.api.list(self.objName)))
39
40     def __getitem__(self, key):
41         """ Function __getitem__
42         Get an host
43
44         @param key: The host name or ID
45         @return RETURN: The ItemHost object of an host
46         """
47         return ItemHost(self.api, key, self.api.get(self.objName, key))
48
49     def __printProgression__(self, status, msg, eol):
50         """ Function __printProgression__
51         Print the creation progression or not
52         It uses the foreman.printer lib
53
54         @param status: Status of the message
55         @param msg: Message
56         @param eol: End Of Line (to get a new line or not)
57         @return RETURN: None
58         """
59         if self.printHostProgress:
60             self.__printProgression__(status, msg, eol=eol)
61
62     def createVM(self, key, attributes, printHostProgress=False):
63         """ Function createVM
64         Create a Virtual Machine
65
66         The creation of a VM with libVirt is a bit complexe.
67         We first create the element in foreman, the ask to start before
68         the result of the creation.
69         To do so, we make async calls to the API and check the results
70
71         @param key: The host name or ID
72         @param attributes:The payload of the host creation
73         @param printHostProgress: The link to opensteak.printerlib
74                                 to print or not the
75                                 progression of the host creation
76         @return RETURN: The API result
77         """
78
79         self.printHostProgress = printHostProgress
80         self.async = True
81         # Create the VM in foreman
82         self.__printProgression__('In progress',
83                                   key + ' creation: push in Foreman', eol='\r')
84         future1 = self.api.create('hosts', attributes, async=True)
85
86         #  Wait before asking to power on the VM
87         sleep = 5
88         for i in range(0, sleep):
89             time.sleep(1)
90             self.__printProgression__('In progress',
91                                       key + ' creation: start in {0}s'
92                                       .format(sleep - i),
93                                       eol='\r')
94
95         #  Power on the VM
96         self.__printProgression__('In progress',
97                                   key + ' creation: starting', eol='\r')
98         future2 = self[key].powerOn()
99
100         #  Show Power on result
101         if future2.result().status_code is 200:
102             self.__printProgression__('In progress',
103                                       key + ' creation: wait for end of boot',
104                                       eol='\r')
105         else:
106             self.__printProgression__(False,
107                                       key + ' creation: Error',
108                                       failed=str(future2.result().status_code))
109             return False
110         #  Show creation result
111         if future1.result().status_code is 200:
112             self.__printProgression__('In progress',
113                                       key + ' creation: created',
114                                       eol='\r')
115         else:
116             self.__printProgression__(False,
117                                       key + ' creation: Error',
118                                       failed=str(future1.result().status_code))
119             return False
120
121         # Wait for puppet catalog to be applied
122         loop_stop = False
123         while not loop_stop:
124             status = self[key].getStatus()
125             if status == 'No Changes' or status == 'Active':
126                 self.__printProgression__(True,
127                                           key + ' creation: provisioning OK')
128                 loop_stop = True
129             elif status == 'Error':
130                 self.__printProgression__(False,
131                                           key + ' creation: Error',
132                                           failed="Error during provisioning")
133                 loop_stop = True
134                 return False
135             else:
136                 self.__printProgression__('In progress',
137                                           key + ' creation: provisioning ({})'
138                                           .format(status),
139                                           eol='\r')
140             time.sleep(5)
141
142         return True