Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_check_hokusai_required_version(self):
self.assertTrue(self.config._check_required_version(None, '0.0.1'))
with self.assertRaises(HokusaiError):
self.config._check_required_version('0.0.1', '0.0.1')
with self.assertRaises(HokusaiError):
self.config._check_required_version('someinvalidversionspecifier', '0.0.1')
self.assertTrue(self.config._check_required_version('~=0.1', '0.1.1'))
self.assertTrue(self.config._check_required_version('~=0.1', '0.2'))
self.assertFalse(self.config._check_required_version('~=0.1', '1.0'))
self.assertTrue(self.config._check_required_version('==0.0.1', '0.0.1'))
self.assertFalse(self.config._check_required_version('==0.0.1', '0.0.2'))
self.assertFalse(self.config._check_required_version('==1.0', '0.2'))
self.assertTrue(self.config._check_required_version('<0.0.2.post2', '0.0.2.post1'))
self.assertTrue(self.config._check_required_version('<1.1', '1.0.2'))
self.assertFalse(self.config._check_required_version('<1.1', '2.0.2'))
self.assertFalse(self.config._check_required_version('<=0.0.2', '2.2'))
self.assertTrue(self.config._check_required_version('<=2.2', '2.2'))
self.assertTrue(self.config._check_required_version('>0.0.1', '1.2.3'))
self.assertTrue(self.config._check_required_version('>0.0.1.post1', '0.0.1.post3'))
self.assertTrue(self.config._check_required_version('>=0.0.1', '1.2.3'))
self.assertTrue(self.config._check_required_version('>=1.2.3', '1.2.3'))
def wrapper(*args, **kwargs):
try:
if config_check:
config.check()
result = func(*args, **kwargs)
if result is None:
sys.exit(0)
else:
sys.exit(result)
except HokusaiError as e:
print_red(e.message)
sys.exit(e.return_code)
except SystemExit:
raise
except KeyboardInterrupt:
raise
except (CalledProcessError, Exception) as e:
if get_verbosity() or os.environ.get('DEBUG') is '1':
print_red(traceback.format_exc(e))
else:
print_red("ERROR: %s" % str(e))
sys.exit(1)
return wrapper
def run(self, image_tag, cmd, tty=False, env=(), constraint=()):
if not self.ecr.project_repo_exists():
raise HokusaiError("Project repo does not exist. Aborting.")
if os.environ.get('USER') is not None:
uuid = "%s-%s" % (os.environ.get('USER'), k8s_uuid())
else:
uuid = k8s_uuid()
name = "%s-hokusai-run-%s" % (config.project_name, uuid)
image_name = "%s:%s" % (self.ecr.project_repo, image_tag)
container = {
"args": cmd.split(' '),
"name": name,
"image": image_name,
"imagePullPolicy": "Always",
'envFrom': [{'configMapRef': {'name': "%s-environment" % config.project_name}}]
}
def _env_value_for(self, key, _type):
env_var = HOKUSAI_ENV_VAR_PREFIX + key.upper().replace('-', '_')
val = os.environ.get(env_var)
if val is None:
return val
if _type == list:
try:
return _type(val.split(','))
except ValueError:
raise HokusaiError("Environment variable %s could not be split to %s" % (env_var, _type))
try:
return _type(val)
except ValueError:
raise HokusaiError("Environment variable %s could not be cast to %s" % (env_var, _type))
def load_template(self):
try:
env = Environment(loader=FileSystemLoader(os.path.split(self.template_path)[0]), undefined=StrictUndefined)
return env.get_template(os.path.split(self.template_path)[1])
except IOError:
raise HokusaiError("Template not found.")
def gitdiff():
ecr = ECR()
staging_tag = ecr.find_git_sha1_image_tag('staging')
if staging_tag is None:
raise HokusaiError("Could not find a tag for staging. Aborting.")
production_tag = ecr.find_git_sha1_image_tag('production')
if production_tag is None:
raise HokusaiError("Could not find a git SHA1 tag for production. Aborting.")
print_green("Comparing %s to %s" % (production_tag, staging_tag))
for remote in shout('git remote').splitlines():
shout("git fetch %s" % remote)
shout("git diff %s %s" % (production_tag, staging_tag), print_output=True)
ecr = ECR()
deploy_from = Deployment('staging')
tag = deploy_from.current_tag
if tag is None:
raise HokusaiError("Could not find a tag for staging. Aborting.")
tag = ecr.find_git_sha1_image_tag(tag)
if tag is None:
print_red("Could not find a git SHA1 for tag %s. Aborting." % tag)
return 1
if migration is not None:
print_green("Running migration '%s' on production..." % migration, newline_after=True)
return_code = CommandRunner('production').run(tag, migration, constraint=constraint, tty=False)
if return_code:
raise HokusaiError("Migration failed with return code %s" % return_code, return_code=return_code)
deploy_to = Deployment('production').update(tag, constraint, git_remote, timeout)
print_green("Promoted staging to production at %s" % tag)
def update(self, tag, constraint, git_remote, timeout, update_config=False, filename=None):
if not self.ecr.project_repo_exists():
raise HokusaiError("Project repo does not exist. Aborting.")
digest = self.ecr.image_digest_for_tag(tag)
if digest is None:
raise HokusaiError("Could not find an image digest for tag %s. Aborting." % tag)
if self.namespace is None:
print_green("Deploying %s to %s..." % (digest, self.context), newline_after=True)
else:
print_green("Deploying %s to %s/%s..." % (digest, self.context, self.namespace), newline_after=True)
"""
This logic should be refactored, but essentially if namespace and filename are provided, the caller is
a review app, while if namespace is None it is either staging or production. If filename is unset for staging
or production it is targeting the 'canonical' app, i.e. staging.yml or production.yml while if it is set it is
trageting a 'canary' app.
def create_new_app_yaml(source_file, app_name):
with open(source_file, 'r') as stream:
try:
yaml_content = list(yaml.load_all(stream))
except yaml.YAMLError as exc:
raise HokusaiError("Cannot read source yaml file %s." % source_file)
for c in yaml_content: update_namespace(c, clean_string(app_name))
new_namespace = OrderedDict([
('apiVersion', 'v1'),
('kind', 'Namespace'),
('metadata', {
'name': clean_string(app_name)
})
])
yaml_content = [new_namespace] + yaml_content
with open(os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name), 'w') as output:
output.write(YAML_HEADER)
yaml.safe_dump_all(yaml_content, output, default_flow_style=False)
def push(tag, local_tag, build, filename, force, overwrite, skip_latest=False):
if force is None and shout('git status --porcelain'):
raise HokusaiError("Working directory is not clean. Aborting.")
if force is None and shout('git status --porcelain --ignored'):
raise HokusaiError("Working directory contains ignored files and/or directories. Aborting.")
ecr = ECR()
if not ecr.project_repo_exists():
raise HokusaiError("ECR repo %s does not exist... did you run `hokusai setup` for this project?" % config.project_name)
shout(ecr.get_login(), mask=(r'^(docker login -u) .+ (-p) .+ (.+)$', r'\1 ****** \2 ***** \3'))
if tag is None:
tag = shout('git rev-parse HEAD').strip()
if overwrite is None and ecr.tag_exists(tag):
raise HokusaiError("Tag %s already exists in registry. Aborting." % tag)
if build:
Docker().build(filename)
build_tag = "hokusai_%s:%s" % (config.project_name, local_tag)