-
Notifications
You must be signed in to change notification settings - Fork 785
Expand file tree
/
Copy pathlaunch.cpp
More file actions
674 lines (587 loc) · 24.1 KB
/
launch.cpp
File metadata and controls
674 lines (587 loc) · 24.1 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "launch.h"
#include "animated_spinner.h"
#include "common_cli.h"
#include "create_alias.h"
#include <multipass/cli/argparser.h>
#include <multipass/cli/client_platform.h>
#include <multipass/cli/prompters.h>
#include <multipass/constants.h>
#include <multipass/exceptions/cmd_exceptions.h>
#include <multipass/exceptions/snap_environment_exception.h>
#include <multipass/file_ops.h>
#include <multipass/format.h>
#include <multipass/memory_size.h>
#include <multipass/settings/settings.h>
#include <multipass/snap_utils.h>
#include <multipass/standard_paths.h>
#include <multipass/url_downloader.h>
#include <multipass/utils.h>
#include <yaml-cpp/yaml.h>
#include <QDir>
#include <QFileInfo>
#include <QRegularExpression>
#include <QTimeZone>
#include <QUrl>
#include <filesystem>
#include <unordered_map>
namespace mp = multipass;
namespace mpu = multipass::utils;
namespace cmd = multipass::cmd;
namespace
{
auto checked_mode(const std::string& mode)
{
if (mode == "auto")
return mp::LaunchRequest_NetworkOptions_Mode::LaunchRequest_NetworkOptions_Mode_AUTO;
if (mode == "manual")
return mp::LaunchRequest_NetworkOptions_Mode::LaunchRequest_NetworkOptions_Mode_MANUAL;
else
throw mp::ValidationException{
fmt::format("Bad network mode '{}', need 'auto' or 'manual'", mode)};
}
const std::string& checked_mac(const std::string& mac)
{
if (!mpu::valid_mac_address(mac))
throw mp::ValidationException(fmt::format("Invalid MAC address: {}", mac));
return mac;
}
auto net_digest(const QString& options)
{
multipass::LaunchRequest_NetworkOptions net;
QStringList split = options.split(',', Qt::SkipEmptyParts);
for (const auto& key_value_pair : split)
{
QStringList key_value_split = key_value_pair.split('=', Qt::SkipEmptyParts);
if (key_value_split.size() == 2)
{
const auto& key = key_value_split[0].toLower();
const auto& val = key_value_split[1];
if (key == "name")
net.set_id(val.toStdString());
else if (key == "mode")
net.set_mode(checked_mode(val.toLower().toStdString()));
else if (key == "mac")
net.set_mac_address(checked_mac(val.toStdString()));
else
throw mp::ValidationException{fmt::format("Bad network field: {}", key)};
}
// Interpret as "name" the argument when there are no ',' and no '='.
else if (key_value_split.size() == 1 && split.size() == 1)
net.set_id(key_value_split[0].toStdString());
else
throw mp::ValidationException{
fmt::format("Bad network field definition: {}", key_value_pair)};
}
if (net.id().empty())
throw mp::ValidationException{
fmt::format("Bad network definition, need at least a 'name' field")};
return net;
}
} // namespace
mp::ReturnCodeVariant cmd::Launch::run(mp::ArgParser* parser)
{
petenv_name = MP_SETTINGS.get(petenv_key);
if (auto ret = parse_args(parser); ret != ParseCode::Ok)
{
return parser->returnCodeFrom(ret);
}
auto ret = request_launch(parser);
if (ret != ReturnCode::Ok)
return ret;
auto got_petenv = instance_name == petenv_name;
if (!got_petenv && mount_routes.empty())
return ret;
if (MP_SETTINGS.get_as<bool>(mounts_key))
{
auto has_home_mount =
std::count_if(mount_routes.begin(), mount_routes.end(), [](const auto& route) {
return route.second == home_automount_dir;
});
if (got_petenv && !has_home_mount)
{
try
{
mount_routes.emplace_back(QString::fromLocal8Bit(mpu::snap_real_home_dir()),
home_automount_dir);
}
catch (const SnapEnvironmentException&)
{
mount_routes.emplace_back(QDir::toNativeSeparators(QDir::homePath()),
home_automount_dir);
}
}
for (const auto& [source, target] : mount_routes)
{
auto mount_ret = mount(parser, source, target);
if (ret == ReturnCode::Ok)
{
ret = mount_ret;
}
}
}
else
{
cout << "Skipping mount due to disabled mounts feature\n";
}
return ret;
}
std::string cmd::Launch::name() const
{
return "launch";
}
QString cmd::Launch::short_help() const
{
return QStringLiteral("Create and start a cloud instance");
}
QString cmd::Launch::description() const
{
return QStringLiteral("Create and start a new instance.");
}
mp::ParseCode cmd::Launch::parse_args(mp::ArgParser* parser)
{
parser->addPositionalArgument(
"image",
"Optional image to launch. If omitted, then the latest Ubuntu LTS "
"will be used.\n"
"<remote> can be either ‘release’ or ‘daily‘. If <remote> is omitted, "
"‘release’ will be used.\n"
"<image> can be a partial image hash or an Ubuntu release version, "
"codename or alias.\n"
"<url> is a custom image URL that is in http://, https://, or file:// "
"format.\n",
"[[<remote:>]<image> | <url>]");
QCommandLineOption cpusOption({"c", "cpus"},
QString::fromStdString(fmt::format("Number of CPUs to allocate.\n"
"Minimum: {}, default: {}.",
min_cpu_cores,
default_cpu_cores)),
"cpus",
default_cpu_cores);
QCommandLineOption diskOption(
{"d", "disk"},
QString::fromStdString(
fmt::format("Disk space to allocate. Positive integers, in "
"bytes, or decimals, with K, M, G suffix.\nMinimum: {}, default: {}.",
min_disk_size,
default_disk_size)),
"disk",
QString::fromUtf8(default_disk_size));
QCommandLineOption memOption(
{"m", "memory"},
QString::fromStdString(
fmt::format("Amount of memory to allocate. Positive integers, "
"in bytes, or decimals, with K, M, G suffix.\nMinimum: {}, default: {}.",
min_memory_size,
default_memory_size)),
"memory",
QString::fromUtf8(default_memory_size)); // In MB's
const auto valid_name_desc =
QString{"Valid names must consist of letters, numbers, or hyphens, must start with a "
"letter, and must end with an alphanumeric character."};
const auto name_option_desc =
petenv_name.isEmpty()
? QString{"Name for the instance.\n%1"}.arg(valid_name_desc)
: QString{"Name for the instance. If it is '%1' (the configured primary instance "
"name), the user's home directory is mounted inside the newly launched "
"instance, in '%2'.\n%3"}
.arg(petenv_name, mp::home_automount_dir, valid_name_desc);
QCommandLineOption nameOption({"n", "name"}, name_option_desc, "name");
QCommandLineOption cloudInitOption(
"cloud-init",
"Path or URL to a user-data cloud-init configuration, or '-' for stdin.",
"file> | <url");
QCommandLineOption networkOption(
"network",
"Add a network interface to the instance, where <spec> is in the "
"\"key=value,key=value\" format, with the following keys available:\n"
" name: the network to connect to (required), use the networks command for a "
"list of possible values, or use 'bridged' to use the interface configured via "
"`multipass set local.bridged-network`.\n"
" mode: auto|manual (default: auto)\n"
" mac: hardware address (default: random).\n"
"You can also use a shortcut of \"<name>\" to mean \"name=<name>\".",
"spec");
QCommandLineOption bridgedOption("bridged", "Adds one `--network bridged` network.");
QCommandLineOption mountOption(
"mount",
QStringLiteral("Mount a local directory inside the instance. If <target> is omitted, the "
"mount point will be under %1/<source-dir>, where <source-dir> is the name "
"of the <source> directory.")
.arg(home_in_instance),
"source>:<target");
parser->addOptions({cpusOption,
diskOption,
memOption,
nameOption,
cloudInitOption,
networkOption,
bridgedOption,
mountOption});
mp::cmd::add_instance_timeout(parser);
auto status = parser->commandParse(this);
if (status != ParseCode::Ok)
{
return status;
}
if (parser->positionalArguments().count() > 1)
{
cerr << "Too many arguments supplied\n";
return ParseCode::CommandLineError;
}
if (!parser->positionalArguments().isEmpty())
{
auto remote_image_name = parser->positionalArguments().first();
if (remote_image_name.startsWith("file://"))
{
// Convert to absolute because the daemon doesn't know where the client is being
// executed.
auto file_info = QFileInfo(remote_image_name.remove(0, 7));
auto absolute_file_path = file_info.absoluteFilePath();
request.set_image("file://" + absolute_file_path.toStdString());
}
else if (remote_image_name.startsWith("http://") ||
remote_image_name.startsWith("https://"))
{
request.set_image(remote_image_name.toStdString());
}
else
{
auto colon_count = remote_image_name.count(':');
if (colon_count > 1)
{
cerr << "Invalid remote and source image name supplied\n";
return ParseCode::CommandLineError;
}
else if (colon_count == 1)
{
request.set_remote_name(remote_image_name.section(':', 0, 0).toStdString());
request.set_image(remote_image_name.section(':', 1).toStdString());
}
else
{
request.set_image(remote_image_name.toStdString());
}
}
}
if (parser->isSet(nameOption))
{
request.set_instance_name(parser->value(nameOption).toStdString());
}
if (parser->isSet(cpusOption))
{
bool conversion_pass;
const auto& cpu_text = parser->value(cpusOption);
const int cpu_count = cpu_text.toInt(&conversion_pass); // Base 10 conversion with check.
if (!conversion_pass || cpu_count < 1)
{
fmt::print(cerr,
"Error: invalid CPU count '{}', need a positive integer value.\n",
cpu_text);
return ParseCode::CommandLineError;
}
request.set_num_cores(cpu_count);
}
if (parser->isSet(memOption))
{
auto arg_mem_size = parser->value(memOption).toStdString();
mp::MemorySize{arg_mem_size}; // throw if bad
request.set_mem_size(arg_mem_size);
}
if (parser->isSet(diskOption))
{
auto arg_disk_size = parser->value(diskOption).toStdString();
mp::MemorySize{arg_disk_size}; // throw if bad
request.set_disk_space(arg_disk_size);
}
if (parser->isSet(mountOption))
{
for (const auto& value : parser->values(mountOption))
{
// this is needed so that Windows absolute paths are not split at the colon following
// the drive letter
const auto colon_split =
QRegularExpression{R"(^[A-Za-z]:[\\/].*)"}.match(value).hasMatch();
const auto mount_source = value.section(':', 0, colon_split);
const auto mount_target = value.section(':', colon_split + 1);
// Validate source directory of client side mounts
QFileInfo source_dir(mount_source);
if (!MP_FILEOPS.exists(source_dir))
{
cerr << "Mount source path \"" << mount_source.toStdString()
<< "\" does not exist\n";
return ParseCode::CommandLineError;
}
if (!MP_FILEOPS.isDir(source_dir))
{
cerr << "Mount source path \"" << mount_source.toStdString()
<< "\" is not a directory\n";
return ParseCode::CommandLineError;
}
if (!MP_FILEOPS.isReadable(source_dir))
{
cerr << "Mount source path \"" << mount_source.toStdString()
<< "\" is not readable\n";
return ParseCode::CommandLineError;
}
mount_routes.emplace_back(mount_source, mount_target);
}
}
if (parser->isSet(cloudInitOption))
{
constexpr auto err_msg_template = "Could not load cloud-init configuration: {}";
try
{
const QString& cloud_init_file = parser->value(cloudInitOption);
YAML::Node node;
if (cloud_init_file == "-")
{
node = YAML::Load(term->read_all_cin());
}
else if (cloud_init_file.startsWith("http://") ||
cloud_init_file.startsWith("https://"))
{
URLDownloader downloader{std::chrono::minutes{1}};
auto downloaded_yaml = downloader.download(QUrl(cloud_init_file));
node = YAML::Load(downloaded_yaml.toStdString());
}
else
{
auto cloud_init_file_stdstr = cloud_init_file.toStdString();
auto file_type = fs::status(cloud_init_file_stdstr).type();
if (file_type != fs::file_type::regular && file_type != fs::file_type::fifo)
throw YAML::BadFile{cloud_init_file_stdstr};
node = YAML::LoadFile(cloud_init_file_stdstr);
}
request.set_cloud_init_user_data(YAML::Dump(node));
}
catch (const YAML::BadFile& e)
{
auto err_detail =
fmt::format("{}\n{}", e.what(), "Please ensure that Multipass can read it.");
fmt::println(cerr, err_msg_template, err_detail);
return ParseCode::CommandLineError;
}
catch (const YAML::Exception& e)
{
fmt::println(cerr, err_msg_template, e.what());
return ParseCode::CommandLineError;
}
}
if (parser->isSet(bridgedOption))
{
request.mutable_network_options()->Add(net_digest(mp::bridged_network_name));
}
try
{
if (parser->isSet(networkOption))
for (const auto& net : parser->values(networkOption))
request.mutable_network_options()->Add(net_digest(net));
request.set_timeout(mp::cmd::parse_timeout(parser));
}
catch (mp::ValidationException& e)
{
cerr << "error: " << e.what() << "\n";
return ParseCode::CommandLineError;
}
request.set_time_zone(QTimeZone::systemTimeZoneId().toStdString());
request.set_verbosity_level(parser->verbosityLevel());
return status;
}
mp::ReturnCodeVariant cmd::Launch::request_launch(const ArgParser* parser)
{
if (!spinner)
spinner = std::make_unique<multipass::AnimatedSpinner>(
cout); // Creating just in time to work around canonical/multipass#2075
if (timer)
timer->resume();
else if (parser->isSet("timeout"))
{
timer = cmd::make_timer(parser->value("timeout").toInt(),
spinner.get(),
cerr,
"Timed out waiting for instance launch.");
timer->start();
}
auto on_success = [this, &parser](mp::LaunchReply& reply) -> ReturnCodeVariant {
spinner->stop();
if (timer)
timer->pause();
instance_name = QString::fromStdString(
request.instance_name().empty() ? reply.vm_instance_name() : request.instance_name());
std::vector<std::string> warning_aliases;
for (const auto& alias_to_be_created : reply.aliases_to_be_created())
{
AliasDefinition alias_definition{alias_to_be_created.instance(),
alias_to_be_created.command(),
alias_to_be_created.working_directory()};
if (create_alias(aliases,
alias_to_be_created.name(),
alias_definition,
cout,
cerr,
instance_name.toStdString()) == ReturnCode::Ok)
warning_aliases.push_back(alias_to_be_created.name());
}
if (warning_aliases.size())
cerr << fmt::format("Warning: unable to create {} {}.\n",
warning_aliases.size() == 1 ? "alias" : "aliases",
fmt::join(warning_aliases, ", "));
for (const auto& workspace_to_be_created : reply.workspaces_to_be_created())
{
auto home_dir = mpu::in_multipass_snap()
? QString::fromLocal8Bit(mpu::snap_real_home_dir())
: MP_STDPATHS.writableLocation(StandardPaths::HomeLocation);
auto full_path_str =
home_dir + "/multipass/" + QString::fromStdString(workspace_to_be_created);
QDir full_path(full_path_str);
if (full_path.exists())
{
cerr << fmt::format("Folder \"{}\" already exists.\n", full_path_str);
}
else
{
if (!MP_FILEOPS.mkpath(full_path, full_path_str))
{
cerr << fmt::format("Error creating folder {}. Not mounting.\n", full_path_str);
continue;
}
}
if (mount(parser, full_path_str, QString::fromStdString(workspace_to_be_created)) !=
ReturnCode::Ok)
{
cerr << fmt::format("Error mounting folder {}.\n", full_path_str);
}
}
cout << "Launched: " << reply.vm_instance_name() << "\n";
if (term->is_live() && update_available(reply.update_info()))
{
// TODO: daemon doesn't know if client actually shows this notice. Need to be able
// to tell daemon that the notice will be displayed or not.
cout << update_notice(reply.update_info());
}
return ReturnCode::Ok;
};
auto on_failure = [this, &parser](grpc::Status& status,
mp::LaunchReply& reply) -> ReturnCodeVariant {
spinner->stop();
if (timer)
timer->pause();
LaunchError launch_error;
launch_error.ParseFromString(status.error_details());
std::string error_details;
for (const auto& error : launch_error.error_codes())
{
if (error == LaunchError::INVALID_DISK_SIZE)
{
error_details =
fmt::format("Invalid disk size value supplied: {}.", request.disk_space());
}
else if (error == LaunchError::INVALID_MEM_SIZE)
{
error_details =
fmt::format("Invalid memory size value supplied: {}.", request.mem_size());
}
else if (error == LaunchError::INVALID_HOSTNAME)
{
error_details =
fmt::format("Invalid instance name supplied: {}", request.instance_name());
}
else if (error == LaunchError::INVALID_NETWORK)
{
if (reply.nets_need_bridging_size() && ask_bridge_permission(reply))
{
request.set_permission_to_bridge(true);
return request_launch(parser);
}
// TODO: show the option which triggered the error only. This will need a refactor
// in the LaunchError proto.
error_details =
"Invalid network options. "
"To troubleshoot, see "
"https://documentation.ubuntu.com/multipass/stable/how-to-guides/troubleshoot/";
}
}
if (error_details.empty() && status.error_message().find("timed out") != std::string::npos)
{
error_details =
"For troubleshooting, see "
"https://documentation.ubuntu.com/multipass/stable/how-to-guides/troubleshoot/";
}
return standard_failure_handler_for(name(), cerr, status, error_details);
};
auto streaming_callback =
[this](mp::LaunchReply& reply,
grpc::ClientReaderWriterInterface<LaunchRequest, LaunchReply>* client) {
std::unordered_map<int, std::string> progress_messages{
{LaunchProgress_ProgressTypes_IMAGE, "Retrieving image: "},
{LaunchProgress_ProgressTypes_EXTRACT, "Extracting image: "},
{LaunchProgress_ProgressTypes_VERIFY, "Verifying image"},
{LaunchProgress_ProgressTypes_WAITING, "Preparing image: "}};
if (!reply.log_line().empty())
{
spinner->print(cerr, reply.log_line());
}
if (reply.create_oneof_case() == mp::LaunchReply::CreateOneofCase::kLaunchProgress)
{
auto& progress_message = progress_messages[reply.launch_progress().type()];
if (reply.launch_progress().percent_complete() != "-1")
{
spinner->stop();
cout << "\r";
cout << progress_message << reply.launch_progress().percent_complete() << "%"
<< std::flush;
}
else
{
spinner->stop();
spinner->start(progress_message);
}
}
else if (reply.create_oneof_case() == mp::LaunchReply::CreateOneofCase::kCreateMessage)
{
spinner->stop();
spinner->start(reply.create_message());
}
else if (!reply.reply_message().empty())
{
spinner->stop();
spinner->start(reply.reply_message());
}
};
return dispatch(&RpcMethod::launch, request, on_success, on_failure, streaming_callback);
}
auto cmd::Launch::mount(const mp::ArgParser* parser,
const QString& mount_source,
const QString& mount_target) -> ReturnCodeVariant
{
const auto full_mount_target = QString{"%1:%2"}.arg(instance_name, mount_target);
auto ret = run_cmd({"multipass", "mount", mount_source, full_mount_target}, parser, cout, cerr);
if (ret == ReturnCode::Ok)
cout << fmt::format("Mounted '{}' into '{}'\n", mount_source, full_mount_target);
return ret;
}
bool cmd::Launch::ask_bridge_permission(multipass::LaunchReply& reply)
{
std::vector<std::string> nets;
std::copy(reply.nets_need_bridging().cbegin(),
reply.nets_need_bridging().cend(),
std::back_inserter(nets));
mp::BridgePrompter prompter(term);
return prompter.bridge_prompt(nets);
}