Files
devstack-gate/slave_scripts/devstack-vm-reap.py
James E. Blair fd019ba43a Add "--all" option to vm reaper.
In case we need to quickly delete all known VMs.

Change-Id: Ic2bfc8b5ed9512ecf56ebf1278a6d31e7d7fade9
2011-12-15 11:27:50 -08:00

72 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
# Remove old devstack VMs that have been given to developers.
# Copyright (C) 2011 OpenStack LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
import os, sys, time
import getopt
from libcloud.base import NodeImage, NodeSize, NodeLocation
from libcloud.types import Provider
from libcloud.providers import get_driver
from libcloud.deployment import MultiStepDeployment, ScriptDeployment, SSHKeyDeployment
import libcloud
import vmdatabase
CLOUD_SERVERS_DRIVER = os.environ.get('CLOUD_SERVERS_DRIVER','rackspace')
CLOUD_SERVERS_USERNAME = os.environ['CLOUD_SERVERS_USERNAME']
CLOUD_SERVERS_API_KEY = os.environ['CLOUD_SERVERS_API_KEY']
db = vmdatabase.VMDatabase()
if '--all' in sys.argv:
print "Reaping all known machines"
REAP_ALL = True
else:
REAP_ALL = False
print 'Known machines (start):'
for machine in db.getMachines():
print machine
if CLOUD_SERVERS_DRIVER == 'rackspace':
Driver = get_driver(Provider.RACKSPACE)
conn = Driver(CLOUD_SERVERS_USERNAME, CLOUD_SERVERS_API_KEY)
def delete(machine):
node = [n for n in conn.list_nodes() if n.id==str(machine['id'])]
if not node:
print ' Machine id %s not found' % machine['id']
db.delMachine(machine['id'])
return
node = node[0]
node.destroy()
db.delMachine(machine['id'])
now = time.time()
for machine in db.getMachines():
if REAP_ALL or (now-machine['created'] > 24*60*60):
print 'Deleting', machine['name']
delete(machine)
print
print 'Known machines (end):'
for machine in db.getMachines():
print machine