~ubuntu-branches/debian/stretch/waagent/stretch

« back to all changes in this revision

Viewing changes to azurelinuxagent/distro/redhat/osutil.py

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2016-08-24 16:48:22 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20160824164822-vdf8m5xy5gycm1cz
Tags: 2.1.6-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright 2014 Microsoft Corporation
3
 
#
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
7
 
#
8
 
#     http://www.apache.org/licenses/LICENSE-2.0
9
 
#
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.
15
 
#
16
 
# Requires Python 2.4+ and Openssl 1.0+
17
 
#
18
 
 
19
 
import os
20
 
import re
21
 
import pwd
22
 
import shutil
23
 
import socket
24
 
import array
25
 
import struct
26
 
import fcntl
27
 
import time
28
 
import base64
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
38
 
 
39
 
class Redhat6xOSUtil(DefaultOSUtil):
40
 
    def __init__(self):
41
 
        super(Redhat6xOSUtil, self).__init__()
42
 
 
43
 
    def start_network(self):
44
 
        return shellutil.run("/sbin/service networking start", chk_err=False)
45
 
 
46
 
    def restart_ssh_service(self):
47
 
        return shellutil.run("/sbin/service sshd condrestart", chk_err=False)
48
 
 
49
 
    def stop_agent_service(self):
50
 
        return shellutil.run("/sbin/service waagent stop", chk_err=False)
51
 
 
52
 
    def start_agent_service(self):
53
 
        return shellutil.run("/sbin/service waagent start", chk_err=False)
54
 
 
55
 
    def register_agent_service(self):
56
 
        return shellutil.run("chkconfig --add waagent", chk_err=False)
57
 
 
58
 
    def unregister_agent_service(self):
59
 
        return shellutil.run("chkconfig --del waagent", chk_err=False)
60
 
    
61
 
    def openssl_to_openssh(self, input_file, output_file):
62
 
        pubkey = fileutil.read_file(input_file)
63
 
        try:
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)
69
 
 
70
 
    #Override
71
 
    def get_dhcp_pid(self):
72
 
        ret= shellutil.run_get_output("pidof dhclient")
73
 
        return ret[1] if ret[0] == 0 else None
74
 
 
75
 
    def set_hostname(self, hostname):
76
 
        """
77
 
        Set /etc/sysconfig/network
78
 
        """
79
 
        fileutil.update_conf_file('/etc/sysconfig/network',
80
 
                                  'HOSTNAME',
81
 
                                  'HOSTNAME={0}'.format(hostname))
82
 
        shellutil.run("hostname {0}".format(hostname), chk_err=False)
83
 
 
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))
89
 
 
90
 
class RedhatOSUtil(Redhat6xOSUtil):
91
 
    def __init__(self):
92
 
        super(RedhatOSUtil, self).__init__()
93
 
 
94
 
    def set_hostname(self, hostname):
95
 
        """
96
 
        Set /etc/hostname
97
 
        Unlike redhat 6.x, redhat 7.x will set hostname to /etc/hostname
98
 
        """
99
 
        DefaultOSUtil.set_hostname(self, hostname)
100
 
 
101
 
    def publish_hostname(self, hostname):
102
 
        """
103
 
        Restart NetworkManager first before publishing hostname
104
 
        """
105
 
        shellutil.run("service NetworkManager restart")
106
 
        super(RedhatOSUtil, self).publish_hostname(hostname)
107
 
 
108
 
    def register_agent_service(self):
109
 
        return shellutil.run("systemctl enable waagent", chk_err=False)
110
 
 
111
 
    def unregister_agent_service(self):
112
 
        return shellutil.run("systemctl disable waagent", chk_err=False)
113
 
 
114
 
    def openssl_to_openssh(self, input_file, output_file):
115
 
        DefaultOSUtil.openssl_to_openssh(self, input_file, output_file)