Jake Armstrong
ServiceNow Employee
ServiceNow Employee

I was presented with a logistical problem earlier today for someone trying to migrate from CMPv1 to CMPv2, regarding the effort necessary to manually migrate all their existing image templates to the new format for CMPv2. With that in mind I was able to write up a script that can be run in a Scheduled Job or in the background to do the conversions (with two caveats, see notes). 

This script is written in the context of AWS images, but could be modified slightly as needed to convert CMPv1 Azure records. 

How this script converts EC2 images to OS profiles:

  • Iterates through all ec2_image records
  • Takes relevant data from ec2_image and uses it to populate a new cmdb_ci_os_template record
  • Creates new sn_cmp_os_profile records if necessary, named according to the ec2_image OS type (windows, linux)
  • Creates new sn_cmp_os_profile_mapping records to link the new image template records to the OS profiles (getting the region and cloud account from the various ec2_image fields)

 

Things to consider (aka caveats)

  1. With this method there won't be any specificity in the OS profile names, they will be generic (e.g: windows, linux). This is because there isn't a consistent way to tell what kind of OS the ec2_image AMIs are referencing.
  2. This will not attach any Basic Auth credentials to the OS Profiles, as there are not any analogous ones to map from the existing ec2_image records. Basic Auth credentials will need to be added separately.

As always, test on subprod first with a smaller subset of records.

The script for the conversion:

var ec2 = new GlideRecord("ec2_image");
ec2.query();

while (ec2.next()) {
	var cmp = new GlideRecord("cmdb_ci_os_template");
	cmp.initialize();
	cmp.name = ec2.name;
	cmp.object_id = ec2.image_id;
	cmp.guest_os = ec2.platform;
	cmp.root_device_type = ec2.root_device_type;
	cmp.image_source = ec2.image_location;
	cmp.image_type = ec2.image_type;
	var cmpSysId = cmp.insert();

	var osProfile = new GlideRecord("sn_cmp_os_profile");
	if (!osProfile.get("name",ec2.platform)) {
		osProfile.initialize();
		osProfile.name = ec2.platform;
		osProfile.insert();
	}
	osProfile.get("name",ec2.platform);

	var awsRegion = new GlideRecord("aws_region");
	awsRegion.get(ec2.region);
	var ldcRegion = new GlideRecord("cmdb_ci_logical_datacenter");
	ldcRegion.get("region",awsRegion.region);

	var ca = new GlideRecord("cmdb_ci_cloud_service_account");
	ca.get("account_id",ec2.account_id);

	var map = new GlideRecord("sn_cmp_os_profile_mapping");
	map.initialize();
	map.profile = osProfile.getUniqueValue();
	map.template = cmpSysId;
	map.cloud_account = ca.getUniqueValue();
	map.location = ldcRegion.getUniqueValue();
	map.insert();
}

Related Information:

Add an AWS public image to Cloud Management

Version history
Last update:
‎08-24-2018 03:02 PM
Updated by: