#!/usr/bin/python
#
# Copyright 2015 Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires Python 2.4+


import os
import sys
import imp
import base64
import re
import json
import platform
import shutil
import time
import traceback
import datetime
import subprocess

from AbstractPatching import AbstractPatching
from Common import *
from CommandExecutor import *

class redhatPatching(AbstractPatching):
    def __init__(self, logger, distro_info):
        super(redhatPatching, self).__init__(distro_info)
        self.logger = logger
        self.command_executor = CommandExecutor(logger)
        self.distro_info = distro_info
        if distro_info[1].startswith("6."):
            self.base64_path = '/usr/bin/base64'
            self.bash_path = '/bin/bash'
            self.blkid_path = '/sbin/blkid'
            self.cat_path = '/bin/cat'
            self.cryptsetup_path = '/sbin/cryptsetup'
            self.dd_path = '/bin/dd'
            self.e2fsck_path = '/sbin/e2fsck'
            self.echo_path = '/bin/echo'
            self.getenforce_path = '/usr/sbin/getenforce'
            self.setenforce_path = '/usr/sbin/setenforce'
            self.lsblk_path = '/bin/lsblk' 
            self.lsscsi_path = '/usr/bin/lsscsi'
            self.mkdir_path = '/bin/mkdir'
            self.mount_path = '/bin/mount'
            self.openssl_path = '/usr/bin/openssl'
            self.resize2fs_path = '/sbin/resize2fs'
            self.touch_path = '/bin/touch'
            self.umount_path = '/bin/umount'
        else:
            self.base64_path = '/usr/bin/base64'
            self.bash_path = '/usr/bin/bash'
            self.blkid_path = '/usr/bin/blkid'
            self.cat_path = '/bin/cat'
            self.cryptsetup_path = '/usr/sbin/cryptsetup'
            self.dd_path = '/usr/bin/dd'
            self.e2fsck_path = '/sbin/e2fsck'
            self.echo_path = '/usr/bin/echo'
            self.getenforce_path = '/usr/sbin/getenforce'
            self.setenforce_path = '/usr/sbin/setenforce'
            self.lsblk_path = '/usr/bin/lsblk'
            self.lsscsi_path = '/usr/bin/lsscsi'
            self.mkdir_path = '/usr/bin/mkdir'
            self.mount_path = '/usr/bin/mount'
            self.openssl_path = '/usr/bin/openssl'
            self.resize2fs_path = '/sbin/resize2fs'
            self.touch_path = '/usr/bin/touch'
            self.umount_path = '/usr/bin/umount'

    def install_extras(self):
        if self.command_executor.Execute("rpm -q ntfs-3g python2-pip"):
            epel_cmd = ""
            if self.distro_info[1].startswith("6."):
                epel_cmd = "yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm"
            else:
                epel_cmd = "yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm"

            if self.command_executor.Execute("rpm -q epel-release"):
                self.command_executor.Execute(epel_cmd)

            self.command_executor.Execute("yum install -y ntfs-3g python2-pip")

        packages = ['cryptsetup',
                    'lsscsi',
                    'psmisc',
                    'cryptsetup-reencrypt',
                    'lvm2',
                    'uuid',
                    'at',
                    'patch',
                    'procps-ng',
                    'util-linux',
                    'gcc',
                    'libffi-devel',
                    'openssl-devel',
                    'python-devel']

        if self.command_executor.Execute("rpm -q " + " ".join(packages)):
            self.command_executor.Execute("yum install -y " + " ".join(packages))

        if self.command_executor.Execute("pip show adal"):
            self.command_executor.Execute("pip install adal")

@@ -2953,16 +2953,69 @@
 (self):%0A
+        if self.distro_info%5B1%5D.startswith(%226.%22):%0A    
         
@@ -3073,17 +3073,16 @@
 thon
-2
 -pip%22):%0A
     
@@ -3081,87 +3081,8 @@
 %22):%0A
-            epel_cmd = %22%22%0A            if self.distro_info%5B1%5D.startswith(%226.%22):%0A
     
@@ -3192,16 +3192,17 @@
 ch.rpm%22%0A
+%0A
         
@@ -3197,36 +3197,325 @@
 m%22%0A%0A            
-else
+    if self.command_executor.Execute(%22rpm -q epel-release%22):%0A                    self.command_executor.Execute(epel_cmd)%0A%0A                self.command_executor.Execute(%22yum install -y ntfs-3g python-pip%22)%0A        else:%0A            if self.command_executor.Execute(%22rpm -q ntfs-3g python2-pip%22)
 :%0A              
@@ -3620,32 +3620,36 @@
 m%22%0A%0A            
+    
 if self.command_
@@ -3697,32 +3697,36 @@
                 
+  
+  
 self.command_exe
@@ -3742,32 +3742,36 @@
 cute(epel_cmd)%0A%0A
+    
             self

Fix package name on RHEL 6.8