Skip to content

Commit 3629bd3

Browse files
authored
Merge pull request #102 from stevius10/develop
dev/1.1-2
2 parents b0a6cde + 21c966d commit 3629bd3

File tree

8 files changed

+146
-113
lines changed

8 files changed

+146
-113
lines changed

config/libraries/env.rb

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,48 @@ def self.request(ctx, key, body: nil, repo: nil, owner: nil, expect: false)
5353
return response
5454
end
5555

56-
def self.dump(ctx, *keys, repo: nil, owner: nil)
56+
def self.dump(ctx, *args, repo: nil, owner: nil)
5757
Logs.try!("dump variables", [:repo, repo, :owner, owner], raise: true) do
58-
node = Ctx.node(ctx); f = ->(keys) do; case keys
59-
in [key, value] if !key.is_a?(Array)
60-
val = value.respond_to?(:call) ? value.call(node) : (value.is_a?(Array) ? node.dig(*value) : value)
61-
Env.set_variable(node, key, val, repo: repo, owner: owner) unless val.blank?
62-
in Array => a
63-
a.each { |x| f.(x) }
64-
else
65-
val = node[keys]; return if val.blank?
66-
if val.is_a?(Hash)
67-
val.each { |subkey, subvalue| Env.set_variable(node, "#{keys}_#{subkey}", subvalue, repo: repo, owner: owner) unless subvalue.blank? }
68-
elsif val.is_a?(Array)
69-
val.each_with_index { |x, i| Env.set_variable(node, "#{keys}_#{i}", x, repo: repo, owner: owner) unless x.blank? }
58+
node = Ctx.node(ctx)
59+
get = ->(key) { k = key.is_a?(String) ? key : key.to_s; node[key] || node[k] || node[k.to_sym] }
60+
set = ->(key, value) { Env.set_variable(node, key, value, repo: repo, owner: owner) unless value.blank? }
61+
resolve = ->(key, value) do
62+
v = value
63+
v = v.call(node) if v.respond_to?(:call)
64+
v = node.dig(*v) if v.is_a?(Array)
65+
set.(key, v)
66+
end
67+
single = args.length == 1 && args.first.is_a?(Array)
68+
work = single ? args.first : args
69+
rec = nil
70+
rec = ->(arg) do
71+
case arg
72+
when Hash
73+
arg.each { |key, value| resolve.(key, value) }
74+
when Array
75+
if !single && arg.length == 2 && !arg.first.is_a?(Array)
76+
key, value = arg
77+
resolve.(key, value)
78+
else
79+
arg.each { |x| rec.(x) }
80+
end
7081
else
71-
Env.set_variable(node, keys, val, repo: repo, owner: owner)
82+
value = get.(arg)
83+
if value.present?
84+
case value
85+
when Hash
86+
value.each { |subkey, subvalue| set.("#{arg}_#{subkey}", subvalue) unless subvalue.blank? }
87+
when Array
88+
value.each_with_index { |subvalue, i| set.("#{arg}_#{i}", subvalue) unless subvalue.blank? }
89+
else
90+
set.(arg, value)
91+
end
92+
end
7293
end
73-
end end
74-
keys.each { |key| f.(key) }
94+
end
95+
work.each { |x| rec.(x) }
7596
true
76-
end end
77-
78-
end
97+
end
98+
end
99+
100+
end

config/recipes/config.rb

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,86 @@
1+
login = node.run_state['login']
2+
password = node.run_state['password']
3+
email = node.run_state['email']
4+
15
Utils.wait("127.0.0.1:#{node['git']['port']['http']}", timeout: 15, sleep_interval: 1)
26

3-
include_recipe 'config::config_user'
4-
include_recipe 'config::config_org'
7+
# User configuration
8+
9+
execute 'config_set_user' do
10+
user node['app']['user']
11+
command <<-EOH
12+
login="#{login}"
13+
base="#{node['git']['dir']['app']}/gitea admin user --config #{node['git']['dir']['app']}/app.ini"
14+
user="--username #{login} --password #{password}"
15+
create="--email #{email} --admin --must-change-password=false"
16+
if $base list | awk '{print $2}' | grep -q "^#{login}$"; then
17+
$base delete $user
18+
fi
19+
$base create $create $user
20+
EOH
21+
not_if { Utils.request("#{node['git']['api']['endpoint']}/user", user: login, pass: password, expect: true) }
22+
end
23+
24+
ruby_block 'config_set_key' do
25+
block do
26+
key = ::File.read("#{node['key']}.pub").strip
27+
uri = "#{node['git']['api']['endpoint']}/user/keys"
28+
29+
Utils.request(uri, user: login, pass: password).json.each do |k|
30+
Utils.request("#{uri}/#{k['id']}", user: login, pass: password,
31+
method: Net::HTTP::Delete) if k['key'] && k['key'].strip == key
32+
end
33+
34+
response = Utils.request(uri, body: { title: login, key: key }.json, user: login, pass: password, method: Net::HTTP::Post, headers: Constants::HEADER_JSON)
35+
Logs.request!(uri, response, [201, 422], msg: "set key")
36+
end
37+
only_if { ::File.exist?("#{node['key']}.pub") }
38+
not_if do
39+
next false unless ::File.exist?("#{node['key']}.pub")
40+
begin
41+
response = Utils.request("#{node['git']['api']['endpoint']}/user/keys", user: login, pass: password)
42+
response.json.any? { |k| k['key'] && k['key'].strip == ::File.read("#{node['key']}.pub").strip }
43+
end
44+
end
45+
end
46+
47+
# Git configuration
48+
49+
execute 'config_git_safe_directory' do
50+
command <<-SH
51+
git config --global --add safe.directory "*" && \
52+
git config --system --add safe.directory "*"
53+
SH
54+
end
55+
56+
execute 'config_git_user' do
57+
command <<-SH
58+
git config --global user.name "#{login}"
59+
git config --global user.email "#{email}"
60+
git config --global core.excludesfile #{ENV['PWD']}/.gitignore
61+
SH
62+
user node['app']['user']
63+
end
64+
65+
# Organization
66+
67+
[node['git']['org']['main'], node['git']['org']['stage'], node['git']['org']['tasks']].each do |org|
68+
69+
ruby_block "dump_variables_#{org}" do
70+
action :nothing
71+
block do
72+
Env.dump(self, [ 'proxmox', 'host', 'login', 'password', 'email', [:endpoint, node.dig('git','api','endpoint')] ], owner: org)
73+
end
74+
end
75+
76+
ruby_block "create_org_#{org}" do
77+
block do
78+
name, password = Env.creds(self)
79+
uri = "#{node.dig('git','api','endpoint')}/orgs"
80+
r = Utils.request(uri, method: Net::HTTP::Post, headers: Constants::HEADER_JSON, body: { username: org }.to_json, user: name, pass: password)
81+
Logs.request!(uri, r, [201, 409, 422], msg: "create organization '#{org}'")
82+
end
83+
notifies :run, "ruby_block[dump_variables_#{org}]", :immediate
84+
end
85+
86+
end

config/recipes/config_org.rb

Lines changed: 0 additions & 27 deletions
This file was deleted.

config/recipes/config_user.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

config/recipes/default.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
include_recipe 'config::git'
44
include_recipe 'config::runner'
5-
65
include_recipe 'config::config'
76

87
include_recipe 'config::repo'

config/recipes/repo/init.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
name_repo = @name_repo; repository = @repository; path_destination = @path_destination
1+
name_repo = @name_repo; repository = @repository; monorepo = @monorepo; path_destination = @path_destination
22

33
ruby_block "repo_#{name_repo}_request" do
44
only_if { Logs.info?("[#{repository} (#{name_repo})] create repository") }
55
block do
66
uri="#{node['git']['api']['endpoint']}/admin/users/#{node['git']['org']['main']}/repos"
77
response = Utils.request(uri, method: Net::HTTP::Post, headers: Constants::HEADER_JSON,
8-
user: Env.get(self, 'login'), pass: Env.get(self, 'password'),
9-
body: { name: name_repo, private: false, auto_init: false, default_branch: 'main' }.json )
8+
user: Env.get(self, 'login'), pass: Env.get(self, 'password'),
9+
body: { name: name_repo, private: false, auto_init: false, default_branch: 'main' }.json )
1010
Logs.request!(uri, response, [201], msg: "Create repository '#{name_repo}'")
1111
response.json
1212
end
1313
end
1414

15+
ruby_block "dump_variables_#{cookbook_name}" do
16+
block do
17+
Env.dump(self, ['ip', 'git', 'runner'], repo: cookbook_name)
18+
end
19+
only_if { monorepo }
20+
end
21+
1522
execute "repo_#{name_repo}_git_init" do
1623
command <<-EOH
1724
mkdir -p #{path_destination} && cd #{path_destination} && git init -b main

config/recipes/repo/push.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
fi
2222
fi
2323
EOH
24-
notifies :run, "ruby_block[dump_variables_#{cookbook_name}]", :delayed if monorepo
2524
end
2625

27-
name_repo = @name_repo; repository = @repository; path_destination = @path_destination;
28-
2926
execute "repo_#{name_repo}_exists_snapshot_push" do
3027
command <<-EOH
3128
cp -r #{path_destination}/.git #{path_working}

tasks/health/default.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@
33
Dir[File.join(__dir__, 'libraries', '**', '*.rb')].sort.each { |f| require f }
44
ctx = { "endpoint"=>ENV["ENDPOINT"], "host"=>ENV["HOST"], "login"=>ENV["LOGIN"], "password"=>ENV["PASSWORD"], }
55

6-
puts(Utils.proxmox(ctx, 'nodes/pve/lxc')) # TODO: shared library test
6+
list = Utils.proxmox(ctx, 'nodes/pve/lxc')
7+
list.each do |ct|
8+
id = ct['vmid']
9+
cfg = Utils.proxmox(ctx, "nodes/pve/lxc/#{id}/config")
10+
cur = Utils.proxmox(ctx, "nodes/pve/lxc/#{id}/status/current")
11+
hn = cfg['hostname'] || id.to_s
12+
st = cur['status']
13+
ip = cfg['net0'] && cfg['net0'][/ip=(\d+\.\d+\.\d+\.\d+)/, 1]
14+
Env.set(ctx, "#{hn}_status", st, repo: 'health', owner: 'tasks')
15+
Env.set(ctx, "#{hn}_ip", ip, repo: 'health', owner: 'tasks') if ip
16+
end

0 commit comments

Comments
 (0)