Skip to content

Commit 4e4d3d5

Browse files
author
Janez Justin
committed
ADD option to set cache
1 parent 999b14a commit 4e4d3d5

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

rpm/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Here is a minimal requirement for the policy that is included in role:
5353
```
5454
{"Version": "2012-10-17",
5555
"Statement": [
56-
{"Sid": "<THIS IS UNIQE>",
56+
{"Sid": "<THIS IS UNIQUE>",
5757
"Action": [
5858
"s3:GetObject",
5959
"s3:PutObject",
@@ -72,6 +72,7 @@ These are the environmental variables you will have to set:
7272
| GPG_PASS | GPG key password |
7373
| BUCKET_NAME | Bucket Name |
7474
| REPO_DIR | Directory |
75+
| CACHE | Directory |
7576

7677
**PUBLIC** Set to True for the outputs to be publicly readable
7778

@@ -83,6 +84,8 @@ These are the environmental variables you will have to set:
8384

8485
**REPO_DIR** Path to repositroy from bucket root. If none is set, it is assumed root of repository is root of the bucket
8586

87+
**CACHE** Path to cache folder from bucket root (e.g. repo/cache)
88+
8689
### Set up lambda with CLI
8790

8891
[Install aws cli](http://docs.aws.amazon.com/cli/latest/userguide/installing.html)
@@ -96,7 +99,7 @@ aws lambda create-function \
9699
--handler s3rpm.lambda_handler \
97100
--runtime python3.6 \
98101
# Replace '<...>' with environmental variables
99-
--environment Variables='{PUBLIC=<bool>, GPG_KEY=<file>, GPG_PASS=<password>, BUCKET_NAME=<bucket name>, REPO_DIR=<dir>}'
102+
--environment Variables='{PUBLIC=<bool>, GPG_KEY=<file>, GPG_PASS=<password>, BUCKET_NAME=<bucket name>, REPO_DIR=<dir>, CACHE=<dir>}'
100103
```
101104

102105
### Set up lambda manually

rpm/s3rpm.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def lambda_handler(event, context):
1616
prefix = '/'.join(key.split('/')[0:-1])+'/'
1717

1818
s3_repo_dir = os.environ['REPO_DIR'].strip('/')
19-
19+
2020
#make sure we are working with correct files
2121
if bucket == os.environ['BUCKET_NAME'] and key.endswith(".rpm") and prefix.startswith(s3_repo_dir):
2222
#check if repodata already exist, if not create new with key file
@@ -39,7 +39,7 @@ def lambda_handler(event, context):
3939
repo, cache = check_changed_files(repo, s3_repo_dir)
4040
#save cache to bucket
4141
s3 = boto3.resource('s3')
42-
f_index_obj = s3.Object(bucket_name=os.environ['BUCKET_NAME'], key=s3_repo_dir+'/repo_cache')
42+
f_index_obj = s3.Object(bucket_name=os.environ['BUCKET_NAME'], key=os.environ['CACHE']+'/repo_cache')
4343
print("Writing file: %s" % (str(f_index_obj)))
4444
f_index_obj.put(Body=str(json.dumps(cache)))
4545

@@ -106,10 +106,10 @@ def get_cache(repo, s3_repo_dir):
106106
"""
107107
Check for cache file
108108
"""
109-
if check_bucket_file_existance(s3_repo_dir+'/repo_cache'):
110-
print('Repodata cache (%s) found, attempting to write to it' %(s3_repo_dir+'/repo_cache'))
109+
if check_bucket_file_existance(os.environ['CACHE']+'/repo_cache'):
110+
print('Repodata cache (%s) found, attempting to write to it' %(os.environ['CACHE']+'/repo_cache'))
111111
s3 = boto3.client('s3')
112-
s3.download_file(os.environ['BUCKET_NAME'], s3_repo_dir+'/repo_cache', repo.repodir + 'repo_cache')
112+
s3.download_file(os.environ['BUCKET_NAME'], os.environ['CACHE']+'/repo_cache', repo.repodir + 'repo_cache')
113113
with open(repo.repodir + 'repo_cache', 'r') as f:
114114
cache = json.loads(f.read(-1))
115115
else:
@@ -128,7 +128,7 @@ def remove_overwritten_file_from_cache(cache, newfile, s3_repo_dir, repo):
128128

129129
# save cache in case new event occurs
130130
s3 = boto3.resource('s3')
131-
f_index_obj = s3.Object(bucket_name=os.environ['BUCKET_NAME'], key=s3_repo_dir+'/repo_cache')
131+
f_index_obj = s3.Object(bucket_name=os.environ['BUCKET_NAME'], key=os.environ['CACHE']+'/repo_cache')
132132
f_index_obj.put(Body=str(json.dumps(cache)))
133133

134134
repo.remove_package(pkg_id)

rpm/s3rpm_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def setUp(self):
1919
os.environ['GPG_KEY'] = ''
2020
os.environ['PUBLIC'] = 'True'
2121
os.environ['GPG_PASS']='123'
22+
os.environ['CACHE']='test_s3rpm'
2223

2324
def tearDown(self):
2425
if os.path.exists('test_s3rpm'):
@@ -137,6 +138,7 @@ def setUp(self):
137138
os.environ['GPG_KEY'] = ''
138139
os.environ['PUBLIC'] = 'True'
139140
os.environ['GPG_PASS']='123'
141+
os.environ['CACHE']='test_s3rpm'
140142
self.m = mock_open(read_data='')
141143

142144
def tearDown(self):

0 commit comments

Comments
 (0)