]>
Commit | Line | Data |
---|---|---|
989191f0 TL |
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; | |
5c3d53f5 | 11 | use Time::HiRes qw(usleep); |
989191f0 TL |
12 | |
13 | { | |
4b4dfa12 | 14 | my $test_image; |
989191f0 | 15 | GetOptions( |
4b4dfa12 | 16 | 'test-image|t=s' => \$test_image |
989191f0 TL |
17 | ) or die "usage error\n"; |
18 | ||
4b4dfa12 | 19 | Proxmox::Install::ISOEnv::set_test_image($test_image) if $test_image; |
989191f0 | 20 | } |
4b4dfa12 | 21 | |
5c3d53f5 TL |
22 | use Proxmox::Install::ISOEnv; |
23 | use Proxmox::Install::RunEnv; | |
74e54c4d | 24 | use Proxmox::Sys::Udev; |
5c3d53f5 TL |
25 | |
26 | use Proxmox::Sys::File qw(file_write_all); | |
27 | ||
28 | use Proxmox::Log; | |
140f3f40 | 29 | use Proxmox::Install; |
5c3d53f5 TL |
30 | use Proxmox::Install::Config; |
31 | use Proxmox::UI; | |
989191f0 TL |
32 | |
33 | my $commands = { | |
34 | 'dump-env' => 'Dump the current ISO and Hardware environment to base the installer UI on.', | |
74e54c4d | 35 | 'dump-udev' => 'Dump disk and network device info. Used for the auto installation.', |
989191f0 | 36 | 'start-session' => 'Start an installation session, with command and result transmitted via stdin/out', |
140f3f40 | 37 | 'start-session-test' => 'Start an installation TEST session, with command and result transmitted via stdin/out', |
989191f0 TL |
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 | ||
04b0033c CH |
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"); | |
436cc19f | 72 | file_write_all("/tmp/low-level-config.json", to_json(Proxmox::Install::Config::get())); |
04b0033c CH |
73 | } |
74 | ||
091c64a7 CH |
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 { | |
b8504bbe | 84 | Proxmox::UI::finished(1, "Installation finished - reboot now?"); |
091c64a7 CH |
85 | } |
86 | } | |
87 | ||
989191f0 TL |
88 | my $cmd = shift; |
89 | if (!$cmd || $cmd eq 'help' || !exists($commands->{$cmd})) { | |
90 | usage($cmd // ''); | |
91 | } | |
92 | ||
92c7b5c9 TL |
93 | Proxmox::Log::init("/tmp/install-low-level-${cmd}.log"); |
94 | ||
11fd703f | 95 | my $env = Proxmox::Install::ISOEnv::get(); |
989191f0 | 96 | if ($cmd eq 'dump-env') { |
479b0524 | 97 | Proxmox::UI::init_stdio({}, $env); |
989191f0 TL |
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}, | |
11fd703f | 110 | 'run-env-cache-file' => $env->{'run-env-cache-file'}, |
989191f0 TL |
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 | ||
11fd703f TL |
116 | my $run_env_file = Proxmox::Install::ISOEnv::get('run-env-cache-file'); |
117 | ||
db97dbac TL |
118 | my $run_env = Proxmox::Install::RunEnv::query_installation_environment(); |
119 | my $run_env_serialized = to_json($run_env, {canonical => 1, utf8 => 1}) ."\n"; | |
11fd703f | 120 | file_write_all($run_env_file, $run_env_serialized); |
74e54c4d AL |
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); | |
8b39b0aa TL |
133 | } elsif ($cmd eq 'start-session') { |
134 | Proxmox::UI::init_stdio({}, $env); | |
04b0033c | 135 | read_and_merge_config(); |
140f3f40 | 136 | |
b6e671a6 TL |
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. | |
a8fbe0ff TL |
141 | if ($err ne "\n") { |
142 | warn "installation failed - $err\n"; | |
143 | log_error("installation failed: $err"); | |
144 | Proxmox::UI::finished(0, $err); | |
145 | } | |
b6e671a6 | 146 | } else { |
091c64a7 | 147 | send_reboot_ui_message(); |
b6e671a6 | 148 | } |
140f3f40 TL |
149 | } elsif ($cmd eq 'start-session-test') { |
150 | Proxmox::UI::init_stdio({}, $env); | |
04b0033c | 151 | read_and_merge_config(); |
5c3d53f5 TL |
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) { | |
5ab640a1 | 157 | Proxmox::UI::progress($i/100000, 0, 100, "foo $i"); |
5c3d53f5 TL |
158 | if ($i > 500 && $i < 600) { |
159 | usleep(50 * 1000); | |
160 | } else { | |
161 | usleep(10 * 1000); | |
162 | } | |
163 | } | |
164 | ||
091c64a7 | 165 | send_reboot_ui_message(); |
989191f0 TL |
166 | } |
167 | ||
168 | exit(0); |