Saturday, April 23, 2016

Creating a yum repository using AWS S3 buckets

Here's a short bash script I use to create a yum repo in an S3 bucket.  The script does five (5) things:

  1. creates a local repo in a temporary directory
  2. copies an RPM file to the local repo
  3. creates the yum repository using createrepo
  4. syncs the local directory with the S3 bucket
  5. sets public permissions for the bucket (make sure this is what you actually want to do)
Before you get started create the bucket and configure it to host a static website.  No worries if you don't actually have an index.html.

Create the bucket:

$ aws s3 mb s3://openbedrock-repo

Then configure it to host static files:

$ aws s3 website s3://openbedrock-repo --index-document index.html

Here's the bash script to create the repo:

1:  #!/bin/bash  
3:   # $ sudo $0 path-to-rpm [bucket]  
5:   # create a temporary repo  
6:   repo=$(mktemp -d)  
7:   mkdir ${repo}/noarch  
8:   if test -n "$2"; then  
9:    BUCKET="$2"  
10:   else  
11:    BUCKET=openbedrock-repo  
12:   fi  
14:   # create a temporary local repo and sync with AWS S3 repo  
15:   if test -e "$1"; then  
16:     cp "$1" ${repo}/noarch  
17:     createrepo $repo  
18:   # sync local repo with S3 bucket, make it PUBLIC  
19:     PERMISSION="--grants read=uri="  
20:     aws s3 sync --recursive --include="*" ${repo} s3://$BUCKET/ $PERMISSION  
21:     aws s3 ls s3://$BUCKET/  
22:   # cleanup local copy of repo  
23:     rm -rf $repo  
24:   fi  

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.