hue_multiple_home_cleanup.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #Changes owner of Search Dashboard
  18. PARCEL_DIR=/opt/cloudera/parcels/CDH
  19. USAGE="usage: $0"
  20. OVERRIDE=$1
  21. if [[ ! ${USER} =~ .*root* ]]
  22. then
  23. if [[ -z ${OVERRIDE} ]]
  24. then
  25. echo "Script must be run as root: exiting"
  26. exit 1
  27. fi
  28. fi
  29. if [ ! -d "/usr/lib/hadoop" ]
  30. then
  31. CDH_HOME=$PARCEL_DIR
  32. else
  33. CDH_HOME=/usr
  34. fi
  35. if [[ -z ${HUE_CONF_DIR} ]]
  36. then
  37. if [ -d "/var/run/cloudera-scm-agent/process" ]
  38. then
  39. HUE_CONF_DIR="/var/run/cloudera-scm-agent/process/`ls -1 /var/run/cloudera-scm-agent/process | grep HUE_SERVER | sort -n | tail -1 `"
  40. else
  41. HUE_CONF_DIR="/etc/hue/conf"
  42. fi
  43. export HUE_CONF_DIR
  44. fi
  45. if [ -d "${CDH_HOME}/lib/hue/build/env/bin" ]
  46. then
  47. COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue shell"
  48. else
  49. COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue shell"
  50. fi
  51. if [[ -z ${ORACLE_HOME} ]]
  52. then
  53. ORACLE_HOME=/opt/cloudera/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2/
  54. LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}
  55. export ORACLE_HOME LD_LIBRARY_PATH
  56. fi
  57. HUE_IGNORE_PASSWORD_SCRIPT_ERRORS=1
  58. export CDH_HOME COMMAND HUE_IGNORE_PASSWORD_SCRIPT_ERRORS
  59. echo "HUE_CONF_DIR: ${HUE_CONF_DIR}"
  60. echo "COMMAND: ${COMMAND}"
  61. ${COMMAND} <<EOF
  62. from datetime import datetime
  63. from django.db import models
  64. from desktop.models import Document, Document2
  65. from django.contrib.auth.models import User
  66. for user in User.objects.filter():
  67. # print user.username
  68. last_modified=datetime.now()
  69. oldest_doc=0L
  70. movecount=1
  71. homedir_count=0
  72. for document in Document2.objects.filter(owner=user, parent_directory=None, name=Document2.HOME_DIR):
  73. homedir_count=homedir_count + 1
  74. if homedir_count > 1:
  75. print "%s has more than 1 homedir" % user.username
  76. print "Fixing by moving newer ones to subdirectories"
  77. for document in Document2.objects.filter(owner=user, parent_directory=None, name=Document2.HOME_DIR):
  78. homedir_count=homedir_count + 1
  79. if document.last_modified < last_modified:
  80. last_modified=document.last_modified
  81. oldest_doc_id=document.id
  82. print "Oldest doc is %s" % oldest_doc_id
  83. oldest_doc = Document2.objects.get(id=oldest_doc_id)
  84. for document in Document2.objects.filter(owner=user, parent_directory=None, name=Document2.HOME_DIR):
  85. if document.id != oldest_doc_id:
  86. document.name="scriptmoved%s" % movecount
  87. document.parent_directory=oldest_doc
  88. document.save()
  89. movecount=movecount + 1
  90. for user in User.objects.filter():
  91. print user.username
  92. last_modified=datetime.now()
  93. oldest_doc=0L
  94. movecount=1
  95. trashdir_count=0
  96. for document in Document2.objects.filter(owner=user, name=Document2.TRASH_DIR):
  97. print "User has a trash"
  98. trashdir_count=trashdir_count + 1
  99. if trashdir_count > 1:
  100. print "%s has more than 1 trashdir" % user.username
  101. print "Fixing by moving newer ones to subdirectories"
  102. for document in Document2.objects.filter(owner=user, name=Document2.TRASH_DIR):
  103. trashdir_count=trashdir_count + 1
  104. if document.last_modified < last_modified:
  105. last_modified=document.last_modified
  106. oldest_doc_id=document.id
  107. print "Oldest doc is %s" % oldest_doc_id
  108. oldest_doc = Document2.objects.get(id=oldest_doc_id)
  109. for document in Document2.objects.filter(owner=user, name=Document2.TRASH_DIR):
  110. if document.id != oldest_doc_id:
  111. document.name="scriptmoved%s" % movecount
  112. document.parent_directory=oldest_doc
  113. document.save()
  114. movecount=movecount + 1
  115. EOF