Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

from __future__ import absolute_import, division, print_function 

 

import os 

import subprocess 

import sys 

 

import yaml 

 

from appr.client import ApprClient 

from appr.pack import ApprPackage 

from appr.utils import mkdir_p, parse_package_name, parse_version_req 

 

DEFAULT_CHARTS = "appr_charts" 

 

 

class Helm(object): 

def __init__(self): 

pass 

 

def download_appr_deps(self, deps, dest=DEFAULT_CHARTS, tarball=False, requests_verify=True): 

""" 

Creates a directory 'dep_charts' to download and extract all dependencies 

fetched from the app-registry server. 

returns a helm dependency list 

""" 

mkdir_p(dest) 

helm_deps = {} 

for dep in deps: 

package_parts = parse_package_name(dep['name']) 

name = package_parts['package'] 

 

vparts = parse_version_req(dep['version']) 

client = ApprClient(package_parts['host'], requests_verify=requests_verify) 

package_name = '%s/%s' % (package_parts['namespace'], name) 

 

pullpack = client.pull_json(package_name, version_parts=vparts, media_type='helm') 

package = ApprPackage(pullpack['blob'], b64_encoded=True) 

release = pullpack['release'] 

packagepath = os.path.join(dest, package_parts['namespace']) 

print('Pulled package: %s (%s) \n -> %s' % (dep['name'], release, packagepath), 

file=sys.stderr) 

if tarball: 

with open('%s-%s.tgz' % (name, release), 'wb') as tarfile: 

tarfile.write(package.blob) 

package.extract(packagepath) 

 

helm_deps[name] = { 

'name': name, 

'version': release, 

'repository': 'file://%s/%s' % (packagepath, name) 

} 

return helm_deps 

 

def build_dep(self, dest=DEFAULT_CHARTS, overwrite=False): 

""" 

Reads the dependencies from the requirements.yaml, downloads the packages and updates 

the requirements.yaml. 

Returns status 

""" 

with open('requirements.yaml', 'rb') as requirefile: 

deps = yaml.safe_load(requirefile.read()) 

helm_deps = {} 

if 'appr' in deps and deps['appr']: 

helm_deps = self.download_appr_deps(deps['appr'], dest) 

dict_deps = {dep['name']: dep for dep in deps['dependencies']} 

dict_deps.update(helm_deps) 

deps['dependencies'] = dict_deps.values() 

requirement_output = yaml.safe_dump(deps, default_flow_style=False) 

if overwrite: 

with open('requirements.yaml', 'wb') as requirefile: 

requirefile.write(requirement_output) 

return 'Updated requirements.yaml' 

else: 

return requirement_output 

else: 

return "No appr-registries dependencies" 

 

def template(self, path, helm_opts=None): 

try: 

return self.action('template', path, helm_opts) 

except Exception as e: 

raise e 

 

def action(self, cmd, package_path, helm_opts=None): 

cmd = [cmd] 

if helm_opts: 

cmd = cmd + helm_opts 

cmd.append(package_path) 

return self._call(cmd) 

 

def _call(self, cmd): 

command = ['helm'] + cmd 

return subprocess.check_output(command, stderr=subprocess.STDOUT)