Name

sn_agent.AgentUpgradeAjax

Description

No description available

Script

var AgentUpgradeAjax = Class.create();
AgentUpgradeAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  upgradeRequirements: {
      "Linux Red Hat 7": {
          "CheckDef": "37c80fb153d181100112ddeeff7b122d",
          "MinAgentVersion": "2.7.0"
      },
      "Linux Red Hat 8": {
          "CheckDef": "37c80fb153d181100112ddeeff7b122d",
          "MinAgentVersion": "2.7.0"
      },
      "Linux CentOS 7": {
          "CheckDef": "37c80fb153d181100112ddeeff7b122d",
          "MinAgentVersion": "2.7.0"
      },
      "Linux CentOS 8": {
          "CheckDef": "37c80fb153d181100112ddeeff7b122d",
          "MinAgentVersion": "2.7.0"
      },
      "Linux SuSE 12": {
          "CheckDef": "37c80fb153d181100112ddeeff7b122d",
          "MinAgentVersion": "2.7.0"
      },
      "Linux SuSE 15": {
          "CheckDef": "37c80fb153d181100112ddeeff7b122d",
          "MinAgentVersion": "2.7.0"
      },
      "GNU/Linux 9": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "GNU/Linux 10": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "GNU/Linux 11": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "Ubuntu 18": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "Ubuntu 20": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "Ubuntu 21": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "Ubuntu 22": {
          "CheckDef": "313c0cce775a0110a36408ed7f5a99ee",
          "MinAgentVersion": "2.7.0"
      },
      "Windows": {
          "CheckDef": "5f74fe90533d45100112ddeeff7b1229",
          "MinAgentVersion": "2.7.0"
      }
  },

  upgradeAgents: function(agentList) {
      var now = this.getLocalCurrentTime();
      if (agentList == undefined) { // in case called with Ajax
          var agentSysIdList = this.getParameter('sysparm_agentSysIdList');
          agentList = agentSysIdList.split(",");
      }

      var isList = this.getParameter('sysparm_isList');

      if (agentList.length == 0)
          return this.errorPrefix + "no agent was selected";

      /* The reason for the different when handling a form (non-list) is that we can use
      	gs.addInfoMessage() or gs.addErrorMessage() and the customer will understand
      	which agent it is refering to.
      	If multiple agents are used: we will redirect to the sn_agent_upgrade_history
      	and the user can consume mutliple upgrades in the table.
      */
      if (isList && isList.toUpperCase() == "FALSE")
          return JSON.stringify(this.upgradeSingleAgent(agentList[0], now));

      var rtnObject;
      for (var i = 0; i < agentList.length; i++) {
          rtnObject = this.upgradeSingleAgent(agentList[i], now);
      }

      if (agentList.length > 1 || !rtnObject.sequence_id)
          rtnObject = {
              'upgradeTime': now
          };

      return JSON.stringify(rtnObject); // everything is OK. 
  },

  upgradeSingleAgent: function(agentSysId, now) {
      var agnetCiGr = new GlideRecordSecure('sn_agent_cmdb_ci_agent');
      if (!agnetCiGr.get('sys_id', agentSysId))
          return this.errorMessage(gs.getMessage("Could not locate agent based on sysid={0}", agentSysId));

      var agentId = agnetCiGr.getValue('agent_id');

      var agentExtendedInfoGr = new GlideRecordSecure('sn_agent_ci_extended_info');
      agentExtendedInfoGr.addQuery('agent_id', agentId);
      agentExtendedInfoGr.addQuery('status', '0'); // UP
      agentExtendedInfoGr.addQuery('is_duplicate', 0);
      agentExtendedInfoGr.query();
      if (!agentExtendedInfoGr.next())
          return this.errorMessage(gs.getMessage("Could not locate agent based on agent_id={0}", agentId));

      // Make sure the history table is ready for inserts.
      var msg = "";
      var auu = new AgentUpgradeUtil();
      auu.prepareTable(agentSysId);


      var agentVersion = agentExtendedInfoGr.getValue('agent_version');

      var upgradeGuid = auu.startUpgradeSequence(agentSysId, agentVersion, msg);

      if (!agentVersion) {
          msg = gs.getMessage("Could not deterine agent version for agent with sysid={0}. We got: {1}", [agentSysId, agentVersion]);
          return this.errorMessage(msg, true, auu, agentSysId, upgradeGuid);
      }

      if (!AgentVersionFieldStyle.isAgentVersionLowerThanAccfVersion(agentVersion)) {
          msg = gs.getMessage("Agent version is equal to (or higher than) ACC-F version; nothing to upgrade.");
          return this.infoSkippedMessage(msg, auu, agentSysId, upgradeGuid);
      }

      /* Here we make sure that the latest agent is installed.
          For example: if the store app version is 2.9.2 and the agent installation version is 2.9.0,
          but there is no 2.9.2 version of the agent installation and 2.9.0 is the latest - we should stop.
      */
      if (AgentVersionFieldStyle.isLatestAgentVersion(agentVersion)) {
          msg = gs.getMessage("Agent has the latest version {0} based on ACC-F version {1}.", [agentVersion, AgentVersionFieldStyle.getAccfVersion()]);
          return this.infoSkippedMessage(msg, auu, agentSysId, upgradeGuid);
      }

      if (!agentExtendedInfoGr.getValue('cmdb_ci')) {
          msg = gs.getMessage("Could not locate Agent Host CI reference");
          return this.errorMessage(msg, true, auu, agentSysId, upgradeGuid);
      }
      var computerGr = new GlideRecordSecure('cmdb_ci_computer');
      if (!computerGr.get('sys_id', agentExtendedInfoGr.getValue('cmdb_ci'))) {
          msg = gs.getMessage("Could not locate Computer with sysid={0}", agentExtendedInfoGr.getValue('cmdb_ci'));
          return this.errorMessage(msg, true, auu, agentSysId, upgradeGuid);
      }

      var os = computerGr.getValue('os');
      var version = computerGr.getValue('os_version');
      if (version)
          version = version.split('.')[0]; // Get the major version.

      if (os.indexOf('Windows') > -1)
          os = 'Windows';
      else
          os = os + " " + version;

      if (!this.upgradeRequirements[os] || !this.upgradeRequirements[os].CheckDef) {
          msg = gs.getMessage("The agent OS version ({0}) is currently not supported.", os);
          return this.errorMessage(msg, true, auu, agentSysId, upgradeGuid);
      }

      if (!this.upgradeRequirements[os].MinAgentVersion || AgentVersionFieldStyle.agentVersionCompareTo(agentVersion, this.upgradeRequirements[os].MinAgentVersion) < 0) {
          msg = gs.getMessage("The agent's current version is not the minimum agent version for upgrade. Minimum version for {0} is {1}", [os, this.upgradeRequirements[os].MinAgentVersion]);
          return this.errorMessage(msg, true, auu, agentSysId, upgradeGuid);
      }

      // Making sure agent is still up.
      if (agentExtendedInfoGr.getValue('status') != '0') {
          msg = gs.getMessage("Agent {0} is not currently up, canceling upgrade.", agentExtendedInfoGr.getValue('name'));
          return this.errorMessage(msg, true, auu, agentSysId, upgradeGuid);
      }

      // Now perform the upgrade
      msg = gs.getMessage("Instance validation success. Invoking upgrade on agent.");
      auu.updateSequenceStateByStage(agentSysId, upgradeGuid, 'Success', 'InstanceVerification', msg);

      // Upgrade
      var check = {
          "checkDefId": this.upgradeRequirements[os].CheckDef,
          "params": {
              "upgrade_seq": upgradeGuid
          }
      };
      var agentNow = new sn_agent.AgentNowHandler();
      // Must NOT do next for runCheckForCis to function.
      agnetCiGr = new GlideRecordSecure('sn_agent_cmdb_ci_agent');
      agnetCiGr.addQuery('sys_id', agentSysId);
      agnetCiGr.query();
      agentNow.runCheckForCis(agnetCiGr, check, '1', 600000); // 10 minutes

      msg = gs.getMessage("Starting agent side validations...");
      auu.createNewStateForSequence(agentSysId, upgradeGuid, agentVersion, 'AgentVerification', msg);

      var rtnObject = {
          'sequence_id': upgradeGuid
      };
      return rtnObject; // everything is OK. 
  },

  errorMessage: function(msg, shouldCreateMessage, auu, agentSysId, upgradeGuid) {
      header = AgentUpgradeUtil.error;
      if (shouldCreateMessage) {
          auu.updateSequenceStateByStage(agentSysId, upgradeGuid, 'Failed', 'InstanceVerification', header + ": " + msg);
      }

      var rtnObject = {};
      rtnObject[header] = msg;
      return rtnObject;
  },

  infoSkippedMessage: function(msg, auu, agentSysId, upgradeGuid) {
      header = AgentUpgradeUtil.info;
      auu.updateSequenceStateByStage(agentSysId, upgradeGuid, 'Skipped', 'InstanceVerification', header + ": " + msg);

      var rtnObject = {};
      rtnObject[header] = msg;
      return rtnObject;
  },

  getLocalCurrentTime: function() {
      var gnow = new GlideDateTime();
      var local = gnow.getLocalTime();
      var sDate = gnow.getLocalDate() + " " + local.getHourUTC() + ":" + local.getMinutesUTC() + ":" + local.getSeconds();
      return new GlideDateTime(sDate).toString();
  },

  type: 'AgentUpgradeAjax'
});

Sys ID

19ee952f535501100112ddeeff7b120c

Offical Documentation

Official Docs: