/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.rio.eto.os;
import java.io.File;
import br.com.objectos.rio.AbstractRioCommand;
import br.com.objectos.rio.HttpServer;
import br.com.objectos.rio.OsDirs;
import br.com.objectos.rio.core.os.Chroot;
import br.com.objectos.rio.core.os.ChrootMount;
import br.com.objectos.rio.core.os.ChrootUmount;
import br.com.objectos.rio.core.os.Procs;
import br.com.objectos.way.base.io.Directory;
import br.com.objectos.way.base.io.Stdout;
import com.google.inject.Inject;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class EtoOsLivecd extends AbstractRioCommand<EtoOsLivecdOptions> implements EtoOsLivecdCommand {
private final OsDirs dirs;
@Inject
public EtoOsLivecd(OsDirs dirs) {
this.dirs = dirs;
}
@Override
protected String getCommandName() {
return "eto os livecd";
}
@Override
protected EtoOsLivecdOptions newOptions() {
return new EtoOsLivecdOptions();
}
@Override
protected void executeCommand(EtoOsLivecdOptions options) {
if (options.download) {
download(options);
}
if (options.stage) {
stage(options);
}
if (options.emerge) {
emerge(options);
}
}
private void download(EtoOsLivecdOptions options) {
HttpServer server = options.fileServer();
Directory osVar = dirs.osVar();
File isoFile = osVar.fileAt("install-amd64-minimal.iso");
infoAction("download");
info("Downloading required files.");
download(server).to(osVar)
.add(isoFile.getName())
.add("portage.tar.bz2")
.exec();
}
private void stage(EtoOsLivecdOptions options) {
Directory osVar = dirs.osVar();
File isoFile = osVar.fileAt("install-amd64-minimal.iso");
Directory isoDir = dirs.isoMountDir();
Directory livecdDir = dirs.livecdMountDir();
infoAction("stage");
info("Staging required files.");
info();
info("Unpacking iso file.");
mount(isoFile.getAbsolutePath())
.options("-o", "loop")
.to(isoDir);
Procs.newCommand()
.add("unsquashfs")
.add("-f")
.add("-d")
.add(livecdDir.getAbsolutePath())
.add(isoDir.fileAt("image.squashfs").getAbsolutePath())
.exec();
umount(isoDir);
info();
info("Unpacking portage snapshot.");
untar(osVar.fileAt("portage.tar.bz2"))
.bunzip2()
.preserve()
.toDir(livecdDir.dirAt("usr"));
}
private void emerge(EtoOsLivecdOptions options) {
HttpServer server = options.distfilesServer();
Directory mountDir = dirs.livecdMountDir();
ChrootMount mount = ChrootMount.at(mountDir).mount();
infoAction("emerge");
info("Emerging extra packages.");
server.download("jdk-6u45-linux-x64.bin")
.toDir(mountDir.dirAt("usr/portage/distfiles"));
try {
Chroot chroot = Chroot.at(mountDir)
.add("source /etc/profile")
.add("export PS1=\"(chroot) $PS1\"")
.add("emerge -v sun-jdk-bin")
.exec();
Stdout.print(chroot);
} finally {
ChrootUmount umount = mount.umount();
Stdout.print(umount);
}
}
}