[Ovirt-devel] [PATCH viewer] permit hostname / username / password / vm to be passed in via the cmd line

Mohammed Morsi mmorsi at redhat.com
Mon Jul 13 20:50:55 UTC 2009


passing in --hostname <value> will bypass the hostname form
passing --username <value> --password <value> will bypass the login form
passing in --vm <name> will attempt to connect to the vm on login
---
 internal.h   |    9 ++++-
 main.c       |  100 +++++++++++++++++++++++++++++++++++++++++++++++----------
 wui_thread.c |   15 +--------
 3 files changed, 90 insertions(+), 34 deletions(-)

diff --git a/internal.h b/internal.h
index bd65922..8a857eb 100644
--- a/internal.h
+++ b/internal.h
@@ -70,10 +70,15 @@ extern gboolean check_cert;
 
 /* server we're connecting to */
 extern const char* hostname;
+extern const char* username;
+extern const char* password;
 
 /* port which to connect to the server via vnc */
 extern int ovirt_server_vnc_port;
 
+/* selected vm name which to automatically connect to */
+extern const char* selected_vm_name;
+
 /* vm currently in focus */
 extern struct vm* vm_in_focus;
 
@@ -101,8 +106,8 @@ extern void wui_thread_send_connect (const char *uri);
 /* Disconnect, forget URI, credentials, VMs etc. */
 extern void wui_thread_send_disconnect (void);
 
-/* Set the username and password and tell the WUI to try to log in. */
-extern void wui_thread_send_login (const char *username, const char *password);
+/* tell the WUI to try to log in with the username / password variables above */
+extern void wui_thread_send_login (void);
 
 /* Tell the WUI thread to refresh the VM list.  Note that the WUI
  * thread does this automatically anyway after a successful login, and
diff --git a/main.c b/main.c
index dd26c93..1822fdb 100644
--- a/main.c
+++ b/main.c
@@ -73,7 +73,8 @@ gboolean check_cert = FALSE; // do we want this enabled by default ?
 static GSList *vmlist = NULL;
 
 /*  internal.h shared constructs */
-const char* hostname;
+const char *hostname, *username, *password;
+const char* selected_vm_name;
 struct vm* vm_in_focus;
 int ovirt_server_vnc_port = 5900;
 
@@ -82,10 +83,14 @@ static void start_ui (void);
 static GtkWidget *menu_item_new (int which_menu);
 static void refresh_menu_vm_list (GtkWidget *, gpointer);
 static void connect_to_wui_on_enter (GtkWidget *, gpointer);
-static void connect_to_wui (GtkWidget *, gpointer);
+static void connect_to_wui_via_widget (GtkWidget *, gpointer);
+static void connect_to_wui ();
 static void send_key_to_vm (GtkWidget *widget, gpointer _keyComboDef);
 static void login_to_wui_on_enter (GtkWidget *, gpointer);
-static void login_to_wui (GtkWidget *, gpointer);
+static void login_to_wui_via_widget (GtkWidget *, gpointer);
+static void login_to_wui ();
+static void connect_to_vm_name (const char* vm_name);
+static void connect_to_vm (struct vm*);
 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
 static void destroy (GtkWidget *widget, gpointer data);
 static void clear_connectmenu (void);
@@ -201,12 +206,20 @@ static const char *help_msg =
   "Use '" PACKAGE " --help' to see a list of available command line options";
 
 static const GOptionEntry options[] = {
-  { "port", 'p', 0, G_OPTION_ARG_INT, &ovirt_server_vnc_port,
+  { "hostname", 'H', 0, G_OPTION_ARG_STRING, &hostname,
+    "hostname of the server to connect to", NULL},
+  { "port", 'P', 0, G_OPTION_ARG_INT, &ovirt_server_vnc_port,
     "set port which to connect to server via vnc", NULL },
+  { "username", 'u', 0, G_OPTION_ARG_STRING, &username,
+    "username which to connect to the server with", NULL},
+  { "password", 'p', 0, G_OPTION_ARG_STRING, &password,
+    "password which to connect to the server with", NULL},
   { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo,
     "set the path of the CA certificate bundle", NULL },
   { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert,
     "check the SSL certificate of the server", NULL },
+  { "vm", 'm', 0, G_OPTION_ARG_STRING, &selected_vm_name,
+    "name of the vm which to connect to on login", NULL},
   { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
     "turn on debugging messages", NULL },
   { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
@@ -279,6 +292,29 @@ main (int argc, char *argv[])
   start_wui_thread ();
   start_ui ();
 
+  // if parameters were passed in via 
+  // cmd line, perform necessary operations
+  // (and thus hostname/login dialogs won't
+  //  be displayed later on)
+  if (hostname != NULL && !STREQ (hostname, "")){
+    g_print("connecting to %s, may take a moment\n", hostname);
+    connect_to_wui();
+    while(!wui_thread_is_connected()) sleep(1);
+
+    if (username != NULL && !STREQ (username, "")){
+       g_print("logging in as %s, may take a moment\n", username);
+       login_to_wui();
+       while(!wui_thread_is_logged_in()) sleep(1);
+
+       if (selected_vm_name != NULL && !STREQ(selected_vm_name, "")){
+         g_print("waiting for list of vms, may take a moment\n");
+         while(!wui_thread_has_valid_vmlist()) sleep(1);
+         connect_to_vm_name(selected_vm_name);
+
+       }
+    }
+  }
+
   DEBUG ("entering the Gtk main loop");
 
   gtk_main ();
@@ -420,7 +456,7 @@ start_ui (void)
   g_signal_connect (G_OBJECT (ca_hostname), "key-release-event",
 		    G_CALLBACK (connect_to_wui_on_enter), NULL);
   g_signal_connect (G_OBJECT (ca_button), "clicked",
-		    G_CALLBACK (connect_to_wui), NULL);
+		    G_CALLBACK (connect_to_wui_via_widget), NULL);
 
   login_area = gtk_event_box_new ();
   la_vbox = gtk_vbox_new (FALSE, 0);
@@ -448,7 +484,7 @@ start_ui (void)
   g_signal_connect (G_OBJECT (la_password), "key-release-event",
 		    G_CALLBACK (login_to_wui_on_enter), NULL);
   g_signal_connect (G_OBJECT (la_button), "clicked",
-		    G_CALLBACK (login_to_wui), NULL);
+		    G_CALLBACK (login_to_wui_via_widget), NULL);
 
   /* Tabbed notebook. */
   notebook = gtk_notebook_new ();
@@ -570,16 +606,22 @@ connect_to_wui_on_enter (GtkWidget *widget, gpointer data)
   // if key released was not 'enter' key
   if(((GdkEventKey *)data)->type == GDK_KEY_RELEASE && (((GdkEventKey *)data)->keyval & 0xFF) != 13 ) return;
 
-  connect_to_wui(widget, data);
+  connect_to_wui_via_widget(widget, data);
 }
 
 static void
-connect_to_wui (GtkWidget *widget, gpointer data)
+connect_to_wui_via_widget (GtkWidget *widget, gpointer data)
+{
+  hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
+  connect_to_wui();
+}
+
+static void
+connect_to_wui()
 {
   char *uri;
   int len;
 
-  hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
   if (STREQ (hostname, "")) return;
 
   /* https:// + hostname + /ovirt + \0 */
@@ -597,19 +639,23 @@ login_to_wui_on_enter (GtkWidget *widget, gpointer data)
   // if key released was not 'enter' key
   if(((GdkEventKey *)data)->type == GDK_KEY_RELEASE && (((GdkEventKey *)data)->keyval & 0xFF) != 13 ) return;
 
-  login_to_wui(widget, data);
+  login_to_wui_via_widget(widget, data);
 }
 
 static void
-login_to_wui (GtkWidget *widget, gpointer data)
+login_to_wui_via_widget (GtkWidget *widget, gpointer data)
 {
-  const char *username, *password;
+   username = gtk_entry_get_text (GTK_ENTRY (la_username));
+   password = gtk_entry_get_text (GTK_ENTRY (la_password));
+   login_to_wui();
+}
 
-  username = gtk_entry_get_text (GTK_ENTRY (la_username));
+static void 
+login_to_wui()
+{
   if (STREQ (username, "")) return;
-  password = gtk_entry_get_text (GTK_ENTRY (la_password));
 
-  wui_thread_send_login (username, password);
+  wui_thread_send_login();
 }
 
 /* Connect to a virtual machine.  This callback is called from the
@@ -618,9 +664,27 @@ login_to_wui (GtkWidget *widget, gpointer data)
  * makes a new connection.
  */
 static void
-connect_to_vm (GtkWidget *widget, gpointer _vm)
+connect_to_vm_via_widget (GtkWidget *widget, gpointer _vm)
 {
-  struct vm *vm = (struct vm *) _vm;
+    connect_to_vm((struct vm*)_vm);
+}
+
+static void
+connect_to_vm_name(const char* vm_name)
+{
+    int i;
+    main_vmlist_updated(NULL); // dont like running this here, but need to make sure we have latest vm list
+    for(i = 0; i < g_slist_length(vmlist); ++i){
+        if(STREQ(((struct vm*) g_slist_nth_data(vmlist, i))->description, vm_name)){
+            connect_to_vm((struct vm*) g_slist_nth_data(vmlist, i));
+            break;
+        }
+    }
+}
+
+static void
+connect_to_vm(struct vm* _vm){
+  struct vm *vm =  _vm;
   int n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
   int i, uuidlen, len, fd;
   GtkWidget *child;
@@ -1144,5 +1208,5 @@ add_vm_to_connectmenu (gpointer _vm, gpointer data)
   gtk_menu_append (GTK_MENU (connectmenu), item);
 
   g_signal_connect (G_OBJECT (item), "activate",
-		    G_CALLBACK (connect_to_vm), vm);
+		    G_CALLBACK (connect_to_vm_via_widget), vm);
 }
diff --git a/wui_thread.c b/wui_thread.c
index 8bfa8ca..1688b83 100644
--- a/wui_thread.c
+++ b/wui_thread.c
@@ -179,7 +179,7 @@ wui_thread_send_disconnect (void)
 
 /* Send the login message to the WUI thread. */
 void
-wui_thread_send_login (const char *username, const char *password)
+wui_thread_send_login (void)
 {
   struct message *msg;
 
@@ -226,8 +226,6 @@ static int secs_between_refresh = 60;
 static CURL *curl = NULL;
 static char curl_error_buffer[CURL_ERROR_SIZE];
 static char *uri = NULL;
-static char *username = NULL;
-static char *password = NULL;
 
 static gboolean process_message (struct message *);
 
@@ -396,8 +394,6 @@ process_message (struct message *msg)
     write_fn_discard_capture_buffer ();
     if (curl) curl_easy_cleanup (curl);
     if (uri) g_free (uri);
-    if (username) g_free (username);
-    if (password) g_free (password);
     set_connected (FALSE);
     set_logged_in (FALSE);
     return 1;
@@ -424,20 +420,11 @@ process_message (struct message *msg)
     curl = NULL;
     if (uri) g_free (uri);
     uri = NULL;
-    if (username) g_free (username);
-    username = NULL;
-    if (password) g_free (password);
-    password = NULL;
     set_connected (FALSE);
     set_logged_in (FALSE);
     break;
 
   case LOGIN:
-    if (username) g_free (username);
-    username = msg->str1;
-    if (password) g_free (password);
-    password = msg->str2;
-
     /* If we're not connected, this message just updates the
      * username and password.  Otherwise if we are connected,
      * try to login and grab the initial list of VMs.
-- 
1.6.0.6




More information about the ovirt-devel mailing list