Project

General

Profile

Download (4.12 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * upgradeconfig
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2022 Rubicon Communications, LLC (Netgate)
6
 * All rights reserved.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

    
21
function usage() {
22
	echo "Usage: playback upgradeconfig <input filename> <output filename>\n\n";
23
	echo "When run without parameters, will upgrade current config.xml.\n";
24
	echo "When given one file, will overwrite in place with new content.\n";
25
	echo "When given two files, will write new content to output file only.\n";
26
	echo "Examples:\n";
27
	echo "playback upgradeconfig\n";
28
	echo "playback upgradeconfig /root/oldconfig.xml /root/newconfig.xml\n";
29
	echo "\n";
30
}
31

    
32
global $g, $config, $argv, $command_split;
33

    
34
if (is_array($command_split)) {
35
	$args = array_slice($command_split, 2);
36
} else {
37
	$args = array_slice($argv, 3);
38
}
39

    
40
/* No arguments, upgrade live config */
41
if (empty($args[0])) {
42
	echo gettext("Upgrading current live configuration.") . "\n";
43
	require_once('config.inc');
44
	global $config;
45
	parse_config(true);
46
	convert_config();
47
	exit(0);
48
}
49

    
50
if ($args[0] == '-h') {
51
	usage();
52
	exit(1);
53
}
54

    
55
/* Input file */
56
$in_file = $args[0];
57

    
58
if (!file_exists($in_file)) {
59
	echo gettext("Input file does not exist.") . "\n\n";
60
	usage();
61
	exit(-1);
62
}
63

    
64
if (isset($args[1])) {
65
	/* Output file */
66
	$out_file = $args[1];
67

    
68
	if (file_exists($out_file)) {
69
		echo gettext("Output file already exists.") . "\n\n";
70
		usage();
71
		exit(-1);
72
	}
73
} else {
74
	/* Only given one file, so use it for input and output. */
75
	$out_file = $in_file;
76
}
77

    
78
if (!config_validate($in_file)) {
79
	echo gettext("Could not validate input file. Ensure it is a properly formatted config.xml file.") . "\n\n";
80
	usage();
81
	exit(-1);
82
}
83

    
84
require_once('config.lib.inc');
85
include_once('auth.inc');
86
include_once('upgrade_config.inc');
87
if (file_exists("/etc/inc/upgrade_config_custom.inc")) {
88
	include_once("upgrade_config_custom.inc");
89
}
90

    
91
/* We want to work with our local content, not the global running config */
92
$oldconfig = $config;
93
unset($config);
94
/* Re-declare global after unset or functions can't see our new content. */
95
global $config;
96

    
97
/* Read in the desired config */
98
$config = parse_xml_config($in_file, array($g['xml_rootobj'], 'pfsense'));
99
$prev_version = $config['version'];
100

    
101
/* If it's already current, there is nothing to do. */
102
if ($config['version'] == $g['latest_config']) {
103
	echo gettext("Input file is already at the latest version.") . "\n";
104
	exit(0);
105
}
106

    
107
if (!is_array($config['system']['already_run_config_upgrade'])) {
108
	$config['system']['already_run_config_upgrade'] = array();
109
}
110
$already_run = $config['system']['already_run_config_upgrade'];
111

    
112
/* Loop and run upgrade_VER_to_VER() until we're at current version */
113
while ($config['version'] < $g['latest_config']) {
114
	$cur = $config['version'] * 10;
115
	$next = $cur + 1;
116
	$migration_function = sprintf('upgrade_%03d_to_%03d', $cur, $next);
117

    
118
	foreach (array("", "_custom") as $suffix) {
119
		$migration_function .= $suffix;
120
		if (!function_exists($migration_function)) {
121
			continue;
122
		}
123
		if (isset($already_run[$migration_function])) {
124
			/* Already executed, skip now */
125
			unset($config['system']
126
			    ['already_run_config_upgrade']
127
			    [$migration_function]);
128
		} else {
129
			$migration_function();
130
		}
131
	}
132
	$config['version'] = sprintf('%.1f', $next / 10);
133
}
134

    
135
if ($prev_version != $config['version']) {
136
	$desc = sprintf(gettext('Upgraded config version level from %1$s to %2$s'), $prev_version, $config['version']);
137
	$config['revision'] = make_config_revision_entry($desc, 'console');
138
	$xmlconfig = dump_xml_config($config, $g['xml_rootobj']);
139
	file_put_contents($out_file, $xmlconfig);
140
	echo $desc . "\n";
141
}
142

    
143
/* Restore old content */
144
$config = $oldconfig;
145

    
146
echo gettext("Done") . "\n";
(27-27/27)