Skip to content

Commit

Permalink
Merge pull request #17 from ggatward/puppet-fixes
Browse files Browse the repository at this point in the history
Add artifactory push support
  • Loading branch information
ggatward authored Dec 8, 2017
2 parents 302970f + c74f377 commit 56f13c0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```

```
Expand All @@ -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
Expand All @@ -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
```


Expand Down
2 changes: 2 additions & 0 deletions config/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ cleanup:
keep: 3

puppet-forge-server:
servertype: puppet-forge-server
hostname: puppetforge.example.org
modulepath: /opt/puppet-forge/modules
6 changes: 6 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 62 additions & 4 deletions push_puppetforge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 56f13c0

Please sign in to comment.