github.com/flanksource/konfigadm@v0.12.0/pkg/phases/debian.go (about)

     1  package phases
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"gopkg.in/ini.v1"
    10  
    11  	"github.com/flanksource/konfigadm/pkg/types"
    12  	"github.com/flanksource/konfigadm/pkg/utils"
    13  )
    14  
    15  var (
    16  	Ubuntu = ubuntu{}
    17  	Debian = debian{}
    18  )
    19  
    20  type ubuntu struct {
    21  }
    22  
    23  func (u ubuntu) GetName() string {
    24  	return "ubuntu"
    25  }
    26  
    27  func (u ubuntu) GetPackageManager() types.PackageManager {
    28  	return AptPackageManager{}
    29  }
    30  
    31  func (u ubuntu) GetTags() []types.Flag {
    32  	osrelease, _ := ini.Load("/etc/os-release")
    33  	majorVersionID, _ := strconv.Atoi(strings.Split(osrelease.Section("").Key("VERSION_ID").String(), ".")[0])
    34  	if majorVersionID == 20 {
    35  		return []types.Flag{types.UBUNTU, types.UBUNTU20, types.DEBIAN_LIKE}
    36  	} else if majorVersionID == 18 {
    37  		return []types.Flag{types.UBUNTU, types.UBUNTU18, types.DEBIAN_LIKE}
    38  	} else if majorVersionID == 16 {
    39  		return []types.Flag{types.UBUNTU, types.UBUNTU16, types.DEBIAN_LIKE}
    40  	}
    41  	return []types.Flag{types.UBUNTU, types.DEBIAN_LIKE}
    42  }
    43  
    44  func (u ubuntu) DetectAtRuntime() bool {
    45  	return strings.Contains(utils.SafeRead("/etc/os-release"), "Ubuntu")
    46  }
    47  
    48  func (u ubuntu) GetVersionCodeName() string {
    49  	return utils.IniToMap("/etc/os-release")["VERSION_CODENAME"]
    50  }
    51  
    52  func (u ubuntu) GetKernelPackageNames(version string) (string, string) {
    53  	return AptPackageManager{}.GetKernelPackageNames(fmt.Sprintf("%s-generic", version))
    54  }
    55  
    56  func (u ubuntu) UpdateDefaultKernel(version string) types.Commands {
    57  	return GrubConfManager{}.UpdateDefaultKernel(fmt.Sprintf("Advanced options for Ubuntu>Ubuntu, with Linux %s-generic", version))
    58  }
    59  
    60  func (u ubuntu) ReadDefaultKernel() string {
    61  	readkernel, ok := GrubbyManager{}.ReadDefaultKernel()
    62  	if ok {
    63  		re := regexp.MustCompile("Advanced options for Ubuntu>Ubuntu, with Linux (.*?)-generic")
    64  		match := re.FindStringSubmatch(readkernel)
    65  		if len(match) > 1 {
    66  			return match[1]
    67  		}
    68  		return ""
    69  	}
    70  	return ""
    71  }
    72  
    73  type debian struct {
    74  }
    75  
    76  func (d debian) GetName() string {
    77  	return "debian"
    78  }
    79  
    80  func (d debian) GetPackageManager() types.PackageManager {
    81  	return AptPackageManager{}
    82  }
    83  
    84  func (d debian) GetTags() []types.Flag {
    85  	osrelease, err := ini.Load("/etc/os-release")
    86  	if err != nil {
    87  		return []types.Flag{}
    88  	}
    89  	majorVersionID, _ := strconv.Atoi(strings.Split(osrelease.Section("").Key("VERSION_ID").String(), ".")[0])
    90  	if majorVersionID == 9 {
    91  		return []types.Flag{types.DEBIAN, types.DEBIAN9, types.DEBIAN_LIKE}
    92  	} else if majorVersionID == 10 {
    93  		return []types.Flag{types.DEBIAN, types.DEBIAN10, types.DEBIAN_LIKE}
    94  	}
    95  	return []types.Flag{types.DEBIAN, types.DEBIAN_LIKE}
    96  }
    97  
    98  func (d debian) DetectAtRuntime() bool {
    99  	return strings.Contains(utils.SafeRead("/etc/os-release"), "Debian")
   100  }
   101  
   102  func (d debian) GetVersionCodeName() string {
   103  	return utils.IniToMap("/etc/os-release")["VERSION_CODENAME"]
   104  }
   105  
   106  func (d debian) GetKernelPackageNames(version string) (string, string) {
   107  	read, ok := utils.SafeExec("uname -p")
   108  	if !ok {
   109  		return AptPackageManager{}.GetKernelPackageNames(version)
   110  	}
   111  	arch := strings.TrimSuffix(read, "\n")
   112  	if arch == "unknown" {
   113  		arch = "amd64"
   114  	}
   115  	return AptPackageManager{}.GetKernelPackageNames(fmt.Sprintf("%s-%s", version, arch))
   116  }
   117  
   118  func (d debian) UpdateDefaultKernel(version string) types.Commands {
   119  	return GrubConfManager{}.UpdateDefaultKernel(fmt.Sprintf("Debian GNU/Linux, with Linux %s", version))
   120  }
   121  
   122  func (d debian) ReadDefaultKernel() string {
   123  	readkernel, ok := GrubbyManager{}.ReadDefaultKernel()
   124  	if ok {
   125  		re := regexp.MustCompile("Debian GNU/Linux, with Linux (.*?)")
   126  		match := re.FindStringSubmatch(readkernel)
   127  		if len(match) > 1 {
   128  			return match[1]
   129  		}
   130  		return ""
   131  	}
   132  	return ""
   133  }
   134  
   135  func (d debian) Cleanup() string {
   136  	return `
   137  	unset HISTFILE
   138  	find /var/log -type f | while read f; do echo -ne '' > "${f}"; done;
   139  	UBUNTU_VERSION=$(lsb_release -sr)
   140  if [[ ${UBUNTU_VERSION} == 16.04 ]] || [[ ${UBUNTU_VERSION} == 16.10 ]]; then
   141      # Modified version of
   142      # https://github.com/cbednarski/packer-ubuntu/blob/master/scripts-1604/vm_cleanup.sh#L9-L15
   143      # Instead of eth0 the interface is now called ens5 to mach the PCI
   144      # slot, so we need to change the networking scripts to enable the
   145      # correct interface.
   146      #
   147      # NOTE: After the machine is rebooted Packer will not be able to reconnect
   148      # (Vagrant will be able to) so make sure this is done in your final
   149      # provisioner.
   150      sed -i "s/ens3/ens5/g" /etc/network/interfaces
   151  fi
   152  	`
   153  }