]> git.proxmox.com Git - pve-installer.git/blob - proxmox-low-level-installer
auto install: rename network config source
[pve-installer.git] / proxmox-low-level-installer
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib '.'; # FIXME
7
8 use File::Path qw(make_path);
9 use Getopt::Long;
10 use JSON;
11 use Time::HiRes qw(usleep);
12
13 {
14 my $test_image;
15 GetOptions(
16 'test-image|t=s' => \$test_image
17 ) or die "usage error\n";
18
19 Proxmox::Install::ISOEnv::set_test_image($test_image) if $test_image;
20 }
21
22 use Proxmox::Install::ISOEnv;
23 use Proxmox::Install::RunEnv;
24 use Proxmox::Sys::Udev;
25
26 use Proxmox::Sys::File qw(file_write_all);
27
28 use Proxmox::Log;
29 use Proxmox::Install;
30 use Proxmox::Install::Config;
31 use Proxmox::UI;
32
33 my $commands = {
34 'dump-env' => 'Dump the current ISO and Hardware environment to base the installer UI on.',
35 'dump-udev' => 'Dump disk and network device info. Used for the auto installation.',
36 'start-session' => 'Start an installation session, with command and result transmitted via stdin/out',
37 'start-session-test' => 'Start an installation TEST session, with command and result transmitted via stdin/out',
38 'help' => 'Output this usage help.',
39 };
40
41 sub usage {
42 my ($cmd) = @_;
43
44 if (!$cmd) {
45 printf("ERROR: missing command\n\n");
46 } elsif (!exists($commands->{$cmd})) {
47 printf("ERROR: unknown command '$cmd'\n\n");
48 }
49
50 print "USAGE: $0 <cmd>\n";
51 for my $cmd (sort keys $commands->%*) {
52 printf(" %-20s - %s\n", $cmd, $commands->{$cmd});
53 }
54
55 exit($cmd ne 'help' ? 1 : 0);
56 }
57
58 sub read_and_merge_config {
59 my $config_raw;
60 while (my $line = <>) {
61 if ($line =~ /^\s*\{/) {
62 $config_raw = $line;
63 last;
64 }
65 }
66
67 my $config = eval { from_json($config_raw, { utf8 => 1 }) };
68 die "failed to parse config from stdin - $@\n" if $@;
69
70 Proxmox::Install::Config::merge($config);
71 log_info("got installation config: ". to_json(Proxmox::Install::Config::get(), { utf8 => 1, canonical => 1 }) ."\n");
72 file_write_all("/tmp/low-level-config.json", to_json(Proxmox::Install::Config::get()));
73 }
74
75 sub send_reboot_ui_message {
76 if (Proxmox::Install::Config::get_autoreboot()) {
77 my $secs = 5;
78 while ($secs > 0) {
79 Proxmox::UI::finished(1, "Installation finished - auto-rebooting in $secs seconds ..");
80 sleep 1;
81 $secs -= 1;
82 }
83 } else {
84 Proxmox::UI::finished(1, "Installation finished - reboot now?");
85 }
86 }
87
88 my $cmd = shift;
89 if (!$cmd || $cmd eq 'help' || !exists($commands->{$cmd})) {
90 usage($cmd // '');
91 }
92
93 Proxmox::Log::init("/tmp/install-low-level-${cmd}.log");
94
95 my $env = Proxmox::Install::ISOEnv::get();
96 if ($cmd eq 'dump-env') {
97 Proxmox::UI::init_stdio({}, $env);
98
99 my $out_dir = $env->{locations}->{run};
100 make_path($out_dir);
101 die "failed to create output directory '$out_dir'\n" if !-d $out_dir;
102
103 my $locales_serialized = to_json($env->{locales}, {canonical => 1, utf8 => 1}) ."\n";
104 file_write_all("$out_dir/locales.json", $locales_serialized);
105
106 my $iso_info = {
107 'iso-info' => $env->{iso},
108 'product' => $env->{product},
109 'product-cfg' => $env->{cfg},
110 'run-env-cache-file' => $env->{'run-env-cache-file'},
111 'locations' => $env->{locations},
112 };
113 my $iso_serialized = to_json($iso_info, {canonical => 1, utf8 => 1}) ."\n";
114 file_write_all("$out_dir/iso-info.json", $iso_serialized);
115
116 my $run_env_file = Proxmox::Install::ISOEnv::get('run-env-cache-file');
117
118 my $run_env = Proxmox::Install::RunEnv::query_installation_environment();
119 my $run_env_serialized = to_json($run_env, {canonical => 1, utf8 => 1}) ."\n";
120 file_write_all($run_env_file, $run_env_serialized);
121 } elsif ($cmd eq 'dump-udev') {
122 my $out_dir = $env->{locations}->{run};
123 make_path($out_dir);
124 die "failed to create output directory '$out_dir'\n" if !-d $out_dir;
125
126 my $output = {
127 disks => Proxmox::Sys::Block::udevadm_disk_details(),
128 nics => Proxmox::Sys::Net::udevadm_netdev_details(),
129 };
130
131 my $output_serialized = to_json($output, {canonical => 1, utf8 => 1}) ."\n";
132 file_write_all("$out_dir/run-env-udev.json", $output_serialized);
133 } elsif ($cmd eq 'start-session') {
134 Proxmox::UI::init_stdio({}, $env);
135 read_and_merge_config();
136
137 eval { Proxmox::Install::extract_data() };
138 if (my $err = $@) {
139 # suppress "empty" error as we got some case where the user choose to abort on a prompt,
140 # there it doesn't make sense to show them an error again, they "caused" it after all.
141 if ($err ne "\n") {
142 warn "installation failed - $err\n";
143 log_error("installation failed: $err");
144 Proxmox::UI::finished(0, $err);
145 }
146 } else {
147 send_reboot_ui_message();
148 }
149 } elsif ($cmd eq 'start-session-test') {
150 Proxmox::UI::init_stdio({}, $env);
151 read_and_merge_config();
152
153 my $res = Proxmox::UI::prompt("Reply anything?") ? 'ok' : 'not ok';
154 Proxmox::UI::message("Test Message - got $res");
155
156 for (my $i = 1; $i <= 1000; $i += 3) {
157 Proxmox::UI::progress($i/100000, 0, 100, "foo $i");
158 if ($i > 500 && $i < 600) {
159 usleep(50 * 1000);
160 } else {
161 usleep(10 * 1000);
162 }
163 }
164
165 send_reboot_ui_message();
166 }
167
168 exit(0);