This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuller.py
91 lines (75 loc) · 3.26 KB
/
puller.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
## This is deprecated.
import os
import time
import subprocess as sp
from pathlib import Path
class Puller(object):
""" Instances of this class wrap the project: https://github.com/knrc/registry-puller
registry-puller:
a webhook which monitors the creation of ServiceAccounts within kubernetes namespaces
and modifies those ServiceAccounts so they include an additional image pull secret.
This is of use when trying to deploy containers referencing private repos.
Parameter:
- `secret_file`: an image pull secret file from a private registry.
"""
def __init__(self, secret_file=None):
if not Path(secret_file).is_file():
raise ValueError('Missing a secret file')
self.secret_file = secret_file
self.repo = 'https://github.com/knrc/registry-puller.git'
self.savedPath = os.getcwd()
def build(self):
try:
os.chdir('registry-puller')
sp.run(['git', 'pull'])
except OSError:
os.chdir(self.savedPath)
sp.run(['git', 'clone', self.repo])
os.chdir('registry-puller')
except WindowsError:
os.chdir(self.savedPath)
sp.run(['git', 'clone', self.repo])
os.chdir('registry-puller')
sp.run(['make'])
os.chdir(self.savedPath)
def execute(self):
# check environment variable KUBECONFIG
try:
os.environ['KUBECONFIG']
except KeyError:
raise KeyError('Missing environment variable KUBECONFIG')
# check if oc is installed
proc = sp.run(['oc', 'version'])
if proc.returncode != 0:
raise RuntimeError('Missing oc client')
sp.run(['oc', 'new-project', 'registry-puller'])
sp.run(['oc', 'create', 'configmap', '-n', 'registry-puller', 'registry-secret', '--from-file=' + self.secret_file])
os.chdir(self.savedPath)
proc = sp.run(['oc', 'create', '-f', 'registry-puller/registry-puller-4.0.yaml'], stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
print(proc.stdout)
print(proc.stderr)
timeout = time.time() + 60 * 5
while time.time() < timeout:
proc = sp.run(['oc', 'get', 'pod', '-n', 'registry-puller'], stdout=sp.PIPE, universal_newlines=True)
if 'Running' in proc.stdout:
break
proc = sp.run(['oc', 'get', 'pod', '-n', 'registry-puller'], stdout=sp.PIPE, universal_newlines=True)
if 'Running' in proc.stdout:
print('registry-puller pod is running')
else:
print('Error: registry-puller is not running\n' + proc.stdout)