// Settings
@Library('jenkins-devops-cicd-library') _
import com.bnpparibas.release.*

// Control the NID Windows services (Pyro master/kv/queue/huey-storage/consumer/
// pushdown/tasksched) via Windows Task Scheduler. FQDN-targeted, no inventory.
// Shared creds + ansible run live in projects_deploy/_ansible.groovy.
//
// Assumes the app + its Python runtime are already deployed on the target
// (Jenkinsfile_python_installer_win + the app-deploy pipeline). This pipeline
// registers / starts / stops / restarts / removes the services; it ships no code.

pipeline {
    agent { label 'Bnpp-Ansible' }

    parameters {
        choice(name: 'Environment', choices: ['DEV', 'SIT', 'STG', 'PRD'],
               description: 'Selects the CyberArk safe / superuser.')
        string(name: 'TargetHosts', defaultValue: '',
               description: 'Target FQDN(s), comma-separated, e.g. eurv0ii40617.xmp.net.intra')
        choice(name: 'Action',
               choices: ['status', 'register', 'start', 'stop', 'restart', 'enable', 'disable', 'remove'],
               description: 'register = gen XML + import (+ start); the rest control existing tasks.')
        string(name: 'Services', defaultValue: 'all',
               description: "'all' or a comma-separated subset: master,kv,queue,huey-storage,consumer,pushdown,tasksched")
        string(name: 'AppRoot', defaultValue: 'D:\\Apps\\datahub',
               description: 'Deployed app root (holds cli.py).')
        string(name: 'InstallDir', defaultValue: 'D:\\Apps\\python',
               description: 'Python runtime dir (laid down by the runtime installer).')
        string(name: 'PythonExe', defaultValue: 'python.exe',
               description: 'Interpreter path relative to InstallDir.')
        string(name: 'TaskPrefix', defaultValue: '\\NID',
               description: 'Task Scheduler folder prefix, e.g. \\NID -> \\NID\\master.')
        choice(name: 'Logon', choices: ['Password', 'ServiceAccount', 'InteractiveToken'],
               description: 'register only. Password = headless w/ stored creds; ServiceAccount = LocalSystem/SYSTEM; InteractiveToken = runs when the user is logged on.')
        string(name: 'TaskRunAsUser', defaultValue: '',
               description: 'register only. Run-as identity (blank = CyberArk ops account, EURO\\<credential>).')
        choice(name: 'StartAfterRegister', choices: ['yes', 'no'],
               description: 'register only: start the services once imported.')
        choice(name: 'RunOnAllNodes', choices: ['yes', 'no'],
               description: 'yes => act on every target host / no => run once on a single host.')
        choice(name: 'ElevatePrivileges', choices: ['yes', 'no'],
               description: 'yes => run as the service account via CyberArk runas (required to import boot-triggered tasks).')
    }

    stages {
        stage('Validate') {
            steps {
                script {
                    if (!params.TargetHosts?.trim()) { error('TargetHosts is required.') }
                    if (!params.Services?.trim()) { error('Services is required (use "all" or a subset).') }
                }
            }
        }

        stage('Service control') {
            steps {
                script {
                    def ansible = load 'deployment/projects_deploy/_ansible.groovy'
                    ansible.run(
                        environment: params.Environment,
                        os:          'windows',
                        playbook:    'projects_deploy/service_ctl_win.yml',
                        hosts:       params.TargetHosts,
                        extraVars: [
                            Action:             params.Action,
                            Services:           params.Services,
                            AppRoot:            params.AppRoot,
                            InstallDir:         params.InstallDir,
                            PythonExe:          params.PythonExe,
                            TaskPrefix:         params.TaskPrefix,
                            Logon:              params.Logon,
                            TaskRunAsUser:      params.TaskRunAsUser,
                            StartAfterRegister: params.StartAfterRegister,
                            RunOnAllNodes:      params.RunOnAllNodes,
                            ElevatePrivileges:  params.ElevatePrivileges
                        ]
                    )
                }
            }
        }
    }
}
