Преглед изворни кода

HUE-3776 [core] Install jobs and pig examples under examples directory

Jenny Kim пре 9 година
родитељ
комит
7346e2f

+ 20 - 0
apps/oozie/src/oozie/management/commands/oozie_setup.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import json
 import logging
 import os
 from lxml import etree
@@ -25,6 +26,7 @@ from django.utils.translation import ugettext as _
 
 from hadoop import cluster
 
+from desktop.conf import USE_NEW_EDITOR
 from desktop.models import Directory, Document, Document2, Document2Permission
 from liboozie.submittion import create_directories
 from oozie.conf import LOCAL_SAMPLE_DATA_DIR, LOCAL_SAMPLE_DIR, REMOTE_SAMPLE_DIR, ENABLE_V2
@@ -145,6 +147,24 @@ class Command(NoArgsCommand):
       name=Document2.EXAMPLES_DIR
     )
 
+    if USE_NEW_EDITOR.get():
+      docs = Document.objects.get_docs(self.user, Workflow).filter(owner=self.user)
+      for doc in docs:
+        if doc.content_object:
+          data = doc.content_object.data_dict
+          data.update({'content_type': doc.content_type.model, 'object_id': doc.object_id})
+          data = json.dumps(data)
+
+          doc2 = Document2.objects.create(
+            owner=self.user,
+            parent_directory=examples_dir,
+            name=doc.name,
+            type='link-workflow',
+            description=doc.description,
+            data=data)
+
+          LOG.info('Successfully installed sample link to jobsub: %s' % (doc2.name,))
+
     # Share oozie examples with default group
     oozie_examples = Document2.objects.filter(
       type__in=['oozie-workflow2', 'oozie-coordinator2', 'oozie-bundle2'],

+ 42 - 5
apps/pig/src/pig/management/commands/pig_setup.py

@@ -15,27 +15,28 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import json
 import logging
 import os
 
-from django.contrib.auth.models import User
 from django.core import management
 from django.core.management.base import NoArgsCommand
-from django.db import transaction
 from django.utils.translation import ugettext as _
 
 from hadoop import cluster
 
+from desktop.conf import USE_NEW_EDITOR
 from desktop.lib import paths
-from desktop.models import Document
+from desktop.models import Directory, Document, Document2, Document2Permission
 from liboozie.submittion import create_directories
 from pig.conf import LOCAL_SAMPLE_DIR, REMOTE_SAMPLE_DIR
-from useradmin.models import install_sample_user
+from useradmin.models import get_default_user_group, install_sample_user
 
 LOG = logging.getLogger(__name__)
 
 
 class Command(NoArgsCommand):
+
   def handle_noargs(self, **options):
     fs = cluster.get_hdfs()
     create_directories(fs, [REMOTE_SAMPLE_DIR.get()])
@@ -57,6 +58,42 @@ class Command(NoArgsCommand):
     fs.do_as_user(fs.DEFAULT_USER, fs.copyFromLocal, local_dir, remote_data_dir)
 
     # Load jobs
-    install_sample_user()
+    sample_user = install_sample_user()
     management.call_command('loaddata', 'initial_pig_examples.json', verbosity=2)
     Document.objects.sync()
+
+    if USE_NEW_EDITOR.get():
+      try:
+        # Don't overwrite
+        doc = Document.objects.get(object_id=1100713)
+        doc2 = Document2.objects.get(owner=sample_user, name=doc.name, type='link-pigscript')
+      except Document.DoesNotExist:
+        LOG.warn('Sample pig script document not found.')
+      except Document2.DoesNotExist:
+        if doc.content_object:
+          data = doc.content_object.dict
+          data.update({'content_type': doc.content_type.model, 'object_id': doc.object_id})
+          data = json.dumps(data)
+
+          # Get or create sample user directories
+          home_dir = Directory.objects.get_home_directory(sample_user)
+          examples_dir, created = Directory.objects.get_or_create(
+            parent_directory=home_dir,
+            owner=sample_user,
+            name=Document2.EXAMPLES_DIR)
+
+          doc2 = Document2.objects.create(
+            owner=sample_user,
+            parent_directory=examples_dir,
+            name=doc.name,
+            type='link-pigscript',
+            description=doc.description,
+            data=data)
+
+          # Share with default group
+          examples_dir.share(sample_user, Document2Permission.READ_PERM, groups=[get_default_user_group()])
+          doc2.save()
+
+        LOG.info('Successfully installed sample link to pig script: %s' % (doc2.name,))
+
+