<commit_before>/*
 * Copyright (c) 2010-2010 LinkedIn, Inc
 * Portions Copyright (c) 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.linkedin.glu.console.controllers

import org.linkedin.glu.orchestration.engine.agents.AgentsService
import org.linkedin.glu.orchestration.engine.deployment.DeploymentService
import org.linkedin.glu.provisioner.plan.api.IStep
import org.linkedin.glu.agent.tracker.MountPointInfo
import org.linkedin.glu.orchestration.engine.fabric.Fabric
import java.security.AccessControlException
import org.linkedin.glu.orchestration.engine.agents.NoSuchAgentException
import org.linkedin.glu.provisioner.plan.api.IStep.Type

/**
 * @author <EMAIL>
 */
class AgentsController extends ControllerBase
{
  AgentsService agentsService
  DeploymentService deploymentService

  def beforeInterceptor = {
    // we make sure that the fabric is always set before executing any action
    return ensureCurrentFabric()
  }

  /**
   * List all the agents (in the current fabric) with their version (in preparation to upgrade)
   */
  def listVersions = {
    def agents = agentsService.getAgentInfos(request.fabric)

    def versions = agents.values().groupBy { it.version }

    return [versions: versions]
  }

  /**
   * Upgrade
   */
  def upgrade = {
    if(!params.version || !params.coordinates)
    {
      flash.error = "Missing version or coordinates"
      redirect(action: 'listVersions')
      return
    }

    if(params.agents instanceof String)
    {
      params.agents = [params.agents]
    }

    params.fabric = request.fabric
    params.type = Type.PARALLEL

    def plans =
      deploymentService.computeAgentsUpgradePlan(params,
                                                 [name: "Agent upgrade to version ${params.version}".toString()])

    if(plans)
    {
      session.delta = plans
      println plans[0].toXml()
      redirect(controller: 'plan', action: 'view', id: plans[0].id)
    }
    else
    {
      flash.message = "No agent to upgrade"
      redirect(action: 'listVersions')
    }
  }

  /**
   * cleanup
   */
  def cleanup = {
    params.name = "Agent upgrade cleanup"
    params.system = request.system
    params.type = Type.PARALLEL

    def plans = deploymentService.computeAgentsCleanupUpgradePlan(params, null)

    if(plans)
    {
      session.delta = plans
      println plans[0].toXml()
      redirect(controller: 'plan', action: 'view', id: plans[0].id)
    }
    else
    {
      flash.message = "No agent to cleanup"
      redirect(action: 'listVersions')
    }
  }

  /**
   * View a single agent in an fabric
   */
  def view = {
    def agent = agentsService.getAgentInfo(request.fabric, params.id)

    def model = [:]
    def allPlans = [:]

    def title = "agent [${params.id}]"
    
    if(agent)
    {
      params.name = title

      def system = request.system
      system = system?.filterBy("agent='${params.id}'".toString())

      request.system = system
      params.system = system


      session.delta = []

      ['deploy', 'bounce', 'redeploy', 'undeploy'].each { type ->
        params.name = "${type.capitalize()}: ${title}".toString()

        def plans =
          deploymentService."compute${type.capitalize()}Plans"(params,
                                                               [type: type,
                                                                agent: params.id])
        if(plans)
          session.delta.addAll(plans)

        allPlans[type] = plans
      }

      def mountPoints = [] as Set
      system.each { mountPoints << it.mountPoint }
<commit_msg>#33: fixed mountpoint disappearing issue<commit_after>/*
 * Copyright (c) 2010-2010 LinkedIn, Inc
 * Portions Copyright (c) 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.linkedin.glu.console.controllers

import org.linkedin.glu.orchestration.engine.agents.AgentsService
import org.linkedin.glu.orchestration.engine.deployment.DeploymentService
import org.linkedin.glu.provisioner.plan.api.IStep
import org.linkedin.glu.agent.tracker.MountPointInfo
import org.linkedin.glu.orchestration.engine.fabric.Fabric
import java.security.AccessControlException
import org.linkedin.glu.orchestration.engine.agents.NoSuchAgentException
import org.linkedin.glu.provisioner.plan.api.IStep.Type
import org.linkedin.glu.provisioner.core.model.SystemModel

/**
 * @author <EMAIL>
 */
class AgentsController extends ControllerBase
{
  AgentsService agentsService
  DeploymentService deploymentService

  def beforeInterceptor = {
    // we make sure that the fabric is always set before executing any action
    return ensureCurrentFabric()
  }

  /**
   * List all the agents (in the current fabric) with their version (in preparation to upgrade)
   */
  def listVersions = {
    def agents = agentsService.getAgentInfos(request.fabric)

    def versions = agents.values().groupBy { it.version }

    return [versions: versions]
  }

  /**
   * Upgrade
   */
  def upgrade = {
    if(!params.version || !params.coordinates)
    {
      flash.error = "Missing version or coordinates"
      redirect(action: 'listVersions')
      return
    }

    if(params.agents instanceof String)
    {
      params.agents = [params.agents]
    }

    params.fabric = request.fabric
    params.type = Type.PARALLEL

    def plans =
      deploymentService.computeAgentsUpgradePlan(params,
                                                 [name: "Agent upgrade to version ${params.version}".toString()])

    if(plans)
    {
      session.delta = plans
      println plans[0].toXml()
      redirect(controller: 'plan', action: 'view', id: plans[0].id)
    }
    else
    {
      flash.message = "No agent to upgrade"
      redirect(action: 'listVersions')
    }
  }

  /**
   * cleanup
   */
  def cleanup = {
    params.name = "Agent upgrade cleanup"
    params.system = request.system
    params.type = Type.PARALLEL

    def plans = deploymentService.computeAgentsCleanupUpgradePlan(params, null)

    if(plans)
    {
      session.delta = plans
      println plans[0].toXml()
      redirect(controller: 'plan', action: 'view', id: plans[0].id)
    }
    else
    {
      flash.message = "No agent to cleanup"
      redirect(action: 'listVersions')
    }
  }

  /**
   * View a single agent in an fabric
   */
  def view = {
    def agent = agentsService.getAgentInfo(request.fabric, params.id)

    def model = [:]
    def allPlans = [:]

    def title = "agent [${params.id}]"
    
    if(agent)
    {
      params.name = title

      def system = request.system
      system = system?.filterBy("agent='${params.id}'".toString())

      request.system = system
      params.system = system


      session.delta = []

      ['deploy', 'bounce', 'redeploy', 'undeploy'].each { type ->
        params.name = "${type.capitalize()}: ${title}".toString()

        def plans =
          deploymentService."compute${type.capitalize()}Plans"(params,
                                                               [type: type,
                                                                agent: params.id])
        if(plans)
          session.delta.addAll(plans)

        allPlans[type] = plans
      }

      def mountPoints = [] as Set
      system.each { mountPoints << it.mountPoint }

      SystemModel currentModel = agentsService.getCurrentSystemModel(request.fabric)
      currentModel.each { mountPoints << it.mountPoint }

{'fraction_non_alphanumeric': 0.10650396872408482, 'fraction_numerical': 0.00497571377798839, 'mean_word_length': 2.574089754445385, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3234939', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2371, 'n_words': 622}