-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.rb
175 lines (151 loc) · 4.44 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
$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.gsub('"','')
else
"not-linux"
end
$git_host = begin
ENV.fetch("EBR_GIT_HOST")
rescue KeyError
$stderr.puts "EBR_GIT_HOST not defined, Please configure this to be the internal Git server address"
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,
git_host: $git_host,
)
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
define :personal_git, destination: nil do
params[:destination] ? params[:destination] : "#{node.home_dir}/git/#{params[:name]}"
if node.git_host
git "Personal #{params[:name]} repo" do
repository "#{node.git_host}/#{params[:name]}.git"
user node.user
destination params[:destination]
end
else
MItamae.logger.warn("EBR_GIT_HOST not configured, skipping clone of #{params[:name]}")
end
end
# TODO: Upstream a fix to specinfra et al. the runit enable check is incorrect
define :void_service, action: :nothing do
actions = Array(params[:action])
name = params[:name]
unless Dir.exists?("/etc/sv/#{name}")
MItamae.logger.error("Service name #{name} doesn't exist")
end
# sort the array, because we want to enable before we start
actions.sort.each do |action|
case action
when :enable
MItamae.logger.debug("enabling #{name}")
unless Dir.exists?("/var/service/#{name}")
system("ln -s /etc/sv/#{name} /var/service/")
end
when :start
MItamae.logger.debug("starting #{name}")
system("sv up #{name}")
else
MItamae.logger.error("void_service, valid actions are :create, :start")
end
end
end
define :group_add, user: node.user do
name = params[:name]
user = params[:user]
execute "add #{user} to #{name} group" do
command "usermod -aG #{name} #{user}"
not_if "groups #{user} | grep #{name}"
end
end
include_recipe "recipes/prelude/#{node.os}.rb"
include_recipe "#{NODES_DIR}/#{node[:hostname]}.rb"