Top Level Namespace
Defined Under Namespace
Classes: State
Utilities collapse
-
#log(msg, args = {}) ⇒ Object
Prints a message to the STDOUT.
-
#package?(name) ⇒ Boolean
Utility function, returns true of package is installed.
-
#root? ⇒ Boolean
Checks if current user is the root.
-
#sudo(command) ⇒ Object
Runs the command with sudo if current user is not root.
Declarations: collapse
-
#aur(*names) ⇒ Object
aur command to install packages from aur on install step.
-
#copy(src, dest) ⇒ Object
Copy src inside dest during configure step, if src/.
-
#file(path, content) ⇒ Object
Write a file during configure step.
-
#git_clone(from:, to: nil) ⇒ Object
on prepare make sure a git repository is cloned to directory.
-
#github_clone(from:, to: nil) ⇒ Object
git clone for github repositories.
-
#hostname(name) ⇒ Object
Sets the machine hostname.
-
#keyboard(keymap: nil, layout: nil, model: nil, variant: nil, options: nil) ⇒ Object
set keyboard settings during prepare step.
-
#locale(value) ⇒ Object
Sets locale using localectl.
-
#mkdir(*path) ⇒ Object
on prepare make sure the directory exists.
-
#package(*names) ⇒ Object
Install a package on install step and remove packages not registered with this function.
-
#replace(file, pattern, replacement) ⇒ Object
Replace a regex pattern with replacement string in a file during configure step.
-
#service(*names) ⇒ Object
enable system service if root or user service if not during finalize step.
-
#symlink(target, link_name) ⇒ Object
link file to destination.
-
#timedate(timezone: 'UTC', ntp: true) ⇒ Object
set timezone and NTP settings during prepare step.
-
#timer(*names) ⇒ Object
enable system timer if root or user timer if not during finalize step.
-
#ufw(*allow) ⇒ Object
setup add ufw enable it and allow ports during configure step.
-
#user(name, groups: [], autologin: nil, &block) ⇒ Object
create a user and assign a set of group.
Instance Method Summary collapse
Instance Method Details
#aur(*names) ⇒ Object
aur command to install packages from aur on install step
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 |
# File 'lib/declarations/pacman.rb', line 49 def aur(*names) names.flatten! @aurs ||= Set.new @aurs += names.map(&:to_s) on_install do log "Install AUR packages", packages: @aurs cache = "./cache/aur" FileUtils.mkdir_p cache Dir.chdir cache do @aurs.each do |package| unless Dir.exist?(package) system("git clone --depth 1 --shallow-submodules https://aur.archlinux.org/#{package}.git") end Dir.chdir package do pkgbuild = File.readlines('PKGBUILD') pkgver = pkgbuild.find { |l| l.start_with?('pkgver=') }.split('=')[1].strip.chomp('"') package_info = `pacman -Qi #{package}`.strip.lines.to_h { |l| l.strip.split(/\s*:\s*/, 2) } installed = package_info["Version"].to_s.split("-")[0] == pkgver system("makepkg --syncdeps --install --noconfirm --needed") unless installed end end end foreign = Set.new(`pacman -Qm`.lines.map { |l| l.split(/\s+/, 2).first }) unneeded = foreign - @aurs next if unneeded.empty? log "Foreign packages to remove", packages: unneeded sudo("pacman -Rsu #{unneeded.join(" ")}") end end |
#copy(src, dest) ⇒ Object
Copy src inside dest during configure step, if src/. will copy src content to dest
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/declarations/file.rb', line 6 def copy(src, dest) @copy ||= [] @copy << { src: src, dest: dest } on_configure do next unless @copy next if @copy.empty? @copy.each do |item| log "Copying", item FileUtils.cp_r item[:src], item[:dest] end end end |
#file(path, content) ⇒ Object
Write a file during configure step
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/declarations/file.rb', line 76 def file(path, content) @files ||= {} @files[path] = content on_configure do @files.each do |path, content| FileUtils.mkdir_p File.dirname(path) File.write(path, content) rescue Errno::ENOENT => e log "Error: Can't write file", file: path, error: e end end end |
#git_clone(from:, to: nil) ⇒ Object
on prepare make sure a git repository is cloned to directory
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/declarations/git.rb', line 6 def git_clone(from:, to: nil) @git_clone ||= Set.new @git_clone << { from: from, to: to } on_install do @git_clone.each do |item| from = item[:from] to = item[:to] system "git clone #{from} #{to}" unless File.exist?(File.(to)) end end end |
#github_clone(from:, to: nil) ⇒ Object
git clone for github repositories
20 21 22 |
# File 'lib/declarations/git.rb', line 20 def github_clone(from:, to: nil) git_clone(from: "https://github.com/#{from}", to: to) end |
#hostname(name) ⇒ Object
Sets the machine hostname
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/declarations/systemd.rb', line 98 def hostname(name) @hostname = name file '/etc/hostname', "#{@hostname}\n" on_configure do log "Setting hostname", hostname: @hostname sudo "hostnamectl set-hostname #{@hostname}" end end |
#keyboard(keymap: nil, layout: nil, model: nil, variant: nil, options: nil) ⇒ Object
set keyboard settings during prepare step
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/declarations/systemd.rb', line 67 def keyboard(keymap: nil, layout: nil, model: nil, variant: nil, options: nil) @keyboard ||= {} values = { keymap: keymap, layout: layout, model: model, variant: variant, options: }.compact @keyboard.merge!(values) on_prepare do next unless @keyboard[:keymap] sudo "localectl set-keymap #{@keyboard[:keymap]}" m = @keyboard.to_h.slice(:layout, :model, :variant, :options) sudo "localectl set-x11-keymap \"#{m[:layout]}\" \"#{m[:model]}\" \"#{m[:variant]}\" \"#{m[:options]}\"" end end |
#locale(value) ⇒ Object
Sets locale using localectl
89 90 91 92 93 94 95 |
# File 'lib/declarations/systemd.rb', line 89 def locale(value) @locale = value on_prepare do sudo "localectl set-locale #{@locale}" end end |
#log(msg, args = {}) ⇒ Object
Prints a message to the STDOUT
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/utils.rb', line 8 def log(msg, args = {}) puts msg return unless args.any? max = args.keys.map(&:to_s).max_by(&:length).length args.each do |k, v| vs = case v when Array, Set "(#{v.length}) " + v.join(", ") else v end puts "\t#{k.to_s.rjust(max)}: #{vs}" end end |
#mkdir(*path) ⇒ Object
on prepare make sure the directory exists
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/declarations/file.rb', line 63 def mkdir(*path) path.flatten! @mkdir ||= Set.new @mkdir += path on_prepare do @mkdir.each do |path| FileUtils.mkdir_p File.(path) end end end |
#package(*names) ⇒ Object
Install a package on install step and remove packages not registered with this function
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 |
# File 'lib/declarations/pacman.rb', line 15 def package(*names) names.flatten! @packages ||= Set.new @packages += names.map(&:to_s) # install step to install packages required and remove not required on_install do # install missing packages need_install = @packages.reject { |p| package? p } need_install_args = need_install.join(" ") if need_install.any? log "Installing packages", packages: need_install sudo "pacman --noconfirm --needed -S #{need_install_args}" end # expand groups to packages packages_args = @packages.join(" ") group_packages = Set.new(`pacman --quiet -Sg #{packages_args}`.lines.map(&:strip)) # full list of packages that should exist on the system all = @packages + group_packages # actual list on the system installed = Set.new(`pacman -Q --quiet --explicit --unrequired --native`.lines.map(&:strip)) unneeded = installed - all next if unneeded.empty? log "Removing packages", packages: unneeded sudo("pacman -Rsu #{unneeded.join(" ")}") end end |
#package?(name) ⇒ Boolean
Utility function, returns true of package is installed
7 8 9 |
# File 'lib/declarations/pacman.rb', line 7 def package?(name) system("pacman -Qi #{name} &> /dev/null") end |
#replace(file, pattern, replacement) ⇒ Object
Replace a regex pattern with replacement string in a file during configure step
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/declarations/file.rb', line 22 def replace(file, pattern, replacement) @replace ||= [] @replace << { file: file, pattern: pattern, replacement: replacement } on_configure do @replace.each do |params| input = File.read(params[:file]) output = input.gsub(params[:pattern], params[:replacement]) File.write(params[:file], output) end end end |
#require_relative_dir(dir) ⇒ Object
1 2 3 |
# File 'lib/archlinux.rb', line 1 def require_relative_dir(dir) Dir["#{File.dirname(__FILE__)}/#{dir}/**/*.rb"].each { |f| require f } end |
#root? ⇒ Boolean
Checks if current user is the root
27 28 29 |
# File 'lib/utils.rb', line 27 def root? Process.uid == Etc.getpwnam('root').uid end |
#service(*names) ⇒ Object
enable system service if root or user service if not during finalize step
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 |
# File 'lib/declarations/systemd.rb', line 17 def service(*names) names.flatten! @services ||= Set.new @services += names.map(&:to_s) on_finalize do user_flags = root? ? "" : "--user" services = `systemctl list-unit-files #{user_flags} --state=enabled --type=service --no-legend --no-pager` enabled = services.lines enabled.map! { |l| l.strip.split(/\s+/) } enabled.each { |l| l[0].delete_suffix!(".service") } to_enable = @services - enabled.map(&:first) if to_enable.any? log "Enable services", services: to_enable system "systemctl enable #{user_flags} #{to_enable.join(" ")}" end # Disable services that were enabled manually and not in the list we have enabled_manually = enabled.select! { |l| l[2] == 'disabled' }.map(&:first) to_disable = enabled_manually - @services.to_a next if to_disable.empty? log "Services to disable", packages: to_disable system "systemctl disable #{user_flags} #{to_disable.join(" ")}" end end |
#sudo(command) ⇒ Object
Runs the command with sudo if current user is not root
32 33 34 |
# File 'lib/utils.rb', line 32 def sudo(command) root? ? system(command) : system("sudo #{command}") end |
#symlink(target, link_name) ⇒ Object
link file to destination
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 |
# File 'lib/declarations/file.rb', line 36 def symlink(target, link_name) @symlink ||= Set.new @symlink << { target: target, link_name: link_name } on_configure do @symlink.each do |params| target = File. params[:target] link_name = File. params[:link_name] if File.directory?(target) log "Can't link directories", target: target, link_name: link_name exit end log "Linking", target: target, link_name: link_name # make the parent if it doesn't exist dest_dir = File.dirname(link_name) FileUtils.mkdir_p(dest_dir) # link with force FileUtils.ln_s(target, link_name, force: true) end end end |
#timedate(timezone: 'UTC', ntp: true) ⇒ Object
set timezone and NTP settings during prepare step
6 7 8 9 10 11 12 13 14 |
# File 'lib/declarations/systemd.rb', line 6 def timedate(timezone: 'UTC', ntp: true) @timedate = { timezone: timezone, ntp: ntp } on_configure do log "Set timedate", @timedate sudo "timedatectl set-timezone #{@timedate[:timezone]}" sudo "timedatectl set-ntp #{@timedate[:ntp]}" end end |
#timer(*names) ⇒ Object
enable system timer if root or user timer if not during finalize step
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/declarations/systemd.rb', line 49 def timer(*names) names.flatten! @timers ||= Set.new @timers += names.map(&:to_s) on_finalize do log "Enable timers", timers: @timers timers = @timers.map { |t| "#{t}.timer" }.join(" ") if root? sudo "systemctl enable #{timers}" else system "systemctl enable --user #{timers}" end # disable all other timers end end |
#ufw(*allow) ⇒ Object
setup add ufw enable it and allow ports during configure step
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/applications/ufw.rb', line 6 def ufw(*allow) @ufw ||= Set.new @ufw += allow.map(&:to_s) package :ufw service :ufw on_configure do sudo "ufw allow #{@ufw.join(' ')}" end end |
#user(name, groups: [], autologin: nil, &block) ⇒ Object
create a user and assign a set of group. if block is passes the block will run in as this user. block will run during the configure step
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 |
# File 'lib/declarations/user.rb', line 8 def user(name, groups: [], autologin: nil, &block) name = name.to_s @user ||= {} @user[name] ||= {} @user[name][:groups] ||= [] @user[name][:groups] += groups.map(&:to_s) @user[name][:autologin] = autologin unless autologin.nil? @user[name][:state] ||= State.new @user[name][:state].apply(block) if block_given? on_configure do @user.each do |name, conf| exists = Etc.getpwnam name rescue nil sudo "useradd #{name}" unless exists sudo "usermod --groups #{groups.join(",")} #{name}" if groups.any? if conf[:autologin] FileUtils.mkdir_p '/etc/systemd/system/[email protected]' file '/etc/systemd/system/[email protected]/autologin.conf', " [Service]\n ExecStart=\n ExecStart=-/sbin/agetty -o '-p -f -- \\\\u' --noclear --autologin \#{name} %I $TERM\n FILE\n end\n\n fork do\n currentuser = Etc.getpwnam(name)\n Process::GID.change_privilege(currentuser.gid)\n Process::UID.change_privilege(currentuser.uid)\n ENV['XDG_RUNTIME_DIR'] = \"/run/user/\#{currentuser.uid}\"\n ENV['HOME'] = currentuser.dir\n ENV['USER'] = currentuser.name\n ENV['LOGNAME'] = currentuser.name\n conf[:state].run_steps\n end\n\n Process.wait\n end\n end\nend\n" |