Search in sources :

Example 56 with ConfigurationException

use of javax.naming.ConfigurationException in project cloudstack by apache.

the class ProcessUtil method pidCheck.

// paths cannot be hardcoded
public static void pidCheck(String pidDir, String run) throws ConfigurationException {
    String dir = pidDir == null ? "/var/run" : pidDir;
    try {
        final File propsFile = PropertiesUtil.findConfigFile("environment.properties");
        if (propsFile == null) {
            s_logger.debug("environment.properties could not be opened");
        } else {
            final Properties props = PropertiesUtil.loadFromFile(propsFile);
            dir = props.getProperty("paths.pid");
            if (dir == null) {
                dir = pidDir == null ? "/var/run" : pidDir;
            }
        }
    } catch (IOException e) {
        s_logger.debug("environment.properties could not be opened");
    }
    final File pidFile = new File(dir + File.separator + run);
    try {
        if (!pidFile.createNewFile()) {
            if (!pidFile.exists()) {
                throw new ConfigurationException("Unable to write to " + pidFile.getAbsolutePath() + ".  Are you sure you're running as root?");
            }
            final String pidLine = FileUtils.readFileToString(pidFile).trim();
            if (pidLine.isEmpty()) {
                throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
            }
            try {
                final long pid = Long.parseLong(pidLine);
                final Script script = new Script("bash", 120000, s_logger);
                script.add("-c", "ps -p " + pid);
                final String result = script.execute();
                if (result == null) {
                    throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                }
                if (!pidFile.delete()) {
                    throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                }
                if (!pidFile.createNewFile()) {
                    throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                }
            } catch (final NumberFormatException e) {
                throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
            }
        }
        pidFile.deleteOnExit();
        final Script script = new Script("bash", 120000, s_logger);
        script.add("-c", "echo $PPID");
        final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
        script.execute(parser);
        final String pid = parser.getLine();
        FileUtils.writeStringToFile(pidFile, pid + "\n");
    } catch (final IOException e) {
        throw new CloudRuntimeException("Unable to create the " + pidFile.getAbsolutePath() + ".  Are you running as root?", e);
    }
}
Also used : Script(com.cloud.utils.script.Script) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) Properties(java.util.Properties) OutputInterpreter(com.cloud.utils.script.OutputInterpreter) File(java.io.File)

Example 57 with ConfigurationException

use of javax.naming.ConfigurationException in project cloudstack by apache.

the class RemoteAccessVpnManagerImplTest method validateHandleExceptionOnValidateIpRangeErrorWhenConfigurationExceptionThrowConfigurationException.

@Test
public void validateHandleExceptionOnValidateIpRangeErrorWhenConfigurationExceptionThrowConfigurationException() {
    Class<ConfigurationException> exception = ConfigurationException.class;
    String expectedMessage = "Test";
    ConfigurationException assertThrows = Assert.assertThrows(expectedMessage, exception, () -> {
        new RemoteAccessVpnManagerImpl().handleExceptionOnValidateIpRangeError(exception, expectedMessage);
    });
    assertEquals(expectedMessage, assertThrows.getMessage());
}
Also used : ConfigurationException(javax.naming.ConfigurationException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 58 with ConfigurationException

use of javax.naming.ConfigurationException in project cloudstack by apache.

the class LocalSecondaryStorageResource method configure.

@Override
@SuppressWarnings("unchecked")
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    super.configure(name, params);
    _guid = (String) params.get("guid");
    if (_guid == null) {
        throw new ConfigurationException("Unable to find the guid");
    }
    _dc = (String) params.get("zone");
    if (_dc == null) {
        throw new ConfigurationException("Unable to find the zone");
    }
    _pod = (String) params.get("pod");
    _instance = (String) params.get("instance");
    _parent = (String) params.get("mount.path");
    if (_parent == null) {
        throw new ConfigurationException("No directory specified.");
    }
    _storage = (StorageLayer) params.get(StorageLayer.InstanceConfigKey);
    if (_storage == null) {
        String value = (String) params.get(StorageLayer.ClassConfigKey);
        if (value == null) {
            value = "com.cloud.storage.JavaStorageLayer";
        }
        try {
            Class<StorageLayer> clazz = (Class<StorageLayer>) Class.forName(value);
            _storage = ComponentContext.inject(clazz);
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException("Unable to find class " + value);
        }
    }
    if (!_storage.mkdirs(_parent)) {
        s_logger.warn("Unable to create the directory " + _parent);
        throw new ConfigurationException("Unable to create the directory " + _parent);
    }
    s_logger.info("Mount point established at " + _parent);
    params.put("template.parent", _parent);
    params.put(StorageLayer.InstanceConfigKey, _storage);
    _dlMgr = new DownloadManagerImpl();
    _dlMgr.configure("DownloadManager", params);
    return true;
}
Also used : DownloadManagerImpl(org.apache.cloudstack.storage.template.DownloadManagerImpl) StorageLayer(com.cloud.storage.StorageLayer) ConfigurationException(javax.naming.ConfigurationException)

Example 59 with ConfigurationException

use of javax.naming.ConfigurationException in project cloudstack by apache.

the class SecondaryStorageDiscoverer method createNfsSecondaryStorageResource.

protected Map<? extends ServerResource, Map<String, String>> createNfsSecondaryStorageResource(long dcId, Long podId, URI uri) {
    if (_useServiceVM) {
        return createDummySecondaryStorageResource(dcId, podId, uri);
    }
    String mountStr = NfsUtils.uri2Mount(uri);
    Script script = new Script(true, "mount", _timeout, s_logger);
    String mntPoint = null;
    File file = null;
    do {
        mntPoint = _mountParent + File.separator + Integer.toHexString(_random.nextInt(Integer.MAX_VALUE));
        file = new File(mntPoint);
    } while (file.exists());
    if (!file.mkdirs()) {
        s_logger.warn("Unable to make directory: " + mntPoint);
        return null;
    }
    script.add(mountStr, mntPoint);
    String result = script.execute();
    if (result != null && !result.contains("already mounted")) {
        s_logger.warn("Unable to mount " + uri.toString() + " due to " + result);
        file.delete();
        return null;
    }
    script = new Script(true, "umount", 0, s_logger);
    script.add(mntPoint);
    script.execute();
    file.delete();
    Map<NfsSecondaryStorageResource, Map<String, String>> srs = new HashMap<NfsSecondaryStorageResource, Map<String, String>>();
    NfsSecondaryStorageResource storage;
    if (_configDao.isPremium()) {
        Class<?> impl;
        String name = "com.cloud.storage.resource.PremiumSecondaryStorageResource";
        try {
            impl = Class.forName(name);
            final Constructor<?> constructor = impl.getDeclaredConstructor();
            constructor.setAccessible(true);
            storage = (NfsSecondaryStorageResource) constructor.newInstance();
        } catch (final ClassNotFoundException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to ClassNotFoundException");
            return null;
        } catch (final SecurityException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to SecurityException");
            return null;
        } catch (final NoSuchMethodException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to NoSuchMethodException");
            return null;
        } catch (final IllegalArgumentException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to IllegalArgumentException");
            return null;
        } catch (final InstantiationException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to InstantiationException");
            return null;
        } catch (final IllegalAccessException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to IllegalAccessException");
            return null;
        } catch (final InvocationTargetException e) {
            s_logger.error("Unable to load com.cloud.storage.resource.PremiumSecondaryStorageResource due to InvocationTargetException");
            return null;
        }
    } else {
        storage = new NfsSecondaryStorageResource();
    }
    Map<String, String> details = new HashMap<String, String>();
    details.put("mount.path", mountStr);
    details.put("orig.url", uri.toString());
    details.put("mount.parent", _mountParent);
    Map<String, Object> params = new HashMap<String, Object>();
    params.putAll(details);
    params.put("zone", Long.toString(dcId));
    if (podId != null) {
        params.put("pod", podId.toString());
    }
    params.put("guid", uri.toString());
    params.put("secondary.storage.vm", "false");
    params.put("max.template.iso.size", _configDao.getValue("max.template.iso.size"));
    try {
        storage.configure("Storage", params);
    } catch (ConfigurationException e) {
        s_logger.warn("Unable to configure the storage ", e);
        return null;
    }
    srs.put(storage, details);
    return srs;
}
Also used : Script(com.cloud.utils.script.Script) HashMap(java.util.HashMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationException(javax.naming.ConfigurationException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with ConfigurationException

use of javax.naming.ConfigurationException in project cloudstack by apache.

the class DownloadManagerImpl method configure.

@Override
@SuppressWarnings("unchecked")
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    _name = name;
    String value = null;
    _storage = (StorageLayer) params.get(StorageLayer.InstanceConfigKey);
    if (_storage == null) {
        value = (String) params.get(StorageLayer.ClassConfigKey);
        if (value == null) {
            throw new ConfigurationException("Unable to find the storage layer");
        }
        Class<StorageLayer> clazz;
        try {
            clazz = (Class<StorageLayer>) Class.forName(value);
            _storage = clazz.newInstance();
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException("Unable to instantiate " + value);
        } catch (InstantiationException e) {
            throw new ConfigurationException("Unable to instantiate " + value);
        } catch (IllegalAccessException e) {
            throw new ConfigurationException("Unable to instantiate " + value);
        }
    }
    String inSystemVM = (String) params.get("secondary.storage.vm");
    if (inSystemVM != null && "true".equalsIgnoreCase(inSystemVM)) {
        LOGGER.info("DownloadManager: starting additional services since we are inside system vm");
        _nfsVersion = NfsSecondaryStorageResource.retrieveNfsVersionFromParams(params);
        startAdditionalServices();
        blockOutgoingOnPrivate();
    }
    value = (String) params.get("install.timeout.pergig");
    installTimeoutPerGig = NumbersUtil.parseInt(value, 15 * 60) * 1000;
    value = (String) params.get("install.numthreads");
    final int numInstallThreads = NumbersUtil.parseInt(value, 10);
    String scriptsDir = (String) params.get("template.scripts.dir");
    if (scriptsDir == null) {
        scriptsDir = "scripts/storage/secondary";
    }
    listTmpltScr = Script.findScript(scriptsDir, "listvmtmplt.sh");
    if (listTmpltScr == null) {
        throw new ConfigurationException("Unable to find the listvmtmplt.sh");
    }
    LOGGER.info("listvmtmplt.sh found in " + listTmpltScr);
    createTmpltScr = Script.findScript(scriptsDir, "createtmplt.sh");
    if (createTmpltScr == null) {
        throw new ConfigurationException("Unable to find createtmplt.sh");
    }
    LOGGER.info("createtmplt.sh found in " + createTmpltScr);
    listVolScr = Script.findScript(scriptsDir, "listvolume.sh");
    if (listVolScr == null) {
        throw new ConfigurationException("Unable to find the listvolume.sh");
    }
    LOGGER.info("listvolume.sh found in " + listVolScr);
    createVolScr = Script.findScript(scriptsDir, "createvolume.sh");
    if (createVolScr == null) {
        throw new ConfigurationException("Unable to find createvolume.sh");
    }
    LOGGER.info("createvolume.sh found in " + createVolScr);
    _processors = new HashMap<String, Processor>();
    Processor processor = new VhdProcessor();
    processor.configure("VHD Processor", params);
    _processors.put("VHD Processor", processor);
    processor = new IsoProcessor();
    processor.configure("ISO Processor", params);
    _processors.put("ISO Processor", processor);
    processor = new QCOW2Processor();
    processor.configure("QCOW2 Processor", params);
    _processors.put("QCOW2 Processor", processor);
    processor = new OVAProcessor();
    processor.configure("OVA Processor", params);
    _processors.put("OVA Processor", processor);
    processor = new VmdkProcessor();
    processor.configure("VMDK Processor", params);
    _processors.put("VMDK Processor", processor);
    processor = new RawImageProcessor();
    processor.configure("Raw Image Processor", params);
    _processors.put("Raw Image Processor", processor);
    processor = new TARProcessor();
    processor.configure("TAR Processor", params);
    _processors.put("TAR Processor", processor);
    _templateDir = (String) params.get("public.templates.root.dir");
    if (_templateDir == null) {
        _templateDir = TemplateConstants.DEFAULT_TMPLT_ROOT_DIR;
    }
    _templateDir += File.separator + TemplateConstants.DEFAULT_TMPLT_FIRST_LEVEL_DIR;
    _volumeDir = TemplateConstants.DEFAULT_VOLUME_ROOT_DIR + File.separator;
    // Add more processors here.
    threadPool = Executors.newFixedThreadPool(numInstallThreads);
    return true;
}
Also used : IsoProcessor(com.cloud.storage.template.IsoProcessor) StorageLayer(com.cloud.storage.StorageLayer) VhdProcessor(com.cloud.storage.template.VhdProcessor) OVAProcessor(com.cloud.storage.template.OVAProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) TARProcessor(com.cloud.storage.template.TARProcessor) IsoProcessor(com.cloud.storage.template.IsoProcessor) Processor(com.cloud.storage.template.Processor) VmdkProcessor(com.cloud.storage.template.VmdkProcessor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) OVAProcessor(com.cloud.storage.template.OVAProcessor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) ConfigurationException(javax.naming.ConfigurationException) VmdkProcessor(com.cloud.storage.template.VmdkProcessor) VhdProcessor(com.cloud.storage.template.VhdProcessor) TARProcessor(com.cloud.storage.template.TARProcessor)

Aggregations

ConfigurationException (javax.naming.ConfigurationException)168 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)57 IOException (java.io.IOException)44 HashMap (java.util.HashMap)39 File (java.io.File)23 Map (java.util.Map)21 InternalErrorException (com.cloud.exception.InternalErrorException)19 Properties (java.util.Properties)19 StorageLayer (com.cloud.storage.StorageLayer)18 Processor (com.cloud.storage.template.Processor)17 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 Answer (com.cloud.agent.api.Answer)16 ServerResource (com.cloud.resource.ServerResource)16 TemplateLocation (com.cloud.storage.template.TemplateLocation)16 Script (com.cloud.utils.script.Script)16 FormatInfo (com.cloud.storage.template.Processor.FormatInfo)14 FileNotFoundException (java.io.FileNotFoundException)14 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)13 Host (com.cloud.host.Host)11 TransactionStatus (com.cloud.utils.db.TransactionStatus)11