Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static bool ALLCLASSESREPORT = false; /* GLOBAL_P */
static bool ALWAYS_VALIDATE = false; /* GLOBAL_P */
static bool CFPARANOID = false; /* GLOBAL_P */
static bool PERFORM_DB_CHECK = false;
static char *SIMULATE_JSON_FILE = NULL;

static const Rlist *ACCESSLIST = NULL; /* GLOBAL_P */

Expand Down Expand Up @@ -221,6 +222,7 @@ static const struct option OPTIONS[] =
{"skip-bootstrap-service-start", no_argument, 0, 0 },
{"skip-db-check", optional_argument, 0, 0 },
{"simulate", required_argument, 0, 0},
{"simulate-json", required_argument, 0, 0},
{NULL, 0, 0, '\0'}
};

Expand Down Expand Up @@ -257,6 +259,7 @@ static const char *const HINTS[] =
"Do not start CFEngine services as part of the bootstrap process",
"Do not run database integrity checks and repairs at startup",
"Run in simulate mode, either 'manifest', 'manifest-full' or 'diff'",
"Write the change set of a --simulate run to the given file as JSON",
NULL
};

Expand Down Expand Up @@ -385,6 +388,26 @@ int main(int argc, char *argv[])

Nova_NoteAgentExecutionPerformance(config->input_file, start);

/* The simulated change set includes digests of file contents, so it has
* to be written before GenericAgentFinalize() deinitializes the crypto
* (OpenSSL) library. */
if (SIMULATE_JSON_FILE != NULL)
{
if (!WriteChangesJson(SIMULATE_JSON_FILE))
{
Log(LOG_LEVEL_ERR,
"Failed to write the simulated change set to '%s'",
SIMULATE_JSON_FILE);

/* A consumer of the change set must not see a successful run
* without the document it asked for. */
if (ret == 0)
{
ret = EXIT_FAILURE;
}
}
}

GenericAgentFinalize(ctx, config);

StringSetDestroy(SINGLE_COPY_CACHE);
Expand Down Expand Up @@ -795,6 +818,26 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
DoCleanupAndExit(EXIT_FAILURE);
}
}
else if (StringEqual(option_name, "simulate-json"))
{
if (optarg == NULL)
{
Log(LOG_LEVEL_ERR,
"Missing argument for --simulate-json, a file path required");
DoCleanupAndExit(EXIT_FAILURE);
}
else if (!IsAbsPath(optarg))
{
Log(LOG_LEVEL_ERR,
"Invalid argument for --simulate-json, an absolute path required, not '%s'",
optarg);
DoCleanupAndExit(EXIT_FAILURE);
}
else
{
SIMULATE_JSON_FILE = xstrdup(optarg);
}
}
break;
}
default:
Expand Down Expand Up @@ -822,6 +865,13 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
DoCleanupAndExit(EXIT_FAILURE);
}

if ((SIMULATE_JSON_FILE != NULL) && !ChrootChanges())
{
Log(LOG_LEVEL_ERR,
"Option --simulate-json can only be used together with --simulate");
DoCleanupAndExit(EXIT_FAILURE);
}

FreeFixedStringArray(argc_new, argv_new);

return config;
Expand Down
Loading