summaryrefslogtreecommitdiffstats
path: root/kernel/tools/perf/ui/tui
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/tools/perf/ui/tui')
-rw-r--r--kernel/tools/perf/ui/tui/Build4
-rw-r--r--kernel/tools/perf/ui/tui/helpline.c61
-rw-r--r--kernel/tools/perf/ui/tui/progress.c44
-rw-r--r--kernel/tools/perf/ui/tui/setup.c174
-rw-r--r--kernel/tools/perf/ui/tui/tui.h6
-rw-r--r--kernel/tools/perf/ui/tui/util.c256
6 files changed, 545 insertions, 0 deletions
diff --git a/kernel/tools/perf/ui/tui/Build b/kernel/tools/perf/ui/tui/Build
new file mode 100644
index 000000000..9e4c6ca41
--- /dev/null
+++ b/kernel/tools/perf/ui/tui/Build
@@ -0,0 +1,4 @@
+libperf-y += setup.o
+libperf-y += util.o
+libperf-y += helpline.o
+libperf-y += progress.o
diff --git a/kernel/tools/perf/ui/tui/helpline.c b/kernel/tools/perf/ui/tui/helpline.c
new file mode 100644
index 000000000..88f5143a5
--- /dev/null
+++ b/kernel/tools/perf/ui/tui/helpline.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "../../util/debug.h"
+#include "../helpline.h"
+#include "../ui.h"
+#include "../libslang.h"
+
+char ui_helpline__last_msg[1024];
+bool tui_helpline__set;
+
+static void tui_helpline__pop(void)
+{
+}
+
+static void tui_helpline__push(const char *msg)
+{
+ const size_t sz = sizeof(ui_helpline__current);
+
+ SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
+ SLsmg_set_color(0);
+ SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
+ SLsmg_refresh();
+ strncpy(ui_helpline__current, msg, sz)[sz - 1] = '\0';
+}
+
+static int tui_helpline__show(const char *format, va_list ap)
+{
+ int ret;
+ static int backlog;
+
+ pthread_mutex_lock(&ui__lock);
+ ret = vscnprintf(ui_helpline__last_msg + backlog,
+ sizeof(ui_helpline__last_msg) - backlog, format, ap);
+ backlog += ret;
+
+ tui_helpline__set = true;
+
+ if (ui_helpline__last_msg[backlog - 1] == '\n') {
+ ui_helpline__puts(ui_helpline__last_msg);
+ SLsmg_refresh();
+ backlog = 0;
+ }
+ pthread_mutex_unlock(&ui__lock);
+
+ return ret;
+}
+
+struct ui_helpline tui_helpline_fns = {
+ .pop = tui_helpline__pop,
+ .push = tui_helpline__push,
+ .show = tui_helpline__show,
+};
+
+void ui_helpline__init(void)
+{
+ helpline_fns = &tui_helpline_fns;
+ ui_helpline__puts(" ");
+}
diff --git a/kernel/tools/perf/ui/tui/progress.c b/kernel/tools/perf/ui/tui/progress.c
new file mode 100644
index 000000000..c61d14b10
--- /dev/null
+++ b/kernel/tools/perf/ui/tui/progress.c
@@ -0,0 +1,44 @@
+#include "../cache.h"
+#include "../progress.h"
+#include "../libslang.h"
+#include "../ui.h"
+#include "tui.h"
+#include "../browser.h"
+
+static void tui_progress__update(struct ui_progress *p)
+{
+ int bar, y;
+ /*
+ * FIXME: We should have a per UI backend way of showing progress,
+ * stdio will just show a percentage as NN%, etc.
+ */
+ if (use_browser <= 0)
+ return;
+
+ if (p->total == 0)
+ return;
+
+ ui__refresh_dimensions(false);
+ pthread_mutex_lock(&ui__lock);
+ y = SLtt_Screen_Rows / 2 - 2;
+ SLsmg_set_color(0);
+ SLsmg_draw_box(y, 0, 3, SLtt_Screen_Cols);
+ SLsmg_gotorc(y++, 1);
+ SLsmg_write_string((char *)p->title);
+ SLsmg_fill_region(y, 1, 1, SLtt_Screen_Cols - 2, ' ');
+ SLsmg_set_color(HE_COLORSET_SELECTED);
+ bar = ((SLtt_Screen_Cols - 2) * p->curr) / p->total;
+ SLsmg_fill_region(y, 1, 1, bar, ' ');
+ SLsmg_refresh();
+ pthread_mutex_unlock(&ui__lock);
+}
+
+static struct ui_progress_ops tui_progress__ops =
+{
+ .update = tui_progress__update,
+};
+
+void tui_progress__init(void)
+{
+ ui_progress__ops = &tui_progress__ops;
+}
diff --git a/kernel/tools/perf/ui/tui/setup.c b/kernel/tools/perf/ui/tui/setup.c
new file mode 100644
index 000000000..b77e1d771
--- /dev/null
+++ b/kernel/tools/perf/ui/tui/setup.c
@@ -0,0 +1,174 @@
+#include <signal.h>
+#include <stdbool.h>
+#ifdef HAVE_BACKTRACE_SUPPORT
+#include <execinfo.h>
+#endif
+
+#include "../../util/cache.h"
+#include "../../util/debug.h"
+#include "../browser.h"
+#include "../helpline.h"
+#include "../ui.h"
+#include "../util.h"
+#include "../libslang.h"
+#include "../keysyms.h"
+#include "tui.h"
+
+static volatile int ui__need_resize;
+
+extern struct perf_error_ops perf_tui_eops;
+extern bool tui_helpline__set;
+
+extern void hist_browser__init_hpp(void);
+
+void ui__refresh_dimensions(bool force)
+{
+ if (force || ui__need_resize) {
+ ui__need_resize = 0;
+ pthread_mutex_lock(&ui__lock);
+ SLtt_get_screen_size();
+ SLsmg_reinit_smg();
+ pthread_mutex_unlock(&ui__lock);
+ }
+}
+
+static void ui__sigwinch(int sig __maybe_unused)
+{
+ ui__need_resize = 1;
+}
+
+static void ui__setup_sigwinch(void)
+{
+ static bool done;
+
+ if (done)
+ return;
+
+ done = true;
+ pthread__unblock_sigwinch();
+ signal(SIGWINCH, ui__sigwinch);
+}
+
+int ui__getch(int delay_secs)
+{
+ struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
+ fd_set read_set;
+ int err, key;
+
+ ui__setup_sigwinch();
+
+ FD_ZERO(&read_set);
+ FD_SET(0, &read_set);
+
+ if (delay_secs) {
+ timeout.tv_sec = delay_secs;
+ timeout.tv_usec = 0;
+ }
+
+ err = select(1, &read_set, NULL, NULL, ptimeout);
+
+ if (err == 0)
+ return K_TIMER;
+
+ if (err == -1) {
+ if (errno == EINTR)
+ return K_RESIZE;
+ return K_ERROR;
+ }
+
+ key = SLang_getkey();
+ if (key != K_ESC)
+ return key;
+
+ FD_ZERO(&read_set);
+ FD_SET(0, &read_set);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 20;
+ err = select(1, &read_set, NULL, NULL, &timeout);
+ if (err == 0)
+ return K_ESC;
+
+ SLang_ungetkey(key);
+ return SLkp_getkey();
+}
+
+#ifdef HAVE_BACKTRACE_SUPPORT
+static void ui__signal_backtrace(int sig)
+{
+ void *stackdump[32];
+ size_t size;
+
+ ui__exit(false);
+ psignal(sig, "perf");
+
+ printf("-------- backtrace --------\n");
+ size = backtrace(stackdump, ARRAY_SIZE(stackdump));
+ backtrace_symbols_fd(stackdump, size, STDOUT_FILENO);
+
+ exit(0);
+}
+#else
+# define ui__signal_backtrace ui__signal
+#endif
+
+static void ui__signal(int sig)
+{
+ ui__exit(false);
+ psignal(sig, "perf");
+ exit(0);
+}
+
+int ui__init(void)
+{
+ int err;
+
+ SLutf8_enable(-1);
+ SLtt_get_terminfo();
+ SLtt_get_screen_size();
+
+ err = SLsmg_init_smg();
+ if (err < 0)
+ goto out;
+ err = SLang_init_tty(0, 0, 0);
+ if (err < 0)
+ goto out;
+
+ err = SLkp_init();
+ if (err < 0) {
+ pr_err("TUI initialization failed.\n");
+ goto out;
+ }
+
+ SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
+
+ ui_helpline__init();
+ ui_browser__init();
+ tui_progress__init();
+
+ signal(SIGSEGV, ui__signal_backtrace);
+ signal(SIGFPE, ui__signal_backtrace);
+ signal(SIGINT, ui__signal);
+ signal(SIGQUIT, ui__signal);
+ signal(SIGTERM, ui__signal);
+
+ perf_error__register(&perf_tui_eops);
+
+ hist_browser__init_hpp();
+out:
+ return err;
+}
+
+void ui__exit(bool wait_for_ok)
+{
+ if (wait_for_ok && tui_helpline__set)
+ ui__question_window("Fatal Error",
+ ui_helpline__last_msg,
+ "Press any key...", 0);
+
+ SLtt_set_cursor_visibility(1);
+ SLsmg_refresh();
+ SLsmg_reset_smg();
+ SLang_reset_tty();
+
+ perf_error__unregister(&perf_tui_eops);
+}
diff --git a/kernel/tools/perf/ui/tui/tui.h b/kernel/tools/perf/ui/tui/tui.h
new file mode 100644
index 000000000..18961c7b6
--- /dev/null
+++ b/kernel/tools/perf/ui/tui/tui.h
@@ -0,0 +1,6 @@
+#ifndef _PERF_TUI_H_
+#define _PERF_TUI_H_ 1
+
+void tui_progress__init(void);
+
+#endif /* _PERF_TUI_H_ */
diff --git a/kernel/tools/perf/ui/tui/util.c b/kernel/tools/perf/ui/tui/util.c
new file mode 100644
index 000000000..bf890f72f
--- /dev/null
+++ b/kernel/tools/perf/ui/tui/util.c
@@ -0,0 +1,256 @@
+#include "../../util/util.h"
+#include <signal.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/ttydefaults.h>
+
+#include "../../util/cache.h"
+#include "../../util/debug.h"
+#include "../browser.h"
+#include "../keysyms.h"
+#include "../helpline.h"
+#include "../ui.h"
+#include "../util.h"
+#include "../libslang.h"
+
+static void ui_browser__argv_write(struct ui_browser *browser,
+ void *entry, int row)
+{
+ char **arg = entry;
+ bool current_entry = ui_browser__is_current_entry(browser, row);
+
+ ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
+ HE_COLORSET_NORMAL);
+ slsmg_write_nstring(*arg, browser->width);
+}
+
+static int popup_menu__run(struct ui_browser *menu)
+{
+ int key;
+
+ if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
+ return -1;
+
+ while (1) {
+ key = ui_browser__run(menu, 0);
+
+ switch (key) {
+ case K_RIGHT:
+ case K_ENTER:
+ key = menu->index;
+ break;
+ case K_LEFT:
+ case K_ESC:
+ case 'q':
+ case CTRL('c'):
+ key = -1;
+ break;
+ default:
+ continue;
+ }
+
+ break;
+ }
+
+ ui_browser__hide(menu);
+ return key;
+}
+
+int ui__popup_menu(int argc, char * const argv[])
+{
+ struct ui_browser menu = {
+ .entries = (void *)argv,
+ .refresh = ui_browser__argv_refresh,
+ .seek = ui_browser__argv_seek,
+ .write = ui_browser__argv_write,
+ .nr_entries = argc,
+ };
+
+ return popup_menu__run(&menu);
+}
+
+int ui_browser__input_window(const char *title, const char *text, char *input,
+ const char *exit_msg, int delay_secs)
+{
+ int x, y, len, key;
+ int max_len = 60, nr_lines = 0;
+ static char buf[50];
+ const char *t;
+
+ t = text;
+ while (1) {
+ const char *sep = strchr(t, '\n');
+
+ if (sep == NULL)
+ sep = strchr(t, '\0');
+ len = sep - t;
+ if (max_len < len)
+ max_len = len;
+ ++nr_lines;
+ if (*sep == '\0')
+ break;
+ t = sep + 1;
+ }
+
+ pthread_mutex_lock(&ui__lock);
+
+ max_len += 2;
+ nr_lines += 8;
+ y = SLtt_Screen_Rows / 2 - nr_lines / 2;
+ x = SLtt_Screen_Cols / 2 - max_len / 2;
+
+ SLsmg_set_color(0);
+ SLsmg_draw_box(y, x++, nr_lines, max_len);
+ if (title) {
+ SLsmg_gotorc(y, x + 1);
+ SLsmg_write_string((char *)title);
+ }
+ SLsmg_gotorc(++y, x);
+ nr_lines -= 7;
+ max_len -= 2;
+ SLsmg_write_wrapped_string((unsigned char *)text, y, x,
+ nr_lines, max_len, 1);
+ y += nr_lines;
+ len = 5;
+ while (len--) {
+ SLsmg_gotorc(y + len - 1, x);
+ SLsmg_write_nstring((char *)" ", max_len);
+ }
+ SLsmg_draw_box(y++, x + 1, 3, max_len - 2);
+
+ SLsmg_gotorc(y + 3, x);
+ SLsmg_write_nstring((char *)exit_msg, max_len);
+ SLsmg_refresh();
+
+ pthread_mutex_unlock(&ui__lock);
+
+ x += 2;
+ len = 0;
+ key = ui__getch(delay_secs);
+ while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
+ pthread_mutex_lock(&ui__lock);
+
+ if (key == K_BKSPC) {
+ if (len == 0) {
+ pthread_mutex_unlock(&ui__lock);
+ goto next_key;
+ }
+ SLsmg_gotorc(y, x + --len);
+ SLsmg_write_char(' ');
+ } else {
+ buf[len] = key;
+ SLsmg_gotorc(y, x + len++);
+ SLsmg_write_char(key);
+ }
+ SLsmg_refresh();
+
+ pthread_mutex_unlock(&ui__lock);
+
+ /* XXX more graceful overflow handling needed */
+ if (len == sizeof(buf) - 1) {
+ ui_helpline__push("maximum size of symbol name reached!");
+ key = K_ENTER;
+ break;
+ }
+next_key:
+ key = ui__getch(delay_secs);
+ }
+
+ buf[len] = '\0';
+ strncpy(input, buf, len+1);
+ return key;
+}
+
+int ui__question_window(const char *title, const char *text,
+ const char *exit_msg, int delay_secs)
+{
+ int x, y;
+ int max_len = 0, nr_lines = 0;
+ const char *t;
+
+ t = text;
+ while (1) {
+ const char *sep = strchr(t, '\n');
+ int len;
+
+ if (sep == NULL)
+ sep = strchr(t, '\0');
+ len = sep - t;
+ if (max_len < len)
+ max_len = len;
+ ++nr_lines;
+ if (*sep == '\0')
+ break;
+ t = sep + 1;
+ }
+
+ pthread_mutex_lock(&ui__lock);
+
+ max_len += 2;
+ nr_lines += 4;
+ y = SLtt_Screen_Rows / 2 - nr_lines / 2,
+ x = SLtt_Screen_Cols / 2 - max_len / 2;
+
+ SLsmg_set_color(0);
+ SLsmg_draw_box(y, x++, nr_lines, max_len);
+ if (title) {
+ SLsmg_gotorc(y, x + 1);
+ SLsmg_write_string((char *)title);
+ }
+ SLsmg_gotorc(++y, x);
+ nr_lines -= 2;
+ max_len -= 2;
+ SLsmg_write_wrapped_string((unsigned char *)text, y, x,
+ nr_lines, max_len, 1);
+ SLsmg_gotorc(y + nr_lines - 2, x);
+ SLsmg_write_nstring((char *)" ", max_len);
+ SLsmg_gotorc(y + nr_lines - 1, x);
+ SLsmg_write_nstring((char *)exit_msg, max_len);
+ SLsmg_refresh();
+
+ pthread_mutex_unlock(&ui__lock);
+
+ return ui__getch(delay_secs);
+}
+
+int ui__help_window(const char *text)
+{
+ return ui__question_window("Help", text, "Press any key...", 0);
+}
+
+int ui__dialog_yesno(const char *msg)
+{
+ return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
+}
+
+static int __ui__warning(const char *title, const char *format, va_list args)
+{
+ char *s;
+
+ if (vasprintf(&s, format, args) > 0) {
+ int key;
+
+ key = ui__question_window(title, s, "Press any key...", 0);
+ free(s);
+ return key;
+ }
+
+ fprintf(stderr, "%s\n", title);
+ vfprintf(stderr, format, args);
+ return K_ESC;
+}
+
+static int perf_tui__error(const char *format, va_list args)
+{
+ return __ui__warning("Error:", format, args);
+}
+
+static int perf_tui__warning(const char *format, va_list args)
+{
+ return __ui__warning("Warning:", format, args);
+}
+
+struct perf_error_ops perf_tui_eops = {
+ .error = perf_tui__error,
+ .warning = perf_tui__warning,
+};
pan class="o">= os_utils.get_instances(nova_client) for instance in instances: instance_id = getattr(instance, 'id') if instance_id not in default_instances: logger.debug("Waiting for instances to be terminated...") timeout -= 1 time.sleep(1) continue break def remove_images(nova_client, default_images): logger.info("Removing Glance images...") images = os_utils.get_images(nova_client) if images is None or len(images) == 0: logger.debug("No images found.") return for image in images: image_name = getattr(image, 'name') image_id = getattr(image, 'id') logger.debug("'%s', ID=%s " % (image_name, image_id)) if image_id not in default_images: logger.debug("Removing image '%s', ID=%s ..." % (image_name, image_id)) if os_utils.delete_glance_image(nova_client, image_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the" "image %s..." % image_id) else: logger.debug(" > this is a default image and will " "NOT be deleted.") def remove_volumes(cinder_client, default_volumes): logger.info("Removing Cinder volumes...") volumes = os_utils.get_volumes(cinder_client) if volumes is None or len(volumes) == 0: logger.debug("No volumes found.") return for volume in volumes: volume_id = getattr(volume, 'id') volume_name = getattr(volume, 'display_name') logger.debug("'%s', ID=%s " % (volume_name, volume_id)) if volume_id not in default_volumes: logger.debug("Removing cinder volume %s ..." % volume_id) if os_utils.delete_volume(cinder_client, volume_id): logger.debug(" > Done!") else: logger.debug("Trying forced removal...") if os_utils.delete_volume(cinder_client, volume_id, forced=True): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "volume %s..." % volume_id) else: logger.debug(" > this is a default volume and will " "NOT be deleted.") def remove_floatingips(nova_client, default_floatingips): logger.info("Removing floating IPs...") floatingips = os_utils.get_floating_ips(nova_client) if floatingips is None or len(floatingips) == 0: logger.debug("No floating IPs found.") return init_len = len(floatingips) deleted = 0 for fip in floatingips: fip_id = getattr(fip, 'id') fip_ip = getattr(fip, 'ip') logger.debug("'%s', ID=%s " % (fip_ip, fip_id)) if fip_id not in default_floatingips: logger.debug("Removing floating IP %s ..." % fip_id) if os_utils.delete_floating_ip(nova_client, fip_id): logger.debug(" > Done!") deleted += 1 else: logger.error("There has been a problem removing the " "floating IP %s..." % fip_id) else: logger.debug(" > this is a default floating IP and will " "NOT be deleted.") timeout = 50 while timeout > 0: floatingips = os_utils.get_floating_ips(nova_client) if floatingips is None or len(floatingips) == (init_len - deleted): break else: logger.debug("Waiting for floating ips to be released...") timeout -= 1 time.sleep(1) def remove_networks(neutron_client, default_networks, default_routers): logger.info("Removing Neutron objects") network_ids = [] networks = os_utils.get_network_list(neutron_client) if networks is None: logger.debug("There are no networks in the deployment. ") else: logger.debug("Existing networks:") for network in networks: net_id = network['id'] net_name = network['name'] logger.debug(" '%s', ID=%s " % (net_name, net_id)) if net_id in default_networks: logger.debug(" > this is a default network and will " "NOT be deleted.") elif network['router:external'] is True: logger.debug(" > this is an external network and will " "NOT be deleted.") else: logger.debug(" > this network will be deleted.") network_ids.append(net_id) # delete ports ports = os_utils.get_port_list(neutron_client) if ports is None: logger.debug("There are no ports in the deployment. ") else: remove_ports(neutron_client, ports, network_ids) # remove routers routers = os_utils.get_router_list(neutron_client) if routers is None: logger.debug("There are no routers in the deployment. ") else: remove_routers(neutron_client, routers, default_routers) # remove networks if network_ids is not None: for net_id in network_ids: logger.debug("Removing network %s ..." % net_id) if os_utils.delete_neutron_net(neutron_client, net_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "network %s..." % net_id) def remove_ports(neutron_client, ports, network_ids): for port in ports: if port['network_id'] in network_ids: port_id = port['id'] try: subnet_id = port['fixed_ips'][0]['subnet_id'] except: logger.info(" > WARNING: Port %s does not contain 'fixed_ips'" % port_id) print port router_id = port['device_id'] if len(port['fixed_ips']) == 0 and router_id == '': logger.debug("Removing port %s ..." % port_id) if (os_utils.delete_neutron_port(neutron_client, port_id)): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "port %s ..." % port_id) force_remove_port(neutron_client, port_id) elif port['device_owner'] == 'network:router_interface': logger.debug("Detaching port %s (subnet %s) from router %s ..." % (port_id, subnet_id, router_id)) if os_utils.remove_interface_router( neutron_client, router_id, subnet_id): time.sleep(5) # leave 5 seconds to detach logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "interface %s from router %s..." % (subnet_id, router_id)) force_remove_port(neutron_client, port_id) else: force_remove_port(neutron_client, port_id) def force_remove_port(neutron_client, port_id): logger.debug("Clearing device_owner for port %s ..." % port_id) os_utils.update_neutron_port(neutron_client, port_id, device_owner='clear') logger.debug("Removing port %s ..." % port_id) if os_utils.delete_neutron_port(neutron_client, port_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the port %s..." % port_id) def remove_routers(neutron_client, routers, default_routers): for router in routers: router_id = router['id'] router_name = router['name'] if router_id not in default_routers: logger.debug("Checking '%s' with ID=(%s) ..." % (router_name, router_id)) if router['external_gateway_info'] is not None: logger.debug("Router has gateway to external network." "Removing link...") if os_utils.remove_gateway_router(neutron_client, router_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing " "the gateway...") else: logger.debug("Router is not connected to anything." "Ready to remove...") logger.debug("Removing router %s(%s) ..." % (router_name, router_id)) if os_utils.delete_neutron_router(neutron_client, router_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "router '%s'(%s)..." % (router_name, router_id)) def remove_security_groups(neutron_client, default_security_groups): logger.info("Removing Security groups...") secgroups = os_utils.get_security_groups(neutron_client) if secgroups is None or len(secgroups) == 0: logger.debug("No security groups found.") return for secgroup in secgroups: secgroup_name = secgroup['name'] secgroup_id = secgroup['id'] logger.debug("'%s', ID=%s " % (secgroup_name, secgroup_id)) if secgroup_id not in default_security_groups: logger.debug(" Removing '%s'..." % secgroup_name) if os_utils.delete_security_group(neutron_client, secgroup_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "security group %s..." % secgroup_id) else: logger.debug(" > this is a default security group and will NOT " "be deleted.") def remove_users(keystone_client, default_users): logger.info("Removing Users...") users = os_utils.get_users(keystone_client) if users is None: logger.debug("There are no users in the deployment. ") return for user in users: user_name = getattr(user, 'name') user_id = getattr(user, 'id') logger.debug("'%s', ID=%s " % (user_name, user_id)) if user_id not in default_users: logger.debug(" Removing '%s'..." % user_name) if os_utils.delete_user(keystone_client, user_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "user '%s'(%s)..." % (user_name, user_id)) else: logger.debug(" > this is a default user and will " "NOT be deleted.") def remove_tenants(keystone_client, default_tenants): logger.info("Removing Tenants...") tenants = os_utils.get_tenants(keystone_client) if tenants is None: logger.debug("There are no tenants in the deployment. ") return for tenant in tenants: tenant_name = getattr(tenant, 'name') tenant_id = getattr(tenant, 'id') logger.debug("'%s', ID=%s " % (tenant_name, tenant_id)) if tenant_id not in default_tenants: logger.debug(" Removing '%s'..." % tenant_name) if os_utils.delete_tenant(keystone_client, tenant_id): logger.debug(" > Done!") else: logger.error("There has been a problem removing the " "tenant '%s'(%s)..." % (tenant_name, tenant_id)) else: logger.debug(" > this is a default tenant and will " "NOT be deleted.") def main(): logger.info("+++++++++++++++++++++++++++++++") logger.info("Cleaning OpenStack resources...") logger.info("+++++++++++++++++++++++++++++++") try: with open(OS_SNAPSHOT_FILE) as f: snapshot_yaml = yaml.safe_load(f) except Exception: logger.info("The file %s does not exist. The OpenStack snapshot must" " be created first. Aborting cleanup." % OS_SNAPSHOT_FILE) exit(0) default_images = snapshot_yaml.get('images') default_instances = snapshot_yaml.get('instances') default_volumes = snapshot_yaml.get('volumes') default_networks = snapshot_yaml.get('networks') default_routers = snapshot_yaml.get('routers') default_security_groups = snapshot_yaml.get('secgroups') default_floatingips = snapshot_yaml.get('floatingips') default_users = snapshot_yaml.get('users') default_tenants = snapshot_yaml.get('tenants') creds_nova = os_utils.get_credentials("nova") nova_client = novaclient.Client('2', **creds_nova) creds_neutron = os_utils.get_credentials("neutron") neutron_client = neutronclient.Client(**creds_neutron) creds_keystone = os_utils.get_credentials("keystone") keystone_client = keystoneclient.Client(**creds_keystone) creds_cinder = os_utils.get_credentials("cinder") # cinder_client = cinderclient.Client(**creds_cinder) cinder_client = cinderclient.Client('1', creds_cinder['username'], creds_cinder['api_key'], creds_cinder['project_id'], creds_cinder['auth_url'], service_type="volume") if not os_utils.check_credentials(): logger.error("Please source the openrc credentials and run " "the script again.") exit(-1) remove_instances(nova_client, default_instances) separator() remove_images(nova_client, default_images) separator() remove_volumes(cinder_client, default_volumes) separator() remove_floatingips(nova_client, default_floatingips) separator() remove_networks(neutron_client, default_networks, default_routers) separator() remove_security_groups(neutron_client, default_security_groups) separator() remove_users(keystone_client, default_users) separator() remove_tenants(keystone_client, default_tenants) separator() if __name__ == '__main__': main()