Package br.com.objectos.rio.bdo

Source Code of br.com.objectos.rio.bdo.BdoInstall

/*
* 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.bdo;

import java.io.File;
import java.net.URL;

import br.com.objectos.rio.AbstractRioCommand;
import br.com.objectos.rio.Rio;
import br.com.objectos.rio.core.os.Procs;
import br.com.objectos.way.base.io.Directory;

import com.google.inject.Inject;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class BdoInstall extends AbstractRioCommand<BdoInstallOptions> implements BdoInstallCommand {

  private final BdoDirs dirs;

  @Inject
  public BdoInstall(BdoDirs dirs) {
    this.dirs = dirs;
  }

  @Override
  protected String getCommandName() {
    return "bdo install";
  }

  @Override
  protected BdoInstallOptions newOptions() {
    return new BdoInstallOptions();
  }

  @Override
  public void install(BdoInstance instance, Directory target) {
    BdoInstallOptions options = instance.toInstallOptions(target);
    initMessages();
    init(options);
    install(options);
  }

  @Override
  protected void executeCommand(BdoInstallOptions options) {
    init(options);

    if (options.install) {
      install(options);
    }

    if (options.start) {
      start(options);
    }
  }

  private void init(BdoInstallOptions options) {
    initPrepare(options);
    initData(options);
    initChown(options);
    initChmod(options);
  }

  private void initPrepare(BdoInstallOptions options) {
    String name = options.name;
    Directory installVar = options.installVar(dirs);

    infoAction("prepare");
    info("Staging files at %s.", installVar.getAbsolutePath());

    resourcesAt("/bdo/install")
        .map("etc/conf.d/mysql", "/etc/conf.d/mysql-%s", name)
        .map("etc/init.d/mysql", "/etc/init.d/mysql-%s", name)
        .map("etc/mysql/my.cnf", "/etc/mysql/my-%s.cnf", name)
        .evalWith(options)
        .only("etc/conf.d/mysql")
        .only("etc/mysql/my.cnf")
        .copyTo(installVar);
  }

  private void initData(BdoInstallOptions options) {
    infoAction("data");
    info("Creating mysql data files.");

    URL url = options.getDataUrl();
    File file = options.getDataFile(dirs);
    Rio.copy(url, file);

    Directory dataDir = options.getDataDir(dirs);

    untar(file)
        .bunzip2()
        .preserve()
        .toDir(dataDir);
  }

  private void initChown(BdoInstallOptions options) {
    infoAction("chown");
    info("Fixing ownerships.");

    Directory installVar = options.installVar(dirs);

    chown(installVar)
        .file("etc").to("root.root").recursively()
        .file("var").to("root.root")
        .file("var/lib").to("root.root")
        .file("var/lib/mysql").to("mysql.mysql").recursively()
        .exec();
  }

  private void initChmod(BdoInstallOptions options) {
    infoAction("chmod");
    info("Fixing modes.");

    Directory installVar = options.installVar(dirs);
    String scriptName = options.getScriptName();

    chmod(installVar)
        .file("etc/init.d/" + scriptName).to(755)
        .file("var/lib/mysql").to(770)
        .exec();
  }

  private void install(BdoInstallOptions options) {
    infoAction("move");
    info("Moving files to destination.");

    Directory installVar = options.installVar(dirs);
    File file = options.getMoveFile(dirs);
    Directory dest = options.getDest();

    tar(installVar)
        .preserve()
        .add(".")
        .toFile(file);

    untar(file)
        .preserve()
        .toDir(dest);
  }

  private void start(BdoInstallOptions options) {
    infoAction("start");
    info("Starting...");

    String name = options.getScriptName();

    Procs.newCommand()
        .add("rc-update")
        .add("add")
        .add(name)
        .add("default")
        .exec();

    Procs.newCommand()
        .add("/etc/init.d/" + name)
        .add("start")
        .exec();
  }

}
TOP

Related Classes of br.com.objectos.rio.bdo.BdoInstall

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.