Adopt model generation for new format

  * Depends-on: https://gerrit.mcp.mirantis.com/#/c/36179/

Change-Id: I11b4ffcc75ae47628c85427c7e9466491187b3ac
Prod-related: PROD-26374(PROD:26374)
diff --git a/generate-cookiecutter-products.groovy b/generate-cookiecutter-products.groovy
index 2f4f13f..22c803a 100644
--- a/generate-cookiecutter-products.groovy
+++ b/generate-cookiecutter-products.groovy
@@ -69,7 +69,7 @@
     }
     // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
     // After 2018q4 releases, need to also check 'static' repo, for example ubuntu.
-    binTest = common.checkRemoteBinary(['mcp_version': distribRevision])
+    def binTest = common.checkRemoteBinary(['mcp_version': distribRevision])
     if (!binTest.linux_system_repo_url || !binTest.linux_system_repo_ubuntu_url) {
         common.errorMsg("Binary release: ${distribRevision} not exist or not full. Fallback to 'proposed'! ")
         distribRevision = 'proposed'
@@ -93,6 +93,7 @@
         def context = globalVariatorsUpdate()
         def RequesterEmail = context.get('email_address', '')
         def templateEnv = "${env.WORKSPACE}/template"
+        // modelEnv - this is reclass root, aka /srv/salt/reclass
         def modelEnv = "${env.WORKSPACE}/model"
         def testEnv = "${env.WORKSPACE}/test"
         def pipelineEnv = "${env.WORKSPACE}/pipelines"
@@ -119,7 +120,7 @@
             }
             stage('Create empty reclass model') {
                 dir(path: modelEnv) {
-                    sh "rm -rfv .git; git init"
+                    sh 'rm -rfv .git; git init'
                     sshagent(credentials: [gerritCredentials]) {
                         sh "git submodule add ${context['shared_reclass_url']} 'classes/system'"
                     }
@@ -130,7 +131,7 @@
                     extensions       : [[$class: 'RelativeTargetDirectory', relativeTargetDir: systemEnv]],
                     userRemoteConfigs: [[url: context['shared_reclass_url'], refspec: context['shared_reclass_branch'], credentialsId: gerritCredentials],],
                 ])
-                git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
+                git.commitGitChanges(modelEnv, 'Added new shared reclass submodule', "${user}@localhost", "${user}")
             }
 
             stage('Generate model') {
@@ -161,9 +162,20 @@
                     if (context.get('cfg_failsafe_ssh_public_key')) {
                         writeFile file: 'failsafe-ssh-key.pub', text: context['cfg_failsafe_ssh_public_key']
                     }
-                    python.setupCookiecutterVirtualenv(cutterEnv)
-                    // FIXME refactor generateModel
-                    python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
+                    if (!fileExists(new File(templateEnv, 'tox.ini').toString())) {
+                        python.setupCookiecutterVirtualenv(cutterEnv)
+                        python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
+                    } else {
+                        // tox-based CC generated structure of reclass,from the root. Otherwise for bw compat, modelEnv
+                        // still expect only lower lvl of project, aka model/classes/cluster/XXX/. So,lets dump result into
+                        // temp dir, and then copy it over initial structure.
+                        reclassTempRootDir = sh(script: "mktemp -d -p ${env.WORKSPACE}", returnStdout: true).trim()
+                        python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, reclassTempRootDir, templateEnv, false)
+                        dir(modelEnv) {
+                            common.warningMsg('Forming reclass-root structure...')
+                            sh("cp -ra ${reclassTempRootDir}/reclass/* .")
+                        }
+                    }
                     git.commitGitChanges(modelEnv, "Create model ${context['cluster_name']}", "${user}@localhost", "${user}")
                 }
             }
@@ -185,7 +197,7 @@
                             'distribRevision'    : distribRevision,
                             'dockerContainerName': DockerCName,
                             'testContext'        : 'salt-model-node',
-                            'dockerExtraOpts'    : [ '--memory=3g' ]
+                            'dockerExtraOpts'    : ['--memory=3g']
                         ]
                         testResult = saltModelTesting.testNode(config)
                         common.infoMsg("Test finished: SUCCESS")
diff --git a/test-cookiecutter-reclass.groovy b/test-cookiecutter-reclass.groovy
index 8076041..bd5ec1e 100644
--- a/test-cookiecutter-reclass.groovy
+++ b/test-cookiecutter-reclass.groovy
@@ -116,7 +116,7 @@
         common.infoMsg("StepPrepareGit: ${gerrit_data}")
         // fetch needed sources
         dir(templateEnvFolder) {
-            if (! gerrit_data['gerritRefSpec']) {
+            if (!gerrit_data['gerritRefSpec']) {
                 // Get clean HEAD
                 gerrit_data['useGerritTriggerBuildChooser'] = false
             }
@@ -133,7 +133,26 @@
         for (contextFile in _contextFileList) {
             def basename = common.GetBaseName(contextFile, '.yml')
             def context = readFile(file: "${_templateEnvDir}/contexts/${contextFile}")
-            python.generateModel(context, basename, 'cfg01', _virtualenv, "${_templateEnvDir}/model", _templateEnvDir)
+            if (!fileExists(new File(_templateEnvDir, 'tox.ini').toString())) {
+                common.warningMsg('Forming NEW reclass-root structure...')
+                python.generateModel(context, basename, 'cfg01', _virtualenv, "${_templateEnvDir}/model", _templateEnvDir)
+            } else {
+                // tox-based CC generated structure of reclass,from the root. Otherwise for bw compat, modelEnv
+                // still expect only lower lvl of project, aka model/classes/cluster/XXX/. So,lets dump result into
+                // temp dir, and then copy it over initial structure.
+                def reclassTempRootDir = sh(script: "mktemp -d -p ${env.WORKSPACE}", returnStdout: true).trim()
+                python.generateModel(context, basename, 'cfg01', _virtualenv, reclassTempRootDir, _templateEnvDir)
+                dir("${_templateEnvDir}/model/${basename}/") {
+                    if (fileExists(new File(reclassTempRootDir, 'reclass').toString())) {
+                        common.warningMsg('Forming NEW reclass-root structure...')
+                        sh("cp -ra ${reclassTempRootDir}/reclass/* .")
+                    } else {
+                        // those hack needed only for period release/2019.2.0 => current patch.
+                        common.warningMsg('Forming OLD reclass-root structure...')
+                        sh("mkdir -p classes/cluster/ ; cd classes/cluster/; cp -ra ${reclassTempRootDir}/* .")
+                    }
+                }
+            }
         }
     }
 }