summaryrefslogtreecommitdiffstats
path: root/inc/trigger.inc.php
blob: e6f7cd3135825c3b14dfeb85e182c3894115603c (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

/**
 * This is one giant class containing various functions that will generate
 * required config files, daemon instances and more, mostly through the Taskmanager.
 * Most function *should* only actually do something if it is required to do so.
 * eg. a "launchSomething" function should only launch something if it isn't already
 * running. Checking if something is running can happen in that very function, or in
 * a task that the function is calling.
 */
class Trigger
{
	
	/**
	 * Compile iPXE pxelinux menu. Needs to be done whenever the server's IP
	 * address changes.
	 * 
	 * @param boolean $force force recompilation even if it seems up to date
	 * @return boolean|string true if already up to date, false if launching task failed, task-id otherwise
	 */
	public static function ipxe()
	{
		$data = Property::getBootMenu();
		$task = Taskmanager::submit('CompileIPxe', $data);
		if (!isset($task['id']))
			return false;
		return $task['id'];
	}
	
	/**
	 * Try to automatically determine the primary IP address of the server.
	 * This only works if the server has either one public IPv4 address (and potentially
	 * one or more non-public addresses), or one private address.
	 */
	public static function autoUpdateServerIp()
	{
		$task = Taskmanager::submit('LocalAddressesList');
		if ($task === false)
			return;
		$task = Taskmanager::waitComplete($task, 10000);
		if (!isset($task['data']['addresses']) || empty($task['data']['addresses']))
			return;

		$serverIp = Property::getServerIp();
		$publicCandidate = 'none';
		$privateCandidate = 'none';
		foreach ($task['data']['addresses'] as $addr) {
			if ($addr['ip'] === $serverIp)
				return;
			if (substr($addr['ip'], 0, 4) === '127.')
				continue;
			if (Util::isPublicIpv4($addr['ip'])) {
				if ($publicCandidate === 'none')
					$publicCandidate = $addr['ip'];
				else
					$publicCandidate = 'many';
			} else {
				if ($privateCandidate === 'none')
					$privateCandidate = $addr['ip'];
				else
					$privateCandidate = 'many';
			}
		}
		if ($publicCandidate !== 'none' && $publicCandidate !== 'many') {
			Property::setServerIp($publicCandidate);
			return;
		}
		if ($privateCandidate !== 'none' && $privateCandidate !== 'many') {
			Property::setServerIp($privateCandidate);
			return;
		}
	}

	/**
	 * Launch all ldadp instances that need to be running.
	 *
	 * @param string $parent if not NULL, this will be the parent task of the launch-task
	 * @return boolean|string false on error, id of task otherwise
	 */
	public static function ldadp($parent = NULL)
	{
		$res = Database::simpleQuery("SELECT moduleid, configtgz.filepath FROM configtgz_module"
			. " INNER JOIN configtgz_x_module USING (moduleid)"
			. " INNER JOIN configtgz USING (configid)"
			. " WHERE moduletype = 'AD_AUTH'");
		// TODO: Multiconfig support
		$id = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			if (readlink('/srv/openslx/www/boot/default/config.tgz') === $row['filepath']) {
				$id[] = (int)$row['moduleid'];
				break;
			}
		}
		$task = Taskmanager::submit('LdadpLauncher', array(
			'ids' => $id,
			'parentTask' => $parent,
			'failOnParentFail' => false
		));
		if (!isset($task['id']))
			return false;
		return $task['id'];
	}
	
	/**
	 * To be called if the server ip changes, as it's embedded in the AD module configs.
	 * This will then recreate all AD tgz modules.
	 */
	public static function rebuildAdModules()
	{
		$res = Database::simpleQuery("SELECT moduleid, filepath, content FROM configtgz_module"
			. " WHERE moduletype = 'AD_AUTH'");
		if ($res->rowCount() === 0)
			return;
		
		$task = Taskmanager::submit('LdadpLauncher', array('ids' => array())); // Stop all running instances
		$parent = isset($task['id']) ? $task['id'] : NULL;
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$config = json_decode($row['contents']);
			$config['proxyip'] = Property::getServerIp();
			$config['moduleid'] = $row['moduleid'];
			$config['filename'] = $row['filepath'];
			$config['parentTask'] = $parent;
			$config['failOnParentFail'] = false;
			$task = Taskmanager::submit('CreateAdConfig', $config);
			$parent = isset($task['id']) ? $task['id'] : NULL;
		}
		
	}
	
	/**
	 * Mount the VM store into the server.
	 *
	 * @return array task status of mount procedure, or false on error
	 */
	public static function mount()
	{
		$vmstore = Property::getVmStoreConfig();
		if (!is_array($vmstore)) return false;
		$storetype = $vmstore['storetype'];
		if ($storetype === 'nfs') $addr = $vmstore['nfsaddr'];
		if ($storetype === 'cifs') $addr = $vmstore['cifsaddr'];
		if ($storetype === 'internal') $addr = 'null';
		return Taskmanager::submit('MountVmStore', array(
			'address' => $addr,
			'type' => 'images',
			'username' => $vmstore['cifsuser'],
			'password' => $vmstore['cifspasswd']
		));
	}

}