Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kubespray
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
Kubespray
Commits
45e12df8
Unverified
Commit
45e12df8
authored
4 years ago
by
Maxime Guyot
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Cleanup OpenStack network things (#6283)
parent
1892cd65
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/openstack-cleanup/main.py
+36
-8
36 additions, 8 deletions
scripts/openstack-cleanup/main.py
with
36 additions
and
8 deletions
scripts/openstack-cleanup/main.py
+
36
−
8
View file @
45e12df8
...
...
@@ -11,7 +11,7 @@ PAUSE_SECONDS = 5
log
=
logging
.
getLogger
(
'
openstack-cleanup
'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'
Cleanup OpenStack
VM
s
'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'
Cleanup OpenStack
resource
s
'
)
parser
.
add_argument
(
'
-v
'
,
'
--verbose
'
,
action
=
'
store_true
'
,
help
=
'
Increase verbosity
'
)
...
...
@@ -29,16 +29,44 @@ def main():
if
args
.
dry_run
:
print
(
'
Running in dry-run mode
'
)
else
:
print
(
'
This will delete
VM
s... (ctrl+c to cancel)
'
)
print
(
'
This will delete
resource
s... (ctrl+c to cancel)
'
)
time
.
sleep
(
PAUSE_SECONDS
)
conn
=
openstack
.
connect
()
for
server
in
conn
.
compute
.
servers
():
created_at
=
datetime
.
datetime
.
strptime
(
server
.
created_at
,
DATE_FORMAT
)
print
(
'
Security groups...
'
)
map_if_old
(
conn
.
network
.
delete_security_group
,
conn
.
network
.
security_groups
())
print
(
'
Servers...
'
)
map_if_old
(
conn
.
compute
.
delete_server
,
conn
.
compute
.
servers
())
print
(
'
Subnets...
'
)
map_if_old
(
conn
.
network
.
delete_subnet
,
conn
.
network
.
subnets
())
print
(
'
Networks...
'
)
for
n
in
conn
.
network
.
networks
():
if
not
n
.
is_router_external
:
fn_if_old
(
conn
.
network
.
delete_network
,
n
)
# runs the given fn to all elements of the that are older than allowed
def
map_if_old
(
fn
,
items
):
for
item
in
items
:
fn_if_old
(
fn
,
item
)
# run the given fn function only if the passed item is older than allowed
def
fn_if_old
(
fn
,
item
):
created_at
=
datetime
.
datetime
.
strptime
(
item
.
created_at
,
DATE_FORMAT
)
if
item
.
name
==
"
default
"
:
# skip default security group
return
if
created_at
<
oldest_allowed
:
print
(
'
Will delete
server
%(name)s
'
%
server
)
print
(
'
Will delete %(name)s
(%(id)s)
'
%
item
)
if
not
args
.
dry_run
:
conn
.
compute
.
delete_server
(
server
)
fn
(
item
)
if
__name__
==
'
__main__
'
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment