def invoke(self): try: doc_data = ReferenceResolver.resolve_reference(self.locations) documents = list() for d in doc_data: documents.extend(list(yaml.safe_load_all(d.decode()))) except InvalidPathException as ex: self.logger.error(str(ex)) return except yaml.YAMLError as yex: self.logger.error("Invalid YAML found: %s" % str(yex)) return if not self.ctx.obj.get('api', False): with Tiller(tiller_host=self.tiller_host, tiller_port=self.tiller_port, tiller_namespace=self.tiller_namespace, dry_run=self.dry_run) as tiller: armada = Armada(documents, disable_update_pre=self.disable_update_pre, disable_update_post=self.disable_update_post, enable_chart_cleanup=self.enable_chart_cleanup, dry_run=self.dry_run, set_ovr=self.set, force_wait=self.wait, timeout=self.timeout, tiller=tiller, values=self.values, target_manifest=self.target_manifest) resp = armada.sync() self.output(resp) else: if len(self.values) > 0: self.logger.error( "Cannot specify local values files when using the API.") return query = { 'disable_update_post': self.disable_update_post, 'disable_update_pre': self.disable_update_pre, 'dry_run': self.dry_run, 'enable_chart_cleanup': self.enable_chart_cleanup, 'tiller_host': self.tiller_host, 'tiller_port': self.tiller_port, 'tiller_namespace': self.tiller_namespace, 'timeout': self.timeout, 'wait': self.wait } client = self.ctx.obj.get('CLIENT') if self.use_doc_ref: resp = client.post_apply(manifest_ref=self.locations, set=self.set, query=query) else: resp = client.post_apply(manifest=documents, set=self.set, query=query) self.output(resp.get('message'))
def on_post(self, req, resp): try: # Load data from request and get options data = self.req_json(req) opts = {} # opts = data['options'] # Encode filename # data['file'] = data['file'].encode('utf-8') armada = Armada( data, disable_update_pre=opts.get('disable_update_pre', False), disable_update_post=opts.get('disable_update_post', False), enable_chart_cleanup=opts.get('enable_chart_cleanup', False), dry_run=opts.get('dry_run', False), wait=opts.get('wait', False), timeout=opts.get('timeout', False)) msg = armada.sync() resp.data = json.dumps({'message': msg}) resp.content_type = 'application/json' resp.status = falcon.HTTP_200 except Exception as e: self.error(req.context, "Failed to apply manifest") self.return_error( resp, falcon.HTTP_500, message="Failed to install manifest: {} {}".format(e, data))
def invoke(self): if not self.ctx.obj.get('api', False): with open(self.filename) as f: armada = Armada(list(yaml.safe_load_all(f.read())), self.disable_update_pre, self.disable_update_post, self.enable_chart_cleanup, self.dry_run, self.set, self.wait, self.timeout, self.tiller_host, self.tiller_port, self.values) resp = armada.sync() self.output(resp) else: query = { 'disable_update_post': self.disable_update_post, 'disable_update_pre': self.disable_update_pre, 'dry_run': self.dry_run, 'enable_chart_cleanup': self.enable_chart_cleanup, 'tiller_host': self.tiller_host, 'tiller_port': self.tiller_port, 'timeout': self.timeout, 'wait': self.wait } client = self.ctx.obj.get('CLIENT') with open(self.filename, 'r') as f: resp = client.post_apply(manifest=f.read(), values=self.values, set=self.set, query=query) self.output(resp.get('message'))
def applyCharts(args): armada = Armada( open(args.file).read(), args.disable_update_pre, args.disable_update_post, args.enable_chart_cleanup, args.dry_run, args.wait, args.timeout, args.tiller_host, args.tiller_port, args.debug_logging) armada.sync()
def handle(self, req, documents, helm): armada = Armada( documents, disable_update_pre=req.get_param_as_bool('disable_update_pre'), disable_update_post=req.get_param_as_bool('disable_update_post'), enable_chart_cleanup=req.get_param_as_bool('enable_chart_cleanup'), force_wait=req.get_param_as_bool('wait'), timeout=req.get_param_as_int('timeout'), helm=helm, target_manifest=req.get_param('target_manifest')) return armada.sync()
def applyCharts(args): armada = Armada(open(args.file).read(), args.disable_update_pre, args.disable_update_post, args.enable_chart_cleanup, args.skip_pre_flight, args.dry_run, args.wait, args.timeout, args.debug_logging) armada.sync()
def handle(self, documents, helm): armada = Armada(documents, disable_update_pre=self.disable_update_pre, disable_update_post=self.disable_update_post, enable_chart_cleanup=self.enable_chart_cleanup, set_ovr=self.set, force_wait=self.wait, timeout=self.timeout, helm=helm, values=self.values, target_manifest=self.target_manifest) return armada.sync()
def test_pre_flight_ops(self, mock_tiller, mock_lint, mock_git): '''Test pre-flight checks and operations''' armada = Armada('') armada.tiller = mock_tiller armada.documents = yaml.safe_load_all(self.test_yaml) armada.config = Manifest(armada.documents).get_manifest() CHART_SOURCES = [('git://github.com/dummy/armada', 'chart_1'), ('/tmp/dummy/armada', 'chart_2')] # mock methods called by pre_flight_ops() mock_tiller.tiller_status.return_value = True mock_lint.valid_manifest.return_value = True mock_git.git_clone.return_value = CHART_SOURCES[0][0] armada.pre_flight_ops() mock_git.git_clone.assert_called_once_with(CHART_SOURCES[0][0], 'master') for group in armada.config.get('armada').get('charts'): for counter, chart in enumerate(group.get('chart_group')): self.assertEqual( chart.get('chart').get('source_dir')[0], CHART_SOURCES[counter][0]) self.assertEqual( chart.get('chart').get('source_dir')[1], CHART_SOURCES[counter][1])
def on_post(self, req, resp): try: # Load data from request and get options data = list(self.req_yaml(req)) if type(data[0]) is list: data = list(data[0]) opts = req.params # Encode filename armada = Armada( data, disable_update_pre=req.get_param_as_bool('disable_update_pre'), disable_update_post=req.get_param_as_bool( 'disable_update_post'), enable_chart_cleanup=req.get_param_as_bool( 'enable_chart_cleanup'), dry_run=req.get_param_as_bool('dry_run'), wait=req.get_param_as_bool('wait'), timeout=int(opts.get('timeout', 3600)), tiller_host=opts.get('tiller_host', None), tiller_port=int(opts.get('tiller_port', 44134)), ) msg = armada.sync() resp.body = json.dumps({ 'message': msg, }) resp.content_type = 'application/json' resp.status = falcon.HTTP_200 except Exception as e: err_message = 'Failed to apply manifest: {}'.format(e) self.error(req.context, err_message) self.return_error(resp, falcon.HTTP_500, message=err_message)
def on_post(self, req, resp): # Load data from request and get options data = json.load(req.stream) opts = data['options'] # Encode filename data['file'] = data['file'].encode('utf-8') armada = Handler(open('../../' + data['file']), disable_update_pre=opts['disable_update_pre'], disable_update_post=opts['disable_update_post'], enable_chart_cleanup=opts['enable_chart_cleanup'], dry_run=opts['dry_run'], wait=opts['wait'], timeout=opts['timeout']) armada.sync() resp.data = json.dumps({'message': 'Success'}) resp.content_type = 'application/json' resp.status = HTTP_200
def test_install(self, mock_tiller, mock_chartbuilder): '''Test install functionality from the sync() method''' # instantiate Armada and Tiller objects armada = Armada('', skip_pre_flight=True, wait=True, timeout=None) armada.tiller = mock_tiller armada.config = yaml.load(self.test_yaml) charts = armada.config['armada']['charts'][0]['chart_group'] chart_1 = charts[0]['chart'] chart_2 = charts[1]['chart'] # mock irrelevant methods called by armada.sync() mock_tiller.list_charts.return_value = [] mock_chartbuilder.source_clone.return_value = None mock_chartbuilder.get_helm_chart.return_value = None mock_chartbuilder.source_cleanup.return_value = None armada.sync() # check params that should be passed to tiller.install_release() method_calls = [mock.call(mock_chartbuilder().get_helm_chart(), armada.dry_run, chart_1['release_name'], chart_1['namespace'], armada.config['armada']['release_prefix'], values=yaml.safe_dump(chart_1['values']), wait=armada.wait, timeout=chart_1['timeout']), mock.call(mock_chartbuilder().get_helm_chart(), armada.dry_run, chart_2['release_name'], chart_2['namespace'], armada.config['armada']['release_prefix'], values=yaml.safe_dump(chart_2['values']), wait=armada.wait, timeout=chart_2['timeout'])] mock_tiller.install_release.assert_has_calls(method_calls)
def on_post(self, req, resp): try: # Load data from request and get options data = list(self.req_yaml(req)) if type(data[0]) is list: data = list(data[0]) opts = {} # Encode filename armada = Armada( data, disable_update_pre=opts.get('disable_update_pre', False), disable_update_post=opts.get('disable_update_post', False), enable_chart_cleanup=opts.get('enable_chart_cleanup', False), dry_run=opts.get('dry_run', False), wait=opts.get('wait', False), timeout=opts.get('timeout', False), tiller_host=opts.get('tiller_host', None), tiller_port=opts.get('tiller_port', 44134), ) msg = armada.sync() resp.data = json.dumps({ 'message': msg, }) resp.content_type = 'application/json' resp.status = falcon.HTTP_200 except Exception as e: self.error(req.context, "Failed to apply manifest") self.return_error(resp, falcon.HTTP_500, message="DATA {} \n Messge {}".format(data, e))
def test_post_flight_ops(self, mock_tiller, mock_lint, mock_git): '''Test post-flight operations''' armada = Armada('') armada.tiller = mock_tiller armada.config = yaml.load(self.test_yaml) CHART_SOURCES = [('git://github.com/dummy/armada', 'chart_1'), ('/tmp/dummy/armada', 'chart_2')] # mock methods called by pre_flight_ops() mock_tiller.tiller_status.return_value = True mock_lint.valid_manifest.return_value = True mock_git.git_clone.return_value = CHART_SOURCES[0][0] armada.pre_flight_ops() armada.post_flight_ops() for group in yaml.load(self.test_yaml).get('armada').get('charts'): for counter, chart in enumerate(group.get('chart_group')): if chart.get('chart').get('source').get('type') == 'git': mock_git.source_cleanup \ .assert_called_with(CHART_SOURCES[counter][0])
def test_install(self, mock_tiller, mock_chartbuilder, mock_pre_flight, mock_post_flight): '''Test install functionality from the sync() method''' # instantiate Armada and Tiller objects armada = Armada('', wait=True, timeout=1000) armada.tiller = mock_tiller tmp_doc = yaml.safe_load_all(self.test_yaml) armada.config = Manifest(tmp_doc).get_manifest() charts = armada.config['armada']['charts'][0]['chart_group'] chart_1 = charts[0]['chart'] chart_2 = charts[1]['chart'] # mock irrelevant methods called by armada.sync() mock_tiller.list_charts.return_value = [] mock_chartbuilder.get_source_path.return_value = None mock_chartbuilder.get_helm_chart.return_value = None armada.sync() # check params that should be passed to tiller.install_release() method_calls = [ mock.call(mock_chartbuilder().get_helm_chart(), "{}-{}".format(armada.config['armada']['release_prefix'], chart_1['release_name']), chart_1['namespace'], dry_run=armada.dry_run, values=yaml.safe_dump(chart_1['values']), wait=armada.wait, timeout=1000), mock.call(mock_chartbuilder().get_helm_chart(), "{}-{}".format(armada.config['armada']['release_prefix'], chart_2['release_name']), chart_2['namespace'], dry_run=armada.dry_run, values=yaml.safe_dump(chart_2['values']), wait=armada.wait, timeout=1000) ] mock_tiller.install_release.assert_has_calls(method_calls)
def test_install(self, mock_tiller, mock_chartbuilder): '''Test install functionality from the sync() method''' # instantiate Armada and Tiller objects armada = Armada('', skip_pre_flight=True, wait=True, timeout=None) armada.tiller = mock_tiller armada.config = yaml.load(self.test_yaml) charts = armada.config['armada']['charts'][0]['chart_group'] chart_1 = charts[0]['chart'] chart_2 = charts[1]['chart'] # mock irrelevant methods called by armada.sync() mock_tiller.list_charts.return_value = [] mock_chartbuilder.source_clone.return_value = None mock_chartbuilder.get_helm_chart.return_value = None mock_chartbuilder.source_cleanup.return_value = None armada.sync() # check params that should be passed to tiller.install_release() method_calls = [ mock.call(mock_chartbuilder().get_helm_chart(), armada.dry_run, chart_1['release_name'], chart_1['namespace'], armada.config['armada']['release_prefix'], values=yaml.safe_dump(chart_1['values']), wait=armada.wait, timeout=chart_1['timeout']), mock.call(mock_chartbuilder().get_helm_chart(), armada.dry_run, chart_2['release_name'], chart_2['namespace'], armada.config['armada']['release_prefix'], values=yaml.safe_dump(chart_2['values']), wait=armada.wait, timeout=chart_2['timeout']) ] mock_tiller.install_release.assert_has_calls(method_calls)
def on_post(self, req, resp): # Load data from request and get options if req.content_type == 'application/x-yaml': data = list(self.req_yaml(req)) if type(data[0]) is list: documents = list(data[0]) else: documents = data elif req.content_type == 'application/json': self.logger.debug("Applying manifest based on reference.") req_body = self.req_json(req) doc_ref = req_body.get('hrefs', None) if not doc_ref: self.logger.info("Request did not contain 'hrefs'.") resp.status = falcon.HTTP_400 return data = ReferenceResolver.resolve_reference(doc_ref) documents = list() for d in data: documents.extend(list(yaml.safe_load_all(d.decode()))) if req_body.get('overrides', None): overrides = Override( documents, overrides=req_body.get('overrides')) documents = overrides.update_manifests() else: self.error(req.context, "Unknown content-type %s" % req.content_type) # TODO(fmontei): Use falcon.<Relevant API Exception Class> instead. return self.return_error( resp, falcon.HTTP_415, message="Request must be in application/x-yaml" "or application/json") try: armada = Armada( documents, disable_update_pre=req.get_param_as_bool('disable_update_pre'), disable_update_post=req.get_param_as_bool( 'disable_update_post'), enable_chart_cleanup=req.get_param_as_bool( 'enable_chart_cleanup'), dry_run=req.get_param_as_bool('dry_run'), force_wait=req.get_param_as_bool('wait'), timeout=req.get_param_as_int('timeout') or 0, tiller_host=req.get_param('tiller_host'), tiller_port=req.get_param_as_int('tiller_port') or CONF.tiller_port, tiller_namespace=req.get_param( 'tiller_namespace', default=CONF.tiller_namespace), target_manifest=req.get_param('target_manifest')) msg = armada.sync() resp.body = json.dumps({ 'message': msg, }) resp.content_type = 'application/json' resp.status = falcon.HTTP_200 except exceptions.ManifestException as e: self.return_error(resp, falcon.HTTP_400, message=str(e)) except Exception as e: self.logger.exception('Caught unexpected exception') err_message = 'Failed to apply manifest: {}'.format(e) self.error(req.context, err_message) self.return_error(resp, falcon.HTTP_500, message=err_message)
def on_post(self, req, resp): try: # Load data from request and get options if req.content_type == 'application/x-yaml': data = list(self.req_yaml(req)) if type(data[0]) is list: documents = list(data[0]) else: documents = data elif req.content_type == 'application/json': self.logger.debug("Applying manifest based on reference.") req_body = self.req_json(req) doc_ref = req_body.get('hrefs', None) if not doc_ref: self.logger.info("Request did not contain 'hrefs'.") resp.status = falcon.HTTP_400 return data = ReferenceResolver.resolve_reference(doc_ref) documents = list() for d in data: documents.extend(list(yaml.safe_load_all(d.decode()))) if req_body.get('overrides', None): overrides = Override(documents, overrides=req_body.get('overrides')) documents = overrides.update_manifests() else: self.error(req.context, "Unknown content-type %s" % req.content_type) self.return_error( resp, falcon.HTTP_415, message="Request must be in application/x-yaml" "or application/json") opts = req.params # Encode filename armada = Armada( documents, disable_update_pre=req.get_param_as_bool('disable_update_pre'), disable_update_post=req.get_param_as_bool( 'disable_update_post'), enable_chart_cleanup=req.get_param_as_bool( 'enable_chart_cleanup'), dry_run=req.get_param_as_bool('dry_run'), wait=req.get_param_as_bool('wait'), timeout=int(opts.get('timeout', 3600)), tiller_host=opts.get('tiller_host', None), tiller_port=int(opts.get('tiller_port', 44134)), ) msg = armada.sync() resp.body = json.dumps({ 'message': msg, }) resp.content_type = 'application/json' resp.status = falcon.HTTP_200 except Exception as e: err_message = 'Failed to apply manifest: {}'.format(e) self.error(req.context, err_message) self.return_error(resp, falcon.HTTP_500, message=err_message)