Browse Source

Dry-run mode

Serj Kalichev 2 years ago
parent
commit
4740d5cd59
6 changed files with 29 additions and 14 deletions
  1. 4 2
      bin/klish/klish.c
  2. 0 2
      klish/kscheme/kaction.c
  3. 6 4
      klish/ktp.h
  4. 7 2
      klish/ktp/ktp_session.c
  5. 11 3
      klish/ktp/ktpd_session.c
  6. 1 1
      klish/ktp_session.h

+ 4 - 2
bin/klish/klish.c

@@ -71,7 +71,8 @@ int main(int argc, char **argv)
 			if (!opts->quiet)
 				fprintf(stderr, "%s\n", line);
 			// Request to server
-			rc = ktp_session_req_cmd(ktp, line, &retcode, error);
+			rc = ktp_session_req_cmd(ktp, line, &retcode,
+				error, opts->dry_run);
 			if (!rc)
 				retcode = -1;
 			if (faux_error_len(error) > 0) {
@@ -100,7 +101,8 @@ int main(int argc, char **argv)
 				if (!opts->quiet)
 					fprintf(stderr, "%s\n", line);
 				// Request to server
-				rc = ktp_session_req_cmd(ktp, line, &retcode, error);
+				rc = ktp_session_req_cmd(ktp, line, &retcode,
+					error, opts->dry_run);
 				if (!rc)
 					retcode = -1;
 				if (faux_error_len(error) > 0) {

+ 0 - 2
klish/kscheme/kaction.c

@@ -17,8 +17,6 @@ struct kaction_s {
 	char *lock; // Named lock
 	bool_t interrupt;
 	bool_t interactive;
-//	bool_t permanent; // Should it be executed on 'dry-run'
-//	bool_t async;
 	kaction_cond_e exec_on;
 	bool_t update_retcode;
 	tri_t permanent;

+ 6 - 4
klish/ktp.h

@@ -41,15 +41,17 @@ typedef enum {
 
 // Status field. Bitmap
 typedef enum {
-	KTP_STATUS_NONE = (uint32_t)0x00000000,
-	KTP_STATUS_ERROR = (uint32_t)0x00000001,
-	KTP_STATUS_INCOMPLETED = (uint32_t)0x00000002,
-	KTP_STATUS_INTERACTIVE = (uint32_t)0x00000100,
+	KTP_STATUS_NONE =		(uint32_t)0x00000000,
+	KTP_STATUS_ERROR =		(uint32_t)0x00000001,
+	KTP_STATUS_INCOMPLETED =	(uint32_t)0x00000002,
+	KTP_STATUS_INTERACTIVE =	(uint32_t)0x00000100,
+	KTP_STATUS_DRY_RUN =		(uint32_t)0x00010000,
 } ktp_status_e;
 
 #define KTP_STATUS_IS_ERROR(status) (status & KTP_STATUS_ERROR)
 #define KTP_STATUS_IS_INCOMPLETED(status) (status & KTP_STATUS_INCOMPLETED)
 #define KTP_STATUS_IS_INTERACTIVE(status) (status & KTP_STATUS_INTERACTIVE)
+#define KTP_STATUS_IS_DRY_RUN(status) (status & KTP_STATUS_DRY_RUN)
 
 
 #endif // _klish_ktp_h

+ 7 - 2
klish/ktp/ktp_session.c

@@ -458,9 +458,10 @@ static bool_t ktp_session_read_cb(faux_async_t *async,
 
 
 bool_t ktp_session_req_cmd(ktp_session_t *ktp, const char *line,
-	int *retcode, faux_error_t *error)
+	int *retcode, faux_error_t *error, bool_t dry_run)
 {
 	faux_msg_t *req = NULL;
+	ktp_status_e status = KTP_STATUS_NONE;
 
 	assert(ktp);
 	if (!ktp)
@@ -472,7 +473,11 @@ bool_t ktp_session_req_cmd(ktp_session_t *ktp, const char *line,
 		return BOOL_FALSE;
 	}
 
-	req = ktp_msg_preform(KTP_CMD, KTP_STATUS_NONE);
+	// Set dry-run flag
+	if (dry_run)
+		status |= KTP_STATUS_DRY_RUN;
+
+	req = ktp_msg_preform(KTP_CMD, status);
 	faux_msg_add_param(req, KTP_PARAM_LINE, line, strlen(line));
 	faux_msg_send_async(req, ktp->async);
 	faux_msg_free(req);

+ 11 - 3
klish/ktp/ktpd_session.c

@@ -49,7 +49,7 @@ static bool_t ktpd_session_read_cb(faux_async_t *async,
 static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
 	void *associated_data, void *user_data);
 static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
-	int *retcode, faux_error_t *error);
+	int *retcode, faux_error_t *error, bool_t dry_run);
 
 
 ktpd_session_t *ktpd_session_new(int sock, const kscheme_t *scheme,
@@ -113,6 +113,7 @@ static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
 	ktp_cmd_e cmd = KTP_CMD_ACK;
 	faux_error_t *error = NULL;
 	bool_t rc = BOOL_FALSE;
+	bool_t dry_run = BOOL_FALSE;
 
 	assert(ktpd);
 	assert(msg);
@@ -123,9 +124,13 @@ static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
 		return BOOL_FALSE;
 	}
 
+	// Get dry-run flag from message
+	if (KTP_STATUS_IS_DRY_RUN(faux_msg_get_status(msg)))
+		dry_run = BOOL_TRUE;
+
 	error = faux_error_new();
 
-	rc = ktpd_session_exec(ktpd, line, &retcode, error);
+	rc = ktpd_session_exec(ktpd, line, &retcode, error, dry_run);
 	faux_str_free(line);
 
 	// Command is scheduled. Eloop will wait for ACTION completion.
@@ -515,7 +520,7 @@ static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
 
 
 static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
-	int *retcode, faux_error_t *error)
+	int *retcode, faux_error_t *error, bool_t dry_run)
 {
 	kexec_t *exec = NULL;
 
@@ -528,6 +533,9 @@ static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
 	if (!exec)
 		return BOOL_FALSE;
 
+	// Set dry-run flag
+	kexec_set_dry_run(exec, dry_run);
+
 	// Session status can be changed while parsing
 	if (ksession_done(ktpd->session)) {
 		kexec_free(exec);

+ 1 - 1
klish/ktp_session.h

@@ -50,7 +50,7 @@ bool_t ktp_session_connected(ktp_session_t *session);
 int ktp_session_fd(const ktp_session_t *session);
 
 bool_t ktp_session_req_cmd(ktp_session_t *ktp, const char *line,
-	int *retcode, faux_error_t *error);
+	int *retcode, faux_error_t *error, bool_t dry_run);
 
 
 // Server KTP session