-4.7 C
New York
Monday, January 15, 2024

Mission Unattainable Code – Compact, Idempotent, DevOps Oriented, Multi-Distro Bundle Installer Script for Linux and Mac


Everybody loves Linux for its potential to stay to fundamentals and customary platform expectations. Besides these of us who do a whole lot of deployment automation.

Why? Two fundamental causes: In a world of stripped again distros (suppose containers), even basic Gnu coreutils and different fundamentals will be lacking. Which results in the second frustration – for some motive distro households thought it could be nice to not solely innovate bundle administration expertise but additionally change up the command set and in addition not present a common command set (like an api) to cover the variations.

So after bashing my head on this 1,000,000 instances, a little bit of bash code finally emerged (yeah from my head). And as you’ll learn, it was not a strategy of random likelihood and pure choice – however slightly it’s reverse – hyper-engineering.

As per Mission Unattainable Code Rules I’ve tried to make it “so simple as potential, however nonetheless have a really broad scope of reuse”

FYI – Home windows isn’t any higher – whereas Home windows PackageManagement (aka OneGet) and Chocolatey each tried to consolidate down to 1 command set for all bundle varieties for the platform, we now have a 3rd one.

TL;DR

I used to take time to broaden the part “Structure Heuristics” and focus on the enablement created by every of the bullets within the part.

I gained’t be doing that any longer for these causes:

  1. I’ve added “Advantages” and “Coding Selections” to every of the beneath to offer a synopsis of how the merchandise advantages the answer and what coding decisions it affected – which supplies a greater data map for quicker studying.
  2. By offering this studying part as mapped bullets – you’ll be able to incrementally be taught a number of factors by scanning and deep diving what pursuits you with out feeling obligated to plow via paragraphs of prose.
  3. It’s faster and simpler for me to doc with out having to generate formal copy – I’ll be extra tempted to share when the authoring course of is lighter.

Structure Heuristics: Necessities, Constraints, Desirements, Serendipities, Applicability, Limitations and Alternate options

This part seems in lots of Mission Unattainable Code samples as a result of it: 1) Demonstrates there may be an precise sample to the considering underlying this strategy, 2) It permits you to be taught the patterns however making the considering plain, 3) It reveals the advanced engineering of making simplicity.

The next listing demonstrates the Architectural thrust of the answer. This strategy is meant to be pure to simplicity of operation and upkeep, slightly than purity of a language or framework or growth methodology. It’s also meant to have the least potential dependencies. It’s an strategy I name “Mission Unattainable Coding” as a result of it allows the code to get it’s job completed it doesn’t matter what.

  • Requirement: (Happy) Adhere to “Adoption Pushed Growth for Tooling”
    • Advantages: everybody will use extra of your stuff, extra individuals will actually ’love’ your stuff, driving implementation determination towards “human cognitive” priorities over machine issues – as a result of all of us have a lot much less time than computing sources
    • Coding Selections: Adhere to Mission Unattainable Methods (aka your complete Structure Heuristics part)
  • Requirement: (Happy) Don’t depend on errors to do detections (passive detection)
    • Advantages: creating automation errors when formal exception checking must be supported
    • Coding Selections: choosing “command -v” to detect existence of a command
  • Requirement: (Happy) Don’t require conditions to work
    • Advantages: keep away from rooster and the egg drawback, works on minimalized containers photographs
    • Coding Selections: choosing “command -v” to detect existence of a command
  • Requirement: (Happy) Be compact.
    • Advantages: Permits “single script” implementations because the code just isn’t overly obnoxious on the high of a script. Many orchestration techniques can ship a single script to an endpoint, however sending information makes their situation rather more advanced – compact code can journey inside the only script.
    • Coding Selections: IFS + learn technique of parsing key worth pairs, some single line if statements
  • Requirement: (Happy) Guarantee parameters will be handed in from an unknown variety of father or mother ranges and unknown applied sciences or computing languages
    • Advantages: very broad reuse with no variations for a lot of ranges of enclosing orchestration.
    • Coding Selections: Work with a 1) listing that’s 2) constructed as a string (in order that it may be handed between all kinds of automation expertise).
  • Requirement: (Happy) Deal with a minimum of yum and apt-get.
  • Requirement: (Happy) Be idempotent by checking for the existence of a command earlier than making an attempt set up operations.
    • Advantages: all the advantages of idempotency that are too many to listing right here.
  • Requirement: (Happy) Work for eventualities the place the identify of the bundle is totally different than the identify of the command that’s wanted.
    • Advantages: broader reuse as a result of dealing with this frequent use case
    • Coding Selections: use key worth pairs for putting in packages to get particular instructions
  • Requirement: (Happy) Auto-detect the bundle supervisor.
  • Requirement: (Happy) Deal with the AWFUL drawback of brew faucets and casks (who constructed that? – can somebody spend a day and summary away this ineffective distinction proper within the brew code like I’ve completed right here 🙂 – yeah and disallow a bundle identifier to be each when you’re at it 😉 )
    • Advantages: broader reuse, declarative state strategy (simply “make it so”)

The Code

New variations of the code should not synced to the beneath article insert, please use the repository hyperlink beneath to get the most recent.

perform ifcmdmissing-instpkg () {



  if [[ -n "$(command -v brew)" ]] ; then
    
    PKGMGR='brew'
  elif [[ -n "$(command -v yum)" ]] ; then
    PKGMGR='yum'
  elif [[ -n "$(command -v apt-get)" ]] ; then
    PKGMGR='apt-get'
  fi
  for cmdpkg in $1 ; do
      IFS=':' learn -ra CMDPKGPAIR <<<"${cmdpkg}"
      CMDNAME=${CMDPKGPAIR[0]}
      PKGNAME=${CMDPKGPAIR[1]}
      echo "If command ${CMDNAME} just isn't on path, the bundle ${PKGNAME} shall be put in."
      if [[ -n "$(command -v ${CMDNAME})" ]]; then
          echo "  '${CMDNAME}' command already current"
      else
          echo "  Lacking command '${CMDNAME}'"
          echo "  Putting in bundle '${PKGNAME}' to resolve lacking command."
          if [[ $PKGMGR != 'brew' ]]; then
            if [[ $PKGMGR == 'apt-get' ]]; then $PKGMGR replace; fi;
            $PKGMGR set up -y ${PKGNAME}
          else
            
            if brew data ${PKGNAME} >/dev/null 2>&1; then
              brew set up ${PKGNAME} --force
            elif brew cask data ${PKGNAME} >/dev/null 2>&1; then
              brew cask set up ${PKGNAME} --force
            else
              echo "  '${PKGNAME}' not discovered as a formulae or cask"
            fi
          fi
      fi
  completed
}

ifcmdmissing-instpkg "jq:jq wget:wget curl:curl"

Examined On

  • MacOS
  • Ubuntu container
  • Amazon Linux 2 container

Supply Code For This Article

ifcmdmissing-instpkg.sh

Appendix: Mission Unattainable Sample Philosophy

Mission Unattainable Code samples are meant to be each 1) usable instantly for manufacturing use and a couple of) a high notch sample to make use of in your personal innovation. Extra particulars on this strategy are within the publish

Appendix: Constructed Simplicity (Conciseness) is The Tip of An Iceberg

Whereas epitomized by the Blaise Pascal quote “I’ve made this longer than regular as a result of I’ve not had the time to make it shorter.”, I discover that the method of making concise, easy observations and options is sophisticated and typically advanced.

Creating concise designs and options is a deep ardour of mine. Nevertheless, there’s a frustration that’s fast on the heals of making one thing that’s concise. In my line of labor, sooner or later, you often must justify your ultimate work. That’s the level at which the remainder of the iceberg of considering that backs the concise tip involves the fore. Often the response is that it could’t probably be that concerned or that you’re spinning up causes on the fly to easily bolster an thought or resolution that was arrived at haphazardly.

Why hassle addressing this sentiment? As a result of concise, mission unimaginable model, options can simply be criticized as missing sophistication of their implementation. However very similar to a Mark Twain quote – that ruddy exterior look belies a tough wrought steadiness of many interrelated tradeoffs to get to a easy resolution.

Stated one other method, options which can be earnestly designed for conciseness will be rewarded by a notion of being the other of what they’re – simplistic, backed with little or no thought.

This Mission Unattainable Coding collection exposes the submerged methodological icebergs beneath the waterline of the seen and concise options it makes an attempt to reach at.



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles