private static String getSCIqn() {
   try {
     Runtime rt = Runtime.getRuntime();
     Process proc =
         rt.exec(
             new String[] {StorageProperties.EUCA_ROOT_WRAPPER, "cat", ISCSI_INITIATOR_NAME_CONF});
     StreamConsumer error = new StreamConsumer(proc.getErrorStream());
     ConfigParser output = new ConfigParser(proc.getInputStream());
     error.start();
     output.start();
     output.join();
     error.join();
     if (output.getValues() != null && output.getValues().containsKey("InitiatorName")) {
       return output.getValues().get("InitiatorName");
     }
   } catch (Exception t) {
     LOG.error("Failed to get local SC's initiator iqn from " + ISCSI_INITIATOR_NAME_CONF, t);
   }
   return null;
 }
Beispiel #2
0
  private void getResponseToFile() {
    byte[] bytes = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOut = null;
    try {
      File outFile = null;
      File outFileUncompressed = null;
      if (compressed) {
        String outFileNameUncompressed =
            tempPath + File.pathSeparator + file.getName() + Hashes.getRandom(16);
        outFileUncompressed = new File(outFileNameUncompressed);
        outFile = new File(outFileNameUncompressed + ".gz");
      } else {
        outFile = file;
      }

      httpClient.executeMethod(method);

      // GZIPInputStream has a bug thats corrupting snapshot file system. Mounting the block device
      // failed with unknown file system error
      /*InputStream httpIn = null;
      if(compressed) {
      	httpIn = new GZIPInputStream(method.getResponseBodyAsStream());
      }
      else {
      	httpIn = method.getResponseBodyAsStream();
      }*/

      InputStream httpIn = method.getResponseBodyAsStream();
      int bytesRead;
      fileOutputStream = new FileOutputStream(outFile);
      // fileOutputStream = new FileOutputStream(file);
      bufferedOut = new BufferedOutputStream(fileOutputStream);
      while ((bytesRead = httpIn.read(bytes)) > 0) {
        bufferedOut.write(bytes, 0, bytesRead);
      }
      bufferedOut.close();

      if (compressed) {
        try {
          Runtime rt = Runtime.getRuntime();
          Process proc = rt.exec(new String[] {"/bin/gunzip", outFile.getAbsolutePath()});
          StreamConsumer error = new StreamConsumer(proc.getErrorStream());
          StreamConsumer output = new StreamConsumer(proc.getInputStream());
          error.start();
          output.start();
          output.join();
          error.join();
        } catch (Exception t) {
          LOG.error(t);
        }
        if ((outFileUncompressed != null) && (!outFileUncompressed.renameTo(file))) {
          LOG.error("Unable to uncompress: " + outFile.getAbsolutePath());
          return;
        }
      }
    } catch (Exception ex) {
      LOG.error(ex, ex);
    } finally {
      method.releaseConnection();
      if (bufferedOut != null) {
        try {
          bufferedOut.close();
        } catch (IOException e) {
          LOG.error(e);
        }
      }
      if (fileOutputStream != null) {
        try {
          fileOutputStream.close();
        } catch (IOException e) {
          LOG.error(e);
        }
      }
    }
  }