forked from mike-scott/core-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to publish images with dockerapps
This allows an image to include references to a docker app. Details on docker apps can be found here: advancedtelematic/aktualizr#1189 Signed-off-by: Andy Doan <[email protected]>
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/usr/bin/python3 | ||
# | ||
# Copyright (c) 2019 Foundries.io | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
import argparse | ||
import json | ||
import os | ||
import sys | ||
|
||
from zipfile import ZipFile | ||
|
||
import requests | ||
|
||
|
||
def get_token(oauth_server, client, secret): | ||
data = {'grant_type': 'client_credentials'} | ||
url = oauth_server | ||
if url[-1] != '/': | ||
url += '/' | ||
url += 'token' | ||
r = requests.post(url, data=data, auth=(client, secret)) | ||
r.raise_for_status() | ||
return r.json()['access_token'] | ||
|
||
|
||
def publish(args): | ||
with ZipFile(args.credentials) as zf: | ||
with zf.open('tufrepo.url') as f: | ||
repourl = f.read().strip().decode() | ||
|
||
with zf.open('treehub.json') as f: | ||
oauth2 = json.load(f)['oauth2'] | ||
|
||
if repourl[-1] != '/': | ||
repourl += '/' | ||
|
||
token = get_token( | ||
oauth2['server'], oauth2['client_id'], oauth2['client_secret']) | ||
|
||
app = os.path.basename(args.dockerapp.name).replace('.dockerapp', '') | ||
url = repourl + 'api/v1/user_repo/targets/' + app + '-' + args.version | ||
r = requests.put(url, | ||
params={'name': app, 'version': args.version}, | ||
files={'file': ('filename', args.dockerapp)}, | ||
headers={'Authorization: Bearer ' + token}) | ||
if r.status_code != 201: | ||
sys.exit('Unable to create %s: HTTP_%d\n%s' % ( | ||
url, r.status_code, r.text)) | ||
|
||
|
||
def merge(args): | ||
with open(args.targets_json) as f: | ||
targets = json.load(f)['targets'] | ||
|
||
app = os.path.basename(args.dockerapp.replace('.dockerapp', '')) | ||
name = app + '-' + args.version | ||
assert name in targets, '%s not in targets' % name | ||
|
||
img = targets[args.machine + '-' + args.ostree_branch + '-' + args.version] | ||
img['custom']['docker_apps'] = {app: {'filename': name}} | ||
|
||
with open(args.targets_json, 'w') as f: | ||
json.dump(targets, f, indent=2) | ||
|
||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser( | ||
'Manage dockerapps on the OTA Connect reposerver') | ||
|
||
cmds = parser.add_subparsers(title='Commands') | ||
|
||
p = cmds.add_parser('publish') | ||
p.set_defaults(func=publish) | ||
p.add_argument('dockerapp', type=argparse.FileType('r')) | ||
p.add_argument('credentials', type=argparse.FileType('rb')) | ||
p.add_argument('version') | ||
|
||
p = cmds.add_parser('merge') | ||
p.set_defaults(func=merge) | ||
p.add_argument('targets_json') | ||
p.add_argument('dockerapp') | ||
p.add_argument('version') | ||
p.add_argument('machine') | ||
p.add_argument('ostree_branch') | ||
|
||
return parser.parse_args() | ||
|
||
|
||
if __name__ == '__main__': | ||
args = get_args() | ||
if getattr(args, 'func', None): | ||
args.func(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters