summaryrefslogtreecommitdiff
path: root/hscript/meta.cc
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2019-10-12 09:48:49 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2019-10-12 09:48:49 -0500
commiteaad2886ee1c2fff25f473e4ab933e53ef959beb (patch)
tree9c2ca8cb00b9ddcdc2aa14199c7dba24ecf85240 /hscript/meta.cc
parent6a2bbf81ae81a409565f55a825d330cd63772087 (diff)
downloadhorizon-eaad2886ee1c2fff25f473e4ab933e53ef959beb.tar.gz
horizon-eaad2886ee1c2fff25f473e4ab933e53ef959beb.tar.bz2
horizon-eaad2886ee1c2fff25f473e4ab933e53ef959beb.tar.xz
horizon-eaad2886ee1c2fff25f473e4ab933e53ef959beb.zip
hscript: Add hostname execution
Diffstat (limited to 'hscript/meta.cc')
-rw-r--r--hscript/meta.cc53
1 files changed, 50 insertions, 3 deletions
diff --git a/hscript/meta.cc b/hscript/meta.cc
index 33a9326..6bf4587 100644
--- a/hscript/meta.cc
+++ b/hscript/meta.cc
@@ -10,7 +10,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
+#include <fstream>
#include <regex>
+#include <unistd.h>
#include "meta.hh"
#include "util/output.hh"
@@ -63,9 +65,54 @@ bool Hostname::validate(ScriptOptions) const {
return !any_failure;
}
-bool Hostname::execute(ScriptOptions) const {
- /* Write the hostname to /etc/hostname in the target environment. */
- return false;
+bool Hostname::execute(ScriptOptions opts) const {
+ /* Set the hostname of the target computer */
+ std::string actual;
+
+ if(this->_value.size() > 64) {
+ /* Linux has a nodename limit of 64 characters.
+ * That's fine, because we have a limit of 64 chars per segment.
+ * Assuming a dot is present, just chop at the first dot. */
+ std::string::size_type dot = this->_value.find_first_of('.');
+ if(dot == std::string::npos) {
+ output_error("installfile:" + std::to_string(this->lineno()),
+ "hostname: nodename too long",
+ "Linux requires nodename to be <= 64 characters.");
+ return false;
+ }
+ std::copy_n(this->_value.cbegin(), dot, actual.begin());
+ } else {
+ actual = this->_value;
+ }
+
+ /* Runner.Execute.hostname. */
+ if(opts.test(Simulate)) {
+ output_info("installfile:" + std::to_string(this->lineno()),
+ "hostname: set hostname to '" + actual + "'");
+ } else {
+ if(sethostname(actual.c_str(), actual.size()) == -1) {
+ output_error("installfile:" + std::to_string(this->lineno()),
+ "hostname: failed to set host name",
+ std::string(strerror(errno)));
+ return false;
+ }
+ }
+
+ /* Runner.Execute.hostname.Write. */
+ if(opts.test(Simulate)) {
+ output_info("installfile:" + std::to_string(this->lineno()),
+ "hostname: write '" + actual + "' to /etc/hostname");
+ } else {
+ std::ofstream hostname_f("/target/etc/hostname");
+ if(!hostname_f) {
+ output_error("installfile:" + std::to_string(this->lineno()),
+ "hostname: could not open /etc/hostname for writing");
+ return false;
+ }
+ hostname_f << actual;
+ }
+
+ return true;
}