module Adminix module Helpers class Systemctl TMP_SCRIPT_PATH = '/tmp/write_systemctl_script.sh'.freeze def initialize(opts = {}) @adminix_bin = opts[:adminix_bin] || Helpers::Command.which('adminix') @user = opts[:user] || Helpers::Command.whoami @group = opts[:group] || Helpers::Command.whoami @working_dir = opts[:working_dir] || Helpers::Command.home @systemctl_path = Adminix.config.systemctl[:watcher_service_path] end def write Helpers::Files.rm(TMP_SCRIPT_PATH) Helpers::Files.write_plain_file(TMP_SCRIPT_PATH, to_s) Helpers::Output.display_multiline_message([ "sudo sh #{TMP_SCRIPT_PATH}", '# Run this command to configure your shell:', '# eval $(adminix write_systemctl)' ]) end def to_s "#!/bin/bash\n" \ "touch #{@systemctl_path}\n" \ "cat << EOT > #{@systemctl_path}\n" \ "[Unit]\n" \ "Description=Adminix\n" \ "Requires=network.target\n\n" \ "[Service]\n" \ "Type=simple\n" \ "User=#{@user}\n" \ "Group=#{@group}\n" \ "WorkingDirectory=#{@working_dir}\n" \ "ExecStart=#{@adminix_bin} watch\n" \ "Restart=always\n" \ "RestartSec=5\n" \ "StandardInput=null\n" \ "StandardOutput=syslog\n" \ "StandardError=syslog\n" \ "SyslogIdentifier=%n\n" \ "KillMode=mixed\n" \ "TimeoutStopSec=5\n" \ "TimeoutSec=300\n\n" \ "[Install]\n" \ "WantedBy=multi-user.target\n" \ "EOT\n" \ "# Run this command to configure your shell:\n" \ '# eval $(adminix write_systemctl)' end end end end