{-# LANGUAGE TypeFamilies #-}

module Propellor.Property.Debootstrap (
	Url,
	DebootstrapConfig(..),
	built,
	built',
	extractSuite,
	installed,
	sourceInstall,
) where

import Propellor.Base
import qualified Propellor.Property.Apt as Apt
import Propellor.Property.Chroot.Util
import Propellor.Property.Qemu
import Utility.Path

import Data.List
import Data.Char
import qualified Data.Semigroup as Sem
import System.Posix.Directory
import System.Posix.Files

type Url = String

-- | A data type for debootstrap configuration.
-- mempty is a default debootstrapped system.
data DebootstrapConfig
	= DefaultConfig
	| MinBase
	| BuilddD
	| DebootstrapParam String
	| UseEmulation
	| DebootstrapProxy Url
	| DebootstrapMirror Url
	| UseOldGpgKeyring
	-- ^ Debootstrap using the keyring of old and removed gpg keys.
	-- This is needed to debootstrap ancient stable releases of Debian.
	| DebootstrapConfig :+ DebootstrapConfig
	deriving (Int -> DebootstrapConfig -> ShowS
[DebootstrapConfig] -> ShowS
DebootstrapConfig -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [DebootstrapConfig] -> ShowS
$cshowList :: [DebootstrapConfig] -> ShowS
show :: DebootstrapConfig -> [Char]
$cshow :: DebootstrapConfig -> [Char]
showsPrec :: Int -> DebootstrapConfig -> ShowS
$cshowsPrec :: Int -> DebootstrapConfig -> ShowS
Show)

instance Sem.Semigroup DebootstrapConfig where
	<> :: DebootstrapConfig -> DebootstrapConfig -> DebootstrapConfig
(<>) = DebootstrapConfig -> DebootstrapConfig -> DebootstrapConfig
(:+)

instance Monoid DebootstrapConfig where
	mempty :: DebootstrapConfig
mempty  = DebootstrapConfig
DefaultConfig
	mappend :: DebootstrapConfig -> DebootstrapConfig -> DebootstrapConfig
mappend = forall a. Semigroup a => a -> a -> a
(Sem.<>)

toParams :: DebootstrapConfig -> [CommandParam]
toParams :: DebootstrapConfig -> [CommandParam]
toParams DebootstrapConfig
DefaultConfig = []
toParams DebootstrapConfig
MinBase = [[Char] -> CommandParam
Param [Char]
"--variant=minbase"]
toParams DebootstrapConfig
BuilddD = [[Char] -> CommandParam
Param [Char]
"--variant=buildd"]
toParams (DebootstrapParam [Char]
p) = [[Char] -> CommandParam
Param [Char]
p]
toParams DebootstrapConfig
UseEmulation = []
toParams (DebootstrapProxy [Char]
_) = []
toParams (DebootstrapMirror [Char]
_) = []
toParams DebootstrapConfig
UseOldGpgKeyring = [[Char] -> CommandParam
Param [Char]
"--keyring=/usr/share/keyrings/debian-archive-removed-keys.gpg"]
toParams (DebootstrapConfig
c1 :+ DebootstrapConfig
c2) = DebootstrapConfig -> [CommandParam]
toParams DebootstrapConfig
c1 forall a. Semigroup a => a -> a -> a
<> DebootstrapConfig -> [CommandParam]
toParams DebootstrapConfig
c2

useEmulation :: DebootstrapConfig -> Bool
useEmulation :: DebootstrapConfig -> Bool
useEmulation DebootstrapConfig
UseEmulation = Bool
True
useEmulation (DebootstrapConfig
a :+ DebootstrapConfig
b) = DebootstrapConfig -> Bool
useEmulation DebootstrapConfig
a Bool -> Bool -> Bool
|| DebootstrapConfig -> Bool
useEmulation DebootstrapConfig
b
useEmulation DebootstrapConfig
_ = Bool
False

debootstrapProxy :: DebootstrapConfig -> Maybe Url
debootstrapProxy :: DebootstrapConfig -> Maybe [Char]
debootstrapProxy (DebootstrapProxy [Char]
u) = forall a. a -> Maybe a
Just [Char]
u
debootstrapProxy (DebootstrapConfig
a :+ DebootstrapConfig
b) = DebootstrapConfig -> Maybe [Char]
debootstrapProxy DebootstrapConfig
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> DebootstrapConfig -> Maybe [Char]
debootstrapProxy DebootstrapConfig
b
debootstrapProxy DebootstrapConfig
_ = forall a. Maybe a
Nothing

debootstrapMirror :: DebootstrapConfig -> Maybe Url
debootstrapMirror :: DebootstrapConfig -> Maybe [Char]
debootstrapMirror (DebootstrapMirror [Char]
u) = forall a. a -> Maybe a
Just [Char]
u
debootstrapMirror (DebootstrapConfig
a :+ DebootstrapConfig
b) = DebootstrapConfig -> Maybe [Char]
debootstrapMirror DebootstrapConfig
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> DebootstrapConfig -> Maybe [Char]
debootstrapMirror DebootstrapConfig
b
debootstrapMirror DebootstrapConfig
_ = forall a. Maybe a
Nothing

-- | Builds a chroot in the given directory using debootstrap.
--
-- The System can be any OS and architecture that debootstrap
-- and the kernel support.
--
-- When the System is architecture that the kernel does not support,
-- it can still be bootstrapped using emulation. This is determined
-- by checking `supportsArch`, or can be configured with `UseEmulation`.
built :: FilePath -> System -> DebootstrapConfig -> Property Linux
built :: [Char] -> System -> DebootstrapConfig -> Property Linux
built [Char]
target system :: System
system@(System Distribution
_ Architecture
targetarch) DebootstrapConfig
config =
	forall {k} (metatypes :: k).
SingI metatypes =>
[Char]
-> (OuterMetaTypesWitness metatypes
    -> Maybe System -> Propellor Result)
-> Property (MetaTypes metatypes)
withOS ([Char]
"debootstrapped " forall a. [a] -> [a] -> [a]
++ [Char]
target) OuterMetaTypesWitness
  '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish,
     'Targeting 'OSArchLinux]
-> Maybe System -> Propellor Result
go
  where
	go :: OuterMetaTypesWitness
  '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish,
     'Targeting 'OSArchLinux]
-> Maybe System -> Propellor Result
go OuterMetaTypesWitness
  '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish,
     'Targeting 'OSArchLinux]
w (Just System
hostos)
		| System -> Architecture -> Bool
supportsArch System
hostos Architecture
targetarch Bool -> Bool -> Bool
&& Bool -> Bool
not (DebootstrapConfig -> Bool
useEmulation DebootstrapConfig
config) =
			forall (inner :: [MetaType]) (outer :: [MetaType]).
EnsurePropertyAllowed inner outer =>
OuterMetaTypesWitness outer
-> Property (MetaTypes inner) -> Propellor Result
ensureProperty OuterMetaTypesWitness
  '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish,
     'Targeting 'OSArchLinux]
w forall a b. (a -> b) -> a -> b
$
				Property Linux
-> [Char] -> System -> DebootstrapConfig -> Property Linux
built' (forall setupmetatypes undometatypes.
RevertableProperty setupmetatypes undometatypes
-> Property setupmetatypes
setupRevertableProperty RevertableProperty Linux Linux
installed)
					[Char]
target System
system DebootstrapConfig
config
	go OuterMetaTypesWitness
  '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish,
     'Targeting 'OSArchLinux]
w Maybe System
_ = forall (inner :: [MetaType]) (outer :: [MetaType]).
EnsurePropertyAllowed inner outer =>
OuterMetaTypesWitness outer
-> Property (MetaTypes inner) -> Propellor Result
ensureProperty OuterMetaTypesWitness
  '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish,
     'Targeting 'OSArchLinux]
w forall a b. (a -> b) -> a -> b
$ do
		let p :: CombinedType (Property Linux) (Property Linux)
p = forall setupmetatypes undometatypes.
RevertableProperty setupmetatypes undometatypes
-> Property setupmetatypes
setupRevertableProperty RevertableProperty Linux Linux
foreignBinariesEmulated
			forall x y. Combines x y => x -> y -> CombinedType x y
`before` forall setupmetatypes undometatypes.
RevertableProperty setupmetatypes undometatypes
-> Property setupmetatypes
setupRevertableProperty RevertableProperty Linux Linux
installed
		Property Linux
-> [Char] -> System -> DebootstrapConfig -> Property Linux
built' Property Linux
p [Char]
target System
system (DebootstrapConfig
config DebootstrapConfig -> DebootstrapConfig -> DebootstrapConfig
:+ DebootstrapConfig
UseEmulation)

-- | Like `built`,  but uses the provided Property to install debootstrap.
built' :: Property Linux -> FilePath -> System -> DebootstrapConfig -> Property Linux
built' :: Property Linux
-> [Char] -> System -> DebootstrapConfig -> Property Linux
built' Property Linux
installprop [Char]
target system :: System
system@(System Distribution
_ Architecture
arch) DebootstrapConfig
config = 
	CombinedType (Property Linux) (Property Linux)
go forall x y. Combines x y => x -> y -> CombinedType x y
`before` Property Linux
oldpermfix
  where
	go :: CombinedType (Property Linux) (Property Linux)
go = forall (p :: * -> *) i (m :: * -> *).
(Checkable p i, LiftPropellor m) =>
m Bool -> p i -> Property i
check ([Char] -> IO Bool
isUnpopulated [Char]
target forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
<||> IO Bool
ispartial) Property Linux
setupprop
		forall x y. Combines x y => x -> y -> CombinedType x y
`requires` Property Linux
installprop

	setupprop :: Property Linux
	setupprop :: Property Linux
setupprop = forall {k} (metatypes :: k).
SingI metatypes =>
[Char] -> Propellor Result -> Property (MetaTypes metatypes)
property ([Char]
"debootstrapped " forall a. [a] -> [a] -> [a]
++ [Char]
target) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
		Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
True [Char]
target
		[Char]
suite <- case System -> Maybe [Char]
extractSuite System
system of
			Maybe [Char]
Nothing -> forall (m :: * -> *) a. MonadIO m => [Char] -> m a
errorMessage forall a b. (a -> b) -> a -> b
$ [Char]
"don't know how to debootstrap " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show System
system
			Just [Char]
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
s
		let params :: [CommandParam]
params = DebootstrapConfig -> [CommandParam]
toParams DebootstrapConfig
config forall a. [a] -> [a] -> [a]
++
			[ [Char] -> CommandParam
Param forall a b. (a -> b) -> a -> b
$ [Char]
"--arch=" forall a. [a] -> [a] -> [a]
++ Architecture -> [Char]
architectureToDebianArchString Architecture
arch
			, [Char] -> CommandParam
Param [Char]
suite
			, [Char] -> CommandParam
Param [Char]
target
			] forall a. [a] -> [a] -> [a]
++ case DebootstrapConfig -> Maybe [Char]
debootstrapMirror DebootstrapConfig
config of
				Just [Char]
u -> [[Char] -> CommandParam
Param [Char]
u]
				Maybe [Char]
Nothing -> []
		[Char]
cmd <- forall a. a -> Maybe a -> a
fromMaybe [Char]
"debootstrap" forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe [Char])
programPath
		[([Char], [Char])]
de <- case DebootstrapConfig -> Maybe [Char]
debootstrapProxy DebootstrapConfig
config of
			Just [Char]
u -> forall k v. Eq k => k -> v -> [(k, v)] -> [(k, v)]
addEntry [Char]
"http_proxy" [Char]
u forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [([Char], [Char])]
standardPathEnv
			Maybe [Char]
Nothing -> IO [([Char], [Char])]
standardPathEnv
		forall (m :: * -> *) a. Monad m => m Bool -> (m a, m a) -> m a
ifM ([Char] -> [CommandParam] -> Maybe [([Char], [Char])] -> IO Bool
boolSystemEnv [Char]
cmd [CommandParam]
params (forall a. a -> Maybe a
Just [([Char], [Char])]
de))
			( forall (m :: * -> *) a. Monad m => a -> m a
return Result
MadeChange
			, forall (m :: * -> *) a. Monad m => a -> m a
return Result
FailedChange
			)

	-- A failed debootstrap run will leave a debootstrap directory;
	-- recover by deleting it and trying again.
	ispartial :: IO Bool
ispartial = forall (m :: * -> *) a. Monad m => m Bool -> (m a, m a) -> m a
ifM ([Char] -> IO Bool
doesDirectoryExist ([Char]
target [Char] -> ShowS
</> [Char]
"debootstrap"))
		( do
			[Char] -> IO ()
removeChroot [Char]
target
			forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
		, forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
		)
	
	-- May want to remove this after some appropriate length of time,
	-- as it's a workaround for chroots set up with too tight
	-- permissions.
	oldpermfix :: Property Linux
	oldpermfix :: Property Linux
oldpermfix = forall {k} (metatypes :: k).
SingI metatypes =>
[Char] -> Propellor Result -> Property (MetaTypes metatypes)
property ([Char]
"fixed old chroot file mode") forall a b. (a -> b) -> a -> b
$ do
		forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ [Char] -> (FileMode -> FileMode) -> IO ()
modifyFileMode [Char]
target forall a b. (a -> b) -> a -> b
$
			[FileMode] -> FileMode -> FileMode
addModes [FileMode
otherReadMode, FileMode
otherExecuteMode]
		forall (m :: * -> *) a. Monad m => a -> m a
return Result
NoChange

extractSuite :: System -> Maybe String
extractSuite :: System -> Maybe [Char]
extractSuite (System (Debian DebianKernel
_ DebianSuite
s) Architecture
_) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ DebianSuite -> [Char]
Apt.showSuite DebianSuite
s
extractSuite (System (Buntish [Char]
r) Architecture
_) = forall a. a -> Maybe a
Just [Char]
r
extractSuite (System (Distribution
ArchLinux) Architecture
_) = forall a. Maybe a
Nothing
extractSuite (System (FreeBSD FreeBSDRelease
_) Architecture
_) = forall a. Maybe a
Nothing

-- | Ensures debootstrap is installed.
--
-- When necessary, falls back to installing debootstrap from source.
-- Note that installation from source is done by downloading the tarball
-- from a Debian mirror, with no cryptographic verification.
installed :: RevertableProperty Linux Linux
installed :: RevertableProperty Linux Linux
installed = Property Linux
install forall setupmetatypes undometatypes.
Property setupmetatypes
-> Property undometatypes
-> RevertableProperty setupmetatypes undometatypes
<!> Property Linux
remove
  where
	install :: Property Linux
install = forall (p :: * -> *) i (m :: * -> *).
(Checkable p i, LiftPropellor m) =>
m Bool -> p i -> Property i
check (forall a. Maybe a -> Bool
isNothing forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe [Char])
programPath) forall a b. (a -> b) -> a -> b
$
		(Property DebianLike
aptinstall forall {k} ka kb (c :: k) (a :: ka) (b :: kb).
(HasCallStack, SingKind 'KProxy, SingKind 'KProxy,
 DemoteRep 'KProxy ~ [MetaType], DemoteRep 'KProxy ~ [MetaType],
 SingI c) =>
Property (MetaTypes a)
-> Property (MetaTypes b) -> Property (MetaTypes c)
`pickOS` Property Linux
sourceInstall)
			forall p. IsProp p => p -> [Char] -> p
`describe` [Char]
"debootstrap installed"

	remove :: Property Linux
remove = (Property DebianLike
aptremove forall {k} ka kb (c :: k) (a :: ka) (b :: kb).
(HasCallStack, SingKind 'KProxy, SingKind 'KProxy,
 DemoteRep 'KProxy ~ [MetaType], DemoteRep 'KProxy ~ [MetaType],
 SingI c) =>
Property (MetaTypes a)
-> Property (MetaTypes b) -> Property (MetaTypes c)
`pickOS` Property Linux
sourceRemove)
		forall p. IsProp p => p -> [Char] -> p
`describe` [Char]
"debootstrap removed"

	aptinstall :: Property DebianLike
aptinstall = [[Char]] -> Property DebianLike
Apt.installed [[Char]
"debootstrap"]
	aptremove :: Property DebianLike
aptremove = [[Char]] -> Property DebianLike
Apt.removed [[Char]
"debootstrap"]

sourceInstall :: Property Linux
sourceInstall :: Property Linux
sourceInstall = Property Linux
go
	forall x y. Combines x y => x -> y -> CombinedType x y
`requires` Property Linux
perlInstalled
	forall x y. Combines x y => x -> y -> CombinedType x y
`requires` Property Linux
arInstalled
  where
	go :: Property Linux
	go :: Property Linux
go = forall {k} (metatypes :: k).
SingI metatypes =>
[Char] -> Propellor Result -> Property (MetaTypes metatypes)
property [Char]
"debootstrap installed from source" (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Result
sourceInstall')

perlInstalled :: Property Linux
perlInstalled :: Property Linux
perlInstalled = forall (p :: * -> *) i (m :: * -> *).
(Checkable p i, LiftPropellor m) =>
m Bool -> p i -> Property i
check (Bool -> Bool
not forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO Bool
inPath [Char]
"perl") forall a b. (a -> b) -> a -> b
$ forall {k} (metatypes :: k).
SingI metatypes =>
[Char] -> Propellor Result -> Property (MetaTypes metatypes)
property [Char]
"perl installed" forall a b. (a -> b) -> a -> b
$
	forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall t. ToResult t => t -> Result
toResult forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> Bool
isJust forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> [a] -> m (Maybe a)
firstM forall a. a -> a
id
		[ [Char] -> IO Bool
yumInstall [Char]
"perl"
		]

arInstalled :: Property Linux
arInstalled :: Property Linux
arInstalled = forall (p :: * -> *) i (m :: * -> *).
(Checkable p i, LiftPropellor m) =>
m Bool -> p i -> Property i
check (Bool -> Bool
not forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO Bool
inPath [Char]
"ar") forall a b. (a -> b) -> a -> b
$ forall {k} (metatypes :: k).
SingI metatypes =>
[Char] -> Propellor Result -> Property (MetaTypes metatypes)
property [Char]
"ar installed" forall a b. (a -> b) -> a -> b
$
	forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall t. ToResult t => t -> Result
toResult forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> Bool
isJust forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> [a] -> m (Maybe a)
firstM forall a. a -> a
id
		[ [Char] -> IO Bool
yumInstall [Char]
"binutils"
		]

yumInstall :: String -> IO Bool
yumInstall :: [Char] -> IO Bool
yumInstall [Char]
p = [Char] -> [CommandParam] -> IO Bool
boolSystem [Char]
"yum" [[Char] -> CommandParam
Param [Char]
"-y", [Char] -> CommandParam
Param [Char]
"install", [Char] -> CommandParam
Param [Char]
p]

sourceInstall' :: IO Result
sourceInstall' :: IO Result
sourceInstall' = forall (m :: * -> *) a.
(MonadMask m, MonadIO m) =>
[Char] -> ([Char] -> m a) -> m a
withTmpDir [Char]
"debootstrap" forall a b. (a -> b) -> a -> b
$ \[Char]
tmpd -> do
	let indexfile :: [Char]
indexfile = [Char]
tmpd [Char] -> ShowS
</> [Char]
"index.html"
	forall {m :: * -> *}. Monad m => m Bool -> m () -> m ()
unlessM ([Char] -> [Char] -> IO Bool
download [Char]
baseurl [Char]
indexfile) forall a b. (a -> b) -> a -> b
$
		forall (m :: * -> *) a. MonadIO m => [Char] -> m a
errorMessage forall a b. (a -> b) -> a -> b
$ [Char]
"Failed to download " forall a. [a] -> [a] -> [a]
++ [Char]
baseurl
	[[Char]]
urls <- forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Ord a => a -> a -> Ordering
compare) -- highest version first
		forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ([Char]
"debootstrap_" forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf`)
		forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ([Char]
".tar." forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf`)
		forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [[Char]]
extractUrls [Char]
baseurl forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
		[Char] -> IO [Char]
readFileStrict [Char]
indexfile
	[Char] -> IO ()
nukeFile [Char]
indexfile

	[Char]
tarfile <- case [[Char]]
urls of
		([Char]
tarurl:[[Char]]
_) -> do
			let f :: [Char]
f = [Char]
tmpd [Char] -> ShowS
</> ShowS
takeFileName [Char]
tarurl
			forall {m :: * -> *}. Monad m => m Bool -> m () -> m ()
unlessM ([Char] -> [Char] -> IO Bool
download [Char]
tarurl [Char]
f) forall a b. (a -> b) -> a -> b
$
				forall (m :: * -> *) a. MonadIO m => [Char] -> m a
errorMessage forall a b. (a -> b) -> a -> b
$ [Char]
"Failed to download " forall a. [a] -> [a] -> [a]
++ [Char]
tarurl
			forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
f
		[[Char]]
_ -> forall (m :: * -> *) a. MonadIO m => [Char] -> m a
errorMessage forall a b. (a -> b) -> a -> b
$ [Char]
"Failed to find any debootstrap tarballs listed on " forall a. [a] -> [a] -> [a]
++ [Char]
baseurl

	Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
True [Char]
localInstallDir
	forall (m :: * -> *) a c b.
MonadMask m =>
m a -> (a -> m c) -> (a -> m b) -> m b
bracket IO [Char]
getWorkingDirectory [Char] -> IO ()
changeWorkingDirectory forall a b. (a -> b) -> a -> b
$ \[Char]
_ -> do
		[Char] -> IO ()
changeWorkingDirectory [Char]
localInstallDir
		forall {m :: * -> *}. Monad m => m Bool -> m () -> m ()
unlessM ([Char] -> [CommandParam] -> IO Bool
boolSystem [Char]
"tar" [[Char] -> CommandParam
Param [Char]
"xf", [Char] -> CommandParam
File [Char]
tarfile]) forall a b. (a -> b) -> a -> b
$
			forall (m :: * -> *) a. MonadIO m => [Char] -> m a
errorMessage [Char]
"Failed to extract debootstrap tar file"
		[Char] -> IO ()
nukeFile [Char]
tarfile
		[[Char]]
l <- [Char] -> IO [[Char]]
dirContents [Char]
"."
		case [[Char]]
l of
			([Char]
subdir:[]) -> do
				[Char] -> IO ()
changeWorkingDirectory [Char]
subdir
				[Char] -> IO ()
makeWrapperScript ([Char]
localInstallDir [Char] -> ShowS
</> [Char]
subdir)
				forall (m :: * -> *) a. Monad m => a -> m a
return Result
MadeChange
			[[Char]]
_ -> forall (m :: * -> *) a. MonadIO m => [Char] -> m a
errorMessage [Char]
"debootstrap tar file did not contain exactly one directory"

sourceRemove :: Property Linux
sourceRemove :: Property Linux
sourceRemove = forall {k} (metatypes :: k).
SingI metatypes =>
[Char] -> Propellor Result -> Property (MetaTypes metatypes)
property [Char]
"debootstrap not installed from source" forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
	forall (m :: * -> *) a. Monad m => m Bool -> (m a, m a) -> m a
ifM ([Char] -> IO Bool
doesDirectoryExist [Char]
sourceInstallDir)
		( do
			[Char] -> IO ()
removeDirectoryRecursive [Char]
sourceInstallDir
			forall (m :: * -> *) a. Monad m => a -> m a
return Result
MadeChange
		, forall (m :: * -> *) a. Monad m => a -> m a
return Result
NoChange
		)

sourceInstallDir :: FilePath
sourceInstallDir :: [Char]
sourceInstallDir = [Char]
"/usr/local/propellor/debootstrap"

wrapperScript :: FilePath
wrapperScript :: [Char]
wrapperScript = [Char]
sourceInstallDir [Char] -> ShowS
</> [Char]
"debootstrap.wrapper"

-- | Finds debootstrap in PATH, but fall back to looking for the
-- wrapper script that is installed, outside the PATH, when debootstrap
-- is installed from source.
programPath :: IO (Maybe FilePath)
programPath :: IO (Maybe [Char])
programPath = forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> [a] -> m (Maybe b)
getM [Char] -> IO (Maybe [Char])
searchPath
	[ [Char]
"debootstrap"
	, [Char]
wrapperScript
	]

makeWrapperScript :: FilePath -> IO ()
makeWrapperScript :: [Char] -> IO ()
makeWrapperScript [Char]
dir = do
	Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
True (ShowS
takeDirectory [Char]
wrapperScript)
	[Char] -> [Char] -> IO ()
writeFile [Char]
wrapperScript forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines
		[ [Char]
"#!/bin/sh"
		, [Char]
"set -e"
		, [Char]
"DEBOOTSTRAP_DIR=" forall a. [a] -> [a] -> [a]
++ [Char]
dir
		, [Char]
"export DEBOOTSTRAP_DIR"
		, [Char]
dir [Char] -> ShowS
</> [Char]
"debootstrap" forall a. [a] -> [a] -> [a]
++ [Char]
" \"$@\""
		]
	[Char] -> (FileMode -> FileMode) -> IO ()
modifyFileMode [Char]
wrapperScript ([FileMode] -> FileMode -> FileMode
addModes forall a b. (a -> b) -> a -> b
$ [FileMode]
readModes forall a. [a] -> [a] -> [a]
++ [FileMode]
executeModes)

localInstallDir :: FilePath
localInstallDir :: [Char]
localInstallDir = [Char]
"/usr/local/debootstrap"

-- This http server directory listing is relied on to be fairly sane,
-- which is one reason why it's using a specific server and not a
-- round-robin address.
baseurl :: Url
baseurl :: [Char]
baseurl = [Char]
"http://ftp.debian.org/debian/pool/main/d/debootstrap/"

download :: Url -> FilePath -> IO Bool
download :: [Char] -> [Char] -> IO Bool
download [Char]
url [Char]
dest = forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyM forall a. a -> a
id
	[ [Char] -> [CommandParam] -> IO Bool
boolSystem [Char]
"curl" [[Char] -> CommandParam
Param [Char]
"-o", [Char] -> CommandParam
File [Char]
dest, [Char] -> CommandParam
Param [Char]
url]
	, [Char] -> [CommandParam] -> IO Bool
boolSystem [Char]
"wget" [[Char] -> CommandParam
Param [Char]
"-O", [Char] -> CommandParam
File [Char]
dest, [Char] -> CommandParam
Param [Char]
url]
	]

-- Pretty hackish, but I don't want to pull in a whole html parser
-- or parsec dependency just for this.
--
-- To simplify parsing, lower case everything. This is ok because
-- the filenames are all lower-case anyway.
extractUrls :: Url -> String -> [Url]
extractUrls :: [Char] -> [Char] -> [[Char]]
extractUrls [Char]
base = [[Char]] -> [Char] -> [[Char]]
collect [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower
  where
	collect :: [[Char]] -> [Char] -> [[Char]]
collect [[Char]]
l [] = [[Char]]
l
	collect [[Char]]
l (Char
'h':Char
'r':Char
'e':Char
'f':Char
'=':[Char]
r) = case [Char]
r of
		(Char
'"':[Char]
r') -> [[Char]] -> [Char] -> [[Char]]
findend [[Char]]
l [Char]
r'
		[Char]
_ -> [[Char]] -> [Char] -> [[Char]]
findend [[Char]]
l [Char]
r
	collect [[Char]]
l (Char
_:[Char]
cs) = [[Char]] -> [Char] -> [[Char]]
collect [[Char]]
l [Char]
cs

	findend :: [[Char]] -> [Char] -> [[Char]]
findend [[Char]]
l [Char]
s =
		let ([Char]
u, [Char]
r) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
== Char
'"') [Char]
s
		    u' :: [Char]
u' = if [Char]
"http" forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [Char]
u
			then [Char]
u
			else [Char]
base [Char] -> ShowS
</> [Char]
u
		in [[Char]] -> [Char] -> [[Char]]
collect ([Char]
u'forall a. a -> [a] -> [a]
:[[Char]]
l) [Char]
r