-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallerLiveCD.cpp
236 lines (194 loc) · 8.7 KB
/
InstallerLiveCD.cpp
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
/*
* libcloudos
* Copyright (C) 2014 Tony Wolf <wolf@os-forge.net>
*
* 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 2 of the License.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "InstallerLiveCD.hpp"
#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/join.hpp>
namespace cloudos {
namespace system {
InstallerLiveCD::InstallerLiveCD ( const std::string& p_root_dir ) : InstallerExtended ( p_root_dir ) {
init();
}
InstallerLiveCD::InstallerLiveCD ( const fs::path& p_root_dir ) : InstallerExtended ( p_root_dir ) {
init();
}
InstallerLiveCD::InstallerLiveCD ( std::string && p_root_dir ) : InstallerExtended ( std::move(p_root_dir) ) {
init();
}
InstallerLiveCD::InstallerLiveCD ( fs::path && p_root_dir ) : InstallerExtended ( std::move(p_root_dir) ) {
init();
}
const fs::path& InstallerLiveCD::getTarFilePath() const {
return c_tar_file;
}
//
// P R O T E C T E D
//
bool InstallerLiveCD::stepInstallLiveCDPackages() {
if( searchForRPMsInDirectory("installer") == false ) {
LOG_E() << "installer RPMs are required! Abort!";
return false;
}
return installPackages();
}
bool InstallerLiveCD::stepCleanUpSetup() {
// FIXME: clean up packages and remove some of these clean ups here...
if( InstallerExtended::stepCleanUpSetup() == false ) {
LOG_E() << "InstallerExtended clean up process failed... Abort!";
return false;
}
fs::remove_all( getRootDirAsPath() / "usr/share/gtk-doc" );
fs::remove_all( getRootDirAsPath() / "usr/lib/python2.7/test" );
fs::remove_all( getRootDirAsPath() / "usr/lib/python2.7/debug" );
fs::remove_all( getRootDirAsPath() / "usr/share/vim/vim73/spell/he.vim" );
fs::remove_all( getRootDirAsPath() / "usr/share/vim/vim73/spell/yi.vim" );
std::vector<std::string> to_be_removed;
// $ROOT_DIR/usr/share/vim/vim73/lang/{af,ca,cs,de,eo,es,fi,fr,ga,it,ja,ko}*
to_be_removed = boost::assign::list_of("af")("ca")("cs")("de")("eo")("es")("fi")("fr")("ga")("it")("ja")("ko").convert_to_container<std::vector<std::string> >();
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "usr/share/vim/vim73/lang", LOOKUP_BY_FULLMATCH );
// $ROOT_DIR/usr/share/locale/e{l,o,s,t,u}*
to_be_removed = boost::assign::list_of("el")("eo")("es")("et")("eu").convert_to_container<std::vector<std::string> >();
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "usr/share/locale", LOOKUP_BY_FULLMATCH );
// $ROOT_DIR/usr/share/locale/[a-cf-z]*
to_be_removed = std::vector<std::string>();
to_be_removed.push_back("^([a-cf-z].*)");
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "usr/share/locale", LOOKUP_BY_REGEX );
// $ROOT_DIR/usr/share/vim/vim73/lang/menu_[a-df-z]*
to_be_removed = std::vector<std::string>();
to_be_removed.push_back("^(menu_[a-df-z].*)");
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "usr/share/locale", LOOKUP_BY_REGEX );
// $ROOT_DIR/usr/lib/*.a
to_be_removed = std::vector<std::string>();
to_be_removed.push_back(".a");
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "usr/lib", LOOKUP_BY_ENDSWITH );
// $ROOT_DIR/usr/share/vim/vim73/doc/*.txt
to_be_removed = std::vector<std::string>();
to_be_removed.push_back(".txt");
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "usr/lib", LOOKUP_BY_ENDSWITH );
// $ROOT_DIR/var/cache/zypper/RPMS/*.rpm
to_be_removed = std::vector<std::string>();
to_be_removed.push_back(".rpm");
removeFiles( std::move(to_be_removed), getRootDirAsPath() / "var/cache/zypper/RPMS", LOOKUP_BY_ENDSWITH );
return true;
}
bool InstallerLiveCD::stepCopyBasesystemRpms() {
File from( "/usr/share/cloudos/installer/basesystem-rpms.tar" );
fs::path to( getRootDirAsPath() / "usr/share/cloudos/installer/basesystem-rpms.tar" );
if( from.isAccessible() == false ) {
LOG_E() << from.getFileName() << " is not available... Abort!";
return false;
}
boost::system::error_code ec;
fs::copy_file(from.getFileNameAsPath(), to, ec);
if( ec ) {
LOG_E() << "copy file failed... " << ec.message() << " ...Abort!";
return false;
}
return true;
}
bool InstallerLiveCD::stepCreateTarFile() {
// get image version:
Command rpm("rpm");
rpm.setChRootController(c_chroot);
rpm << "-q" << "--queryformat" << "%{VERSION}-%{RELEASE}" << "cloudos-installer";
if( rpm.waitUntilFinished() != 0 ) {
LOG_E() << "could not determine image version and release number... Abort!";
return false;
}
std::string version( rpm.getStdoutOutput().str() );
boost::trim(version);
if( c_chroot->disable() == false ) {
LOG_E() << "failed to disable chroot env... Abort!";
return false;
}
Directory root(getRootDirAsPath());
if( root.isAccessible() == false ) {
LOG_E() << "root dir is not accessible... Abort!";
return false;
}
std::vector<fs::path> root_files;
if( root.findFiles(root_files, ".*", LOOKUP_BY_REGEX, FILE_TYPE_ALL) == false ) {
LOG_E() << "no files found within our root dir... Abort!";
return false;
}
c_tar_file = "/tmp/installer-livecd-"+version+".tar.gz";
if( fs::exists(c_tar_file) ) {
boost::system::error_code ec;
LOG_W() << c_tar_file.string() << " already exists! Removing the old one...";
fs::remove( c_tar_file, ec );
if( fs::exists(c_tar_file) ) {
LOG_E() << "could not remove the old image data!" << ec.message() << " ...Abort!";
return false;
}
LOG_W() << "old image data removed! SUCCESS!";
}
Command tar("tar");
tar << "--transform" << "s,^"+getRootDir()+",.,S"; // remove leading dirs with ".", while creating the tar file...
tar << "--absolute-names"; // don't remove leading / as we need it for --transform
tar << "-czf" << c_tar_file.string();
for(fs::path& file : root_files) {
tar.addArgument( std::move(file.string()) );
}
return tar.waitUntilFinished() == 0;
}
bool InstallerLiveCD::stepCleanUpEnvironment() {
if( c_chroot->disable() == false ) {
LOG_E() << "failed to disable chroot... Abort!";
return false;
}
LOG_I() << "removing root dir files...";
fs::remove_all( getRootDirAsPath() );
return true;
}
void InstallerLiveCD::init() {
LOG_I() << "loading steps...";
appendInstallerStep( "prepare_coredata", boost::bind(&InstallerLiveCD::stepPrepareCoreSystem, this) );
appendInstallerStep( "package_install", boost::bind(&InstallerLiveCD::stepInstallLiveCDPackages, this) );
appendInstallerStep( "cleanup", boost::bind(&InstallerLiveCD::stepCleanUpSetup, this) );
appendInstallerStep( "copy_baserpms", boost::bind(&InstallerLiveCD::stepCopyBasesystemRpms, this) );
appendInstallerStep( "create_tar_file", boost::bind(&InstallerLiveCD::stepCreateTarFile, this) );
appendInstallerStep( "cleanup_env", boost::bind(&InstallerLiveCD::stepCleanUpEnvironment, this) );
LOG_I() << "steps loaded";
}
//
// P R I V A T E
//
void InstallerLiveCD::removeFiles ( std::vector< std::string > && p_elements, fs::path&& p_prepend_path, FileLookupMode p_mode ) {
for(auto& element : p_elements) {
LOG_I() << "try to remove files in " << p_prepend_path.string() << " and prase: " << element;
if( fs::exists( p_prepend_path ) == false ) {
LOG_I() << "does not exists";
continue;
}
Directory dir( p_prepend_path );
if( dir.isAccessible() == false ) {
LOG_I() << "is not accessible";
continue;
}
std::vector<fs::path> results;
if( dir.findFiles(results, element, p_mode, FILE_TYPE_ALL) == false ) { // no results found
LOG_I() << "no files found";
continue;
}
for(auto& rm_element : results) {
LOG_D() << "removing all from " << rm_element.string();
fs::remove_all( rm_element );
}
}
}
}}