/
base.rb
120 lines (103 loc) · 2.87 KB
/
base.rb
1
2
3
4
5
6
7
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
puts ENV['PATH']
$os = run_command('uname').stdout.strip.downcase
$home_dir = case $os
when "linux"
run_command("getent passwd #{ENV["SUDO_USER"] || ENV["USER"]}")
.stdout.split(':')[5].strip
else
ENV["HOME"]
end
$distro = case $os
when "linux"
run_command("awk '/^ID=/' /etc/*-release | tr -d 'ID='")
.stdout.strip
else
"not-linux"
end
node.reverse_merge!(
os: $os,
distro: $distro,
hostname: run_command('hostname').stdout.split('.').first.downcase.strip,
user: ENV['SUDO_USER'] || ENV['USER'],
home_dir: $home_dir,
)
REPO_ROOT = File.dirname(__FILE__)
RECIPES_DIR = File.expand_path("recipes", REPO_ROOT)
NODES_DIR = File.expand_path("nodes", REPO_ROOT)
FILES_DIR = File.expand_path("files", REPO_ROOT)
TEMPLATES_DIR = File.expand_path("templates", REPO_ROOT)
define :include_local_recipe do
include_recipe "#{RECIPES_DIR}/#{params[:name]}.rb"
end
define :dotfile_template, source: nil, variables: {} do
template_path = "#{node.home_dir}/#{params[:name]}"
template_root = File.dirname(template_path)
directory template_root do
user node[:user]
end
template template_path do
action :create
owner node.user
group node.user
source "#{TEMPLATES_DIR}/#{params[:source]}"
variables(**params[:variables])
end
end
define :dotfile, source: nil, owner: node[:user] do
links = if params[:name].is_a?(String)
{ params[:name] => params[:source] }
else
params[:name]
end
links.each do |to, from|
destination = File.expand_path(to, node[:home_dir])
dest_dir = File.dirname(destination)
source = File.expand_path(from, FILES_DIR)
directory dest_dir do
user node[:user]
end
link destination do
to source
user params[:owner]
force true
end
end
end
define :systemd_service, enable: false do
Array(params[:action]).each do |action|
case action
when :enable
execute "systemctl enable #{params[:name]}" do
not_if "[[ `systemctl is-enabled #{params[:name]}` =~ ^enabled ]]"
end
when :start
execute "systemctl start #{params[:name]}" do
not_if "[[ `systemctl is-active #{params[:name]}` =~ ^active ]]"
end
end
end
end
define :fisher do
repo, package = params[:name].split('/')
execute "fish -c \"fisher install #{params[:name]}\"" do
user node[:user]
not_if "test $(cat #{node.home_dir}/.config/fish/fish_plugins | grep #{package} | wc -l) -gt 0"
end
user node.user
end
define :pip, use_pipx: false do
if params[:use_pipx]
pip = "pipx"
condition = "pipx list | grep #{params[:name]}"
else
pip = "pip3"
condition = "pip3 show #{params[:name]}"
end
execute "#{pip} install #{params[:name]}" do
user node[:user]
not_if "test $(#{condition} | wc -l) -gt 0"
end
user node.user
end
include_recipe "recipes/prelude/#{node.os}.rb"
include_recipe "#{NODES_DIR}/#{node[:hostname]}.rb"