diff --git a/application/views/scripts/setup/parts/install.phtml b/application/views/scripts/setup/parts/install.phtml index 0c102808b..ce8262500 100644 --- a/application/views/scripts/setup/parts/install.phtml +++ b/application/views/scripts/setup/parts/install.phtml @@ -1,12 +1,17 @@
+ - state ? ' class="error"' : ''; ?>> - message; ?> -

+ + +
+ + + + -

+

diff --git a/library/Icinga/Application/Installation/AuthenticationStep.php b/library/Icinga/Application/Installation/AuthenticationStep.php new file mode 100644 index 000000000..4bea703a9 --- /dev/null +++ b/library/Icinga/Application/Installation/AuthenticationStep.php @@ -0,0 +1,133 @@ +data = $data; + } + + public function apply() + { + $success = $this->createAuthenticationIni(); + if (isset($this->data['adminAccountData']['resourceConfig'])) { + $success &= $this->createAccount(); + } + + $success &= $this->defineInitialAdmin(); + return $success; + } + + protected function createAuthenticationIni() + { + $config = array(); + $backendConfig = $this->data['backendConfig']; + $backendName = $backendConfig['name']; + unset($backendConfig['name']); + $config[$backendName] = $backendConfig; + if (isset($this->data['resourceName'])) { + $config[$backendName]['resource'] = $this->data['resourceName']; + } + + try { + $writer = new PreservingIniWriter(array( + 'config' => new Zend_Config($config), + 'filename' => Config::resolvePath('authentication.ini'), + 'filemode' => octdec($this->data['fileMode']) + )); + $writer->write(); + } catch (Exception $e) { + $this->authIniError = $e; + return false; + } + + $this->authIniError = false; + return true; + } + + protected function defineInitialAdmin() + { + $this->permIniError = false; + return true; + } + + protected function createAccount() + { + try { + $backend = new DbUserBackend( + ResourceFactory::createResource(new Zend_Config($this->data['adminAccountData']['resourceConfig'])) + ); + + if (array_search($this->data['adminAccountData']['username'], $backend->listUsers()) === false) { + $backend->addUser( + $this->data['adminAccountData']['username'], + $this->data['adminAccountData']['password'] + ); + } + } catch (Exception $e) { + $this->dbError = $e; + return false; + } + + $this->dbError = false; + return true; + } + + public function getSummary() + { + return ''; + } + + public function getReport() + { + $report = ''; + if ($this->authIniError === false) { + $message = t('Authentication configuration has been successfully written to: %s'); + $report .= '

' . sprintf($message, Config::resolvePath('authentication.ini')) . '

'; + } elseif ($this->authIniError !== null) { + $message = t('Authentication configuration could not be written to: %s; An error occured:'); + $report .= '

' . sprintf($message, Config::resolvePath('authentication.ini')) . '

' + . '

' . $this->authIniError->getMessage() . '

'; + } + + if ($this->dbError === false) { + $message = t('Account "%s" has been successfully created.'); + $report .= '

' . sprintf($message, $this->data['adminAccountData']['username']) . '

'; + } elseif ($this->dbError !== null) { + $message = t('Unable to create account "%s". An error occured:'); + $report .= '

' . sprintf($message, $this->data['adminAccountData']['username']) . '

' + . '

' . $this->dbError->getMessage() . '

'; + } + + if ($this->permIniError === false) { + $message = t('Account "%s" has been successfully defined as initial administrator.'); + $report .= '

' . sprintf($message, $this->data['adminAccountData']['username']) . '

'; + } elseif ($this->permIniError !== null) { + $message = t('Unable to define account "%s" as initial administrator. An error occured:'); + $report .= '

' . sprintf($message, $this->data['adminAccountData']['username']) . '

' + . '

' . $this->permIniError->getMessage() . '

'; + } + + return $report; + } +} diff --git a/library/Icinga/Application/Installation/DatabaseStep.php b/library/Icinga/Application/Installation/DatabaseStep.php new file mode 100644 index 000000000..170d6ff3f --- /dev/null +++ b/library/Icinga/Application/Installation/DatabaseStep.php @@ -0,0 +1,176 @@ +data = $data; + $this->messages = array(); + } + + public function apply() + { + $resourceConfig = $this->data['resourceConfig']; + if (isset($this->data['adminName'])) { + $resourceConfig['username'] = $this->data['adminName']; + if (isset($this->data['adminPassword'])) { + $resourceConfig['password'] = $this->data['adminPassword']; + } + } + + $db = new DbTool($resourceConfig); + + try { + if ($resourceConfig['db'] === 'mysql') { + $this->setupMysqlDatabase($db); + } elseif ($resourceConfig['db'] === 'pgsql') { + $this->setupPgsqlDatabase($db); + } + } catch (Exception $e) { + $this->error = $e; + throw new InstallException(); + } + + $this->error = false; + return true; + } + + protected function setupMysqlDatabase(DbTool $db) + { + try { + $db->connectToDb(); + $this->log( + t('Successfully connected to existing database "%s"...'), + $this->data['resourceConfig']['dbname'] + ); + } catch (PDOException $e) { + $db->connectToHost(); + $this->log(t('Creating new database "%s"...'), $this->data['resourceConfig']['dbname']); + $db->exec('CREATE DATABASE ' . $db->quoteIdentifier($this->data['resourceConfig']['dbname'])); + $db->reconnect($this->data['resourceConfig']['dbname']); + } + + if ($db->hasLogin($this->data['resourceConfig']['username'])) { + $this->log(t('Login "%s" already exists...'), $this->data['resourceConfig']['username']); + } else { + $this->log(t('Creating login "%s"...'), $this->data['resourceConfig']['username']); + $db->addLogin($this->data['resourceConfig']['username'], $this->data['resourceConfig']['password']); + } + + if (array_search('account', $db->listTables()) !== false) { + $this->log(t('Database schema already exists...')); + } else { + $this->log(t('Creating database schema...')); + $db->import(Icinga::app()->getApplicationDir() . '/../etc/schema/mysql.sql'); + } + + $privileges = array('SELECT', 'INSERT', 'UPDATE', 'DELETE', 'EXECUTE', 'CREATE TEMPORARY TABLES'); + if ($db->checkPrivileges(array_merge($privileges, array('GRANT OPTION')))) { + $this->log(t('Granting required privileges to login "%s"...'), $this->data['resourceConfig']['username']); + $db->exec(sprintf( + "GRANT %s ON %s.* TO %s@%s", + join(',', $privileges), + $db->quoteIdentifier($this->data['resourceConfig']['dbname']), + $db->quoteIdentifier($this->data['resourceConfig']['username']), + $db->quoteIdentifier(Platform::getFqdn()) + )); + } else { + $this->log( + t('Required privileges were already granted to login "%s".'), + $this->data['resourceConfig']['username'] + ); + } + } + + protected function setupPgsqlDatabase(DbTool $db) + { + try { + $db->connectToDb(); + $this->log( + t('Successfully connected to existing database "%s"...'), + $this->data['resourceConfig']['dbname'] + ); + } catch (PDOException $e) { + $db->connectToHost(); + $this->log(t('Creating new database "%s"...'), $this->data['resourceConfig']['dbname']); + $db->exec('CREATE DATABASE ' . $db->quoteIdentifier($this->data['resourceConfig']['dbname'])); + $db->reconnect($this->data['resourceConfig']['dbname']); + } + + if ($db->hasLogin($this->data['resourceConfig']['username'])) { + $this->log(t('Login "%s" already exists...'), $this->data['resourceConfig']['username']); + } else { + $this->log(t('Creating login "%s"...'), $this->data['resourceConfig']['username']); + $db->addLogin($this->data['resourceConfig']['username'], $this->data['resourceConfig']['password']); + } + + if (array_search('account', $db->listTables()) !== false) { + $this->log(t('Database schema already exists...')); + } else { + $this->log(t('Creating database schema...')); + $db->import(Icinga::app()->getApplicationDir() . '/../etc/schema/pgsql.sql'); + } + + $privileges = array('SELECT', 'INSERT', 'UPDATE', 'DELETE'); + if ($db->checkPrivileges(array_merge($privileges, array('GRANT OPTION')))) { + $this->log(t('Granting required privileges to login "%s"...'), $this->data['resourceConfig']['username']); + $db->exec(sprintf( + "GRANT %s ON TABLE account TO %s", + join(',', $privileges), + $db->quoteIdentifier($this->data['resourceConfig']['username']) + )); + $db->exec(sprintf( + "GRANT %s ON TABLE preference TO %s", + join(',', $privileges), + $db->quoteIdentifier($this->data['resourceConfig']['username']) + )); + } else { + $this->log( + t('Required privileges were already granted to login "%s".'), + $this->data['resourceConfig']['username'] + ); + } + } + + public function getSummary() + { + return ''; + } + + public function getReport() + { + if ($this->error === false) { + return '

' . join('

', $this->messages) . '

' + . '

' . t('The database has been fully set up!') . '

'; + } elseif ($this->error !== null) { + $message = t('Failed to fully setup the database. An error occured:'); + return '

' . join('

', $this->messages) . '

' + . '

' . $message . '

' . $this->error->getMessage() . '

'; + } + + return ''; + } + + protected function log() + { + $this->messages[] = call_user_func_array('sprintf', func_get_args()); + } +} diff --git a/library/Icinga/Application/Installation/GeneralConfigStep.php b/library/Icinga/Application/Installation/GeneralConfigStep.php new file mode 100644 index 000000000..7277d3f1e --- /dev/null +++ b/library/Icinga/Application/Installation/GeneralConfigStep.php @@ -0,0 +1,71 @@ +data = $data; + } + + public function apply() + { + $config = array(); + foreach ($this->data['generalConfig'] as $sectionAndPropertyName => $value) { + list($section, $property) = explode('_', $sectionAndPropertyName); + $config[$section][$property] = $value; + } + + $config['preferences']['type'] = $this->data['preferencesType']; + if (isset($this->data['preferencesResource'])) { + $config['preferences']['resource'] = $this->data['preferencesResource']; + } + + try { + $writer = new PreservingIniWriter(array( + 'config' => new Zend_Config($config), + 'filename' => Config::resolvePath('config.ini'), + 'filemode' => octdec($this->data['fileMode']) + )); + $writer->write(); + } catch (Exception $e) { + $this->error = $e; + return false; + } + + $this->error = false; + return true; + } + + public function getSummary() + { + return ''; + } + + public function getReport() + { + if ($this->error === false) { + $message = t('General configuration has been successfully written to: %s'); + return '

' . sprintf($message, Config::resolvePath('config.ini')) . '

'; + } elseif ($this->error !== null) { + $message = t('General configuration could not be written to: %s; An error occured:'); + return '

' . sprintf($message, Config::resolvePath('config.ini')) . '

' + . '

' . $this->error->getMessage() . '

'; + } + + return ''; + } +} diff --git a/library/Icinga/Application/Installation/ResourceStep.php b/library/Icinga/Application/Installation/ResourceStep.php new file mode 100644 index 000000000..704203ff6 --- /dev/null +++ b/library/Icinga/Application/Installation/ResourceStep.php @@ -0,0 +1,75 @@ +data = $data; + } + + public function apply() + { + $resourceConfig = array(); + if (isset($this->data['dbResourceConfig'])) { + $dbConfig = $this->data['dbResourceConfig']; + $resourceName = $dbConfig['name']; + unset($dbConfig['name']); + $resourceConfig[$resourceName] = $dbConfig; + } + + if (isset($this->data['ldapResourceConfig'])) { + $ldapConfig = $this->data['ldapResourceConfig']; + $resourceName = $ldapConfig['name']; + unset($ldapConfig['name']); + $resourceConfig[$resourceName] = $ldapConfig; + } + + try { + $writer = new PreservingIniWriter(array( + 'config' => new Zend_Config($resourceConfig), + 'filename' => Config::resolvePath('resources.ini'), + 'filemode' => octdec($this->data['fileMode']) + )); + $writer->write(); + } catch (Exception $e) { + $this->error = $e; + return false; + } + + $this->error = false; + return true; + } + + public function getSummary() + { + return ''; + } + + public function getReport() + { + if ($this->error === false) { + $message = t('Resource configuration has been successfully written to: %s'); + return '

' . sprintf($message, Config::resolvePath('resources.ini')) . '

'; + } elseif ($this->error !== null) { + $message = t('Resource configuration could not be written to: %s; An error occured:'); + return '

' . sprintf($message, Config::resolvePath('resources.ini')) . '

' + . '

' . $this->error->getMessage() . '

'; + } + + return ''; + } +} diff --git a/library/Icinga/Application/WebInstaller.php b/library/Icinga/Application/WebInstaller.php deleted file mode 100644 index 8c64b95a2..000000000 --- a/library/Icinga/Application/WebInstaller.php +++ /dev/null @@ -1,487 +0,0 @@ -pageData = $pageData; - $this->report = array(); - } - - /** - * @see Installer::run() - */ - public function run() - { - $success = true; - - if (isset($this->pageData['setup_db_resource']) - && ! $this->pageData['setup_db_resource']['skip_validation'] - && (false === isset($this->pageData['setup_database_creation']) - || ! $this->pageData['setup_database_creation']['skip_validation'] - ) - ) { - try { - $this->setupDatabase(); - } catch (Exception $e) { - $this->log(sprintf(t('Failed to set up the database: %s'), $e->getMessage()), false); - return false; // Bail out as early as possible as not being able to setup the database is fatal - } - - $this->log(t('The database has been successfully set up!')); - } - - $configIniPath = Config::resolvePath('config.ini'); - try { - $this->writeConfigIni($configIniPath); - $this->log(sprintf(t('Successfully created: %s'), $configIniPath)); - } catch (Exception $e) { - $success = false; - $this->log(sprintf(t('Unable to create: %s (%s)'), $configIniPath, $e->getMessage()), false); - } - - $resourcesIniPath = Config::resolvePath('resources.ini'); - try { - $this->writeResourcesIni($resourcesIniPath); - $this->log(sprintf(t('Successfully created: %s'), $resourcesIniPath)); - } catch (Exception $e) { - $success = false; - $this->log(sprintf(t('Unable to create: %s (%s)'), $resourcesIniPath, $e->getMessage()), false); - } - - $authenticationIniPath = Config::resolvePath('authentication.ini'); - try { - $this->writeAuthenticationIni($authenticationIniPath); - $this->log(sprintf(t('Successfully created: %s'), $authenticationIniPath)); - } catch (Exception $e) { - $success = false; - $this->log(sprintf(t('Unable to create: %s (%s)'), $authenticationIniPath, $e->getMessage()), false); - } - - try { - $this->setupAdminAccount(); - $this->log(t('Successfully defined initial administrative account.')); - } catch (Exception $e) { - $success = false; - $this->log(sprintf(t('Failed to define initial administrative account: %s'), $e->getMessage()), false); - } - - return $success; - } - - /** - * Write application configuration to the given filepath - * - * @param string $configPath - */ - protected function writeConfigIni($configPath) - { - $preferencesConfig = array(); - $preferencesConfig['type'] = $this->pageData['setup_preferences_type']['type']; - if ($this->pageData['setup_preferences_type']['type'] === 'db') { - $preferencesConfig['resource'] = $this->pageData['setup_db_resource']['name']; - } - - $loggingConfig = array(); - $loggingConfig['log'] = $this->pageData['setup_general_config']['logging_log']; - if ($this->pageData['setup_general_config']['logging_log'] !== 'none') { - $loggingConfig['level'] = $this->pageData['setup_general_config']['logging_level']; - if ($this->pageData['setup_general_config']['logging_log'] === 'syslog') { - $loggingConfig['application'] = $this->pageData['setup_general_config']['logging_application']; - //$loggingConfig['facility'] = $this->pageData['setup_general_config']['logging_facility']; - } else { // $this->pageData['setup_general_config']['logging_log'] === 'file' - $loggingConfig['file'] = $this->pageData['setup_general_config']['logging_file']; - } - } - - $config = array( - 'global' => array( - 'modulepath' => $this->pageData['setup_general_config']['global_modulePath'], - 'filemode' => $this->pageData['setup_general_config']['global_filemode'] - ), - 'preferences' => $preferencesConfig, - 'logging' => $loggingConfig - ); - - $writer = new PreservingIniWriter(array( - 'config' => new Zend_Config($config), - 'filename' => $configPath, - 'filemode' => octdec($this->pageData['setup_general_config']['global_filemode']) - )); - $writer->write(); - } - - /** - * Write resource configuration to the given filepath - * - * @param string $configPath - */ - protected function writeResourcesIni($configPath) - { - $resourceConfig = array(); - if (isset($this->pageData['setup_db_resource'])) { - $resourceConfig[$this->pageData['setup_db_resource']['name']] = array( - 'type' => $this->pageData['setup_db_resource']['type'], - 'db' => $this->pageData['setup_db_resource']['db'], - 'host' => $this->pageData['setup_db_resource']['host'], - 'port' => $this->pageData['setup_db_resource']['port'], - 'dbname' => $this->pageData['setup_db_resource']['dbname'], - 'username' => $this->pageData['setup_db_resource']['username'], - 'password' => $this->pageData['setup_db_resource']['password'] - ); - } - - if (isset($this->pageData['setup_ldap_resource'])) { - $resourceConfig[$this->pageData['setup_ldap_resource']['name']] = array( - 'type' => $this->pageData['setup_ldap_resource']['type'], - 'hostname' => $this->pageData['setup_ldap_resource']['hostname'], - 'port' => $this->pageData['setup_ldap_resource']['port'], - 'root_dn' => $this->pageData['setup_ldap_resource']['root_dn'], - 'bind_dn' => $this->pageData['setup_ldap_resource']['bind_dn'], - 'bind_pw' => $this->pageData['setup_ldap_resource']['bind_pw'] - ); - } - - if (empty($resourceConfig)) { - return; // No need to write nothing :) - } - - $writer = new PreservingIniWriter(array( - 'config' => new Zend_Config($resourceConfig), - 'filename' => $configPath, - 'filemode' => octdec($this->pageData['setup_general_config']['global_filemode']) - )); - $writer->write(); - } - - /** - * Write authentication backend configuration to the given filepath - * - * @param string $configPath - */ - protected function writeAuthenticationIni($configPath) - { - $backendConfig = array(); - if ($this->pageData['setup_authentication_type']['type'] === 'db') { - $backendConfig[$this->pageData['setup_authentication_backend']['name']] = array( - 'backend' => $this->pageData['setup_authentication_backend']['backend'], - 'resource' => $this->pageData['setup_db_resource']['name'] - ); - } elseif ($this->pageData['setup_authentication_type']['type'] === 'ldap') { - $backendConfig[$this->pageData['setup_authentication_backend']['backend']] = array( - 'backend' => $this->pageData['setup_authentication_backend']['backend'], - 'resource' => $this->pageData['setup_ldap_resource']['name'], - 'base_dn' => $this->pageData['setup_authentication_backend']['base_dn'], - 'user_class' => $this->pageData['setup_authentication_backend']['user_class'], - 'user_name_attribute' => $this->pageData['setup_authentication_backend']['user_name_attribute'] - ); - } else { // $this->pageData['setup_authentication_type']['type'] === 'autologin' - $backendConfig[$this->pageData['setup_authentication_backend']['name']] = array( - 'backend' => $this->pageData['setup_authentication_backend']['backend'], - 'strip_username_regexp' => $this->pageData['setup_authentication_backend']['strip_username_regexp'] - ); - } - - $writer = new PreservingIniWriter(array( - 'config' => new Zend_Config($backendConfig), - 'filename' => $configPath, - 'filemode' => octdec($this->pageData['setup_general_config']['global_filemode']) - )); - $writer->write(); - } - - /** - * Setup the database - */ - protected function setupDatabase() - { - $resourceConfig = $this->pageData['setup_db_resource']; - if (isset($this->pageData['setup_database_creation'])) { - $resourceConfig['username'] = $this->pageData['setup_database_creation']['username']; - $resourceConfig['password'] = $this->pageData['setup_database_creation']['password']; - } - - $db = new DbTool($resourceConfig); - if ($resourceConfig['db'] === 'mysql') { - $this->setupMysqlDatabase($db); - } elseif ($resourceConfig['db'] === 'pgsql') { - $this->setupPgsqlDatabase($db); - } - } - - /** - * Setup a MySQL database - * - * @param DbTool $db The database connection wrapper to use - */ - private function setupMysqlDatabase(DbTool $db) - { - try { - $db->connectToDb(); - $this->log(sprintf( - t('Successfully connected to existing database "%s"...'), - $this->pageData['setup_db_resource']['dbname'] - )); - } catch (PDOException $e) { - $db->connectToHost(); - $this->log(sprintf( - t('Creating new database "%s"...'), - $this->pageData['setup_db_resource']['dbname'] - )); - $db->exec('CREATE DATABASE ' . $db->quoteIdentifier($this->pageData['setup_db_resource']['dbname'])); - $db->reconnect($this->pageData['setup_db_resource']['dbname']); - } - - if ($db->hasLogin($this->pageData['setup_db_resource']['username'])) { - $this->log(sprintf( - t('Login "%s" already exists...'), - $this->pageData['setup_db_resource']['username'] - )); - } else { - $this->log(sprintf( - t('Creating login "%s"...'), - $this->pageData['setup_db_resource']['username'] - )); - $db->addLogin( - $this->pageData['setup_db_resource']['username'], - $this->pageData['setup_db_resource']['password'] - ); - } - - if (array_search('account', $db->listTables()) !== false) { - $this->log(t('Database schema already exists...')); - } else { - $this->log(t('Creating database schema...')); - $db->import(Icinga::app()->getApplicationDir() . '/../etc/schema/mysql.sql'); - } - - $privileges = array('SELECT', 'INSERT', 'UPDATE', 'DELETE', 'EXECUTE', 'CREATE TEMPORARY TABLES'); - if ($db->checkPrivileges(array_merge($privileges, array('GRANT OPTION')))) { - $this->log(sprintf( - t('Granting required privileges to login "%s"...'), - $this->pageData['setup_db_resource']['username'] - )); - $db->exec(sprintf( - "GRANT %s ON %s.* TO %s@%s", - join(',', $privileges), - $db->quoteIdentifier($this->pageData['setup_db_resource']['dbname']), - $db->quoteIdentifier($this->pageData['setup_db_resource']['username']), - $db->quoteIdentifier(Platform::getFqdn()) - )); - } - } - - /** - * Setup a PostgreSQL database - * - * @param DbTool $db The database connection wrapper to use - */ - private function setupPgsqlDatabase(DbTool $db) - { - try { - $db->connectToDb(); - $this->log(sprintf( - t('Successfully connected to existing database "%s"...'), - $this->pageData['setup_db_resource']['dbname'] - )); - } catch (PDOException $e) { - $db->connectToHost(); - $this->log(sprintf( - t('Creating new database "%s"...'), - $this->pageData['setup_db_resource']['dbname'] - )); - $db->exec('CREATE DATABASE ' . $db->quoteIdentifier($this->pageData['setup_db_resource']['dbname'])); - $db->reconnect($this->pageData['setup_db_resource']['dbname']); - } - - if ($db->hasLogin($this->pageData['setup_db_resource']['username'])) { - $this->log(sprintf( - t('Login "%s" already exists...'), - $this->pageData['setup_db_resource']['username'] - )); - } else { - $this->log(sprintf( - t('Creating login "%s"...'), - $this->pageData['setup_db_resource']['username'] - )); - $db->addLogin( - $this->pageData['setup_db_resource']['username'], - $this->pageData['setup_db_resource']['password'] - ); - } - - if (array_search('account', $db->listTables()) !== false) { - $this->log(t('Database schema already exists...')); - } else { - $this->log(t('Creating database schema...')); - $db->import(Icinga::app()->getApplicationDir() . '/../etc/schema/pgsql.sql'); - } - - $privileges = array('SELECT', 'INSERT', 'UPDATE', 'DELETE'); - if ($db->checkPrivileges(array_merge($privileges, array('GRANT OPTION')))) { - $this->log(sprintf( - t('Granting required privileges to login "%s"...'), - $this->pageData['setup_db_resource']['username'] - )); - $db->exec(sprintf( - "GRANT %s ON TABLE account TO %s", - join(',', $privileges), - $db->quoteIdentifier($this->pageData['setup_db_resource']['username']) - )); - $db->exec(sprintf( - "GRANT %s ON TABLE preference TO %s", - join(',', $privileges), - $db->quoteIdentifier($this->pageData['setup_db_resource']['username']) - )); - } - } - - /** - * Define the initial administrative account - */ - protected function setupAdminAccount() - { - if ($this->pageData['setup_admin_account']['user_type'] === 'new_user' - && ! $this->pageData['setup_db_resource']['skip_validation'] - && (false === isset($this->pageData['setup_database_creation']) - || ! $this->pageData['setup_database_creation']['skip_validation'] - ) - ) { - $backend = new DbUserBackend( - ResourceFactory::createResource(new Zend_Config($this->pageData['setup_db_resource'])) - ); - - if (array_search($this->pageData['setup_admin_account']['new_user'], $backend->listUsers()) === false) { - $backend->addUser( - $this->pageData['setup_admin_account']['new_user'], - $this->pageData['setup_admin_account']['new_user_password'] - ); - } - } - } - - /** - * @see Installer::getSummary() - */ - public function getSummary() - { - $summary = $this->pageData; - if (isset($this->pageData['setup_db_resource'])) { - $resourceConfig = $this->pageData['setup_db_resource']; - if (isset($this->pageData['setup_database_creation'])) { - $resourceConfig['username'] = $this->pageData['setup_database_creation']['username']; - $resourceConfig['password'] = $this->pageData['setup_database_creation']['password']; - } - - $db = new DbTool($resourceConfig); - try { - $db->connectToDb(); - if (array_search('account', $db->listTables()) === false) { - $message = sprintf( - t( - 'The database user "%s" will be used to setup the missing' - . ' schema required by Icinga Web 2 in database "%s".' - ), - $resourceConfig['username'], - $resourceConfig['dbname'] - ); - } else { - $message = sprintf( - t('The database "%s" already seems to be fully set up. No action required.'), - $resourceConfig['dbname'] - ); - } - } catch (PDOException $e) { - try { - $db->connectToHost(); - if ($db->hasLogin($this->pageData['setup_db_resource']['username'])) { - $message = sprintf( - t( - 'The database user "%s" will be used to create the missing ' - . 'database "%s" with the schema required by Icinga Web 2.' - ), - $resourceConfig['username'], - $resourceConfig['dbname'] - ); - } else { - $message = sprintf( - t( - 'The database user "%s" will be used to create the missing database "%s" ' - . 'with the schema required by Icinga Web 2 and a new login called "%s".' - ), - $resourceConfig['username'], - $resourceConfig['dbname'], - $this->pageData['setup_db_resource']['username'] - ); - } - } catch (PDOException $e) { - $message = t( - 'No connection to database host possible. You\'ll need to setup the' - . ' database with the schema required by Icinga Web 2 manually.' - ); - } - } - - $summary['database_info'] = $message; - } - - return $summary; - } - - /** - * @see Installer::getReport() - */ - public function getReport() - { - return $this->report; - } - - /** - * Add a message to the report - * - * @param string $message The message to add - * @param bool $success Whether the message represents a success (true) or a failure (false) - */ - protected function log($message, $success = true) - { - $this->report[] = (object) array( - 'state' => (bool) $success, - 'message' => $message - ); - } -} diff --git a/library/Icinga/Application/WebSetup.php b/library/Icinga/Application/WebSetup.php index a8b6575da..64356f52f 100644 --- a/library/Icinga/Application/WebSetup.php +++ b/library/Icinga/Application/WebSetup.php @@ -18,10 +18,15 @@ use Icinga\Form\Setup\RequirementsPage; use Icinga\Form\Setup\GeneralConfigPage; use Icinga\Form\Setup\AuthenticationPage; use Icinga\Form\Setup\DatabaseCreationPage; +use Icinga\Application\Installation\DatabaseStep; +use Icinga\Application\Installation\GeneralConfigStep; +use Icinga\Application\Installation\ResourceStep; +use Icinga\Application\Installation\AuthenticationStep; use Icinga\Web\Form; use Icinga\Web\Wizard; use Icinga\Web\Request; use Icinga\Web\Setup\DbTool; +use Icinga\Web\Setup\Installer; use Icinga\Web\Setup\SetupWizard; use Icinga\Web\Setup\Requirements; @@ -221,7 +226,74 @@ class WebSetup extends Wizard implements SetupWizard */ public function getInstaller() { - return new WebInstaller($this->getPageData()); + $pageData = $this->getPageData(); + $installer = new Installer(); + + if (isset($pageData['setup_db_resource']) + && ! $pageData['setup_db_resource']['skip_validation'] + && (false === isset($pageData['setup_database_creation']) + || ! $pageData['setup_database_creation']['skip_validation'] + ) + ) { + $installer->addStep( + new DatabaseStep(array( + 'resourceConfig' => $pageData['setup_db_resource'], + 'adminName' => isset($pageData['setup_database_creation']['username']) + ? $pageData['setup_database_creation']['username'] + : null, + 'adminPassword' => isset($pageData['setup_database_creation']['password']) + ? $pageData['setup_database_creation']['password'] + : null + )) + ); + } + + $installer->addStep( + new GeneralConfigStep(array( + 'generalConfig' => $pageData['setup_general_config'], + 'preferencesType' => $pageData['setup_preferences_type']['type'], + 'preferencesResource' => $pageData['setup_db_resource']['name'], + 'fileMode' => $pageData['setup_general_config']['global_filemode'] + )) + ); + + if (isset($pageData['setup_db_resource']) || isset($pageData['setup_ldap_resource'])) { + $installer->addStep( + new ResourceStep(array( + 'fileMode' => $pageData['setup_general_config']['global_filemode'], + 'dbResourceConfig' => isset($pageData['setup_db_resource']) + ? array_diff_key($pageData['setup_db_resource'], array('skip_validation' => null)) + : null, + 'ldapResourceConfig' => isset($pageData['setup_ldap_resource']) + ? array_diff_key($pageData['setup_ldap_resource'], array('skip_validation' => null)) + : null + )) + ); + } + + $adminAccountType = $pageData['setup_admin_account']['user_type']; + $adminAccountData = array('username' => $pageData['setup_admin_account'][$adminAccountType]); + if ($adminAccountType === 'new_user' && ! $pageData['setup_db_resource']['skip_validation'] + && (false === isset($pageData['setup_database_creation']) + || ! $pageData['setup_database_creation']['skip_validation'] + ) + ) { + $adminAccountData['resourceConfig'] = $pageData['setup_db_resource']; + $adminAccountData['password'] = $pageData['setup_admin_account']['new_user_password']; + } + $authType = $pageData['setup_authentication_type']['type']; + $installer->addStep( + new AuthenticationStep(array( + 'adminAccountData' => $adminAccountData, + 'fileMode' => $pageData['setup_general_config']['global_filemode'], + 'backendConfig' => $pageData['setup_authentication_backend'], + 'resourceName' => $authType === 'db' ? $pageData['setup_db_resource']['name'] : ( + $authType === 'ldap' ? $pageData['setup_ldap_resource']['name'] : null + ) + )) + ); + + return $installer; } /** diff --git a/library/Icinga/Exception/InstallException.php b/library/Icinga/Exception/InstallException.php new file mode 100644 index 000000000..0f14533d5 --- /dev/null +++ b/library/Icinga/Exception/InstallException.php @@ -0,0 +1,14 @@ +steps = array(); + } + + public function getIterator() + { + return new ArrayIterator($this->getSteps()); + } + + public function addStep(Step $step) + { + $this->steps[] = $step; + } + + public function addSteps(array $steps) + { + foreach ($steps as $step) { + $this->addStep($step); + } + } + + public function getSteps() + { + return $this->steps; + } + /** * Run the installation and return whether it succeeded * * @return bool */ - public function run(); + public function run() + { + $this->state = true; + + try { + foreach ($this->steps as $step) { + $this->state &= $step->apply(); + } + } catch (InstallException $e) { + $this->state = false; + } + + return $this->state; + } /** * Return a summary of all actions designated to run * - * @return array + * @return array An array of HTML strings */ - public function getSummary(); + public function getSummary() + { + $summaries = array(); + foreach ($this->steps as $step) { + $summaries[] = $step->getSummary(); + } + + return $summaries; + } /** * Return a report of all actions that were run * - * @return array + * @return array An array of HTML strings */ - public function getReport(); + public function getReport() + { + $reports = array(); + foreach ($this->steps as $step) { + $reports[] = $step->getReport(); + } + + return $reports; + } } diff --git a/library/Icinga/Web/Setup/Step.php b/library/Icinga/Web/Setup/Step.php new file mode 100644 index 000000000..44622c8b3 --- /dev/null +++ b/library/Icinga/Web/Setup/Step.php @@ -0,0 +1,32 @@ +