Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
daa23387
Commit
daa23387
authored
Sep 30, 2021
by
Rémy Coutable
Committed by
Igor Drozdov
Sep 30, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ci: Delete stopped environments not updated since specific threshold
parent
432314e3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
14 deletions
+37
-14
scripts/review_apps/automated_cleanup.rb
scripts/review_apps/automated_cleanup.rb
+36
-13
tooling/lib/tooling/helm3_client.rb
tooling/lib/tooling/helm3_client.rb
+1
-1
No files found.
scripts/review_apps/automated_cleanup.rb
View file @
daa23387
...
...
@@ -8,6 +8,10 @@ class AutomatedCleanup
attr_reader
:project_path
,
:gitlab_token
DEPLOYMENTS_PER_PAGE
=
100
ENVIRONMENT_PREFIX
=
{
review_app:
'review/'
,
docs_review_app:
'review-docs/'
}.
freeze
IGNORED_HELM_ERRORS
=
[
'transport is closing'
,
'error upgrading connection'
,
...
...
@@ -62,13 +66,14 @@ class AutomatedCleanup
releases_to_delete
=
[]
# Delete environments via deployments
gitlab
.
deployments
(
project_path
,
per_page:
DEPLOYMENTS_PER_PAGE
,
sort:
'desc'
).
auto_paginate
do
|
deployment
|
break
if
Time
.
parse
(
deployment
.
created_at
)
<
deployments_look_back_threshold
environment
=
deployment
.
environment
next
unless
environment
next
unless
environment
.
name
.
start_with?
(
'review/'
)
next
unless
environment
.
name
.
start_with?
(
ENVIRONMENT_PREFIX
[
:review_app
]
)
next
if
checked_environments
.
include?
(
environment
.
slug
)
last_deploy
=
deployment
.
created_at
...
...
@@ -92,6 +97,10 @@ class AutomatedCleanup
checked_environments
<<
environment
.
slug
end
delete_stopped_environments
(
environment_type: :review_app
,
checked_environments:
checked_environments
,
last_updated_threshold:
delete_threshold
)
do
|
environment
|
releases_to_delete
<<
Tooling
::
Helm3Client
::
Release
.
new
(
environment
.
slug
,
1
,
environment
.
updated_at
,
nil
,
nil
,
review_apps_namespace
)
end
delete_helm_releases
(
releases_to_delete
)
end
...
...
@@ -102,14 +111,12 @@ class AutomatedCleanup
stop_threshold
=
threshold_time
(
days:
days_for_stop
)
delete_threshold
=
threshold_time
(
days:
days_for_delete
)
max_delete_count
=
1000
delete_count
=
0
# Delete environments via deployments
gitlab
.
deployments
(
project_path
,
per_page:
DEPLOYMENTS_PER_PAGE
,
sort:
'desc'
).
auto_paginate
do
|
deployment
|
environment
=
deployment
.
environment
next
unless
environment
next
unless
environment
.
name
.
start_with?
(
'review-docs/'
)
next
unless
environment
.
name
.
start_with?
(
ENVIRONMENT_PREFIX
[
:docs_review_app
]
)
next
if
checked_environments
.
include?
(
environment
.
slug
)
last_deploy
=
deployment
.
created_at
...
...
@@ -120,15 +127,12 @@ class AutomatedCleanup
stop_environment
(
environment
,
deployment
)
if
environment_state
&&
environment_state
!=
'stopped'
end
if
deployed_at
<
delete_threshold
delete_environment
(
environment
,
deployment
)
delete_count
+=
1
break
if
delete_count
>
max_delete_count
end
delete_environment
(
environment
,
deployment
)
if
deployed_at
<
delete_threshold
checked_environments
<<
environment
.
slug
end
delete_stopped_environments
(
environment_type: :docs_review_app
,
checked_environments:
checked_environments
,
last_updated_threshold:
delete_threshold
)
end
def
perform_helm_releases_cleanup!
(
days
:)
...
...
@@ -171,8 +175,9 @@ class AutomatedCleanup
nil
end
def
delete_environment
(
environment
,
deployment
)
print_release_state
(
subject:
'Review app'
,
release_name:
environment
.
slug
,
release_date:
deployment
.
created_at
,
action:
'deleting'
)
def
delete_environment
(
environment
,
deployment
=
nil
)
release_date
=
deployment
?
deployment
.
created_at
:
environment
.
updated_at
print_release_state
(
subject:
'Review app'
,
release_name:
environment
.
slug
,
release_date:
release_date
,
action:
'deleting'
)
gitlab
.
delete_environment
(
project_path
,
environment
.
id
)
rescue
Gitlab
::
Error
::
Forbidden
...
...
@@ -187,6 +192,24 @@ class AutomatedCleanup
puts
"Review app '
#{
environment
.
name
}
' / '
#{
environment
.
slug
}
' (#
#{
environment
.
id
}
) is forbidden: skipping it"
end
def
delete_stopped_environments
(
environment_type
:,
checked_environments
:,
last_updated_threshold
:)
gitlab
.
environments
(
project_path
,
per_page:
DEPLOYMENTS_PER_PAGE
,
sort:
'desc'
,
states:
'stopped'
,
search:
ENVIRONMENT_PREFIX
[
environment_type
]).
auto_paginate
do
|
environment
|
next
if
skip_environment?
(
environment:
environment
,
checked_environments:
checked_environments
,
last_updated_threshold:
delete_threshold
,
environment_type:
environment_type
)
yield
environment
if
delete_environment
(
environment
)
checked_environments
<<
environment
.
slug
end
end
def
skip_environment?
(
environment
:,
checked_environments
:,
last_updated_threshold
:,
environment_type
:)
return
true
unless
environment
.
name
.
start_with?
(
ENVIRONMENT_PREFIX
[
environment_type
])
return
true
if
checked_environments
.
include?
(
environment
.
slug
)
return
true
if
Time
.
parse
(
environment
.
updated_at
)
>
last_updated_threshold
false
end
def
helm_releases
args
=
[
'--all'
,
'--date'
]
...
...
tooling/lib/tooling/helm3_client.rb
View file @
daa23387
...
...
@@ -19,7 +19,7 @@ module Tooling
end
def
last_update
@last_update
||=
Time
.
parse
(
self
[
:last_update
])
@last_update
||=
self
[
:last_update
]
?
Time
.
parse
(
self
[
:last_update
])
:
nil
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment