RepoInstaller.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import logging
  18. import os
  19. import json
  20. from pprint import pformat
  21. import ast
  22. from shell import shellRunner
  23. from manifestGenerator import writeImports
  24. import AmbariConfig
  25. PUPPET_EXT=".pp"
  26. logger = logging.getLogger()
  27. class RepoInstaller:
  28. def __init__(self, parsedJson, path, modulesdir, taskId, config):
  29. self.parsedJson = parsedJson
  30. self.path = path
  31. self.modulesdir = modulesdir
  32. self.taskId = taskId
  33. self.sh = shellRunner()
  34. self.config = config
  35. def prepareReposInfo(self):
  36. params = {}
  37. self.repoInfoList = []
  38. if self.parsedJson.has_key('hostLevelParams'):
  39. params = self.parsedJson['hostLevelParams']
  40. if params.has_key('repo_info'):
  41. self.repoInfoList = params['repo_info']
  42. logger.info("Repo List Info " + pformat(self.repoInfoList))
  43. if (isinstance(self.repoInfoList, basestring)):
  44. if (self.repoInfoList is not None and (len(self.repoInfoList) > 0)):
  45. self.repoInfoList = ast.literal_eval(self.repoInfoList)
  46. else:
  47. self.repoInfoList = []
  48. def generateFiles(self):
  49. repoPuppetFiles = []
  50. for repo in self.repoInfoList:
  51. repoFile = open(self.path + os.sep + repo['repoId'] + '-' +
  52. str(self.taskId) + PUPPET_EXT, 'w+')
  53. writeImports(repoFile, self.modulesdir, AmbariConfig.imports)
  54. baseUrl = ''
  55. mirrorList = ''
  56. if repo.has_key('baseUrl'):
  57. baseUrl = repo['baseUrl']
  58. baseUrl = baseUrl.decode('unicode-escape').encode('utf-8')
  59. # Hack to take care of $ signs in the repo url
  60. baseUrl = baseUrl.replace('$', '\$')
  61. if repo.has_key('mirrorsList'):
  62. mirrorList = repo['mirrorsList']
  63. mirrorList = mirrorList.decode('unicode-escape').encode('utf-8')
  64. # Hack to take care of $ signs in the repo url
  65. mirrorList = mirrorList.replace('$', '\$')
  66. repoFile.write('node /default/ {')
  67. repoFile.write('class{ "hdp-repos::process_repo" : ' + ' os_type => "' + repo['osType'] +
  68. '", repo_id => "' + repo['repoId'] + '", base_url => "' + baseUrl +
  69. '", mirror_list => "' + mirrorList +'", repo_name => "' + repo['repoName'] + '" }' )
  70. repoFile.write('}')
  71. repoFile.close()
  72. repoPuppetFiles.append(repoFile.name)
  73. return repoPuppetFiles
  74. def generate_repo_manifests(self):
  75. self.prepareReposInfo()
  76. repoPuppetFiles = self.generateFiles()
  77. return repoPuppetFiles
  78. def main():
  79. #Test code
  80. logging.basicConfig(level=logging.DEBUG)
  81. #test code
  82. jsonFile = open('test.json', 'r')
  83. jsonStr = jsonFile.read()
  84. parsedJson = json.loads(jsonStr)
  85. repoInstaller = RepoInstaller(parsedJson, '/tmp', '/home/centos/ambari_ws/ambari-agent/src/main/puppet/modules',0)
  86. repoInstaller.generate_repo_manifests()
  87. if __name__ == '__main__':
  88. main()