Search in sources :

Example 1 with Script

use of com.cloud.utils.script.Script in project CloudStack-archive by CloudStack-extras.

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 FileInputStream finputstream = new FileInputStream(propsFile);
            final Properties props = new Properties();
            props.load(finputstream);
            finputstream.close();
            dir = props.getProperty("paths.pid");
            if (dir == null) {
                dir = "/var/run";
            }
        }
    } 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 FileInputStream is = new FileInputStream(pidFile);
            final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            final String pidLine = reader.readLine();
            if (pidLine == null) {
                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();
        final FileOutputStream strm = new FileOutputStream(pidFile);
        strm.write((pid + "\n").getBytes());
        strm.close();
    } 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) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputInterpreter(com.cloud.utils.script.OutputInterpreter) File(java.io.File)

Example 2 with Script

use of com.cloud.utils.script.Script in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method executeBashScript.

private String executeBashScript(String script) {
    Script command = new Script("/bin/bash", _timeout, s_logger);
    command.add("-c");
    command.add(script);
    return command.execute();
}
Also used : Script(com.cloud.utils.script.Script)

Example 3 with Script

use of com.cloud.utils.script.Script in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
    String secondaryStorageURL = cmd.getSecondaryStorageUrl();
    KVMStoragePool secondaryStorage = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        String templateFolder = cmd.getAccountId() + File.separator + cmd.getTemplateId() + File.separator;
        String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = _storagePoolMgr.getStoragePoolByURI(secondaryStorageURL);
        KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
        KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
        String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        _storage.mkdirs(tmpltPath);
        Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
        command.add("-f", disk.getPath());
        command.add("-t", tmpltPath);
        command.add("-n", cmd.getUniqueName() + ".qcow2");
        String result = command.execute();
        if (result != null) {
            s_logger.debug("failed to create template: " + result);
            return new CreatePrivateTemplateAnswer(cmd, false, result);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(tmpltPath, null, cmd.getUniqueName());
        TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
        loc.create(1, true, cmd.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(cmd, true, null, templateInstallFolder + cmd.getUniqueName() + ".qcow2", info.virtualSize, info.size, cmd.getUniqueName(), ImageFormat.QCOW2);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get secondary storage pool: " + e.toString());
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (IOException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 4 with Script

use of com.cloud.utils.script.Script in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getVersionStrings.

private Map<String, String> getVersionStrings() {
    final Script command = new Script(_versionstringpath, _timeout, s_logger);
    KeyValueInterpreter kvi = new KeyValueInterpreter();
    String result = command.execute(kvi);
    if (result == null) {
        return kvi.getKeyValues();
    } else {
        return new HashMap<String, String>(1);
    }
}
Also used : Script(com.cloud.utils.script.Script) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with Script

use of com.cloud.utils.script.Script in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected Answer execute(DeleteSnapshotsDirCommand cmd) {
    Long dcId = cmd.getDcId();
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    KVMStoragePool secondaryStoragePool = null;
    try {
        secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl());
        String ssPmountPath = secondaryStoragePool.getLocalPath();
        String snapshotDestPath = ssPmountPath + File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
        final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
        command.add("-d", snapshotDestPath);
        command.add("-f");
        command.execute();
    } catch (CloudRuntimeException e) {
        return new Answer(cmd, false, e.toString());
    } finally {
        if (secondaryStoragePool != null) {
            secondaryStoragePool.delete();
        }
    }
    return new Answer(cmd, true, null);
}
Also used : Script(com.cloud.utils.script.Script) FenceAnswer(com.cloud.agent.api.FenceAnswer) ConsoleProxyLoadAnswer(com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer) DeleteSnapshotBackupAnswer(com.cloud.agent.api.DeleteSnapshotBackupAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) AttachVolumeAnswer(com.cloud.agent.api.AttachVolumeAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) CheckNetworkAnswer(com.cloud.agent.api.CheckNetworkAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

Script (com.cloud.utils.script.Script)308 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)92 File (java.io.File)72 OutputInterpreter (com.cloud.utils.script.OutputInterpreter)54 IOException (java.io.IOException)42 InternalErrorException (com.cloud.exception.InternalErrorException)35 ConfigurationException (javax.naming.ConfigurationException)30 HashMap (java.util.HashMap)29 Test (org.junit.Test)25 LibvirtException (org.libvirt.LibvirtException)24 Answer (com.cloud.agent.api.Answer)23 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)16 RemoteException (java.rmi.RemoteException)16 FileNotFoundException (java.io.FileNotFoundException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)15 Connect (org.libvirt.Connect)15 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)14 FormatInfo (com.cloud.storage.template.Processor.FormatInfo)14 Processor (com.cloud.storage.template.Processor)13 TemplateLocation (com.cloud.storage.template.TemplateLocation)13