Skip to content

Instantly share code, notes, and snippets.

@drewr
Last active January 6, 2016 18:30
Show Gist options
  • Select an option

  • Save drewr/0e1d67c0eed8e4ad039d to your computer and use it in GitHub Desktop.

Select an option

Save drewr/0e1d67c0eed8e4ad039d to your computer and use it in GitHub Desktop.
Normalize some paths until we fix the AMIs
{-# LANGUAGE OverloadedStrings #-}
module Main where
-- Make static:
-- ghc -O2 --make -static -optc-static -optl-static -fvia-C -optl-pthread fix-facter-path.hs
import Turtle
import Data.Text (Text, pack)
import Filesystem.Path.CurrentOS (fromText, basename, dirname)
import Prelude hiding (FilePath)
import System.Directory (getHomeDirectory)
facter = "/usr/local/bin/facter"
linkFacter :: [Text] -> IO ()
linkFacter [] = do
echo "no facters to symlink"
linkFacter (path:paths) = do
pathExists <- testfile . fromText $ path
if pathExists
then do
let cmd = "ln -s " <> path <> " " <> facter
--echo cmd
res <- shell cmd empty
case res of
ExitSuccess -> echo ("linked " <> path)
ExitFailure err -> echo . pack . show $ err
else
linkFacter paths
main :: IO ()
main = do
usrLocal <- testfile . fromText $ facter
home <- getHomeDirectory
if usrLocal
then echo (facter <> " already exists")
else linkFacter [ (pack home) <> "/src/puppetlabs/facter/release/bin/facter"
, "/opt/puppetlabs/bin/facter"
, "/usr/bin/facter"
]
@joehillen
Copy link
Copy Markdown

@drewr Here is a quick stab at your script written in Craft

I didn't try compiling it, but I hope this gives you an idea.

To do something more interesting than just running locally, replace runCraftLocal with runCraftSSH.

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Lens
import Craft
import Craft.Log
import qualified Craft.File as File
import qualified Craft.User as User
import Craft.File.Link (Link(..))
import qualified Craft.File.Link as Link


facter = "/usr/local/bin/facter"


linkFacter :: [FilePath] ->  ()
linkFacter [] =
  $logError "no paths to symlink"
linkFacter (path:paths) = do
  e <- File.exists path
  if e
    then craft_ $ Link (Link.Target path) (Link.Path facter)
    else linkFacter paths


main :: IO ()
main = runCraftLocal craftEnv $ do
  usrLocal <- Link.exists facter
  if usrLocal
    then $logInfo "link already present"
    else do
      username <- view stdout <$> ($errorOnFail =<< exec "whoami" [])
      userMB <- User.get username
      case userMB of
        Nothing -> $craftError $ "Failed to get user: " ++ username
        Just u ->
          linkFacter [ u ^. User.home <> "/src/puppetlabs/facter/release/bin/facter"
                      , "/opt/puppetlabs/bin/facter"
                      , "/usr/bin/facter"
                      ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment