Search in sources :

Example 71 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class DatabaseConfig method saveUser.

@DB
protected void saveUser() {
    // insert system account
    final String insertSystemAccount = "INSERT INTO `cloud`.`account` (id, account_name, type, domain_id) VALUES (1, 'system', '1', '1')";
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSystemAccount);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        s_logger.error("error creating system account", ex);
    }
    // insert system user
    final String insertSystemUser = "INSERT INTO `cloud`.`user` (id, username, password, account_id, firstname, lastname, created)" + " VALUES (1, 'system', RAND(), 1, 'system', 'cloud', now())";
    txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSystemUser);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        s_logger.error("error creating system user", ex);
    }
    // insert admin user
    long id = Long.parseLong(_currentObjectParams.get("id"));
    String username = _currentObjectParams.get("username");
    String firstname = _currentObjectParams.get("firstname");
    String lastname = _currentObjectParams.get("lastname");
    String password = _currentObjectParams.get("password");
    String email = _currentObjectParams.get("email");
    if (email == null || email.equals("")) {
        printError("An email address for each user is required.");
    }
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        s_logger.error("error saving user", e);
        return;
    }
    md5.reset();
    BigInteger pwInt = new BigInteger(1, md5.digest(password.getBytes()));
    String pwStr = pwInt.toString(16);
    int padding = 32 - pwStr.length();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < padding; i++) {
        // make sure the MD5 password is 32 digits long
        sb.append('0');
    }
    sb.append(pwStr);
    // create an account for the admin user first
    final String insertAdminAccount = "INSERT INTO `cloud`.`account` (id, account_name, type, domain_id) VALUES (?, ?, '1', '1')";
    txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(insertAdminAccount);
        stmt.setLong(1, id);
        stmt.setString(2, username);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        s_logger.error("error creating account", ex);
    }
    // now insert the user
    final String insertUser = "INSERT INTO `cloud`.`user` (id, username, password, account_id, firstname, lastname, email, created) " + "VALUES (?,?,?, 2, ?,?,?,now())";
    txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(insertUser);
        stmt.setLong(1, id);
        stmt.setString(2, username);
        stmt.setString(3, sb.toString());
        stmt.setString(4, firstname);
        stmt.setString(5, lastname);
        stmt.setString(6, email);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        s_logger.error("error creating user", ex);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) BigInteger(java.math.BigInteger) PreparedStatement(java.sql.PreparedStatement) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) DB(com.cloud.utils.db.DB)

Example 72 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class DatabaseConfig method saveVirtualRouterProvider.

private void saveVirtualRouterProvider() {
    long id = Long.parseLong(_currentObjectParams.get("id"));
    long nspId = Long.parseLong(_currentObjectParams.get("nspId"));
    String uuid = UUID.randomUUID().toString();
    String type = _currentObjectParams.get("type");
    String insertSql1 = "INSERT INTO `virtual_router_providers` (`id`, `nsp_id`, `uuid` , `type` , `enabled`) " + "VALUES (?,?,?,?,?)";
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql1);
        stmt.setLong(1, id);
        stmt.setLong(2, nspId);
        stmt.setString(3, uuid);
        stmt.setString(4, type);
        stmt.setInt(5, 1);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        System.out.println("Error creating virtual router provider: " + ex.getMessage());
        s_logger.error("error creating virtual router provider ", ex);
        return;
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 73 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class DatabaseConfig method saveThrottlingRates.

@DB
protected void saveThrottlingRates() {
    boolean saveNetworkThrottlingRate = (_networkThrottlingRate != null);
    boolean saveMulticastThrottlingRate = (_multicastThrottlingRate != null);
    if (!saveNetworkThrottlingRate && !saveMulticastThrottlingRate) {
        return;
    }
    String insertNWRateSql = "UPDATE `cloud`.`service_offering` SET `nw_rate` = ?";
    String insertMCRateSql = "UPDATE `cloud`.`service_offering` SET `mc_rate` = ?";
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt;
        if (saveNetworkThrottlingRate) {
            stmt = txn.prepareAutoCloseStatement(insertNWRateSql);
            stmt.setString(1, _networkThrottlingRate);
            stmt.executeUpdate();
        }
        if (saveMulticastThrottlingRate) {
            stmt = txn.prepareAutoCloseStatement(insertMCRateSql);
            stmt.setString(1, _multicastThrottlingRate);
            stmt.executeUpdate();
        }
    } catch (SQLException ex) {
        s_logger.error("error saving network and multicast throttling rates to all service offerings", ex);
        return;
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DB(com.cloud.utils.db.DB)

Example 74 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class DatabaseConfig method saveSQL.

public static void saveSQL(String sql, String errorMsg) {
    TransactionLegacy txn = TransactionLegacy.open("saveSQL");
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(sql);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        System.out.println("SQL Exception: " + ex.getMessage());
        printError(errorMsg);
    } finally {
        txn.close();
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 75 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class DatabaseConfig method saveConfiguration.

@DB
protected void saveConfiguration(String name, String value, String category) {
    String instance = "DEFAULT";
    String description = s_configurationDescriptions.get(name);
    String component = s_configurationComponents.get(name);
    if (category == null) {
        category = "Advanced";
    }
    String instanceNameError = "Please enter a non-blank value for the field: ";
    if (name.equals("instance.name")) {
        if (value == null || value.isEmpty() || !value.matches("^[A-Za-z0-9]{1,8}$")) {
            printError(instanceNameError + "configuration: instance.name can not be empty and can only contain numbers and alphabets up to 8 characters long");
        }
    }
    if (name.equals("network.throttling.rate")) {
        if (value != null && !value.isEmpty()) {
            _networkThrottlingRate = value;
        }
    }
    if (name.equals("multicast.throttling.rate")) {
        if (value != null && !value.isEmpty()) {
            _multicastThrottlingRate = value;
        }
    }
    String insertSql = "INSERT INTO `cloud`.`configuration` (instance, component, name, value, description, category) " + "VALUES (?,?,?,?,?,?)";
    String selectSql = "SELECT name FROM cloud.configuration WHERE name = ?";
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
        stmt.setString(1, name);
        ResultSet result = stmt.executeQuery();
        Boolean hasRow = result.next();
        if (!hasRow) {
            stmt = txn.prepareAutoCloseStatement(insertSql);
            stmt.setString(1, instance);
            stmt.setString(2, component);
            stmt.setString(3, name);
            stmt.setString(4, value);
            stmt.setString(5, description);
            stmt.setString(6, category);
            stmt.executeUpdate();
        }
    } catch (SQLException ex) {
        s_logger.error("error creating configuration", ex);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DB(com.cloud.utils.db.DB)

Aggregations

TransactionLegacy (com.cloud.utils.db.TransactionLegacy)368 PreparedStatement (java.sql.PreparedStatement)174 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)149 SQLException (java.sql.SQLException)133 ResultSet (java.sql.ResultSet)102 ArrayList (java.util.ArrayList)98 DB (com.cloud.utils.db.DB)95 ConfigurationException (javax.naming.ConfigurationException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)35 Date (java.util.Date)34 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 HashMap (java.util.HashMap)29 URISyntaxException (java.net.URISyntaxException)28 AccountVO (com.cloud.user.AccountVO)21 CloudException (com.cloud.exception.CloudException)20 Account (com.cloud.user.Account)20 Field (java.lang.reflect.Field)19 MockVolumeVO (com.cloud.simulator.MockVolumeVO)18 AgentManager (com.cloud.agent.AgentManager)13 IPAddressDao (com.cloud.network.dao.IPAddressDao)13