Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cli: add net/filter options

  • Loading branch information...
commit dad8f00caf3c1a1e80c0d73eef5973b617c551a9 1 parent 276b1db
Michael Santos authored September 16, 2012
6  bin/get_image.escript
@@ -99,6 +99,6 @@ interface(Bridge) ->
99 99
     ).
100 100
 
101 101
 macaddr() ->
102  
-    "52:54:00:" ++ lists:flatten(string:join(
103  
-                [ io_lib:format("~.16B", [N]) || <<N>> <= crypto:rand_bytes(3) ],
104  
-                ":")).
  102
+    "52:54:00:" ++ string:join(
  103
+                [ httpd_util:integer_to_hexlist(N) || <<N>> <= crypto:rand_bytes(3) ],
  104
+                ":").
160  bin/verx
@@ -19,6 +19,13 @@ main(Opt) ->
19 19
 
20 20
     call(Opt).
21 21
 
  22
+call(["autostart", Name | Arg]) ->
  23
+    Opt = getopt(Arg),
  24
+    Disable = proplists:get_value("disable", Opt, false),
  25
+    {ok, Ref} = connect(Opt),
  26
+    {ok, [Res]} = lookup(Ref, {domain, Name}),
  27
+    rp(verx:domain_set_autostart(Ref, [Res, 1 bxor bool(Disable)]));
  28
+
22 29
 call(["capabilities" | Arg]) ->
23 30
     Opt = getopt(Arg),
24 31
     {ok, Ref} = connect(Opt),
@@ -61,27 +68,24 @@ call(["dumpxml", Name | Arg]) ->
61 68
     {ok, [Domain]} = lookup(Ref, {domain, Name}),
62 69
     rp(verx:domain_get_xml_desc(Ref, [Domain, Flags]));
63 70
 
64  
-call(["list", "--all" | Arg]) ->
  71
+call(["list" | Arg]) ->
65 72
     Opt = getopt(Arg),
66 73
     {ok, Ref} = connect(Opt),
67  
-
68  
-    {ok, [NumDef]} = verx:num_of_defined_domains(Ref),
  74
+    All = proplists:get_value("all", Opt, false),
69 75
 
70 76
     {ok, [NumRun]} = verx:num_of_domains(Ref),
71  
-
72  
-    {ok, [Shutoff]} = verx:list_defined_domains(Ref, [NumDef]),
73 77
     {ok, [Running]} = verx:list_domains(Ref, [NumRun]),
74 78
 
75  
-    rp({ok, [{running, domains(Ref, Running)},
76  
-             {shutoff, domains(Ref, Shutoff)}]});
77  
-
78  
-call(["list" | Arg]) ->
79  
-    Opt = getopt(Arg),
80  
-    {ok, Ref} = connect(Opt),
81  
-    {ok, [N]} = verx:num_of_domains(Ref),
82  
-    {ok, [Domains]} =  verx:list_domains(Ref, [N]),
  79
+    N = case All of
  80
+        true ->
  81
+            {ok, [NumDef]} = verx:num_of_defined_domains(Ref),
  82
+            {ok, [Shutoff]} = verx:list_defined_domains(Ref, [NumDef]),
  83
+            [{shutoff, domains(Ref, Shutoff)}];
  84
+        false ->
  85
+            []
  86
+    end,
83 87
 
84  
-    rp(domains(Ref, Domains));
  88
+    rp({ok, [{running, domains(Ref, Running)}|N]});
85 89
 
86 90
 call(["screenshot", Name | Arg]) ->
87 91
     Opt = getopt(Arg),
@@ -192,8 +196,105 @@ call(["vol-download", Name, File | Arg]) ->
192 196
     download(Ref, File, Timeout),
193 197
     rp(ok);
194 198
 
  199
+call(["net-autostart", Name | Arg]) ->
  200
+    Opt = getopt(Arg),
  201
+    Disable = proplists:get_value("disable", Opt, false),
  202
+    {ok, Ref} = connect(Opt),
  203
+    {ok, [Res]} = lookup(Ref, {net, Name}),
  204
+    rp(verx:network_set_autostart(Ref, [Res, 1 bxor bool(Disable)]));
  205
+
  206
+call(["net-create", Name | Arg]) ->
  207
+    Opt = getopt(Arg),
  208
+    {ok, Ref} = connect(Opt),
  209
+
  210
+    {ok, [Res]} = case file:read_file(Name) of
  211
+        {ok, XML} ->
  212
+            verx:network_define_xml(Ref, [XML]);
  213
+        {error, enoent} ->
  214
+            lookup(Ref, {net, Name})
  215
+        end,
  216
+    rp(verx:network_create(Ref, [Res]));
  217
+
  218
+call(["net-define", File | Arg]) ->
  219
+    Opt = getopt(Arg),
  220
+    {ok, Ref} = connect(Opt),
  221
+    {ok, XML} = file:read_file(File),
  222
+    rp(verx:network_define_xml(Ref, [XML]));
  223
+
  224
+call(["net-destroy", Name | Arg]) ->
  225
+    Opt = getopt(Arg),
  226
+    {ok, Ref} = connect(Opt),
  227
+    {ok, [Res]} = lookup(Ref, {net, Name}),
  228
+    rp(verx:network_destroy(Ref, [Res]));
  229
+
  230
+call(["net-dumpxml", Name | Arg]) ->
  231
+    Opt = getopt(Arg),
  232
+    Flags = proplists:get_value("flags", Opt, 0),
  233
+    {ok, Ref} = connect(Opt),
  234
+    {ok, [Res]} = lookup(Ref, {net, Name}),
  235
+    rp(verx:network_get_xml_desc(Ref, [Res, Flags]));
  236
+
  237
+call(["net-info", Name | Arg]) ->
  238
+    Opt = getopt(Arg),
  239
+    {ok, Ref} = connect(Opt),
  240
+    {ok, [Res]} = lookup(Ref, {net, Name}),
  241
+
  242
+    {ok, [Persistent]} = verx:network_is_persistent(Ref, [Res]),
  243
+    {ok, [Autostart]} = verx:network_get_autostart(Ref, [Res]),
  244
+    {ok, [Bridge]} = verx:network_get_bridge_name(Ref, [Res]),
  245
+
  246
+    rp({ok, [{persistent, bool(Persistent)},
  247
+             {autostart, bool(Autostart)},
  248
+             {bridgename, Bridge}]});
  249
+
  250
+call(["net-list" | Arg]) ->
  251
+    Opt = getopt(Arg),
  252
+    {ok, Ref} = connect(Opt),
  253
+
  254
+    {ok, [Num]} = verx:num_of_networks(Ref),
  255
+    {ok, [List]} = verx:list_networks(Ref, [Num]),
  256
+
  257
+    rp({ok, [ begin
  258
+                {ok, [Res]} = lookup(Ref, {net, Name}),
  259
+                {ok, [Active]} = verx:network_is_active(Ref, [Res]),
  260
+                {ok, [Autostart]} = verx:network_get_autostart(Ref, [Res]),
  261
+                {Name, [{state, bool(Active)}, {autostart, bool(Autostart)}]}
  262
+              end || Name <- List ]});
  263
+
  264
+call(["net-undefine", Name | Arg]) ->
  265
+    Opt = getopt(Arg),
  266
+    {ok, Ref} = connect(Opt),
  267
+    {ok, [Res]} = lookup(Ref, {net, Name}),
  268
+    rp(verx:network_undefine(Ref, [Res]));
  269
+
  270
+call(["nwfilter-define", File | Arg]) ->
  271
+    Opt = getopt(Arg),
  272
+    {ok, Ref} = connect(Opt),
  273
+    {ok, XML} = file:read_file(File),
  274
+    rp(verx:nwfilter_define_xml(Ref, [XML]));
  275
+
  276
+call(["nwfilter-dumpxml", Name | Arg]) ->
  277
+    Opt = getopt(Arg),
  278
+    Flags = proplists:get_value("flags", Opt, 0),
  279
+    {ok, Ref} = connect(Opt),
  280
+    {ok, [Res]} = lookup(Ref, {filter, Name}),
  281
+    rp(verx:nwfilter_get_xml_desc(Ref, [Res, Flags]));
  282
+
  283
+call(["nwfilter-list" | Arg]) ->
  284
+    Opt = getopt(Arg),
  285
+    {ok, Ref} = connect(Opt),
  286
+    {ok, [Num]} = verx:num_of_nwfilters(Ref),
  287
+    rp(verx:list_nwfilters(Ref, [Num]));
  288
+
  289
+call(["nwfilter-undefine", Name | Arg]) ->
  290
+    Opt = getopt(Arg),
  291
+    {ok, Ref} = connect(Opt),
  292
+    {ok, [Res]} = lookup(Ref, {filter, Name}),
  293
+    rp(verx:nwfilter_undefine(Ref, [Res]));
  294
+
195 295
 call(_) ->
196 296
     Help = [
  297
+        "autostart",
197 298
         "capabilities",
198 299
         "console",
199 300
         "create",
@@ -202,7 +303,6 @@ call(_) ->
202 303
         "destroy",
203 304
         "dumpxml",
204 305
         "list",
205  
-        "list --all",
206 306
         "screenshot",
207 307
         "recv",
208 308
         "send",
@@ -210,6 +310,22 @@ call(_) ->
210 310
         "shutdown",
211 311
         "undefine",
212 312
 
  313
+        "net-autostart",
  314
+        "net-create",
  315
+        "net-define",
  316
+        "net-destroy ",
  317
+        "net-dumpxml",
  318
+        "net-info",
  319
+        "net-list",
  320
+        "net-start",
  321
+        "net-undefine",
  322
+        "net-uuid",
  323
+
  324
+        "nwfilter-define",
  325
+        "nwfilter-dumpxml",
  326
+        "nwfilter-list",
  327
+        "nwfilter-undefine"
  328
+
213 329
         "pool-list",
214 330
         "vol-list",
215 331
         "vol-download"
@@ -286,6 +402,16 @@ lookup(Ref, {domain, Name}) ->
286 402
             fun() -> verx:domain_lookup_by_uuid(Ref, [uuid:string_to_uuid(Name)]) end ],
287 403
     lookup_1(Fun);
288 404
 
  405
+lookup(Ref, {filter, Name}) ->
  406
+    Fun = [ fun() -> verx:nwfilter_lookup_by_uuid(Ref, [uuid:string_to_uuid(Name)]) end,
  407
+            fun() -> verx:nwfilter_lookup_by_name(Ref, [maybe_binary(Name)]) end ],
  408
+    lookup_1(Fun);
  409
+
  410
+lookup(Ref, {net, Name}) ->
  411
+    Fun = [ fun() -> verx:network_lookup_by_uuid(Ref, [uuid:string_to_uuid(Name)]) end,
  412
+            fun() -> verx:network_lookup_by_name(Ref, [maybe_binary(Name)]) end ],
  413
+    lookup_1(Fun);
  414
+
289 415
 lookup(Ref, {pool, Name}) ->
290 416
     Fun = [ fun() -> verx:storage_pool_lookup_by_name(Ref, [maybe_binary(Name)]) end,
291 417
             fun() -> verx:storage_pool_lookup_by_uuid(Ref, [uuid:string_to_uuid(Name)]) end ],
@@ -318,7 +444,9 @@ maybe_binary(N) when is_binary(N) -> N;
318 444
 maybe_binary(N) when is_list(N) -> list_to_binary(N).
319 445
 
320 446
 bool(0) -> false;
321  
-bool(1) -> true.
  447
+bool(1) -> true;
  448
+bool(false) -> 0;
  449
+bool(true) -> 1.
322 450
 
323 451
 default_port(verx_client_tcp) -> ?LIBVIRT_TCP_PORT;
324 452
 default_port(verx_client_tls) -> ?LIBVIRT_TLS_PORT;

0 notes on commit dad8f00

Please sign in to comment.
Something went wrong with that request. Please try again.