Skip to content

Commit 0844ec0

Browse files
author
Andreas Brandl
committed
Merge branch 'use-background-migration-for-self-monitoring-project' into 'master'
Create self-monitoring project in background migration See merge request gitlab-org/gitlab-ce!32819
2 parents d02ee54 + 4a0d7a6 commit 0844ec0

File tree

4 files changed

+251
-214
lines changed

4 files changed

+251
-214
lines changed

db/post_migrate/20190801072937_add_gitlab_instance_administration_project.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
class AddGitlabInstanceAdministrationProject < ActiveRecord::Migration[5.2]
44
DOWNTIME = false
55

6+
disable_ddl_transaction!
7+
68
def up
7-
Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService.new.execute!
9+
BackgroundMigrationWorker.perform_async('AddGitlabInstanceAdministrationProject', [])
810
end
911

1012
def down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
# rubocop:disable Style/Documentation
3+
4+
module Gitlab
5+
module BackgroundMigration
6+
class AddGitlabInstanceAdministrationProject
7+
def perform
8+
Rails.logger.info("Creating Gitlab instance administration project") # rubocop:disable Gitlab/RailsLogger
9+
10+
Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService.new.execute!
11+
end
12+
end
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Gitlab::BackgroundMigration::AddGitlabInstanceAdministrationProject, :migration, schema: 20190725080128 do
6+
let(:application_settings) { table(:application_settings) }
7+
let(:users) { table(:users) }
8+
let(:projects) { table(:projects) }
9+
let(:namespaces) { table(:namespaces) }
10+
let(:members) { table(:members) }
11+
12+
let(:service_class) do
13+
Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
14+
end
15+
16+
let(:prometheus_settings) do
17+
{
18+
enable: true,
19+
listen_address: 'localhost:9090'
20+
}
21+
end
22+
23+
before do
24+
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
25+
26+
stub_config(prometheus: prometheus_settings)
27+
end
28+
29+
describe 'perform' do
30+
context 'without application_settings' do
31+
it 'does not fail' do
32+
subject.perform
33+
34+
expect(Project.count).to eq(0)
35+
end
36+
end
37+
38+
context 'without admin users' do
39+
let!(:application_setting) { application_settings.create! }
40+
41+
it 'does not fail' do
42+
subject.perform
43+
44+
expect(Project.count).to eq(0)
45+
end
46+
end
47+
48+
context 'with admin users' do
49+
let(:project) { Project.last }
50+
let(:group) { Group.last }
51+
let!(:application_setting) { application_settings.create! }
52+
let!(:user) { users.create!(admin: true, email: '[email protected]', projects_limit: 10, state: :active) }
53+
54+
before do
55+
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
56+
end
57+
58+
shared_examples 'has prometheus service' do |listen_address|
59+
it do
60+
subject.perform
61+
62+
prometheus = project.prometheus_service
63+
expect(prometheus).to be_persisted
64+
expect(prometheus).not_to eq(nil)
65+
expect(prometheus.api_url).to eq(listen_address)
66+
expect(prometheus.active).to eq(true)
67+
expect(prometheus.manual_configuration).to eq(true)
68+
end
69+
end
70+
71+
it_behaves_like 'has prometheus service', 'http://localhost:9090'
72+
73+
it 'creates GitLab Instance Administrator group' do
74+
subject.perform
75+
76+
expect(group).to be_persisted
77+
expect(group.name).to eq('GitLab Instance Administrators')
78+
expect(group.path).to start_with('gitlab-instance-administrators')
79+
expect(group.path.split('-').last.length).to eq(8)
80+
expect(group.visibility_level).to eq(service_class::VISIBILITY_LEVEL)
81+
end
82+
83+
it 'creates project with internal visibility' do
84+
subject.perform
85+
86+
expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
87+
expect(project).to be_persisted
88+
end
89+
90+
it 'creates project with correct name and description' do
91+
subject.perform
92+
93+
path = 'administration/monitoring/gitlab_instance_administration_project/index'
94+
docs_path = Rails.application.routes.url_helpers.help_page_path(path)
95+
96+
expect(project.name).to eq(service_class::PROJECT_NAME)
97+
expect(project.description).to eq(
98+
'This project is automatically generated and will be used to help monitor this GitLab instance. ' \
99+
"[More information](#{docs_path})"
100+
)
101+
expect(File).to exist("doc/#{path}.md")
102+
end
103+
104+
it 'adds all admins as maintainers' do
105+
admin1 = users.create!(admin: true, email: '[email protected]', projects_limit: 10, state: :active)
106+
admin2 = users.create!(admin: true, email: '[email protected]', projects_limit: 10, state: :active)
107+
users.create!(email: '[email protected]', projects_limit: 10, state: :active)
108+
109+
subject.perform
110+
111+
expect(project.owner).to eq(group)
112+
expect(group.members.collect(&:user).collect(&:id)).to contain_exactly(user.id, admin1.id, admin2.id)
113+
expect(group.members.collect(&:access_level)).to contain_exactly(
114+
Gitlab::Access::OWNER,
115+
Gitlab::Access::MAINTAINER,
116+
Gitlab::Access::MAINTAINER
117+
)
118+
end
119+
120+
it 'saves the project id' do
121+
subject.perform
122+
123+
application_setting.reload
124+
expect(application_setting.instance_administration_project_id).to eq(project.id)
125+
end
126+
127+
it 'does not fail when a project already exists' do
128+
group = namespaces.create!(
129+
path: 'gitlab-instance-administrators',
130+
name: 'GitLab Instance Administrators',
131+
type: 'Group'
132+
)
133+
project = projects.create!(
134+
namespace_id: group.id,
135+
name: 'GitLab Instance Administration'
136+
)
137+
138+
admin1 = users.create!(admin: true, email: '[email protected]', projects_limit: 10, state: :active)
139+
admin2 = users.create!(admin: true, email: '[email protected]', projects_limit: 10, state: :active)
140+
141+
members.create!(
142+
user_id: admin1.id,
143+
source_id: group.id,
144+
source_type: 'Namespace',
145+
type: 'GroupMember',
146+
access_level: GroupMember::MAINTAINER,
147+
notification_level: NotificationSetting.levels[:global]
148+
)
149+
members.create!(
150+
user_id: admin2.id,
151+
source_id: group.id,
152+
source_type: 'Namespace',
153+
type: 'GroupMember',
154+
access_level: GroupMember::MAINTAINER,
155+
notification_level: NotificationSetting.levels[:global]
156+
)
157+
158+
stub_application_setting(instance_administration_project: project)
159+
160+
subject.perform
161+
162+
expect(Project.last.id).to eq(project.id)
163+
expect(Group.last.id).to eq(group.id)
164+
end
165+
166+
context 'when local requests from hooks and services are not allowed' do
167+
before do
168+
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
169+
end
170+
171+
it_behaves_like 'has prometheus service', 'http://localhost:9090'
172+
173+
it 'does not overwrite the existing whitelist' do
174+
application_setting.update!(outbound_local_requests_whitelist: ['example.com'])
175+
176+
subject.perform
177+
178+
application_setting.reload
179+
expect(application_setting.outbound_local_requests_whitelist).to contain_exactly(
180+
'example.com', 'localhost'
181+
)
182+
end
183+
end
184+
185+
context 'with non default prometheus address' do
186+
let(:prometheus_settings) do
187+
{
188+
enable: true,
189+
listen_address: 'https://localhost:9090'
190+
}
191+
end
192+
193+
it_behaves_like 'has prometheus service', 'https://localhost:9090'
194+
end
195+
196+
context 'when prometheus setting is not present in gitlab.yml' do
197+
before do
198+
allow(Gitlab.config).to receive(:prometheus).and_raise(Settingslogic::MissingSetting)
199+
end
200+
201+
it 'does not fail' do
202+
subject.perform
203+
204+
expect(project.prometheus_service).to be_nil
205+
end
206+
end
207+
208+
context 'when prometheus setting is disabled in gitlab.yml' do
209+
let(:prometheus_settings) do
210+
{
211+
enable: false,
212+
listen_address: 'localhost:9090'
213+
}
214+
end
215+
216+
it 'does not configure prometheus' do
217+
subject.perform
218+
219+
expect(project.prometheus_service).to be_nil
220+
end
221+
end
222+
223+
context 'when prometheus listen address is blank in gitlab.yml' do
224+
let(:prometheus_settings) { { enable: true, listen_address: '' } }
225+
226+
it 'does not configure prometheus' do
227+
subject.perform
228+
229+
expect(project.prometheus_service).to be_nil
230+
end
231+
end
232+
end
233+
end
234+
end

0 commit comments

Comments
 (0)