summaryrefslogtreecommitdiffstats
path: root/inc/event.inc.php
blob: 16e5323a85062078d0ead7a3d030ff3cfbe1f044 (plain) (blame)
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
<?php

/**
 * Class with static functions that are called when a specific event
 * took place, like the server has been booted, or the interface address
 * has been changed.
 * In contrast to the trigger class, this class should contain functions
 * for things that happen semi-automatically in reaction to something else
 * (which in turn might have been triggered explicitly).
 */
class Event
{

	/**
	 * Called when the system (re)booted. Could be implemented
	 * by a @reboot entry in crontab (running as the same user php does)
	 */
	public static function systemBooted()
	{
		EventLog::info('System boot...');
		$everythingFine = true;

		// Delete job entries that might have been running when system rebooted
		Property::clearList('cron.key.status');
		Property::clearList('cron.key.blocked');

		// Do this before any hooks, might be used by some
		$autoIp = Trigger::autoUpdateServerIp();

		// Hooks
		foreach (Hook::load('bootup') as $hook) {
			// Isolate for local vars
			$fun = function() use ($hook) {
				include_once($hook->file);
			};
			$fun();
		}

		// TODO: Modularize (hooks)

		// Tasks: fire away
		$mountStatus = false;
		$mountId = Trigger::mount();
		$ldadpId = Trigger::ldadp();
		$ipxeId = Trigger::ipxe();

		// Check status of all tasks
		// Mount vm store
		if ($mountId === false) {
			EventLog::info('No VM store type defined.');
			$everythingFine = false;
		} else {
			$mountStatus = Taskmanager::waitComplete($mountId, 5000);

		}
		// LDAP AD Proxy
		if ($ldadpId === false) {
			EventLog::failure('Cannot start LDAP-AD-Proxy: Taskmanager unreachable!');
			$everythingFine = false;
		} else {
			$res = Taskmanager::waitComplete($ldadpId, 5000);
			if (Taskmanager::isFailed($res)) {
				EventLog::failure('Starting LDAP-AD-Proxy failed', $res['data']['messages']);
				$everythingFine = false;
			}
		}
		// Primary IP address
		if (!$autoIp) {
			EventLog::failure("The server's IP address could not be determined automatically, and there is no valid address configured.");
			$everythingFine = false;
		}
		// iPXE generation
		if ($ipxeId === false) {
			EventLog::failure('Cannot generate PXE menu: Taskmanager unreachable!');
			$everythingFine = false;
		} else {
			$res = Taskmanager::waitComplete($ipxeId, 5000);
			if (Taskmanager::isFailed($res)) {
				EventLog::failure('Update PXE Menu failed', $res['data']['error']);
				$everythingFine = false;
			}
		}

		if ($mountStatus !== false && !Taskmanager::isFinished($mountStatus)) {
			$mountStatus = Taskmanager::waitComplete($mountStatus, 5000);
		}
		if (Taskmanager::isFailed($mountStatus)) {
			// One more time, network could've been down before
			sleep(10);
			$mountId = Trigger::mount();
			$mountStatus = Taskmanager::waitComplete($mountId, 10000);
		}
		if ($mountId !== false && Taskmanager::isFailed($mountStatus)) {
			EventLog::failure('Mounting VM store failed', $mountStatus['data']['messages']);
			$everythingFine = false;
		} elseif ($mountId !== false && !Taskmanager::isFinished($mountStatus)) {
			// TODO: Still running - create callback
		}

		// Just so we know booting is done (and we don't expect any more errors from booting up)
		if ($everythingFine) {
			EventLog::info('Bootup finished without errors.');
		} else {
			EventLog::warning('There were errors during bootup. Maybe the server is not fully configured yet.');
		}
	}

	/**
	 * Server's primary IP address changed.
	 */
	public static function serverIpChanged()
	{
		Trigger::ipxe();
		if (Module::isAvailable('sysconfig')) { // TODO: Modularize events
			ConfigModule::serverIpChanged();
		}
	}

	/**
	 * The activated configuration changed.
	 */
	public static function activeConfigChanged()
	{
		$task = Trigger::ldadp();
		if ($task === false)
			return;
		TaskmanagerCallback::addCallback($task, 'ldadpStartup');
	}

}