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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
19 from opensteak.foreman_objects.objects import ForemanObjects
20 from opensteak.foreman_objects.itemHost import ItemHost
24 class Hosts(ForemanObjects):
35 @return RETURN: List of ItemHost objects
37 return list(map(lambda x: ItemHost(self.api, x['id'], x),
38 self.api.list(self.objName)))
40 def __getitem__(self, key):
41 """ Function __getitem__
44 @param key: The host name or ID
45 @return RETURN: The ItemHost object of an host
47 return ItemHost(self.api, key, self.api.get(self.objName, key))
49 def __printProgression__(self, status, msg, eol):
50 """ Function __printProgression__
51 Print the creation progression or not
52 It uses the foreman.printer lib
54 @param status: Status of the message
56 @param eol: End Of Line (to get a new line or not)
59 if self.printHostProgress:
60 self.__printProgression__(status, msg, eol=eol)
62 def createVM(self, key, attributes, printHostProgress=False):
64 Create a Virtual Machine
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
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
75 progression of the host creation
76 @return RETURN: The API result
79 self.printHostProgress = printHostProgress
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)
86 # Wait before asking to power on the VM
88 for i in range(0, sleep):
90 self.__printProgression__('In progress',
91 key + ' creation: start in {0}s'
96 self.__printProgression__('In progress',
97 key + ' creation: starting', eol='\r')
98 future2 = self[key].powerOn()
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',
106 self.__printProgression__(False,
107 key + ' creation: Error',
108 failed=str(future2.result().status_code))
110 # Show creation result
111 if future1.result().status_code is 200:
112 self.__printProgression__('In progress',
113 key + ' creation: created',
116 self.__printProgression__(False,
117 key + ' creation: Error',
118 failed=str(future1.result().status_code))
121 # Wait for puppet catalog to be applied
124 status = self[key].getStatus()
125 if status == 'No Changes' or status == 'Active':
126 self.__printProgression__(True,
127 key + ' creation: provisioning OK')
129 elif status == 'Error':
130 self.__printProgression__(False,
131 key + ' creation: Error',
132 failed="Error during provisioning")
136 self.__printProgression__('In progress',
137 key + ' creation: provisioning ({})'