private void startAdditionalServices() {

    Script command = new Script("rm", s_logger);
    command.add("-rf");
    command.add(extractMountPoint);
    String result = command.execute();
    if (result != null) {
      s_logger.warn("Error in creating file " + extractMountPoint + " ,error: " + result);
      return;
    }

    command = new Script("touch", s_logger);
    command.add(extractMountPoint);
    result = command.execute();
    if (result != null) {
      s_logger.warn("Error in creating file " + extractMountPoint + " ,error: " + result);
      return;
    }

    command = new Script("/bin/bash", s_logger);
    command.add("-c");
    command.add("ln -sf " + parentDir + " " + extractMountPoint);
    result = command.execute();
    if (result != null) {
      s_logger.warn("Error in linking  err=" + result);
      return;
    }
  }
예제 #2
0
  protected String mount(String path, String parent) {
    String mountPoint = setupMountPoint(parent);
    if (mountPoint == null) {
      s_logger.warn("Unable to create a mount point");
      return null;
    }

    Script script = null;
    String result = null;
    Script command = new Script(true, "mount", _timeout, s_logger);
    command.add("-t", "nfs");
    // command.add("-o", "soft,timeo=133,retrans=2147483647,tcp,acdirmax=0,acdirmin=0");
    command.add(path);
    command.add(mountPoint);
    result = command.execute();
    if (result != null) {
      s_logger.warn("Unable to mount " + path + " due to " + result);
      File file = new File(mountPoint);
      if (file.exists()) {
        file.delete();
      }
      return null;
    }

    // Change permissions for the mountpoint
    script = new Script(true, "chmod", _timeout, s_logger);
    script.add("777", mountPoint);
    result = script.execute();
    if (result != null) {
      s_logger.warn("Unable to set permissions for " + mountPoint + " due to " + result);
      return null;
    }
    return mountPoint;
  }
 private void addRouteToInternalIpOrCidr(
     String localgw, String eth1ip, String eth1mask, String destIpOrCidr) {
   s_logger.debug(
       "addRouteToInternalIp: localgw="
           + localgw
           + ", eth1ip="
           + eth1ip
           + ", eth1mask="
           + eth1mask
           + ",destIp="
           + destIpOrCidr);
   if (destIpOrCidr == null) {
     s_logger.debug("addRouteToInternalIp: destIp is null");
     return;
   }
   if (!NetUtils.isValidIp(destIpOrCidr) && !NetUtils.isValidCIDR(destIpOrCidr)) {
     s_logger.warn(" destIp is not a valid ip address or cidr destIp=" + destIpOrCidr);
     return;
   }
   boolean inSameSubnet = false;
   if (NetUtils.isValidIp(destIpOrCidr)) {
     if (eth1ip != null && eth1mask != null) {
       inSameSubnet = NetUtils.sameSubnet(eth1ip, destIpOrCidr, eth1mask);
     } else {
       s_logger.warn(
           "addRouteToInternalIp: unable to determine same subnet: _eth1ip="
               + eth1ip
               + ", dest ip="
               + destIpOrCidr
               + ", _eth1mask="
               + eth1mask);
     }
   } else {
     inSameSubnet =
         NetUtils.isNetworkAWithinNetworkB(
             destIpOrCidr, NetUtils.ipAndNetMaskToCidr(eth1ip, eth1mask));
   }
   if (inSameSubnet) {
     s_logger.debug(
         "addRouteToInternalIp: dest ip "
             + destIpOrCidr
             + " is in the same subnet as eth1 ip "
             + eth1ip);
     return;
   }
   Script command = new Script("/bin/bash", s_logger);
   command.add("-c");
   command.add("ip route delete " + destIpOrCidr);
   command.execute();
   command = new Script("/bin/bash", s_logger);
   command.add("-c");
   command.add("ip route add " + destIpOrCidr + " via " + localgw);
   String result = command.execute();
   if (result != null) {
     s_logger.warn("Error in configuring route to internal ip err=" + result);
   } else {
     s_logger.debug(
         "addRouteToInternalIp: added route to internal ip=" + destIpOrCidr + " via " + localgw);
   }
 }
 private void deletExitingLinkLocalRoutTable(String linkLocalBr) {
   Script command = new Script("/bin/bash", _timeout);
   command.add("-c");
   command.add("ip route | grep " + NetUtils.getLinkLocalCIDR());
   OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
   String result = command.execute(parser);
   boolean foundLinkLocalBr = false;
   if (result == null && parser.getLines() != null) {
     String[] lines = parser.getLines().split("\\n");
     for (String line : lines) {
       String[] tokens = line.split(" ");
       if (!tokens[2].equalsIgnoreCase(linkLocalBr)) {
         Script.runSimpleBashScript("ip route del " + NetUtils.getLinkLocalCIDR());
       } else {
         foundLinkLocalBr = true;
       }
     }
   }
   if (!foundLinkLocalBr) {
     Script.runSimpleBashScript(
         "ifconfig "
             + linkLocalBr
             + " 169.254.0.1;"
             + "ip route add "
             + NetUtils.getLinkLocalCIDR()
             + " dev "
             + linkLocalBr
             + " src "
             + NetUtils.getLinkLocalGateway());
   }
 }
예제 #5
0
  private void startupCleanup(String parent) {
    s_logger.info("Cleanup mounted NFS mount points used in previous session");

    long mshostId = ManagementServerNode.getManagementServerId();

    // cleanup left-over NFS mounts from previous session
    String[] mounts = _storage.listFiles(parent + File.separator + String.valueOf(mshostId) + ".*");
    if (mounts != null && mounts.length > 0) {
      for (String mountPoint : mounts) {
        s_logger.info("umount NFS mount from previous session: " + mountPoint);

        String result = null;
        Script command = new Script(true, "umount", _timeout, s_logger);
        command.add(mountPoint);
        result = command.execute();
        if (result != null) {
          s_logger.warn("Unable to umount " + mountPoint + " due to " + result);
        }
        File file = new File(mountPoint);
        if (file.exists()) {
          file.delete();
        }
      }
    }
  }
  private Answer execute(SetStaticNatRulesCommand cmd) {
    String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    String[] results = new String[cmd.getRules().length];
    int i = 0;
    boolean endResult = true;
    for (StaticNatRuleTO rule : cmd.getRules()) {
      String result = null;
      final Script command = new Script(_firewallPath, _timeout, s_logger);
      command.add(routerIp);
      command.add(rule.revoked() ? "-D" : "-A");

      // 1:1 NAT needs instanceip;publicip;domrip;op
      command.add(" -l ", rule.getSrcIp());
      command.add(" -r ", rule.getDstIp());

      if (rule.getProtocol() != null) {
        command.add(" -P ", rule.getProtocol().toLowerCase());
      }

      command.add(" -d ", rule.getStringSrcPortRange());
      command.add(" -G ");

      result = command.execute();
      if (result == null) {
        results[i++] = null;
      } else {
        results[i++] = "Failed";
        endResult = false;
      }
    }

    return new SetStaticNatRulesAnswer(cmd, results, endResult);
  }
  private Answer execute(SetFirewallRulesCommand cmd) {
    String[] results = new String[cmd.getRules().length];
    for (int i = 0; i < cmd.getRules().length; i++) {
      results[i] = "Failed";
    }
    String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);

    if (routerIp == null) {
      return new SetFirewallRulesAnswer(cmd, false, results);
    }

    String[][] rules = cmd.generateFwRules();
    final Script command = new Script(_firewallPath, _timeout, s_logger);
    command.add(routerIp);
    command.add("-F");

    StringBuilder sb = new StringBuilder();
    String[] fwRules = rules[0];
    if (fwRules.length > 0) {
      for (int i = 0; i < fwRules.length; i++) {
        sb.append(fwRules[i]).append(',');
      }
      command.add("-a", sb.toString());
    }

    String result = command.execute();
    if (result != null) {
      return new SetFirewallRulesAnswer(cmd, false, results);
    }
    return new SetFirewallRulesAnswer(cmd, true, null);
  }
  @Override
  public CreateEntityDownloadURLAnswer handleCreateEntityURLCommand(
      CreateEntityDownloadURLCommand cmd) {

    boolean isApacheUp = checkAndStartApache();
    if (!isApacheUp) {
      String errorString = "Error in starting Apache server ";
      s_logger.error(errorString);
      return new CreateEntityDownloadURLAnswer(
          errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
    }
    // Create the directory structure so that its visible under apache server root
    Script command = new Script("mkdir", s_logger);
    command.add("-p");
    command.add("/var/www/html/copy");
    String result = command.execute();
    if (result != null) {
      String errorString = "Error in creating directory =" + result;
      s_logger.error(errorString);
      return new CreateEntityDownloadURLAnswer(
          errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
    }

    // Create a symbolic link from the actual directory to the template location. The entity would
    // be directly visible under /var/www/html/
    cmd.getInstallPath();
    command = new Script("/bin/bash", s_logger);
    command.add("-c");
    command.add(
        "ln -sf "
            + extractMountPoint
            + File.separator
            + cmd.getInstallPath()
            + " /var/www/html/copy/");
    result = command.execute();
    if (result != null) {
      String errorString = "Error in linking  err=" + result;
      s_logger.error(errorString);
      return new CreateEntityDownloadURLAnswer(
          errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
    }

    return new CreateEntityDownloadURLAnswer("", CreateEntityDownloadURLAnswer.RESULT_SUCCESS);
  }
예제 #9
0
  @Override
  public FormatInfo process(String templatePath, ImageFormat format, String templateName)
      throws InternalErrorException {
    if (format != null) {
      if (s_logger.isInfoEnabled()) {
        s_logger.info("We currently don't handle conversion from " + format + " to OVA.");
      }
      return null;
    }

    s_logger.info(
        "Template processing. templatePath: " + templatePath + ", templateName: " + templateName);
    String templateFilePath =
        templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension();
    if (!_storage.exists(templateFilePath)) {
      if (s_logger.isInfoEnabled()) {
        s_logger.info("Unable to find the vmware template file: " + templateFilePath);
      }
      return null;
    }

    s_logger.info(
        "Template processing - untar OVA package. templatePath: "
            + templatePath
            + ", templateName: "
            + templateName);
    String templateFileFullPath =
        templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension();
    File templateFile = new File(templateFileFullPath);

    Script command = new Script("tar", 0, s_logger);
    command.add("--no-same-owner");
    command.add("-xf", templateFileFullPath);
    command.setWorkDir(templateFile.getParent());
    String result = command.execute();
    if (result != null) {
      s_logger.info(
          "failed to untar OVA package due to "
              + result
              + ". templatePath: "
              + templatePath
              + ", templateName: "
              + templateName);
      throw new InternalErrorException("failed to untar OVA package");
    }

    FormatInfo info = new FormatInfo();
    info.format = ImageFormat.OVA;
    info.filename = templateName + "." + ImageFormat.OVA.getFileExtension();
    info.size = _storage.getSize(templateFilePath);
    info.virtualSize = getTemplateVirtualSize(templatePath, info.filename);

    // delete original OVA file
    // templateFile.delete();
    return info;
  }
예제 #10
0
  private void deleteVnetBr(String brName) {
    synchronized (_vnetBridgeMonitor) {
      String cmdout = Script.runSimpleBashScript("ls /sys/class/net/" + brName);
      if (cmdout == null)
        // Bridge does not exist
        return;
      cmdout = Script.runSimpleBashScript("ls /sys/class/net/" + brName + "/brif | tr '\n' ' '");
      if (cmdout != null && cmdout.contains("vnet")) {
        // Active VM remains on that bridge
        return;
      }

      Pattern oldStyleBrNameRegex = Pattern.compile("^cloudVirBr(\\d+)$");
      Pattern brNameRegex = Pattern.compile("^br(\\S+)-(\\d+)$");
      Matcher oldStyleBrNameMatcher = oldStyleBrNameRegex.matcher(brName);
      Matcher brNameMatcher = brNameRegex.matcher(brName);

      String pName = null;
      String vNetId = null;
      if (oldStyleBrNameMatcher.find()) {
        // Actually modifyvlan.sh doesn't require pif name when deleting its bridge so far.
        pName = "undefined";
        vNetId = oldStyleBrNameMatcher.group(1);
      } else if (brNameMatcher.find()) {
        if (brNameMatcher.group(1) != null || !brNameMatcher.group(1).isEmpty()) {
          pName = brNameMatcher.group(1);
        } else {
          pName = "undefined";
        }
        vNetId = brNameMatcher.group(2);
      }

      if (vNetId == null || vNetId.isEmpty()) {
        s_logger.debug("unable to get a vNet ID from name " + brName);
        return;
      }

      String scriptPath = null;
      if (cmdout != null && cmdout.contains("vxlan")) {
        scriptPath = _modifyVxlanPath;
      } else {
        scriptPath = _modifyVlanPath;
      }

      final Script command = new Script(scriptPath, _timeout, s_logger);
      command.add("-o", "delete");
      command.add("-v", vNetId);
      command.add("-p", pName);
      command.add("-b", brName);

      final String result = command.execute();
      if (result != null) {
        s_logger.debug("Delete bridge " + brName + " failed: " + result);
      }
    }
  }
 protected String getDomRVersion(String routerIP) {
   final Script command = new Script(_getDomRVersionPath, _timeout, s_logger);
   final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
   command.add(routerIP);
   String result = command.execute(parser);
   if (result == null) {
     return parser.getLine();
   }
   return null;
 }
 public String getRouterStatus(String routerIP) {
   final Script command = new Script(_getRouterStatusPath, _timeout, s_logger);
   final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
   command.add(routerIP);
   String result = command.execute(parser);
   if (result == null) {
     return parser.getLine();
   }
   return null;
 }
  @Override
  public DeleteEntityDownloadURLAnswer handleDeleteEntityDownloadURLCommand(
      DeleteEntityDownloadURLCommand cmd) {

    // Delete the soft link. Example path = volumes/8/74eeb2c6-8ab1-4357-841f-2e9d06d1f360.vhd
    s_logger.warn(
        "handleDeleteEntityDownloadURLCommand Path:"
            + cmd.getPath()
            + " Type:"
            + cmd.getType().toString());
    String path = cmd.getPath();
    Script command = new Script("/bin/bash", s_logger);
    command.add("-c");
    // We just need to remove the UUID.vhd
    command.add(
        "unlink /var/www/html/copy/" + path.substring(path.lastIndexOf(File.separator) + 1));
    String result = command.execute();
    if (result != null) {
      String errorString = "Error in deleting =" + result;
      s_logger.warn(errorString);
      return new DeleteEntityDownloadURLAnswer(
          errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
    }

    // If its a volume also delete the Hard link since it was created only for the purpose of
    // download.
    if (cmd.getType() == Upload.Type.VOLUME) {
      command = new Script("/bin/bash", s_logger);
      command.add("-c");
      command.add("rm -f " + parentDir + File.separator + path);
      s_logger.warn(" " + parentDir + File.separator + path);
      result = command.execute();
      if (result != null) {
        String errorString = "Error in linking  err=" + result;
        s_logger.warn(errorString);
        return new DeleteEntityDownloadURLAnswer(
            errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
      }
    }

    return new DeleteEntityDownloadURLAnswer("", CreateEntityDownloadURLAnswer.RESULT_SUCCESS);
  }
 protected Answer execute(BumpUpPriorityCommand cmd) {
   final String routerPrivateIPAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
   final Script command = new Script(_bumpUpPriorityPath, _timeout, s_logger);
   final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
   command.add(routerPrivateIPAddress);
   String result = command.execute(parser);
   if (result != null) {
     return new Answer(cmd, false, "BumpUpPriorityCommand failed: " + result);
   }
   return new Answer(cmd, true, null);
 }
 private boolean isDNSmasqRunning(String dnsmasqName) {
   Script cmd = new Script("/bin/sh", _timeout);
   cmd.add("-c");
   cmd.add("ls -l /var/run/libvirt/network/" + dnsmasqName + ".pid");
   String result = cmd.execute();
   if (result != null) {
     return false;
   } else {
     return true;
   }
 }
 private boolean isBridgeExists(String bridgeName) {
   Script command = new Script("/bin/sh", _timeout);
   command.add("-c");
   command.add("brctl show|grep " + bridgeName);
   final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
   String result = command.execute(parser);
   if (result != null || parser.getLine() == null) {
     return false;
   } else {
     return true;
   }
 }
  public synchronized String savePassword(
      final String privateIpAddress,
      final String vmIpAddress,
      final String password,
      final String localPath) {
    final Script command = new Script(_savepasswordPath, _startTimeout, s_logger);
    command.add("-r", privateIpAddress);
    command.add("-v", vmIpAddress);
    command.add("-p", password);
    command.add(localPath);

    return command.execute();
  }
  public String assignPublicIpAddress(
      final String vmName,
      final long id,
      final String vnet,
      final String privateIpAddress,
      final String macAddress,
      final String publicIpAddress) {

    final Script command = new Script(_ipassocPath, _timeout, s_logger);
    command.add("-A");
    command.add("-f"); // first ip is source nat ip
    command.add("-r", vmName);
    command.add("-i", privateIpAddress);
    command.add("-a", macAddress);
    command.add("-l", publicIpAddress);

    return command.execute();
  }
  private Answer execute(VpnUsersCfgCommand cmd) {
    for (VpnUsersCfgCommand.UsernamePassword userpwd : cmd.getUserpwds()) {
      Script command = new Script(_l2tpVpnPath, _timeout, s_logger);
      command.add(cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP));
      if (!userpwd.isAdd()) {
        command.add("-U ", userpwd.getUsername());
      } else {
        command.add("-u ", userpwd.getUsernamePassword());
      }
      String result = command.execute();
      if (result != null) {
        return new Answer(
            cmd, false, "Configure VPN user failed for user " + userpwd.getUsername());
      }
    }

    return new Answer(cmd);
  }
 private Answer execute(RemoteAccessVpnCfgCommand cmd) {
   Script command = new Script(_l2tpVpnPath, _timeout, s_logger);
   command.add(cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP));
   if (cmd.isCreate()) {
     command.add("-r ", cmd.getIpRange());
     command.add("-p ", cmd.getPresharedKey());
     command.add("-s ", cmd.getVpnServerIp());
     command.add("-l ", cmd.getLocalIp());
     command.add("-c ");
   } else {
     command.add("-d ");
     command.add("-s ", cmd.getVpnServerIp());
   }
   String result = command.execute();
   if (result != null) {
     return new Answer(cmd, false, "Configure VPN failed");
   }
   return new Answer(cmd);
 }
예제 #21
0
  private void shutdownCleanup() {
    s_logger.info("Cleanup mounted NFS mount points used in current session");

    for (String mountPoint : _storageMounts.values()) {
      s_logger.info("umount NFS mount: " + mountPoint);

      String result = null;
      Script command = new Script(true, "umount", _timeout, s_logger);
      command.add(mountPoint);
      result = command.execute();
      if (result != null) {
        s_logger.warn("Unable to umount " + mountPoint + " due to " + result);
      }
      File file = new File(mountPoint);
      if (file.exists()) {
        file.delete();
      }
    }
  }
예제 #22
0
  private void createVnet(String vnetId, String pif, String brName, String protocol)
      throws InternalErrorException {
    synchronized (_vnetBridgeMonitor) {
      String script = _modifyVlanPath;
      if (protocol.equals(Networks.BroadcastDomainType.Vxlan.scheme())) {
        script = _modifyVxlanPath;
      }
      final Script command = new Script(script, _timeout, s_logger);
      command.add("-v", vnetId);
      command.add("-p", pif);
      command.add("-b", brName);
      command.add("-o", "add");

      final String result = command.execute();
      if (result != null) {
        throw new InternalErrorException("Failed to create vnet " + vnetId + ": " + result);
      }
    }
  }
  private String setLoadBalancerConfig(
      final String cfgFile,
      final String[] addRules,
      final String[] removeRules,
      final String[] statsRules,
      String routerIp) {

    if (routerIp == null) {
      routerIp = "none";
    }

    final Script command = new Script(_loadbPath, _timeout, s_logger);

    command.add("-i", routerIp);
    command.add("-f", cfgFile);

    StringBuilder sb = new StringBuilder();
    if (addRules.length > 0) {
      for (int i = 0; i < addRules.length; i++) {
        sb.append(addRules[i]).append(',');
      }
      command.add("-a", sb.toString());
    }

    sb = new StringBuilder();
    if (removeRules.length > 0) {
      for (int i = 0; i < removeRules.length; i++) {
        sb.append(removeRules[i]).append(',');
      }
      command.add("-d", sb.toString());
    }

    sb = new StringBuilder();
    if (statsRules.length > 0) {
      for (int i = 0; i < statsRules.length; i++) {
        sb.append(statsRules[i]).append(',');
      }
      command.add("-s", sb.toString());
    }

    return command.execute();
  }
  protected synchronized Answer execute(final DhcpEntryCommand cmd) {
    final Script command = new Script(_dhcpEntryPath, _timeout, s_logger);
    command.add("-r", cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP));
    command.add("-v", cmd.getVmIpAddress());
    command.add("-m", cmd.getVmMac());
    command.add("-n", cmd.getVmName());

    if (cmd.getDefaultRouter() != null) {
      command.add(" -d " + cmd.getDefaultRouter());
    }
    if (cmd.getStaticRoutes() != null) {
      command.add(" -s " + cmd.getStaticRoutes());
    }

    if (cmd.getDefaultDns() != null) {
      command.add(" -N " + cmd.getDefaultDns());
    }

    final String result = command.execute();
    return new Answer(cmd, result == null, result);
  }
  public String assignPublicIpAddress(
      final String vmName,
      final String privateIpAddress,
      final String publicIpAddress,
      final boolean add,
      final boolean firstIP,
      final boolean sourceNat,
      final String vlanId,
      final String vlanGateway,
      final String vlanNetmask,
      final String vifMacAddress,
      String guestIp,
      int nicNum) {

    final Script command = new Script(_ipassocPath, _timeout, s_logger);
    command.add(privateIpAddress);
    if (add) {
      command.add("-A");
    } else {
      command.add("-D");
    }
    String cidrSize = Long.toString(NetUtils.getCidrSize(vlanNetmask));
    if (sourceNat) {
      command.add("-f");
      command.add("-l", publicIpAddress + "/" + cidrSize);
    } else if (firstIP) {
      command.add("-f");
      command.add("-l", publicIpAddress + "/" + cidrSize);
    } else {
      command.add("-l", publicIpAddress);
    }

    String publicNic = "eth" + nicNum;
    command.add("-c", publicNic);

    return command.execute();
  }
  @Override
  public Answer execute(
      final OvsCreateTunnelCommand command,
      final LibvirtComputingResource libvirtComputingResource) {
    final String bridge = command.getNetworkName();
    try {
      if (!libvirtComputingResource.findOrCreateTunnelNetwork(bridge)) {
        s_logger.debug("Error during bridge setup");
        return new OvsCreateTunnelAnswer(command, false, "Cannot create network", bridge);
      }

      libvirtComputingResource.configureTunnelNetwork(
          command.getNetworkId(), command.getFrom(), command.getNetworkName());

      final Script scriptCommand =
          new Script(
              libvirtComputingResource.getOvsTunnelPath(),
              libvirtComputingResource.getTimeout(),
              s_logger);
      scriptCommand.add("create_tunnel");
      scriptCommand.add("--bridge", bridge);
      scriptCommand.add("--remote_ip", command.getRemoteIp());
      scriptCommand.add("--key", command.getKey().toString());
      scriptCommand.add("--src_host", command.getFrom().toString());
      scriptCommand.add("--dst_host", command.getTo().toString());

      final String result = scriptCommand.execute();
      if (result != null) {
        return new OvsCreateTunnelAnswer(command, true, result, null, bridge);
      } else {
        return new OvsCreateTunnelAnswer(command, false, result, bridge);
      }
    } catch (final Exception e) {
      s_logger.warn("Caught execption when creating ovs tunnel", e);
      return new OvsCreateTunnelAnswer(command, false, e.getMessage(), bridge);
    }
  }
예제 #27
0
 private void deleteExistingLinkLocalRouteTable(String linkLocalBr) {
   Script command = new Script("/bin/bash", _timeout);
   command.add("-c");
   command.add("ip route | grep " + NetUtils.getLinkLocalCIDR());
   OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
   String result = command.execute(parser);
   boolean foundLinkLocalBr = false;
   if (result == null && parser.getLines() != null) {
     String[] lines = parser.getLines().split("\\n");
     for (String line : lines) {
       String[] tokens = line.split(" ");
       if (tokens != null && tokens.length < 2) {
         continue;
       }
       final String device = tokens[2];
       if (!Strings.isNullOrEmpty(device) && !device.equalsIgnoreCase(linkLocalBr)) {
         Script.runSimpleBashScript(
             "ip route del " + NetUtils.getLinkLocalCIDR() + " dev " + tokens[2]);
       } else {
         foundLinkLocalBr = true;
       }
     }
   }
   if (!foundLinkLocalBr) {
     Script.runSimpleBashScript(
         "ip address add 169.254.0.1/16 dev "
             + linkLocalBr
             + ";"
             + "ip route add "
             + NetUtils.getLinkLocalCIDR()
             + " dev "
             + linkLocalBr
             + " src "
             + NetUtils.getLinkLocalGateway());
   }
 }
  private boolean checkAndStartApache() {

    // Check whether the Apache server is running
    Script command = new Script("/bin/bash", s_logger);
    command.add("-c");
    command.add(
        "if [ -d /etc/apache2 ] ; then service apache2 status | grep pid; else service httpd status | grep pid; fi ");
    String result = command.execute();

    // Apache Server is not running. Try to start it.
    if (result != null) {

      s_logger.warn("Apache server not running, trying to start it");
      String port = Integer.toString(TemplateConstants.DEFAULT_TMPLT_COPY_PORT);
      String intf = TemplateConstants.DEFAULT_TMPLT_COPY_INTF;

      command = new Script("/bin/bash", s_logger);
      command.add("-c");
      command.add(
          "iptables -D INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + port
              + " -j DROP;"
              + "iptables -D INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + port
              + " -j HTTP;"
              + "iptables -D INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + "443"
              + " -j DROP;"
              + "iptables -D INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + "443"
              + " -j HTTP;"
              + "iptables -F HTTP;"
              + "iptables -X HTTP;"
              + "iptables -N HTTP;"
              + "iptables -I INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + port
              + " -j DROP;"
              + "iptables -I INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + "443"
              + " -j DROP;"
              + "iptables -I INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + port
              + " -j HTTP;"
              + "iptables -I INPUT -i "
              + intf
              + " -p tcp -m state --state NEW -m tcp --dport "
              + "443"
              + " -j HTTP;");

      result = command.execute();
      if (result != null) {
        s_logger.warn("Error in opening up httpd port err=" + result);
        return false;
      }

      command = new Script("/bin/bash", s_logger);
      command.add("-c");
      command.add(
          "if [ -d /etc/apache2 ] ; then service apache2 start; else service httpd start; fi ");
      result = command.execute();
      if (result != null) {
        s_logger.warn("Error in starting httpd service err=" + result);
        return false;
      }
    }

    return true;
  }
예제 #29
0
    @Override
    protected void runInContext() {
      synchronized (_storagePool) {
        Set<String> removedPools = new HashSet<String>();
        for (String uuid : _storagePool.keySet()) {
          NfsStoragePool primaryStoragePool = _storagePool.get(uuid);

          // check for any that have been deregistered with libvirt and
          // skip,remove them

          StoragePool storage = null;
          try {
            Connect conn = LibvirtConnection.getConnection();
            storage = conn.storagePoolLookupByUUIDString(uuid);
            if (storage == null) {
              s_logger.debug("Libvirt storage pool " + uuid + " not found, removing from HA list");
              removedPools.add(uuid);
              continue;

            } else if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
              s_logger.debug(
                  "Libvirt storage pool "
                      + uuid
                      + " found, but not running, removing from HA list");

              removedPools.add(uuid);
              continue;
            }
            s_logger.debug("Found NFS storage pool " + uuid + " in libvirt, continuing");

          } catch (LibvirtException e) {
            s_logger.debug("Failed to lookup libvirt storage pool " + uuid + " due to: " + e);

            // we only want to remove pool if it's not found, not if libvirt
            // connection fails
            if (e.toString().contains("pool not found")) {
              s_logger.debug("removing pool from HA monitor since it was deleted");
              removedPools.add(uuid);
              continue;
            }
          }

          String result = null;
          for (int i = 0; i < 5; i++) {
            Script cmd = new Script(s_heartBeatPath, _heartBeatUpdateTimeout, s_logger);
            cmd.add("-i", primaryStoragePool._poolIp);
            cmd.add("-p", primaryStoragePool._poolMountSourcePath);
            cmd.add("-m", primaryStoragePool._mountDestPath);
            cmd.add("-h", _hostIP);
            result = cmd.execute();
            if (result != null) {
              s_logger.warn("write heartbeat failed: " + result + ", retry: " + i);
            } else {
              break;
            }
          }

          if (result != null) {
            s_logger.warn("write heartbeat failed: " + result + "; reboot the host");
            Script cmd = new Script(s_heartBeatPath, _heartBeatUpdateTimeout, s_logger);
            cmd.add("-i", primaryStoragePool._poolIp);
            cmd.add("-p", primaryStoragePool._poolMountSourcePath);
            cmd.add("-m", primaryStoragePool._mountDestPath);
            cmd.add("-c");
            result = cmd.execute();
          }
        }

        if (!removedPools.isEmpty()) {
          for (String uuid : removedPools) {
            removeStoragePool(uuid);
          }
        }
      }
    }
  @Override
  public Answer execute(
      final BackupSnapshotCommand command,
      final LibvirtComputingResource libvirtComputingResource) {
    final Long dcId = command.getDataCenterId();
    final Long accountId = command.getAccountId();
    final Long volumeId = command.getVolumeId();
    final String secondaryStoragePoolUrl = command.getSecondaryStorageUrl();
    final String snapshotName = command.getSnapshotName();
    String snapshotDestPath = null;
    String snapshotRelPath = null;
    final String vmName = command.getVmName();
    KVMStoragePool secondaryStoragePool = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();

    try {
      final LibvirtUtilitiesHelper libvirtUtilitiesHelper =
          libvirtComputingResource.getLibvirtUtilitiesHelper();
      final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);

      secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);

      final String ssPmountPath = secondaryStoragePool.getLocalPath();
      snapshotRelPath =
          File.separator
              + "snapshots"
              + File.separator
              + dcId
              + File.separator
              + accountId
              + File.separator
              + volumeId;

      snapshotDestPath =
          ssPmountPath
              + File.separator
              + "snapshots"
              + File.separator
              + dcId
              + File.separator
              + accountId
              + File.separator
              + volumeId;
      final KVMStoragePool primaryPool =
          storagePoolMgr.getStoragePool(
              command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
      final KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(command.getVolumePath());

      final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath();
      final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();

      /**
       * RBD snapshots can't be copied using qemu-img, so we have to use the Java bindings for
       * librbd here.
       *
       * <p>These bindings will read the snapshot and write the contents to the secondary storage
       * directly
       *
       * <p>It will stop doing so if the amount of time spend is longer then cmds.timeout
       */
      if (primaryPool.getType() == StoragePoolType.RBD) {
        try {
          final Rados r = new Rados(primaryPool.getAuthUserName());
          r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
          r.confSet("key", primaryPool.getAuthSecret());
          r.confSet("client_mount_timeout", "30");
          r.connect();
          s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));

          final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir());
          final Rbd rbd = new Rbd(io);
          final RbdImage image = rbd.open(snapshotDisk.getName(), snapshotName);
          final File fh = new File(snapshotDestPath);
          try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fh)); ) {
            final int chunkSize = 4194304;
            long offset = 0;
            s_logger.debug(
                "Backuping up RBD snapshot " + snapshotName + " to  " + snapshotDestPath);
            while (true) {
              final byte[] buf = new byte[chunkSize];
              final int bytes = image.read(offset, buf, chunkSize);
              if (bytes <= 0) {
                break;
              }
              bos.write(buf, 0, bytes);
              offset += bytes;
            }
            s_logger.debug(
                "Completed backing up RBD snapshot "
                    + snapshotName
                    + " to  "
                    + snapshotDestPath
                    + ". Bytes written: "
                    + offset);
          } catch (final IOException ex) {
            s_logger.error("BackupSnapshotAnswer:Exception:" + ex.getMessage());
          }
          r.ioCtxDestroy(io);
        } catch (final RadosException e) {
          s_logger.error("A RADOS operation failed. The error was: " + e.getMessage());
          return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
        } catch (final RbdException e) {
          s_logger.error(
              "A RBD operation on "
                  + snapshotDisk.getName()
                  + " failed. The error was: "
                  + e.getMessage());
          return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
        }
      } else {
        final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
        scriptCommand.add("-b", snapshotDisk.getPath());
        scriptCommand.add("-n", snapshotName);
        scriptCommand.add("-p", snapshotDestPath);
        scriptCommand.add("-t", snapshotName);
        final String result = scriptCommand.execute();

        if (result != null) {
          s_logger.debug("Failed to backup snaptshot: " + result);
          return new BackupSnapshotAnswer(command, false, result, null, true);
        }
      }
      /* Delete the snapshot on primary */

      DomainState state = null;
      Domain vm = null;
      if (vmName != null) {
        try {
          vm = libvirtComputingResource.getDomain(conn, command.getVmName());
          state = vm.getInfo().state;
        } catch (final LibvirtException e) {
          s_logger.trace("Ignoring libvirt error.", e);
        }
      }

      final KVMStoragePool primaryStorage =
          storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPool().getUuid());

      if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
        final MessageFormat snapshotXML =
            new MessageFormat(
                "   <domainsnapshot>"
                    + "       <name>{0}</name>"
                    + "          <domain>"
                    + "            <uuid>{1}</uuid>"
                    + "        </domain>"
                    + "    </domainsnapshot>");

        final String vmUuid = vm.getUUIDString();
        final Object[] args = new Object[] {snapshotName, vmUuid};
        final String snapshot = snapshotXML.format(args);
        s_logger.debug(snapshot);
        final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
        if (snap != null) {
          snap.delete(0);
        } else {
          throw new CloudRuntimeException("Unable to find vm snapshot with name -" + snapshotName);
        }

        /*
         * libvirt on RHEL6 doesn't handle resume event emitted from
         * qemu
         */
        vm = libvirtComputingResource.getDomain(conn, command.getVmName());
        state = vm.getInfo().state;
        if (state == DomainState.VIR_DOMAIN_PAUSED) {
          vm.resume();
        }
      } else {
        final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
        scriptCommand.add("-d", snapshotDisk.getPath());
        scriptCommand.add("-n", snapshotName);
        final String result = scriptCommand.execute();
        if (result != null) {
          s_logger.debug("Failed to backup snapshot: " + result);
          return new BackupSnapshotAnswer(
              command, false, "Failed to backup snapshot: " + result, null, true);
        }
      }
    } catch (final LibvirtException e) {
      return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
    } catch (final CloudRuntimeException e) {
      return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
    } finally {
      if (secondaryStoragePool != null) {
        storagePoolMgr.deleteStoragePool(
            secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
      }
    }
    return new BackupSnapshotAnswer(
        command, true, null, snapshotRelPath + File.separator + snapshotName, true);
  }