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>
20 from string import Template
21 from opensteak.foreman_objects.item import ForemanItem
24 class ItemHost(ForemanItem):
27 Represent the content of a foreman hostgroup as a dict
33 def __init__(self, api, key, *args, **kwargs):
35 Represent the content of a foreman object as a dict
37 @param api: The foreman api
38 @param key: The object Key
39 @param *args, **kwargs: the dict representation
40 @return RETURN: Itself
42 ForemanItem.__init__(self, api, key,
43 self.objName, self.payloadObj,
45 self.update({'puppetclass_ids':
46 self.api.list('{}/{}/puppetclass_ids'
47 .format(self.objName, key))})
48 self.update({'param_ids':
49 list(self.api.list('{}/{}/parameters'
50 .format(self.objName, key),
56 """ Function getStatus
57 Get the status of an host
59 @return RETURN: The host status
61 return self.api.get('hosts', self.key, 'status')['status']
67 @return RETURN: The API result
69 return self.api.set('hosts', self.key,
70 {"power_action": "start"},
71 'power', async=self.async)
73 def getParamFromEnv(self, var, default=''):
74 """ Function getParamFromEnv
75 Search a parameter in the host environment
77 @param var: the var name
78 @param hostgroup: the hostgroup item linked to this host
79 @param default: default value
80 @return RETURN: the value
82 if self.getParam(var):
83 return self.getParam(var)
85 if self.hostgroup.getParam(var):
86 return self.hostgroup.getParam(var)
87 if self.domain.getParam('password'):
88 return self.domain.getParam('password')
98 tplFolder='templates_metadata/'):
99 """ Function getUserData
100 Generate a userdata script for metadata server from Foreman API
102 @param domain: the domain item linked to this host
103 @param hostgroup: the hostgroup item linked to this host
104 @param defaultPwd: the default password if no password is specified
105 in the host>hostgroup>domain params
106 @param defaultSshKey: the default ssh key if no password is specified
107 in the host>hostgroup>domain params
108 @param proxyHostname: hostname of the smartproxy
109 @param tplFolder: the templates folder
110 @return RETURN: the user data
112 if 'user-data' in self.keys():
113 return self['user-data']
115 self.hostgroup = hostgroup
117 if proxyHostname == '':
118 proxyHostname = 'foreman.' + domain
119 password = self.getParamFromEnv('password', defaultPwd)
120 sshauthkeys = self.getParamFromEnv('global_sshkey', defaultSshKey)
121 with open(tplFolder+'puppet.conf', 'rb') as puppet_file:
122 p = MyTemplate(puppet_file.read())
123 enc_puppet_file = base64.b64encode(p.substitute(
124 foremanHostname=proxyHostname))
125 with open(tplFolder+'cloud-init.tpl', 'r') as content_file:
126 s = MyTemplate(content_file.read())
128 sshauthkeys = ' - '+sshauthkeys
129 self.userdata = s.substitute(
132 sshauthkeys=sshauthkeys,
133 foremanurlbuilt="http://{}/unattended/built"
134 .format(proxyHostname),
135 puppet_conf_content=enc_puppet_file.decode('utf-8'))
139 class MyTemplate(Template):
141 idpattern = r'[a-z][_a-z0-9]*'