def common = new com.mirantis.mk.Common()
def salt = new com.mirantis.mk.Salt()
def python = new com.mirantis.mk.Python()
def pepperEnv = "pepperEnv"
def maasNodes = []
def restoreSaltMasterAndMaas = (env.getProperty('RESTORE_SALTMASTER_AND_MAAS') ?: true).toBoolean()
def restoreDogtag = (env.getProperty('RESTORE_DOGTAG') ?: true).toBoolean()
def restoreKeystone = (env.getProperty('RESTORE_KEYSTONE_CREDENTIAL_KEYS') ?: true).toBoolean()

timeout(time: 12, unit: 'HOURS') {
    node() {
        stage('Setup virtualenv for Pepper') {
            python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
        }
        stage('Verify pillar for restore') {
            if (restoreSaltMasterAndMaas) {
                try {
                    def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data').get('return')[0].values()[0]
                    if(masterPillar.isEmpty()) {
                        throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.")
                    }
                    def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data').get('return')[0].values()[0]
                    if(minionPillar.isEmpty()) {
                        throw new Exception("Problem with salt-minion pillar on 'I@salt:master' node.")
                    }
                }
                catch (Exception e){
                    common.errorMsg(e.getMessage())
                    common.errorMsg('Please fix your pillar. For more information check docs: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/salt-master/salt-master-restore.html')
                    throw e
                }
                maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
            }
            if (!maasNodes.isEmpty()) {
                try {
                    def maaSPillar = salt.getPillar(pepperEnv, "I@maas:region", 'maas:region:database:initial_data').get('return')[0].values()[0]
                    if (maaSPillar.isEmpty()) {
                        throw new Exception("Problem with MaaS pillar on 'I@maas:region' node.")
                    }
                }
                catch (Exception e) {
                    common.errorMsg(e.getMessage())
                    common.errorMsg('Please fix your pillar. For more information check docs: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/maas-postgresql/backupninja-postgresql-restore.html')
                    throw e
                }
            } else {
                common.warningMsg("No MaaS Pillar was found. You can ignore this if it's expected. Otherwise you should fix you pillar. Check: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/maas-postgresql/backupninja-postgresql-restore.html")
            }
            if (restoreDogtag) {
                def barbicanBackendPresent = salt.getPillar(pepperEnv, "I@salt:master", "_param:barbican_backend").get('return')[0].values()[0]
                if (barbicanBackendPresent == 'dogtag') {
                    try {
                        def dogtagPillar = salt.getPillar(pepperEnv, "I@dogtag:server:role:master", 'dogtag:server:initial_data').get('return')[0].values()[0]
                        if (dogtagPillar.isEmpty()) {
                            throw new Exception("Problem with Dogtag pillar on 'I@dogtag:server:role:master' node.")
                        }
                        def mineCertPresent = salt.runSaltProcessStep(pepperEnv, "I@dogtag:server:role:master", 'mine.get', ['*', 'dogtag_admin_cert'], null, false).get('return')[0].values()[0]
                        if (mineCertPresent.isEmpty()) {
                            throw new Exception("Problem with Dogtag Admin cert mine data on 'I@dogtag:server:role:master' node.")
                        }
                    }
                    catch (Exception e) {
                        common.errorMsg(e.getMessage())
                        common.errorMsg('Please fix your pillar or missed mine data. For more information check docs: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/dogtag/2.6-and-newer/restore-dogtag.html')
                        throw e
                    }
                }  else {
                    restoreDogtag = false
                    common.warningMsg('Restore for Dogtag is enabled, but service itself is not present. Skipping...')
                }
            }
        }
        stage('Restore') {
            if (restoreSaltMasterAndMaas) {
                common.infoMsg('Starting salt-master restore')
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@salt:master', 'state': 'salt.master.restore'])
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@salt:master', 'state': 'salt.minion.restore'])
                salt.fullRefresh(pepperEnv, '*')
                common.infoMsg('Validating output')
                common.infoMsg('Salt-Keys')
                salt.cmdRun(pepperEnv, 'I@salt:master', "salt-key")
                common.infoMsg('Salt-master CA')
                salt.cmdRun(pepperEnv, 'I@salt:master', "ls -la /etc/pki/ca/salt_master_ca/")
                if (!maasNodes.isEmpty()) {
                    common.infoMsg('Starting MaaS restore')
                    salt.enforceState(['saltId': pepperEnv, 'target': 'I@maas:region', 'state': 'maas.region'])
                }
            }
            if (restoreDogtag) {
                salt.runSaltProcessStep(pepperEnv, 'I@dogtag:server:role:slave', 'service.stop', ['dirsrv@pki-tomcat.service'])
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@dogtag:server:role:master', 'state': 'dogtag.server.restore'])
                salt.runSaltProcessStep(pepperEnv, 'I@dogtag:server:role:slave', 'service.start', ['dirsrv@pki-tomcat.service'])
            }
            if (restoreKeystone) {
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@keystone:server:role:primary', 'state': 'keystone.restore'])
            }
        }
        stage('After restore steps') {
            if (restoreSaltMasterAndMaas) {
                common.infoMsg("No more steps for Salt Master and MaaS restore are required.")
            }
            if (restoreDogtag) {
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@dogtag:server:role:master', 'state': 'dogtag.server'])
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@dogtag:server', 'state': 'dogtag.server'])
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@barbican:server:role:primary', 'state': 'barbican.server'])
                salt.enforceState(['saltId': pepperEnv, 'target': 'I@barbican:server', 'state': 'barbican.server'])
                salt.cmdRun(pepperEnv, 'I@barbican:server', 'rm -rf /etc/barbican/alias')
                salt.runSaltProcessStep(pepperEnv, 'I@barbican:server', 'service.restart', 'apache2')
            }
        }
    }
}
