aboutsummaryrefslogtreecommitdiff
path: root/zen/zen.cpp
blob: 2b6a529fee67b0bc6d01427d3bac3b630c8233e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright Epic Games, Inc. All Rights Reserved.

// Zen command line client utility
//

#include "zen.h"

#include "chunk/chunk.h"
#include "cmds/cache.h"
#include "cmds/copy.h"
#include "cmds/dedup.h"
#include "cmds/hash.h"
#include "cmds/print.h"
#include "cmds/projectstore.h"
#include "cmds/scrub.h"
#include "cmds/status.h"
#include "cmds/top.h"
#include "cmds/up.h"
#include "cmds/version.h"

#include <zencore/filesystem.h>
#include <zencore/logging.h>
#include <zencore/scopeguard.h>
#include <zencore/string.h>
#include <zencore/zencore.h>

#include <zenhttp/httpcommon.h>

#if ZEN_WITH_TESTS
#	define ZEN_TEST_WITH_RUNNER
#	include <zencore/testing.h>
#endif

ZEN_THIRD_PARTY_INCLUDES_START
#include <cpr/cpr.h>
#include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

#if ZEN_USE_MIMALLOC
#	include <mimalloc-new-delete.h>
#endif

//////////////////////////////////////////////////////////////////////////

class TemplateCommand : public ZenCmdBase
{
public:
	TemplateCommand() { m_Options.add_options()("r,root", "Root directory for CAS pool", cxxopts::value<std::string>(m_RootDirectory)); }

	virtual int Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) override
	{
		ZEN_UNUSED(GlobalOptions, argc, argv);
		return 0;
	}

	virtual cxxopts::Options& Options() override { return m_Options; }

private:
	cxxopts::Options m_Options{"template", "EDIT THIS COMMAND DESCRIPTION"};
	std::string		 m_RootDirectory;
};

//////////////////////////////////////////////////////////////////////////

bool
ZenCmdBase::ParseOptions(int argc, char** argv)
{
	cxxopts::Options&	 CmdOptions = Options();
	cxxopts::ParseResult Result		= CmdOptions.parse(argc, argv);
	if (Result.count("help"))
	{
		printf("%s\n", CmdOptions.help({}).c_str());
		return false;
	}
	if (!Result.unmatched().empty())
	{
		zen::ExtendableStringBuilder<64> StringBuilder;
		for (bool First = true; const auto& Param : Result.unmatched())
		{
			if (!First)
			{
				StringBuilder.Append(", ");
			}
			StringBuilder.Append('"');
			StringBuilder.Append(Param);
			StringBuilder.Append('"');
			First = false;
		}
		throw cxxopts::OptionParseException(fmt::format("Invalid arguments: {}", StringBuilder.ToView()));
	}

	return true;
}

std::string
ZenCmdBase::FormatResponse(const cpr::Response& Response)
{
	if (Response.error.code != cpr::ErrorCode::OK)
	{
		if (Response.error.message.empty())
		{
			return fmt::format("Request '{}' failed, error code {}", Response.url.str(), static_cast<int>(Response.error.code));
		}
		return fmt::format("Request '{}' failed. Reason: '{}' ({})",
						   Response.url.str(),
						   Response.error.message,
						   static_cast<int>(Response.error.code));
	}

	std::string Content;
	if (auto It = Response.header.find("Content-Type"); It != Response.header.end())
	{
		zen::HttpContentType ContentType = zen::ParseContentType(It->second);
		if (ContentType == zen::HttpContentType::kText)
		{
			Content = fmt::format("'{}'", Response.text);
		}
		else if (ContentType == zen::HttpContentType::kJSON)
		{
			Content = fmt::format("\n{}", Response.text);
		}
		else if (!Response.text.empty())
		{
			Content = fmt::format("[{}]", MapContentTypeToString(ContentType));
		}
	}

	std::string_view ResponseString = zen::ReasonStringForHttpResultCode(
		Response.status_code == static_cast<long>(zen::HttpResponseCode::NoContent) ? static_cast<long>(zen::HttpResponseCode::OK)
																					: Response.status_code);
	if (Content.empty())
	{
		return std::string(ResponseString);
	}
	return fmt::format("{}: {}", ResponseString, Content);
}

int
ZenCmdBase::GetReturnCode(const cpr::Response& Response)
{
	if (zen::IsHttpSuccessCode(Response.status_code))
	{
		return 0;
	}
	if (Response.error.code != cpr::ErrorCode::OK)
	{
		return static_cast<int>(Response.error.code);
	}
	return 1;
}

#if ZEN_WITH_TESTS

class RunTestsCommand : public ZenCmdBase
{
public:
	virtual int Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) override
	{
		ZEN_UNUSED(GlobalOptions);

		// Set output mode to handle virtual terminal sequences
#	if ZEN_PLATFORM_WINDOWS
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		if (hOut == INVALID_HANDLE_VALUE)
			return GetLastError();

		DWORD dwMode = 0;
		if (!GetConsoleMode(hOut, &dwMode))
			return GetLastError();

		dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
		if (!SetConsoleMode(hOut, dwMode))
			return GetLastError();
#	endif	// ZEN_PLATFORM_WINDOWS

		return ZEN_RUN_TESTS(argc, argv);
	}

	virtual cxxopts::Options& Options() override { return m_Options; }

private:
	cxxopts::Options m_Options{"runtests", "Run tests"};
};

#endif

//////////////////////////////////////////////////////////////////////////
// TODO: should make this Unicode-aware so we can pass anything in on the
// command line.

int
main(int argc, char** argv)
{
	using namespace zen;

#if ZEN_USE_MIMALLOC
	mi_version();
#endif

	zen::logging::InitializeLogging();
	zen::MaximizeOpenFileCount();

	//////////////////////////////////////////////////////////////////////////

	auto _ = zen::MakeGuard([] { spdlog::shutdown(); });

	HashCommand			 HashCmd;
	CopyCommand			 CopyCmd;
	DedupCommand		 DedupCmd;
	DropCommand			 DropCmd;
	StatusCommand		 StatusCmd;
	TopCommand			 TopCmd;
	PrintCommand		 PrintCmd;
	PrintPackageCommand	 PrintPkgCmd;
	PsCommand			 PsCmd;
	UpCommand			 UpCmd;
	DownCommand			 DownCmd;
	VersionCommand		 VersionCmd;
	CacheInfoCommand	 CacheInfoCmd;
	DropProjectCommand	 ProjectDropCmd;
	ProjectInfoCommand	 ProjectInfoCmd;
	CreateProjectCommand CreateProjectCmd;
	CreateOplogCommand	 CreateOplogCmd;
	GcCommand			 GcCmd;
	GcStatusCommand		 GcStatusCmd;

#if ZEN_WITH_TESTS
	RunTestsCommand RunTestsCmd;
#endif

	const struct CommandInfo
	{
		const char* CmdName;
		ZenCmdBase* Cmd;
		const char* CmdSummary;
	} Commands[] = {
		// clang-format off
//		{"chunk",				&ChunkCmd,			"Perform chunking"},
		{"copy",				&CopyCmd,			"Copy file(s)"},
		{"dedup",				&DedupCmd,			"Dedup files"},
		{"drop",				&DropCmd,			"Drop cache namespace or bucket"},
		{"hash",				&HashCmd,			"Compute file hashes"},
		{"print",				&PrintCmd,			"Print compact binary object"},
		{"printpackage",		&PrintPkgCmd,		"Print compact binary package"},
		{"status",				&StatusCmd,			"Show zen status"},
		{"ps",					&PsCmd,				"Enumerate running zen server instances"},
		{"top",					&TopCmd,			"Monitor zen server activity"},
		{"up",					&UpCmd,				"Bring zen server up"},
		{"down",				&DownCmd,			"Bring zen server down"},
		{"version",				&VersionCmd,		"Get zen server version"},
		{"cache-info",			&CacheInfoCmd,		"Info on cache, namespace or bucket"},
		{"project-drop",		&ProjectDropCmd,	"Drop project or project oplog"},
		{"project-info",		&ProjectInfoCmd,	"Info on project or project oplog"},
		{"project-create",		&CreateProjectCmd,	"Create a project"},
		{"oplog-create",		&CreateOplogCmd,	"Create a project oplog"},
		{"gc",					&GcCmd,				"Garbage collect zen storage"},
		{"gc-status",			&GcStatusCmd,		"Garbage collect zen storage status check"},
#if ZEN_WITH_TESTS
		{"runtests",			&RunTestsCmd,		"Run zen tests"},
#endif
		// clang-format on
	};

	// Build set containing available commands

	std::unordered_set<std::string> CommandSet;

	for (const auto& Cmd : Commands)
		CommandSet.insert(Cmd.CmdName);

	// Split command line into options, commands and any pass-through arguments

	std::string				 Passthrough;
	std::vector<std::string> PassthroughV;

	for (int i = 1; i < argc; ++i)
	{
		if (strcmp(argv[i], "--") == 0)
		{
			bool							  IsFirst = true;
			zen::ExtendableStringBuilder<256> Line;

			for (int j = i + 1; j < argc; ++j)
			{
				if (!IsFirst)
				{
					Line.AppendAscii(" ");
				}

				std::string_view ThisArg(argv[j]);
				PassthroughV.push_back(std::string(ThisArg));

				const bool NeedsQuotes = (ThisArg.find(' ') != std::string_view::npos);

				if (NeedsQuotes)
				{
					Line.AppendAscii("\"");
				}

				Line.Append(ThisArg);

				if (NeedsQuotes)
				{
					Line.AppendAscii("\"");
				}

				IsFirst = false;
			}

			Passthrough = Line.c_str();

			// This will "truncate" the arg vector and terminate the loop
			argc = i - 1;
		}
	}

	// Split command line into global vs command options. We do this by simply
	// scanning argv for a string we recognise as a command and split it there

	std::vector<char*> CommandArgVec;
	CommandArgVec.push_back(argv[0]);

	for (int i = 1; i < argc; ++i)
	{
		if (CommandSet.find(argv[i]) != CommandSet.end())
		{
			int commandArgCount = /* exec name */ 1 + argc - (i + 1);
			CommandArgVec.resize(commandArgCount);
			std::copy(argv + i + 1, argv + argc, CommandArgVec.begin() + 1);

			argc = i + 1;

			break;
		}
	}

	// Parse global CLI arguments

	ZenCliOptions GlobalOptions;

	GlobalOptions.PassthroughArgs = Passthrough;
	GlobalOptions.PassthroughV	  = PassthroughV;

	std::string SubCommand = "<None>";

	cxxopts::Options Options("zen", "Zen management tool");
	Options.add_options()("d, debug", "Enable debugging", cxxopts::value<bool>(GlobalOptions.IsDebug));
	Options.add_options()("v, verbose", "Enable verbose logging", cxxopts::value<bool>(GlobalOptions.IsVerbose));
	Options.add_options()("help", "Show command line help");
	Options.add_options()("c, command", "Sub command", cxxopts::value<std::string>(SubCommand));

	Options.parse_positional({"command"});

	const bool IsNullInvoke = (argc == 1);	// If no arguments are passed we want to print usage information

	try
	{
		auto ParseResult = Options.parse(argc, argv);

		if (ParseResult.count("help") || IsNullInvoke == 1)
		{
			std::string Help = Options.help();

			printf("%s\n", Help.c_str());

			printf("available commands:\n");

			for (const auto& CmdInfo : Commands)
			{
				printf("  %-10s %s\n", CmdInfo.CmdName, CmdInfo.CmdSummary);
			}

			exit(0);
		}

		if (GlobalOptions.IsDebug)
		{
			spdlog::set_level(spdlog::level::debug);
		}

		for (const CommandInfo& CmdInfo : Commands)
		{
			if (StrCaseCompare(SubCommand.c_str(), CmdInfo.CmdName) == 0)
			{
				cxxopts::Options& VerbOptions = CmdInfo.Cmd->Options();
				try
				{
					return CmdInfo.Cmd->Run(GlobalOptions, (int)CommandArgVec.size(), CommandArgVec.data());
				}
				catch (cxxopts::OptionParseException& Ex)
				{
					std::string help = VerbOptions.help();

					printf("Error parsing arguments for command '%s': %s\n\n%s", SubCommand.c_str(), Ex.what(), help.c_str());

					exit(11);
				}
			}
		}

		printf("Unknown command specified: '%s', exiting\n", SubCommand.c_str());
	}
	catch (cxxopts::OptionParseException& Ex)
	{
		std::string HelpMessage = Options.help();

		printf("Error parsing snapshot program arguments: %s\n\n%s", Ex.what(), HelpMessage.c_str());

		return 9;
	}
	catch (std::exception& Ex)
	{
		printf("Exception caught from 'main': %s\n", Ex.what());

		return 10;
	}

	return 0;
}