diff --git a/README.md b/README.md index ba389e5..c074c14 100644 --- a/README.md +++ b/README.md @@ -276,7 +276,9 @@ optional arguments: This script allows users with an offline puppet-forge-server (rubygem) instance to perform a special export of puppetforge modules from the Satellite puppet-forge repository (-r) in the directory structure required by the puppet-forge-server -application. After exporting, the modules are copied via rsync to the puppet-forge-server. +application. After exporting, the modules are copied to the puppet-forge-server. The format +of the export is controlled with the type (-t) flag, as either 'puppet-forge-server' for the +rubygem based server, or 'artifiactory' for JFrog Artifiactory puppet server format. The puppet-forge-server hostname can be defined in the config.yml, or overridden with (-s), as can the module path (-m) on the remote server (default is /opt/puppet-forge/modules). The user performing the rsync will be the user that is running the script, unless @@ -285,7 +287,9 @@ overridden with (-u). The config.yml block that defines the puppet-forge-server hostname is: ``` puppet-forge-server: + servertype: puppet-forge-server hostname: puppetforge.example.org + modulepath: /opt/puppet-forge/modules ``` ``` @@ -297,6 +301,7 @@ optional arguments: -h, --help show this help message and exit -o ORG, --org ORG Organization (Uses default if not specified) -r REPO, --repo REPO Puppetforge repository label + -t TYPE, --type TYPE Puppetforge server type (puppet-forge-server|artifiactory) -s SERVER, --server SERVER puppet-forge-server hostname -m MODULEPATH, --modulepath MODULEPATH @@ -309,7 +314,7 @@ optional arguments: ``` ./push_puppetforge.py -r Puppet_Forge ./push_puppetforge.py -r Puppet_Forge -u fred -./push_puppetforge.py -r Puppet_Forge -s test.example.org -m /opt/tmp +./push_puppetforge.py -r Puppet_Forge -s test.example.org -m /opt/tmp -t artifiactory ``` diff --git a/config/config.yml.example b/config/config.yml.example index 7f823a1..2f3cee6 100644 --- a/config/config.yml.example +++ b/config/config.yml.example @@ -45,4 +45,6 @@ cleanup: keep: 3 puppet-forge-server: + servertype: puppet-forge-server hostname: puppetforge.example.org + modulepath: /opt/puppet-forge/modules diff --git a/helpers.py b/helpers.py index 6007e03..f01a255 100644 --- a/helpers.py +++ b/helpers.py @@ -59,8 +59,14 @@ PROMOTEBATCH = CONFIG['promotion']['batch'] else: PROMOTEBATCH = 255 +if 'servertype' in CONFIG['puppet-forge-server']: + PFMETHOD = CONFIG['puppet-forge-server']['servertype'] +else: + PFMETHOD = 'puppet-forge-server' if 'hostname' in CONFIG['puppet-forge-server']: PFSERVER = CONFIG['puppet-forge-server']['hostname'] +if 'modulepath' in CONFIG['puppet-forge-server']: + PFMODPATH = CONFIG['puppet-forge-server']['modulepath'] # 'Global' Satellite 6 parameters # Satellite API diff --git a/push_puppetforge.py b/push_puppetforge.py index fcec883..5ce78b9 100644 --- a/push_puppetforge.py +++ b/push_puppetforge.py @@ -99,6 +99,39 @@ def copy_to_pfserver(export_dir, pfserver, pfmodpath, pfuser): os.system('rsync -avrzc ' + export_dir + '/* ' + target) +def splitext(path): + """ + Method to split the exported filename into author-module-version parts + """ + for ext in ['.tar.gz']: + if path.endswith(ext): + return path[:-len(ext)], path[-len(ext):] + return os.path.splitext(path) + + +def postModule(moduleTar, moduleInputDir, pfserver, pfmodpath) + """ + Function to push puppet modules using curl to Artifiactory repository + """ + # Remove module's extension (.tar.gz) + puppetModuleNameNoExt = splitext(moduleTar)[0] + + # Remove the path from the module + puppetModuleName = puppetModuleNameNoExt.split('/')[-1] + + # Split the module name into the required parts + puppetModuleNameList = puppetModuleName.split('-') + author = puppetModuleNameList[0] + moduleName = puppetModuleNameList[1] + version = puppetModuleNameList[2] + + url = "http://" + pfserver + pfmodpath + "/" + author + "/" + moduleName + "/" + moduleTar + fileName = moduleInputDir + "/" + moduleTar + + # Put the files using curl (need to clean this up) + subprocess.call(['curl', '-XPUT', url, '-T', fileName]) + + def main(args): """ Main Routine @@ -121,6 +154,7 @@ def main(args): parser.add_argument('-o', '--org', help='Organization (Uses default if not specified)', required=False) parser.add_argument('-r', '--repo', help='Puppetforge repo label', required=False) + parser.add_argument('-t', '--type', help='Puppetforge server type (puppet-forge-server|artifiactory)', required=False) parser.add_argument('-s', '--server', help='puppet-forge-server hostname', required=False) parser.add_argument('-m', '--modulepath', help='path to puppet-forge-server modules', required=False) @@ -134,6 +168,16 @@ def main(args): else: org_name = helpers.ORG_NAME + # Define the type of puppet-forge server + if args.type: + pftype = args.type + else: + if not helpers.PFMETHOD: + print "Puppet forge server type not specified" + sys.exit(1) + else: + pftype = helpers.PFMETHOD + # Define the puppet-forge-server hostname if args.server: pfserver = args.server @@ -148,7 +192,11 @@ def main(args): if args.modulepath: modpath = args.modulepath else: - modpath = '/opt/puppet-forge/modules' + if not.helpers.PFMODPATH: + print "Puppet forge module path not defined" + sys.exit(1) + else: + modpath = helpers.PFMODPATH # Set the username to use to push modules if args.user: @@ -206,10 +254,20 @@ def main(args): # Define the location of our exported data. export_dir = helpers.EXPORTDIR + "/puppetforge" - # Now we can copy the content to the puppet-forge-server instance - os.chdir(script_dir) - copy_to_pfserver(export_dir, pfserver, modpath, pfuser) + if (pftype == 'puppet-forge-server'): + # Method for posting to puppet-forge-server + os.chdir(script_dir) + copy_to_pfserver(export_dir, pfserver, modpath, pfuser) + + elif (pftype == 'artifactory'): + # Method for posting to Artifactory repository + for module in os.listdir(export_dir): + print("Posing: " + module) + postModule(module, export_dir, pfserver, modpath) + else: + print("Unknown puppet-forge server type defined") + sys.exit(1) # And we're done! print helpers.GREEN + "Puppet Forge export complete.\n" + helpers.ENDC