Name

sn_agent.PluginsToMidSync

Description

Synchronize plugins (assets) to mid server

Script

var PluginsToMidSync = Class.create();
PluginsToMidSync.prototype = {
  initialize: function() {},

  // Find or create directories to be synced to MID. The directory structure is os/platform/version/arch. For example:
  // linux/ubuntu/16.4/amd64
  createAssetDirectories: function(currentPlugin) {

      var osDirId = this.findOrCreateDirectory(currentPlugin.os + '');
      var platformDirId = this.findOrCreateDirectory(currentPlugin.platform + '', osDirId);
      var versionDirId = this.findOrCreateDirectory(currentPlugin.os_version + '', platformDirId);
      var archDirId = this.findOrCreateDirectory(currentPlugin.arch + '', versionDirId);

      // the parent of this record is the archDirId record
      var agentAssetGr = new GlideRecord('sn_agent_asset');
      if (agentAssetGr.get(currentPlugin.getUniqueValue())) {
          agentAssetGr.setWorkflow(false);
          agentAssetGr.setValue('parent', archDirId);
          agentAssetGr.update();
      }
  },

  /*
   * check if required directory already has a record in sn_agent_asset. If not, create it now
   */
  findOrCreateDirectory: function(name, parent_id) {
      var dirId = this.findDirectory(name, parent_id);
      if (dirId)
          return dirId;

      // Create os directory
      var assetGr = new GlideRecord('sn_agent_asset');
      assetGr.setValue('directory', true);
      assetGr.setValue('name', name);
      var parent_id_val = parent_id ? parent_id : '';
      assetGr.setValue('parent', parent_id_val);
      assetGr.setValue('active', true);
      return assetGr.insert();
  },

  /*
   * check if required directory already has a record in sn_agent_asset. If found, return its sys_id
   */
  findDirectory: function(name, parent_id) {
      var assetGr = new GlideRecord('sn_agent_asset');
      assetGr.addQuery('directory', true);
      assetGr.addQuery('name', name);
      if (parent_id)
          assetGr.addQuery('parent', parent_id);
      else
          assetGr.addNullQuery('parent');
      assetGr.query();

      if (assetGr.next())
          return assetGr.getUniqueValue();
  },
  createDirsAndNotifyMid: function() {

      var assetWithoutParentGr = this.getAssetsWithoutParent();
      var toNotifyMid = false;
      while (assetWithoutParentGr.next()) {
          this.createAssetDirectories(assetWithoutParentGr);
          if (!toNotifyMid) {
              toNotifyMid = true;
          }
      }
      if (toNotifyMid) {
          // Notify MIDs that changes happened in assets, so they will pull those changes
          new MIDNotificationHandler().notifyMIDServers('FileChange', 'sn_agent_asset');
      }
  },

  getAssetsWithoutParent: function() {
      var fileWithoutParentGr = new GlideRecord('sn_agent_asset');
      fileWithoutParentGr.addQuery('directory', false);
      fileWithoutParentGr.addNullQuery('parent').addOrCondition('parent.sys_id', 'NULL'); //parent can be with invalid sys_id
      fileWithoutParentGr.query();

      return fileWithoutParentGr;
  },

  type: 'PluginsToMidSync'
};

Sys ID

b8c26371733500105c35d777faf6a7af

Offical Documentation

Official Docs: