2
# Copyright 2014 Microsoft Corporation
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
16
# Requires Python 2.4+ and Openssl 1.0+
29
import azurelinuxagent.conf as conf
30
import azurelinuxagent.logger as logger
31
from azurelinuxagent.future import ustr, bytebuffer
32
from azurelinuxagent.exception import OSUtilError, CryptError
33
import azurelinuxagent.utils.fileutil as fileutil
34
import azurelinuxagent.utils.shellutil as shellutil
35
import azurelinuxagent.utils.textutil as textutil
36
from azurelinuxagent.utils.cryptutil import CryptUtil
37
from azurelinuxagent.distro.default.osutil import DefaultOSUtil
39
class Redhat6xOSUtil(DefaultOSUtil):
41
super(Redhat6xOSUtil, self).__init__()
43
def start_network(self):
44
return shellutil.run("/sbin/service networking start", chk_err=False)
46
def restart_ssh_service(self):
47
return shellutil.run("/sbin/service sshd condrestart", chk_err=False)
49
def stop_agent_service(self):
50
return shellutil.run("/sbin/service waagent stop", chk_err=False)
52
def start_agent_service(self):
53
return shellutil.run("/sbin/service waagent start", chk_err=False)
55
def register_agent_service(self):
56
return shellutil.run("chkconfig --add waagent", chk_err=False)
58
def unregister_agent_service(self):
59
return shellutil.run("chkconfig --del waagent", chk_err=False)
61
def openssl_to_openssh(self, input_file, output_file):
62
pubkey = fileutil.read_file(input_file)
64
cryptutil = CryptUtil(conf.get_openssl_cmd())
65
ssh_rsa_pubkey = cryptutil.asn1_to_ssh(pubkey)
66
except CryptError as e:
67
raise OSUtilError(ustr(e))
68
fileutil.write_file(output_file, ssh_rsa_pubkey)
71
def get_dhcp_pid(self):
72
ret= shellutil.run_get_output("pidof dhclient")
73
return ret[1] if ret[0] == 0 else None
75
def set_hostname(self, hostname):
77
Set /etc/sysconfig/network
79
fileutil.update_conf_file('/etc/sysconfig/network',
81
'HOSTNAME={0}'.format(hostname))
82
shellutil.run("hostname {0}".format(hostname), chk_err=False)
84
def set_dhcp_hostname(self, hostname):
85
ifname = self.get_if_name()
86
filepath = "/etc/sysconfig/network-scripts/ifcfg-{0}".format(ifname)
87
fileutil.update_conf_file(filepath, 'DHCP_HOSTNAME',
88
'DHCP_HOSTNAME={0}'.format(hostname))
90
class RedhatOSUtil(Redhat6xOSUtil):
92
super(RedhatOSUtil, self).__init__()
94
def set_hostname(self, hostname):
97
Unlike redhat 6.x, redhat 7.x will set hostname to /etc/hostname
99
DefaultOSUtil.set_hostname(self, hostname)
101
def publish_hostname(self, hostname):
103
Restart NetworkManager first before publishing hostname
105
shellutil.run("service NetworkManager restart")
106
super(RedhatOSUtil, self).publish_hostname(hostname)
108
def register_agent_service(self):
109
return shellutil.run("systemctl enable waagent", chk_err=False)
111
def unregister_agent_service(self):
112
return shellutil.run("systemctl disable waagent", chk_err=False)
114
def openssl_to_openssh(self, input_file, output_file):
115
DefaultOSUtil.openssl_to_openssh(self, input_file, output_file)