diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 00:44:14 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 00:44:14 -0500 |
commit | 1f0a1dec1af315e6465e1c1dd503aa942a85b1f4 (patch) | |
tree | e8464d9f1e12d3366fa181a2c1dc95a68e817b9c | |
parent | 47558ea71d523fb71307d394be4bfad8da4664d8 (diff) | |
download | horizon-1f0a1dec1af315e6465e1c1dd503aa942a85b1f4.tar.gz horizon-1f0a1dec1af315e6465e1c1dd503aa942a85b1f4.tar.bz2 horizon-1f0a1dec1af315e6465e1c1dd503aa942a85b1f4.tar.xz horizon-1f0a1dec1af315e6465e1c1dd503aa942a85b1f4.zip |
hscript: Pass opts to all validate()s, implement 'mount' validation
-rw-r--r-- | hscript/disk.cc | 14 | ||||
-rw-r--r-- | hscript/disk.hh | 4 | ||||
-rw-r--r-- | hscript/key.cc | 4 | ||||
-rw-r--r-- | hscript/key.hh | 9 | ||||
-rw-r--r-- | hscript/meta.cc | 4 | ||||
-rw-r--r-- | hscript/meta.hh | 8 | ||||
-rw-r--r-- | hscript/network.cc | 2 | ||||
-rw-r--r-- | hscript/network.hh | 2 | ||||
-rw-r--r-- | hscript/script.cc | 33 | ||||
-rw-r--r-- | hscript/user.cc | 4 | ||||
-rw-r--r-- | hscript/user.hh | 4 |
11 files changed, 56 insertions, 32 deletions
diff --git a/hscript/disk.cc b/hscript/disk.cc index 093c111..c0b0de9 100644 --- a/hscript/disk.cc +++ b/hscript/disk.cc @@ -12,6 +12,7 @@ #include <algorithm> #include <string> +#include <unistd.h> #include "disk.hh" #include "util/output.hh" @@ -63,10 +64,17 @@ Key *Mount::parseFromData(const std::string data, int lineno, int *errors, return new Mount(lineno, dev, where, opt); } -bool Mount::validate() const { - return false; +bool Mount::validate(ScriptOptions options) const { + /* We only validate if running in an Installation Environment. */ + if(!options.test(InstallEnvironment)) return true; + + /* XXX TODO: This will fail validation if the block device does not + * already exist. However, we must take in to account that block devices + * may not yet exist during the script validation phase. This check may + * need to happen in Script::validate like the Uniqueness tests. */ + return(access(this->mountpoint().c_str(), F_OK)); } -bool Mount::execute() const { +bool Mount::execute(ScriptOptions) const { return false; } diff --git a/hscript/disk.hh b/hscript/disk.hh index 475b1ab..36e1e1d 100644 --- a/hscript/disk.hh +++ b/hscript/disk.hh @@ -61,8 +61,8 @@ public: static Key *parseFromData(const std::string data, int lineno, int *errors, int *warnings); - bool validate() const override; - bool execute() const override; + bool validate(ScriptOptions) const override; + bool execute(ScriptOptions) const override; }; } diff --git a/hscript/key.cc b/hscript/key.cc index 6077e65..80ebfc6 100644 --- a/hscript/key.cc +++ b/hscript/key.cc @@ -35,12 +35,12 @@ bool Horizon::Keys::BooleanKey::parse(const std::string what, return true; } -bool Horizon::Keys::BooleanKey::validate() const { +bool Horizon::Keys::BooleanKey::validate(ScriptOptions) const { /* Key will fail init if it is not valid, so this is always a no-op. */ return true; } -bool Horizon::Keys::StringKey::validate() const { +bool Horizon::Keys::StringKey::validate(ScriptOptions) const { /* Key will fail init if it is not valid, so this is always a no-op. */ return true; } diff --git a/hscript/key.hh b/hscript/key.hh index 8b29212..aa8ed81 100644 --- a/hscript/key.hh +++ b/hscript/key.hh @@ -14,6 +14,7 @@ #define __HSCRIPT_KEY_HH_ #include <string> +#include "script.hh" namespace Horizon { namespace Keys { @@ -45,12 +46,12 @@ public: #undef UNUSED /*! Determines if the data associated with the Key is valid. */ - virtual bool validate() const = 0; + virtual bool validate(ScriptOptions) const = 0; /*! Executes the action associated with this Key. * @note Will always return `false` if `validate` is `false`. */ - virtual bool execute() const = 0; + virtual bool execute(ScriptOptions) const = 0; int lineno() const { return this->line; } }; @@ -83,7 +84,7 @@ public: bool test() const { return this->value; } /*! Key will fail to init if valid is invalid. */ - bool validate() const override; + bool validate(ScriptOptions) const override; }; @@ -100,7 +101,7 @@ public: /*! By default, key will be considered valid since it parsed. * This method should be overridden when further consideration is needed. */ - bool validate() const override; + bool validate(ScriptOptions) const override; }; } diff --git a/hscript/meta.cc b/hscript/meta.cc index 4d75819..84e026e 100644 --- a/hscript/meta.cc +++ b/hscript/meta.cc @@ -29,12 +29,12 @@ Key *Hostname::parseFromData(const std::string data, int lineno, int *errors, return new Hostname(lineno, data); } -bool Hostname::validate() const { +bool Hostname::validate(ScriptOptions) const { /* Validate that the name is a valid machine or DNS name */ return false; } -bool Hostname::execute() const { +bool Hostname::execute(ScriptOptions) const { /* Write the hostname to /etc/hostname in the target environment. */ return false; } diff --git a/hscript/meta.hh b/hscript/meta.hh index fadd837..3574a6f 100644 --- a/hscript/meta.hh +++ b/hscript/meta.hh @@ -27,8 +27,8 @@ private: public: static Key *parseFromData(const std::string data, int lineno, int *errors, int *warnings); - bool validate() const override; - bool execute() const override; + bool validate(ScriptOptions) const override; + bool execute(ScriptOptions) const override; }; class PkgInstall : public Key { @@ -40,8 +40,8 @@ public: static Key *parseFromData(const std::string data, int lineno, int *errors, int *warnings); const std::set<std::string> packages() const { return _pkgs; } - bool validate() const override { return true; } - bool execute() const override { return true; } + bool validate(ScriptOptions) const override { return true; } + bool execute(ScriptOptions) const override { return true; } }; diff --git a/hscript/network.cc b/hscript/network.cc index c494485..1c8edd1 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -25,6 +25,6 @@ Key *Network::parseFromData(const std::string data, int lineno, int *errors, return new Network(lineno, value); } -bool Network::execute() const { +bool Network::execute(ScriptOptions) const { return false; } diff --git a/hscript/network.hh b/hscript/network.hh index 52e41f9..f24dd0f 100644 --- a/hscript/network.hh +++ b/hscript/network.hh @@ -25,7 +25,7 @@ private: public: static Key *parseFromData(const std::string data, int lineno, int *errors, int *warnings); - bool execute() const override; + bool execute(ScriptOptions) const override; }; class NetAddress : public Key { diff --git a/hscript/script.cc b/hscript/script.cc index ddd6cd3..dbf845d 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -296,23 +296,38 @@ const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { bool Script::validate() const { int failures = 0; - if(!this->internal->network->validate()) failures++; - if(!this->internal->hostname->validate()) failures++; - if(!this->internal->rootpw->validate()) failures++; + std::set<std::string> seen_mounts; + + if(!this->internal->network->validate(this->opts)) failures++; + if(!this->internal->hostname->validate(this->opts)) failures++; + if(!this->internal->rootpw->validate(this->opts)) failures++; + for(auto &mount : this->internal->mounts) { - if(!mount->validate()) { + if(!mount->validate(this->opts)) { failures++; continue; } - /* TODO requirements to implement: - * Runner.Validate.mount.Unique. - * Runner.Validate.mount.Root. - */ + + /* Runner.Validate.mount.Unique */ + if(seen_mounts.find(mount->mountpoint()) != seen_mounts.end()) { + failures++; + output_error("installfile:" + std::to_string(mount->lineno()), + "mount: mountpoint " + mount->mountpoint() + + " has already been specified; " + mount->device() + + " is a duplicate", ""); + } + seen_mounts.insert(mount->mountpoint()); if(this->opts.test(InstallEnvironment)) { - /* TODO: Runner.Validate.mount.Block. */ + /* TODO: Runner.Validate.mount.Block for not-yet-created devs. */ } } + /* Runner.Validate.mount.Root */ + if(seen_mounts.find("/") == seen_mounts.end()) { + failures++; + output_error("installfile:0", "mount: no root mount specified", ""); + } + output_message("validator", "0", "installfile", std::to_string(failures) + " failure(s).", ""); return (failures == 0); diff --git a/hscript/user.cc b/hscript/user.cc index ec4a213..d02dcfc 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -27,10 +27,10 @@ Key *RootPassphrase::parseFromData(const std::string data, int lineno, return new RootPassphrase(lineno, data); } -bool RootPassphrase::validate() const { +bool RootPassphrase::validate(ScriptOptions) const { return false; } -bool RootPassphrase::execute() const { +bool RootPassphrase::execute(ScriptOptions) const { return false; } diff --git a/hscript/user.hh b/hscript/user.hh index 9bb1bd7..5b3750c 100644 --- a/hscript/user.hh +++ b/hscript/user.hh @@ -25,8 +25,8 @@ private: public: static Key *parseFromData(const std::string data, int lineno, int *errors, int *warnings); - bool validate() const override; - bool execute() const override; + bool validate(ScriptOptions) const override; + bool execute(ScriptOptions) const override; }; class Username : public StringKey { |