summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/OS.hs
blob: 5a3ccc70541c73b8306559b9d726ceaf2fb32626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
module Propellor.Property.OS (
	cleanInstallOnce,
	Confirmation(..),
	preserveNetwork,
	preserveResolvConf,
	preserveRootSshAuthorized,
	oldOSRemoved,
) where

import Propellor.Base
import qualified Propellor.Property.Debootstrap as Debootstrap
import qualified Propellor.Property.Ssh as Ssh
import qualified Propellor.Property.Network as Network
import qualified Propellor.Property.User as User
import qualified Propellor.Property.File as File
import qualified Propellor.Property.Reboot as Reboot
import Propellor.Property.Mount
import Propellor.Property.Chroot.Util (stdPATH)

import System.Posix.Files (rename, fileExist)
import Control.Exception (throw)

-- | Replaces whatever OS was installed before with a clean installation
-- of the OS that the Host is configured to have.
-- 
-- This is experimental; use with caution!
--
-- This can replace one Linux distribution with different one.
-- But, it can also fail and leave the system in an unbootable state.
--
-- To avoid this property being accidentially used, you have to provide
-- a Confirmation containing the name of the host that you intend to apply
-- the property to.
--
-- This property only runs once. The cleanly installed system will have
-- a file </etc/propellor-cleaninstall>, which indicates it was cleanly
-- installed.
-- 
-- The files from the old os will be left in </old-os>
--
-- After the OS is installed, and if all properties of the host have
-- been successfully satisfied, the host will be rebooted to properly load
-- the new OS.
--
-- You will typically want to run some more properties after the clean
-- install succeeds, to bootstrap from the cleanly installed system to
-- a fully working system. For example:
--
-- > & osDebian Unstable "amd64"
-- > & cleanInstallOnce (Confirmed "foo.example.com")
-- >    `onChange` propertyList "fixing up after clean install"
-- >        [ preserveNetwork
-- >        , preserveResolvConf
-- >        , preserveRootSshAuthorized
-- >        , Apt.update
-- >        -- , Grub.boots "/dev/sda"
-- >        --   `requires` Grub.installed Grub.PC
-- >        -- , oldOsRemoved (Confirmed "foo.example.com")
-- >        ]
-- > & Hostname.sane
-- > & Apt.installed ["linux-image-amd64"]
-- > & Apt.installed ["ssh"]
-- > & User.hasSomePassword "root"
-- > & User.accountFor "joey"
-- > & User.hasSomePassword "joey"
-- > -- rest of system properties here
cleanInstallOnce :: Confirmation -> Property Linux
cleanInstallOnce confirmation = check (not <$> doesFileExist flagfile) $
	go `requires` confirmed "clean install confirmed" confirmation
  where
	go = 
		finalized
			`requires`
		-- easy to forget and system may not boot without shadow pw!
		User.shadowConfig True
			`requires`
		-- reboot at end if the rest of the propellor run succeeds
		Reboot.atEnd True (/= FailedChange)
			`requires`
		propellorbootstrapped
			`requires`
		flipped
			`requires`
		osbootstrapped

	osbootstrapped :: Property Linux
	osbootstrapped = withOS (newOSDir ++ " bootstrapped") $ \w o -> case o of
		(Just d@(System (Debian _) _)) -> ensureProperty w $
			debootstrap d
		(Just u@(System (Buntish _) _)) -> ensureProperty w $
			debootstrap u
		_ -> unsupportedOS'
	
	debootstrap :: System -> Property Linux
	debootstrap targetos =
		-- Install debootstrap from source, since we don't know
		-- what OS we're currently running in.
		Debootstrap.built' Debootstrap.sourceInstall
			newOSDir targetos Debootstrap.DefaultConfig
		-- debootstrap, I wish it was faster.. 
		-- TODO eatmydata to speed it up
		-- Problem: Installing eatmydata on some random OS like
		-- Fedora may be difficult. Maybe configure dpkg to not
		-- sync instead?

	-- This is the fun bit.
	flipped :: Property Linux
	flipped = property (newOSDir ++ " moved into place") $ liftIO $ do
		-- First, unmount most mount points, lazily, so
		-- they don't interfere with moving things around.
		devfstype <- fromMaybe "devtmpfs" <$> getFsType "/dev"
		mnts <- filter (`notElem` ("/": trickydirs)) <$> mountPoints
		-- reverse so that deeper mount points come first
		forM_ (reverse mnts) umountLazy

		renamesout <- map (\d -> (d, oldOSDir ++ d, pure $ d `notElem` (oldOSDir:newOSDir:trickydirs)))
			<$> dirContents "/"
		renamesin <- map (\d -> let dest = "/" ++ takeFileName d in (d, dest, not <$> fileExist dest))
			<$> dirContents newOSDir
		createDirectoryIfMissing True oldOSDir
		massRename (renamesout ++ renamesin)
		removeDirectoryRecursive newOSDir
		
		-- Prepare environment for running additional properties,
		-- overriding old OS's environment.
		void $ setEnv "PATH" stdPATH True
		void $ unsetEnv "LANG"

		-- Remount /dev, so that block devices etc are
		-- available for other properties to use.
		unlessM (mount devfstype devfstype "/dev" mempty) $ do
			warningMessage $ "failed mounting /dev using " ++ devfstype ++ "; falling back to MAKEDEV generic"
			void $ boolSystem "sh" [Param "-c", Param "cd /dev && /sbin/MAKEDEV generic"]

		-- Mount /sys too, needed by eg, grub-mkconfig.
		unlessM (mount "sysfs" "sysfs" "/sys" mempty) $
			warningMessage "failed mounting /sys"

		-- And /dev/pts, used by apt.
		unlessM (mount "devpts" "devpts" "/dev/pts" mempty) $
			warningMessage "failed mounting /dev/pts"

		return MadeChange

	propellorbootstrapped :: Property UnixLike
	propellorbootstrapped = property "propellor re-debootstrapped in new os" $
		return NoChange
		-- re-bootstrap propellor in /usr/local/propellor,
		--   (using git repo bundle, privdata file, and possibly
		--   git repo url, which all need to be arranged to
		--   be present in /old-os's /usr/local/propellor)
		-- TODO
	
	finalized :: Property UnixLike
	finalized = property "clean OS installed" $ do
		liftIO $ writeFile flagfile ""
		return MadeChange

	flagfile = "/etc/propellor-cleaninstall"
	
	trickydirs = 
		-- /tmp can contain X's sockets, which prevent moving it
		-- so it's left as-is.
		[ "/tmp"
		-- /proc is left mounted
		, "/proc"
		]

-- Performs all the renames. If any rename fails, rolls back all
-- previous renames. Thus, this either successfully performs all
-- the renames, or does not change the system state at all.
massRename :: [(FilePath, FilePath, IO Bool)] -> IO ()
massRename = go []
  where
	go _ [] = return ()
	go undo ((from, to, test):rest) = ifM test
		( tryNonAsync (rename from to)
			>>= either
				(rollback undo)
				(const $ go ((to, from):undo) rest)
		, go undo rest
		)
	rollback undo e = do
		mapM_ (uncurry rename) undo
		throw e

data Confirmation = Confirmed HostName

confirmed :: Desc -> Confirmation -> Property UnixLike
confirmed desc (Confirmed c) = property desc $ do
	hostname <- asks hostName
	if hostname /= c
		then do
			warningMessage "Run with a bad confirmation, not matching hostname."
			return FailedChange
		else return NoChange

-- | </etc/network/interfaces> is configured to bring up the network 
-- interface that currently has a default route configured, using
-- the same (static) IP address.
preserveNetwork :: Property DebianLike
preserveNetwork = go `requires` Network.cleanInterfacesFile
  where
	go :: Property DebianLike
	go = property' "preserve network configuration" $ \w -> do
		ls <- liftIO $ lines <$> readProcess "ip"
			["route", "list", "scope", "global"]
		case words <$> headMaybe ls of
			Just ("default":"via":_:"dev":iface:_) ->
				ensureProperty w $ Network.static iface
			_ -> do
				warningMessage "did not find any default ipv4 route"
				return FailedChange 

-- | </etc/resolv.conf> is copied from the old OS
preserveResolvConf :: Property Linux
preserveResolvConf = check (fileExist oldloc) $
	property' (newloc ++ " copied from old OS") $ \w -> do
		ls <- liftIO $ lines <$> readFile oldloc
		ensureProperty w $ newloc `File.hasContent` ls
  where
	newloc = "/etc/resolv.conf"
	oldloc = oldOSDir ++ newloc

-- | </root/.ssh/authorized_keys> has added to it any ssh keys that
-- were authorized in the old OS. Any other contents of the file are
-- retained.
preserveRootSshAuthorized :: Property UnixLike
preserveRootSshAuthorized = check (fileExist oldloc) $
	property' desc $ \w -> do
		ks <- liftIO $ lines <$> readFile oldloc
		ensureProperty w $ combineProperties desc $
			toProps $ map (setupRevertableProperty . Ssh.authorizedKey (User "root")) ks
  where
	desc = newloc ++ " copied from old OS"
	newloc = "/root/.ssh/authorized_keys"
	oldloc = oldOSDir ++ newloc

-- Removes the old OS's backup from </old-os>
oldOSRemoved :: Confirmation -> Property UnixLike
oldOSRemoved confirmation = check (doesDirectoryExist oldOSDir) $
	go `requires` confirmed "old OS backup removal confirmed" confirmation
  where
	go :: Property UnixLike
	go = property "old OS backup removed" $ do
		liftIO $ removeDirectoryRecursive oldOSDir
		return MadeChange

oldOSDir :: FilePath
oldOSDir = "/old-os"

newOSDir :: FilePath
newOSDir = "/new-os"