summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/adapter/vyos1x_adapter.ml2
-rw-r--r--src/commit.ml103
-rw-r--r--src/commit.mli17
-rw-r--r--src/dune48
-rw-r--r--src/session.ml6
-rw-r--r--src/startup.ml10
-rw-r--r--src/validate.ml2
-rw-r--r--src/vycall_client.ml47
-rw-r--r--src/vycall_client.mli2
-rw-r--r--src/vycall_pbt.ml36
-rw-r--r--src/vycall_pbt.mli4
-rw-r--r--src/vycli.ml4
-rw-r--r--src/vyconf_client.ml24
-rw-r--r--src/vyconf_client.mli7
-rw-r--r--src/vyconf_pbt.ml18
-rw-r--r--src/vyconf_pbt.mli2
-rw-r--r--src/vyconfd.ml69
17 files changed, 232 insertions, 169 deletions
diff --git a/src/adapter/vyos1x_adapter.ml b/src/adapter/vyos1x_adapter.ml
index dedb2cd..d4dbac7 100644
--- a/src/adapter/vyos1x_adapter.ml
+++ b/src/adapter/vyos1x_adapter.ml
@@ -44,7 +44,7 @@ let delete_path_reversed handle path _len =
let path = List.rev path in
cstore_delete_path handle path
-module VC = Client.Vyconf_client_session
+module VC = Vyconfd_client.Vyconf_client_session
let get_sockname =
"/var/run/vyconfd.sock"
diff --git a/src/commit.ml b/src/commit.ml
index a2b9f6e..ba740b5 100644
--- a/src/commit.ml
+++ b/src/commit.ml
@@ -2,7 +2,8 @@ module VT = Vyos1x.Vytree
module CT = Vyos1x.Config_tree
module CD = Vyos1x.Config_diff
module RT = Vyos1x.Reference_tree
-module FP = FilePath
+
+exception Commit_error of string
type tree_source = DELETE | ADD
@@ -33,31 +34,36 @@ let default_node_data = {
arg_value = None;
path = [];
source = ADD;
- reply = Some { success = false; out = ""; };
+ reply = None;
}
type commit_data = {
session_id: string;
- named_active : string option;
- named_proposed : string option;
dry_run: bool;
atomic: bool;
background: bool;
init: status option;
node_list: node_data list;
+ config_diff: CT.t;
+ config_result: CT.t;
+ result : status;
} [@@deriving to_yojson]
let default_commit_data = {
session_id = "";
- named_active = None;
- named_proposed = None;
dry_run = false;
atomic = false;
background = false;
- init = Some { success = false; out = ""; };
+ init = None;
node_list = [];
+ config_diff = CT.default;
+ config_result = CT.default;
+ result = { success = true; out = ""; };
}
+let fail_status msg =
+ { success=false; out=msg }
+
let lex_order c1 c2 =
let c = Vyos1x.Util.lex_order c1.path c2.path in
match c with
@@ -155,8 +161,7 @@ let legacy_order del_t a b =
in
CS.fold shift a (a, b)
-let calculate_priority_lists rt at wt =
- let diff = CD.diff_tree [] at wt in
+let calculate_priority_lists rt diff =
let del_tree = CD.get_tagged_delete_tree diff in
let add_tree = CT.get_subtree diff ["add"] in
let cs_del' = get_commit_set rt del_tree DELETE in
@@ -176,21 +181,69 @@ let commit_store c_data =
in List.fold_left func "" c_data.node_list
in print_endline out
-let show_commit_data at wt =
- let vc =
- Startup.load_daemon_config Defaults.defaults.config_file in
- let rt_opt =
- Startup.read_reference_tree (FP.concat vc.reftree_dir vc.reference_tree)
- in
- match rt_opt with
- | Error msg -> msg
- | Ok rt ->
- let del_list, add_list =
- calculate_priority_lists rt at wt
+(* The base config_result is the intersection of running and proposed
+ configs:
+ on success, added paths are added; deleted paths are ignored
+ on failure, deleted paths are added back in, added paths ignored
+ *)
+let config_result_update c_data n_data =
+ match n_data.reply with
+ | None -> c_data (* already exluded in calling function *)
+ | Some r ->
+ match r.success, n_data.source with
+ | true, ADD ->
+ let add = CT.get_subtree c_data.config_diff ["add"] in
+ let add_tree = CD.clone add (CT.default) n_data.path in
+ let config = CD.tree_union add_tree c_data.config_result in
+ let result =
+ { success = c_data.result.success && true;
+ out = c_data.result.out ^ r.out; }
+ in
+ { c_data with config_result = config; result = result; }
+ | false, DELETE ->
+ let del = CT.get_subtree c_data.config_diff ["del"] in
+ let add_tree = CD.clone del (CT.default) n_data.path in
+ let config = CD.tree_union add_tree c_data.config_result in
+ let result =
+ { success = c_data.result.success && false;
+ out = c_data.result.out ^ r.out; }
in
- let sprint_node_data acc s =
- acc ^ "\n" ^ (node_data_to_yojson s |> Yojson.Safe.to_string)
+ { c_data with config_result = config; result = result; }
+ | true, DELETE ->
+ let result =
+ { success = c_data.result.success && true;
+ out = c_data.result.out ^ r.out; }
in
- let del_out = List.fold_left sprint_node_data "" del_list in
- let add_out = List.fold_left sprint_node_data "" add_list in
- del_out ^ "\n" ^ add_out
+ { c_data with result = result; }
+ | false, ADD ->
+ let result =
+ { success = c_data.result.success && false;
+ out = c_data.result.out ^ r.out; }
+ in
+ { c_data with result = result; }
+
+
+let commit_update c_data =
+ match c_data.init with
+ | None ->
+ { default_commit_data with
+ init=Some (fail_status "commitd failure: no init status provided")
+ }
+ | Some _ ->
+ let func acc_data nd =
+ match nd.reply with
+ | None ->
+ { default_commit_data with
+ init=Some (fail_status"commitd failure: no reply status provided")
+ }
+ | Some _ -> config_result_update acc_data nd
+ in List.fold_left func c_data c_data.node_list
+
+let make_commit_data rt at wt id =
+ let diff = CD.diff_tree [] at wt in
+ let del_list, add_list = calculate_priority_lists rt diff in
+ { default_commit_data with
+ session_id = id;
+ config_diff = diff;
+ config_result = CT.get_subtree diff ["inter"];
+ node_list = del_list @ add_list; }
diff --git a/src/commit.mli b/src/commit.mli
index da97389..12ad084 100644
--- a/src/commit.mli
+++ b/src/commit.mli
@@ -17,21 +17,28 @@ type node_data = {
type commit_data = {
session_id: string;
- named_active : string option;
- named_proposed : string option;
dry_run: bool;
atomic: bool;
background: bool;
init: status option;
node_list: node_data list;
+ config_diff: Vyos1x.Config_tree.t;
+ config_result: Vyos1x.Config_tree.t;
+ result: status;
} [@@deriving to_yojson]
+exception Commit_error of string
+
+val tree_source_to_yojson : tree_source -> [> `String of string ]
+
val default_node_data : node_data
val default_commit_data : commit_data
-val calculate_priority_lists : Vyos1x.Reference_tree.t -> Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> node_data list * node_data list
+val make_commit_data : Vyos1x.Reference_tree.t -> Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> string -> commit_data
-val commit_store : commit_data -> unit
+val calculate_priority_lists : Vyos1x.Reference_tree.t -> Vyos1x.Config_tree.t -> node_data list * node_data list
-val show_commit_data : Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> string
+val commit_update : commit_data -> commit_data
+
+val commit_store : commit_data -> unit
diff --git a/src/dune b/src/dune
index 5e08579..63e1167 100644
--- a/src/dune
+++ b/src/dune
@@ -4,23 +4,8 @@
(name vyconf_connect)
(public_name vyconf.vyconf-connect)
(modules vyconf_pbt message)
- (libraries lwt lwt.unix lwt_log lwt_ppx ocaml-protoc fileutils ppx_deriving_yojson)
- (preprocess (pps lwt_ppx ppx_deriving_yojson)))
-
-(library
- (name vyconfd_config)
- (public_name vyconf.vyconfd-config)
- (modules vyconf_config startup session directories defaults commit)
- (libraries vyos1x-config vyconf_connect toml sha ppx_deriving.show)
- (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving_yojson)))
-
-(library
- (name client)
- (public_name vyconf.vyconf-client)
- (modules vyconf_client vyconf_client_session)
- (libraries vyos1x-config vyconf_connect lwt lwt.unix lwt_log lwt_ppx ocaml-protoc toml sha
- yojson ppx_deriving.show ppx_deriving_yojson)
- (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving_yojson)))
+ (libraries lwt lwt.unix lwt_log lwt_ppx ocaml-protoc ocplib-endian)
+ (preprocess (pps lwt_ppx)))
(library
(name vycall_message)
@@ -31,8 +16,22 @@
(library
(name commitd_client)
(public_name vyconf.vycall-client)
- (modules vycall_client)
- (libraries vyos1x-config vyconfd_config vycall_message lwt lwt.unix lwt_log lwt_ppx ocplib-endian)
+ (modules vycall_client commit)
+ (libraries vyos1x-config vycall_message lwt lwt.unix lwt_log lwt_ppx ocplib-endian)
+ (preprocess (pps lwt_ppx)))
+
+(library
+ (name vyconfd_config)
+ (public_name vyconf.vyconfd-config)
+ (modules vyconf_config startup session directories defaults)
+ (libraries vyos1x-config vyconf_connect commitd_client toml sha ppx_deriving.show yojson ppx_deriving_yojson)
+ (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving_yojson)))
+
+(library
+ (name vyconfd_client)
+ (public_name vyconf.vyconf-client)
+ (modules vyconf_client vyconf_client_session)
+ (libraries vyos1x-config vyconf_connect lwt lwt.unix lwt_log lwt_ppx)
(preprocess (pps lwt_ppx)))
(executable
@@ -46,21 +45,14 @@
(name vycli)
(public_name vycli)
(modules vycli)
- (libraries client)
+ (libraries vyconfd_client)
(preprocess (pps lwt_ppx)))
(executable
(name validate)
(public_name validate)
(modules validate)
- (libraries client))
-
-(executable
- (name test_commit_data)
- (public_name test-commit-data)
- (modules test_commit_data)
- (libraries vyos1x-config vyconfd_config)
- (preprocess (pps lwt_ppx)))
+ (libraries vyconfd_client))
(rule
(mode promote)
diff --git a/src/session.ml b/src/session.ml
index 567f999..602ab14 100644
--- a/src/session.ml
+++ b/src/session.ml
@@ -78,7 +78,11 @@ let set w s path =
let refpath = RT.refpath w.reference_tree path in
let value_behaviour = if RT.is_multi w.reference_tree refpath then CT.AddValue else CT.ReplaceValue in
let op = CfgSet (path, value, value_behaviour) in
- let config = apply_cfg_op op s.proposed_config in
+ let config =
+ apply_cfg_op op s.proposed_config |>
+ (fun c -> RT.set_tag_data w.reference_tree c path) |>
+ (fun c -> RT.set_leaf_data w.reference_tree c path)
+ in
{s with proposed_config=config; changeset=(op :: s.changeset)}
let delete w s path =
diff --git a/src/startup.ml b/src/startup.ml
index db2784d..76599b0 100644
--- a/src/startup.ml
+++ b/src/startup.ml
@@ -78,12 +78,20 @@ let create_server accept_connection sock =
Lwt_unix.accept sock >>= accept_connection >>= serve
in serve
+(* strip commponent version string *)
+let strip_version s =
+ let rex = Pcre.regexp ~flags:[`MULTILINE;`DOTALL] "(^//.*)" in
+ let res = Pcre.split ~max:0 ~rex s in
+ match res with
+ | h :: _ -> h
+ | [] -> panic "Failure applying regex to config string"
+
(** Load the appliance configuration file *)
let load_config file =
try
let chan = open_in file in
let s = really_input_string chan (in_channel_length chan) in
- let config = Vyos1x.Parser.from_string s in
+ let config = strip_version s |> Vyos1x.Parser.from_string in
Ok config
with
| Sys_error msg -> Error msg
diff --git a/src/validate.ml b/src/validate.ml
index 7b3b596..875bc7a 100644
--- a/src/validate.ml
+++ b/src/validate.ml
@@ -1,4 +1,4 @@
-open Client.Vyconf_client_session
+open Vyconfd_client.Vyconf_client_session
let path_opt = ref ""
diff --git a/src/vycall_client.ml b/src/vycall_client.ml
index c7fc811..11bbe3f 100644
--- a/src/vycall_client.ml
+++ b/src/vycall_client.ml
@@ -1,12 +1,10 @@
(* send commit data to Python commit daemon *)
open Vycall_message.Vycall_pbt
-open Vyconfd_config.Commit
+open Commit
module CT = Vyos1x.Config_tree
module IC = Vyos1x.Internal.Make(CT)
-module ST = Vyconfd_config.Startup
-module DF = Vyconfd_config.Defaults
module FP = FilePath
type t = {
@@ -33,8 +31,6 @@ let call_to_node_data ((c: call), (nd: node_data)) =
let commit_data_to_commit_proto cd =
{ session_id = cd.session_id;
- named_active = cd.named_active;
- named_proposed = cd.named_proposed;
dry_run = cd.dry_run;
atomic = cd.atomic;
background = cd.background;
@@ -88,37 +84,10 @@ let create sockfile =
let oc = Lwt_io.of_fd ~mode:Lwt_io.Output sock in
Lwt.return { ic=ic; oc=oc; }
-let update session_data =
- Lwt.return (commit_store session_data)
-
-let do_commit session_data =
- let session = commit_data_to_commit_proto session_data in
- let run () =
- let sockfile = "/run/vyos-commitd.sock" in
- let%lwt client = create sockfile in
- let%lwt resp = do_call client session in
- let%lwt () = Lwt_io.close client.oc in
- update (commit_proto_to_commit_data resp session_data)
- in Lwt_main.run @@ run ()
-
-(* test function *)
-let test_commit at wt =
- let vc =
- ST.load_daemon_config DF.defaults.config_file in
- let () =
- IC.write_internal at (FP.concat vc.session_dir vc.running_cache) in
- let () =
- IC.write_internal wt (FP.concat vc.session_dir vc.session_cache) in
- let rt_opt =
- ST.read_reference_tree (FP.concat vc.reftree_dir vc.reference_tree)
- in
- match rt_opt with
- | Error msg -> print_endline msg
- | Ok rt ->
- let del_list, add_list =
- calculate_priority_lists rt at wt
- in
- let commit_session =
- { default_commit_data with node_list = del_list @ add_list }
- in
- do_commit commit_session
+let do_commit commit_data =
+ let session = commit_data_to_commit_proto commit_data in
+ let sockfile = "/run/vyos-commitd.sock" in
+ let%lwt client = create sockfile in
+ let%lwt resp = do_call client session in
+ let%lwt () = Lwt_io.close client.oc in
+ Lwt.return(commit_proto_to_commit_data resp commit_data)
diff --git a/src/vycall_client.mli b/src/vycall_client.mli
index 8836f73..fe19231 100644
--- a/src/vycall_client.mli
+++ b/src/vycall_client.mli
@@ -1 +1 @@
-val test_commit : Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t -> unit
+val do_commit : Commit.commit_data -> Commit.commit_data Lwt.t
diff --git a/src/vycall_pbt.ml b/src/vycall_pbt.ml
index 8cccb0a..715c453 100644
--- a/src/vycall_pbt.ml
+++ b/src/vycall_pbt.ml
@@ -14,8 +14,6 @@ type call = {
type commit = {
session_id : string;
- named_active : string option;
- named_proposed : string option;
dry_run : bool;
atomic : bool;
background : bool;
@@ -45,8 +43,6 @@ let rec default_call
let rec default_commit
?session_id:((session_id:string) = "")
- ?named_active:((named_active:string option) = None)
- ?named_proposed:((named_proposed:string option) = None)
?dry_run:((dry_run:bool) = false)
?atomic:((atomic:bool) = false)
?background:((background:bool) = false)
@@ -54,8 +50,6 @@ let rec default_commit
?calls:((calls:call list) = [])
() : commit = {
session_id;
- named_active;
- named_proposed;
dry_run;
atomic;
background;
@@ -89,8 +83,6 @@ let default_call_mutable () : call_mutable = {
type commit_mutable = {
mutable session_id : string;
- mutable named_active : string option;
- mutable named_proposed : string option;
mutable dry_run : bool;
mutable atomic : bool;
mutable background : bool;
@@ -100,8 +92,6 @@ type commit_mutable = {
let default_commit_mutable () : commit_mutable = {
session_id = "";
- named_active = None;
- named_proposed = None;
dry_run = false;
atomic = false;
background = false;
@@ -132,8 +122,6 @@ let rec pp_call fmt (v:call) =
let rec pp_commit fmt (v:commit) =
let pp_i fmt () =
Pbrt.Pp.pp_record_field ~first:true "session_id" Pbrt.Pp.pp_string fmt v.session_id;
- Pbrt.Pp.pp_record_field ~first:false "named_active" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.named_active;
- Pbrt.Pp.pp_record_field ~first:false "named_proposed" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.named_proposed;
Pbrt.Pp.pp_record_field ~first:false "dry_run" Pbrt.Pp.pp_bool fmt v.dry_run;
Pbrt.Pp.pp_record_field ~first:false "atomic" Pbrt.Pp.pp_bool fmt v.atomic;
Pbrt.Pp.pp_record_field ~first:false "background" Pbrt.Pp.pp_bool fmt v.background;
@@ -179,18 +167,6 @@ let rec encode_pb_call (v:call) encoder =
let rec encode_pb_commit (v:commit) encoder =
Pbrt.Encoder.string v.session_id encoder;
Pbrt.Encoder.key 1 Pbrt.Bytes encoder;
- begin match v.named_active with
- | Some x ->
- Pbrt.Encoder.string x encoder;
- Pbrt.Encoder.key 2 Pbrt.Bytes encoder;
- | None -> ();
- end;
- begin match v.named_proposed with
- | Some x ->
- Pbrt.Encoder.string x encoder;
- Pbrt.Encoder.key 3 Pbrt.Bytes encoder;
- | None -> ();
- end;
Pbrt.Encoder.bool v.dry_run encoder;
Pbrt.Encoder.key 4 Pbrt.Varint encoder;
Pbrt.Encoder.bool v.atomic encoder;
@@ -296,16 +272,6 @@ let rec decode_pb_commit d =
end
| Some (1, pk) ->
Pbrt.Decoder.unexpected_payload "Message(commit), field(1)" pk
- | Some (2, Pbrt.Bytes) -> begin
- v.named_active <- Some (Pbrt.Decoder.string d);
- end
- | Some (2, pk) ->
- Pbrt.Decoder.unexpected_payload "Message(commit), field(2)" pk
- | Some (3, Pbrt.Bytes) -> begin
- v.named_proposed <- Some (Pbrt.Decoder.string d);
- end
- | Some (3, pk) ->
- Pbrt.Decoder.unexpected_payload "Message(commit), field(3)" pk
| Some (4, Pbrt.Varint) -> begin
v.dry_run <- Pbrt.Decoder.bool d; dry_run_is_set := true;
end
@@ -339,8 +305,6 @@ let rec decode_pb_commit d =
begin if not !session_id_is_set then Pbrt.Decoder.missing_field "session_id" end;
({
session_id = v.session_id;
- named_active = v.named_active;
- named_proposed = v.named_proposed;
dry_run = v.dry_run;
atomic = v.atomic;
background = v.background;
diff --git a/src/vycall_pbt.mli b/src/vycall_pbt.mli
index 5c8bd20..f389302 100644
--- a/src/vycall_pbt.mli
+++ b/src/vycall_pbt.mli
@@ -21,8 +21,6 @@ type call = {
type commit = {
session_id : string;
- named_active : string option;
- named_proposed : string option;
dry_run : bool;
atomic : bool;
background : bool;
@@ -51,8 +49,6 @@ val default_call :
val default_commit :
?session_id:string ->
- ?named_active:string option ->
- ?named_proposed:string option ->
?dry_run:bool ->
?atomic:bool ->
?background:bool ->
diff --git a/src/vycli.ml b/src/vycli.ml
index f793ae0..bd14713 100644
--- a/src/vycli.ml
+++ b/src/vycli.ml
@@ -1,4 +1,4 @@
-open Client.Vyconf_client
+open Vyconfd_client.Vyconf_client
open Vyconf_connect.Vyconf_pbt
type op_t =
@@ -53,7 +53,7 @@ let output_format_of_string s =
| _ -> failwith (Printf.sprintf "Unknown output format %s, should be plain or json" s)
let main socket op path out_format config_format =
- let%lwt client = Client.Vyconf_client.create ~token:!token socket out_format config_format in
+ let%lwt client = create ~token:!token socket out_format config_format in
let%lwt result = match op with
| None -> Error "Operation required" |> Lwt.return
| Some o ->
diff --git a/src/vyconf_client.ml b/src/vyconf_client.ml
index 5437428..1f00f29 100644
--- a/src/vyconf_client.ml
+++ b/src/vyconf_client.ml
@@ -118,6 +118,30 @@ let validate client path =
| Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return
| _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
+let set client path =
+ let req = Set {path=path;} in
+ let%lwt resp = do_request client req in
+ match resp.status with
+ | Success -> Lwt.return (Ok "")
+ | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
+
+let delete client path =
+ let req = Delete {path=path;} in
+ let%lwt resp = do_request client req in
+ match resp.status with
+ | Success -> Lwt.return (Ok "")
+ | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
+
+let commit client =
+ let req = Commit {confirm=None; confirm_timeout=None; comment=None;} in
+ let%lwt resp = do_request client req in
+ match resp.status with
+ | Success -> Ok (Option.value resp.output ~default:"") |> Lwt.return
+ | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
+
let reload_reftree ?(on_behalf_of=None) client =
let id = on_behalf_of |> (function None -> None | Some x -> (Some (Int32.of_int x))) in
let req = Reload_reftree {on_behalf_of=id} in
diff --git a/src/vyconf_client.mli b/src/vyconf_client.mli
index 6d74412..5fd4df4 100644
--- a/src/vyconf_client.mli
+++ b/src/vyconf_client.mli
@@ -43,4 +43,11 @@ val show_config : t -> string list -> (string, string) result Lwt.t
val validate : t -> string list -> (string, string) result Lwt.t
+val set : t -> string list -> (string, string) result Lwt.t
+
+val delete : t -> string list -> (string, string) result Lwt.t
+
+val commit : t -> (string, string) result Lwt.t
+
+
val reload_reftree : ?on_behalf_of:(int option) -> t -> (string, string) result Lwt.t
diff --git a/src/vyconf_pbt.ml b/src/vyconf_pbt.ml
index 9d2845d..5692f77 100644
--- a/src/vyconf_pbt.ml
+++ b/src/vyconf_pbt.ml
@@ -26,7 +26,6 @@ type request_validate = {
type request_set = {
path : string list;
- ephemeral : bool option;
}
type request_delete = {
@@ -195,10 +194,8 @@ let rec default_request_validate
let rec default_request_set
?path:((path:string list) = [])
- ?ephemeral:((ephemeral:bool option) = None)
() : request_set = {
path;
- ephemeral;
}
let rec default_request_delete
@@ -393,12 +390,10 @@ let default_request_validate_mutable () : request_validate_mutable = {
type request_set_mutable = {
mutable path : string list;
- mutable ephemeral : bool option;
}
let default_request_set_mutable () : request_set_mutable = {
path = [];
- ephemeral = None;
}
type request_delete_mutable = {
@@ -636,7 +631,6 @@ let rec pp_request_validate fmt (v:request_validate) =
let rec pp_request_set fmt (v:request_set) =
let pp_i fmt () =
Pbrt.Pp.pp_record_field ~first:true "path" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.path;
- Pbrt.Pp.pp_record_field ~first:false "ephemeral" (Pbrt.Pp.pp_option Pbrt.Pp.pp_bool) fmt v.ephemeral;
in
Pbrt.Pp.pp_brk pp_i fmt ()
@@ -883,12 +877,6 @@ let rec encode_pb_request_set (v:request_set) encoder =
Pbrt.Encoder.string x encoder;
Pbrt.Encoder.key 1 Pbrt.Bytes encoder;
) v.path encoder;
- begin match v.ephemeral with
- | Some x ->
- Pbrt.Encoder.bool x encoder;
- Pbrt.Encoder.key 3 Pbrt.Varint encoder;
- | None -> ();
- end;
()
let rec encode_pb_request_delete (v:request_delete) encoder =
@@ -1303,16 +1291,10 @@ let rec decode_pb_request_set d =
end
| Some (1, pk) ->
Pbrt.Decoder.unexpected_payload "Message(request_set), field(1)" pk
- | Some (3, Pbrt.Varint) -> begin
- v.ephemeral <- Some (Pbrt.Decoder.bool d);
- end
- | Some (3, pk) ->
- Pbrt.Decoder.unexpected_payload "Message(request_set), field(3)" pk
| Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
done;
({
path = v.path;
- ephemeral = v.ephemeral;
} : request_set)
let rec decode_pb_request_delete d =
diff --git a/src/vyconf_pbt.mli b/src/vyconf_pbt.mli
index 56cf4ea..7371d18 100644
--- a/src/vyconf_pbt.mli
+++ b/src/vyconf_pbt.mli
@@ -33,7 +33,6 @@ type request_validate = {
type request_set = {
path : string list;
- ephemeral : bool option;
}
type request_delete = {
@@ -206,7 +205,6 @@ val default_request_validate :
val default_request_set :
?path:string list ->
- ?ephemeral:bool option ->
unit ->
request_set
(** [default_request_set ()] is the default value for type [request_set] *)
diff --git a/src/vyconfd.ml b/src/vyconfd.ml
index 95915b3..d3e4216 100644
--- a/src/vyconfd.ml
+++ b/src/vyconfd.ml
@@ -4,8 +4,11 @@ open Vyconf_connect.Vyconf_pbt
open Vyconfd_config.Defaults
open Vyconfd_config.Vyconf_config
-module FP = FilePath
module CT = Vyos1x.Config_tree
+module IC = Vyos1x.Internal.Make(CT)
+module CC = Commitd_client.Commit
+module VC = Commitd_client.Vycall_client
+module FP = FilePath
module Gen = Vyos1x.Generate
module Session = Vyconfd_config.Session
module Directories = Vyconfd_config.Directories
@@ -144,6 +147,56 @@ let validate world token (req: request_validate) =
response_tmpl
with Session.Session_error msg -> {response_tmpl with status=Fail; error=(Some msg)}
+let set world token (req: request_set) =
+ try
+ let () = (Lwt_log.debug @@ Printf.sprintf "[%s]\n" (Vyos1x.Util.string_of_list req.path)) |> Lwt.ignore_result in
+ let session = Session.set world (find_session token) req.path in
+ Hashtbl.replace sessions token session;
+ response_tmpl
+ with Session.Session_error msg -> {response_tmpl with status=Fail; error=(Some msg)}
+
+let delete world token (req: request_delete) =
+ try
+ let () = (Lwt_log.debug @@ Printf.sprintf "[%s]\n" (Vyos1x.Util.string_of_list req.path)) |> Lwt.ignore_result in
+ let session = Session.delete world (find_session token) req.path in
+ Hashtbl.replace sessions token session;
+ response_tmpl
+ with Session.Session_error msg -> {response_tmpl with status=Fail; error=(Some msg)}
+
+let commit world token (_req: request_commit) =
+ let s = find_session token in
+ let at = world.Session.running_config in
+ let wt = s.proposed_config in
+ let rt = world.reference_tree in
+ let vc = world.vyconf_config in
+ let () = IC.write_internal at (FP.concat vc.session_dir vc.running_cache) in
+ let () = IC.write_internal wt (FP.concat vc.session_dir vc.session_cache) in
+
+ let commit_data = CC.make_commit_data rt at wt token in
+ let%lwt received_commit_data = VC.do_commit commit_data in
+ let%lwt result_commit_data =
+ Lwt.return (CC.commit_update received_commit_data)
+ in
+ match result_commit_data.init with
+ | None ->
+ let out = "Empty init" in
+ Lwt.return {response_tmpl with status=Internal_error; error=(Some out)}
+ | Some init_data ->
+ let res, out =
+ init_data.success, init_data.out
+ in
+ match res with
+ | false ->
+ Lwt.return {response_tmpl with status=Internal_error; error=(Some out)}
+ | true ->
+ world.Session.running_config <- result_commit_data.config_result;
+ let success, msg_str =
+ result_commit_data.result.success, result_commit_data.result.out
+ in
+ match success with
+ | true -> Lwt.return {response_tmpl with status=Success; output=(Some msg_str)}
+ | false -> Lwt.return {response_tmpl with status=Fail; output=(Some msg_str)}
+
let reload_reftree world (_req: request_reload_reftree) =
let config = world.Session.vyconf_config in
let reftree =
@@ -172,11 +225,14 @@ let rec handle_connection world ic oc () =
with Pbrt.Decoder.Failure e -> Lwt.return (Error (Pbrt.Decoder.error_to_string e))
in
let%lwt resp =
- (match req with
- | Error msg -> {response_tmpl with status=Fail; error=(Some (Printf.sprintf "Decoding error: %s" msg))}
+ match req with
+ | Error msg -> Lwt.return {response_tmpl with status=Fail; error=(Some (Printf.sprintf "Decoding error: %s" msg))}
| Ok req ->
+ match req with
+ | Some t, Commit r -> commit world t r
+ | _ as req ->
begin
- match req with
+ (match req with
| _, Status -> response_tmpl
| _, Setup_session r -> setup_session world r
| _, Reload_reftree r -> reload_reftree world r
@@ -190,8 +246,11 @@ let rec handle_connection world ic oc () =
| Some t, List_children r -> list_children world t r
| Some t, Show_config r -> show_config world t r
| Some t, Validate r -> validate world t r
+ | Some t, Set r -> set world t r
+ | Some t, Delete r -> delete world t r
| _ -> failwith "Unimplemented"
- end) |> Lwt.return
+ ) |> Lwt.return
+ end
in
let%lwt () = send_response oc resp in
handle_connection world ic oc ()