sr_load.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <getopt.h>
  5. #include <faux/faux.h>
  6. #include <faux/str.h>
  7. #include <faux/file.h>
  8. #include <faux/argv.h>
  9. #include <sysrepo.h>
  10. #include <sysrepo/xpath.h>
  11. #include <sysrepo/netconf_acm.h>
  12. #include "klish_plugin_sysrepo.h"
  13. #ifndef VERSION
  14. #define VERSION "1.0.0"
  15. #endif
  16. #define DEFAULT_USER "root"
  17. #define DEFAULT_DATASTORE SR_DS_CANDIDATE
  18. typedef struct cmd_opts_s {
  19. bool_t verbose;
  20. char *cfg;
  21. char *file;
  22. char *user;
  23. bool_t stop_on_error;
  24. sr_datastore_t datastore;
  25. } cmd_opts_t;
  26. int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
  27. const char *user, bool_t stop_on_error);
  28. // Command line options
  29. static cmd_opts_t *cmd_opts_init(void);
  30. static void cmd_opts_free(cmd_opts_t *opts);
  31. static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts);
  32. static void help(int status, const char *argv0);
  33. int main(int argc, char **argv)
  34. {
  35. int ret = -1;
  36. pline_opts_t opts;
  37. cmd_opts_t *cmd_opts = NULL;
  38. cmd_opts = cmd_opts_init();
  39. if (cmd_opts_parse(argc, argv, cmd_opts) < 0) {
  40. fprintf(stderr, "Error: Illegal command line options\n");
  41. goto out;
  42. }
  43. pline_opts_init(&opts);
  44. ret = srp_mass_set(STDIN_FILENO, cmd_opts->datastore, &opts,
  45. cmd_opts->user, cmd_opts->stop_on_error);
  46. out:
  47. cmd_opts_free(cmd_opts);
  48. return ret;
  49. }
  50. int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
  51. const char *user, bool_t stop_on_error)
  52. {
  53. int ret = -1;
  54. int err = SR_ERR_OK;
  55. sr_conn_ctx_t *conn = NULL;
  56. sr_session_ctx_t *sess = NULL;
  57. faux_file_t *file = NULL;
  58. char *line = NULL;
  59. size_t err_num = 0;
  60. sr_subscription_ctx_t *nacm_sub = NULL;
  61. err = sr_connect(SR_CONN_DEFAULT, &conn);
  62. if (err) {
  63. fprintf(stderr, "Error: Can't connect to sysrepo\n");
  64. goto out;
  65. }
  66. err = sr_session_start(conn, ds, &sess);
  67. if (err) {
  68. fprintf(stderr, "Error: Can't start session\n");
  69. goto out;
  70. }
  71. sr_session_set_orig_name(sess, user);
  72. // Init NACM session
  73. if (opts->enable_nacm) {
  74. if (sr_nacm_init(sess, 0, &nacm_sub) != SR_ERR_OK) {
  75. fprintf(stderr, "Error: Can't init NACM\n");
  76. goto out;
  77. }
  78. sr_nacm_set_user(sess, user);
  79. }
  80. file = faux_file_fdopen(fd);
  81. if (!file) {
  82. fprintf(stderr, "Error: Can't open input stream\n");
  83. goto out;
  84. }
  85. while ((line = faux_file_getline(file))) {
  86. pline_t *pline = NULL;
  87. faux_argv_t *args = NULL;
  88. args = faux_argv_new();
  89. faux_argv_parse(args, line);
  90. pline = pline_parse(sess, args, opts);
  91. faux_argv_free(args);
  92. if (!pline || pline->invalid) {
  93. err_num++;
  94. fprintf(stderr, "Error: Illegal: %s\n", line);
  95. } else {
  96. faux_list_node_t *iter = NULL;
  97. pexpr_t *expr = NULL;
  98. iter = faux_list_head(pline->exprs);
  99. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  100. if (!(expr->pat & PT_SET)) {
  101. err_num++;
  102. fprintf(stderr, "Error: Illegal expression for set operation\n");
  103. break;
  104. }
  105. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  106. SR_ERR_OK) {
  107. err_num++;
  108. fprintf(stderr, "Error: Can't set data\n");
  109. break;
  110. }
  111. }
  112. }
  113. if (stop_on_error && (err_num > 0)) {
  114. sr_discard_changes(sess);
  115. goto out;
  116. }
  117. pline_free(pline);
  118. faux_str_free(line);
  119. }
  120. if (sr_has_changes(sess)) {
  121. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  122. sr_discard_changes(sess);
  123. fprintf(stderr, "Error: Can't apply changes\n");
  124. goto out;
  125. }
  126. }
  127. ret = 0;
  128. out:
  129. faux_file_close(file);
  130. if (opts->enable_nacm) {
  131. sr_unsubscribe(nacm_sub);
  132. sr_nacm_destroy();
  133. }
  134. sr_disconnect(conn);
  135. return ret;
  136. }
  137. static cmd_opts_t *cmd_opts_init(void)
  138. {
  139. cmd_opts_t *opts = NULL;
  140. opts = faux_zmalloc(sizeof(*opts));
  141. assert(opts);
  142. // Initialize
  143. opts->verbose = BOOL_FALSE;
  144. opts->stop_on_error = BOOL_FALSE;
  145. opts->cfg = NULL;
  146. opts->file = NULL;
  147. opts->user = NULL;
  148. opts->datastore = DEFAULT_DATASTORE;
  149. return opts;
  150. }
  151. static void cmd_opts_free(cmd_opts_t *opts)
  152. {
  153. if (!opts)
  154. return;
  155. faux_str_free(opts->cfg);
  156. faux_str_free(opts->file);
  157. faux_str_free(opts->user);
  158. faux_free(opts);
  159. }
  160. static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
  161. {
  162. static const char *shortopts = "hf:veu:d:";
  163. static const struct option longopts[] = {
  164. {"conf", 1, NULL, 'f'},
  165. {"help", 0, NULL, 'h'},
  166. {"verbose", 0, NULL, 'v'},
  167. {"user", 1, NULL, 'u'},
  168. {"stop-on-error", 0, NULL, 'e'},
  169. {"datastore", 1, NULL, 'd'},
  170. {NULL, 0, NULL, 0}
  171. };
  172. optind = 1;
  173. while(1) {
  174. int opt = 0;
  175. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  176. if (-1 == opt)
  177. break;
  178. switch (opt) {
  179. case 'v':
  180. opts->verbose = BOOL_TRUE;
  181. break;
  182. case 'e':
  183. opts->stop_on_error = BOOL_TRUE;
  184. break;
  185. case 'h':
  186. help(0, argv[0]);
  187. _exit(0);
  188. break;
  189. case 'u':
  190. faux_str_free(opts->user);
  191. opts->user = faux_str_dup(optarg);
  192. break;
  193. case 'f':
  194. faux_str_free(opts->cfg);
  195. opts->cfg = faux_str_dup(optarg);
  196. break;
  197. case 'd':
  198. if (!kly_str2ds(optarg, strlen(optarg), &opts->datastore))
  199. return BOOL_FALSE;
  200. break;
  201. default:
  202. help(-1, argv[0]);
  203. _exit(-1);
  204. break;
  205. }
  206. }
  207. // Input file
  208. if(optind < argc) {
  209. faux_str_free(opts->file);
  210. opts->file = faux_str_dup(argv[optind]);
  211. }
  212. // Validate options
  213. if (!opts->user)
  214. opts->user = faux_str_dup(DEFAULT_USER);
  215. return 0;
  216. }
  217. static void help(int status, const char *argv0)
  218. {
  219. const char *name = NULL;
  220. if (!argv0)
  221. return;
  222. // Find the basename
  223. name = strrchr(argv0, '/');
  224. if (name)
  225. name++;
  226. else
  227. name = argv0;
  228. if (status != 0) {
  229. fprintf(stderr, "Try `%s -h' for more information.\n",
  230. name);
  231. } else {
  232. printf("Version : %s\n", VERSION);
  233. printf("Usage : %s [options] [filename]\n", name);
  234. printf("Load mass of config strings to Sysrepo repository\n");
  235. printf("Options :\n");
  236. printf("\t-h, --help Print this help.\n");
  237. printf("\t-v, --verbose Be verbose.\n");
  238. printf("\t-e, --stop-on-error Stop script execution on error.\n");
  239. printf("\t-u <name>, --user=<name> NACM user.\n");
  240. printf("\t-f <path>, --conf=<path> Config file.\n");
  241. printf("\t-d <ds>, --datastore=<ds> Datastore.\n");
  242. }
  243. }