Skip to content

Commit eeedeb0

Browse files
committed
On successful upgrade, update old systemvm templates to USER type, so that they can be deleted
1 parent 40d9258 commit eeedeb0

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDao.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
// under the License.
1717
package com.cloud.storage.dao;
1818

19-
import java.util.List;
20-
import java.util.Map;
21-
2219
import com.cloud.hypervisor.Hypervisor.HypervisorType;
2320
import com.cloud.storage.Storage;
2421
import com.cloud.storage.VMTemplateVO;
2522
import com.cloud.template.VirtualMachineTemplate;
2623
import com.cloud.utils.db.GenericDao;
2724
import com.cloud.utils.fsm.StateDao;
2825

26+
import java.util.List;
27+
import java.util.Map;
28+
2929
/*
3030
* Data Access Object for vm_templates table
3131
*/
@@ -86,4 +86,6 @@ public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<
8686
List<VMTemplateVO> listByParentTemplatetId(long parentTemplatetId);
8787

8888
VMTemplateVO findLatestTemplateByName(String name);
89+
90+
void updateOldSystemVmTemplateType(HypervisorType hypervisorType);
8991
}

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDaoImpl.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@
1616
// under the License.
1717
package com.cloud.storage.dao;
1818

19-
import java.util.ArrayList;
20-
import java.util.Date;
21-
import java.util.List;
22-
import java.util.Map;
23-
24-
import javax.inject.Inject;
25-
import javax.naming.ConfigurationException;
26-
27-
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
28-
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
29-
import org.apache.log4j.Logger;
30-
import org.springframework.stereotype.Component;
31-
3219
import com.cloud.dc.dao.DataCenterDao;
3320
import com.cloud.domain.dao.DomainDao;
3421
import com.cloud.host.Host;
@@ -57,6 +44,19 @@
5744
import com.cloud.utils.db.TransactionLegacy;
5845
import com.cloud.utils.db.UpdateBuilder;
5946
import com.cloud.utils.exception.CloudRuntimeException;
47+
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
48+
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
49+
import org.apache.log4j.Logger;
50+
import org.springframework.stereotype.Component;
51+
52+
import javax.inject.Inject;
53+
import javax.naming.ConfigurationException;
54+
import java.sql.PreparedStatement;
55+
import java.sql.SQLException;
56+
import java.util.ArrayList;
57+
import java.util.Date;
58+
import java.util.List;
59+
import java.util.Map;
6060

6161
@Component
6262
public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implements VMTemplateDao {
@@ -105,6 +105,9 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
105105
private String routerTmpltName;
106106
private String consoleProxyTmpltName;
107107

108+
private static final String UPDATE_OLD_SYSTEMVM_TEMPLATE_TYPE = "UPDATE `cloud`.`vm_template` SET type = 'USER' WHERE name like '%systemvm%' AND type = 'SYSTEM' AND hypervisor_type = ? " +
109+
"AND id != (SELECT * FROM (SELECT MAX(id) FROM `cloud`.`vm_template` WHERE name like '%systemvm%' AND type = 'SYSTEM' AND hypervisor_type = ? ) as t)";
110+
108111
public VMTemplateDaoImpl() {
109112
super();
110113
LatestTemplateByHypervisorTypeSearch = createSearchBuilder();
@@ -717,4 +720,22 @@ public boolean updateState(
717720
}
718721
return rows > 0;
719722
}
723+
724+
@Override
725+
public void updateOldSystemVmTemplateType(HypervisorType hypervisorType) {
726+
TransactionLegacy txn = TransactionLegacy.currentTxn();
727+
728+
StringBuilder sql = new StringBuilder(UPDATE_OLD_SYSTEMVM_TEMPLATE_TYPE);
729+
try {
730+
PreparedStatement updateStatement = txn.prepareAutoCloseStatement(sql.toString());
731+
updateStatement.setString(1, hypervisorType.name());
732+
updateStatement.setString(2, hypervisorType.name());
733+
updateStatement.executeUpdate();
734+
} catch (SQLException e) {
735+
throw new CloudRuntimeException("DB Exception on: " + sql, e);
736+
} catch (Throwable e) {
737+
throw new CloudRuntimeException("Caught: " + sql, e);
738+
}
739+
740+
}
720741
}

engine/schema/src/main/java/com/cloud/upgrade/SystemVmTemplateRegistration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,18 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
879879
}
880880
}
881881
LOGGER.debug("Updating System Vm Template IDs Complete");
882+
updateOldSystemVMTemplates();
882883
}
883884
});
884885
}
886+
887+
public void updateOldSystemVMTemplates() {
888+
for (Hypervisor.HypervisorType hypervisorType : hypervisorList) {
889+
try {
890+
vmTemplateDao.updateOldSystemVmTemplateType(hypervisorType);
891+
} catch (Exception e) {
892+
LOGGER.error(String.format("Failed to update systemvm template type due to: %s", e.getLocalizedMessage()));
893+
}
894+
}
895+
}
885896
}

0 commit comments

Comments
 (0)