def setup(project_name, template_remote, template_dir, template_vars, allow_missing_vars): mkpath(os.path.join(CWD, HOKUSAI_CONFIG_DIR)) config.create(clean_string(project_name)) ecr = ECR() if ecr.project_repo_exists(): print_green("Project repo %s already exists. Skipping create." % ecr.project_repo) else: ecr.create_project_repo() print_green("Created project repo %s" % ecr.project_repo) scratch_dir = None if template_remote: scratch_dir = tempfile.mkdtemp() git_repo_and_branch = template_remote.split('#', 1) git_repo = git_repo_and_branch[0] if len(git_repo_and_branch) == 2: git_branch = git_repo_and_branch[1] else: git_branch = "master" shout("git clone -b %s --single-branch %s %s" % (git_branch, git_repo, scratch_dir)) custom_template_dir = None if allow_missing_vars: environment_kwargs = {} else: environment_kwargs = {"undefined": StrictUndefined} if scratch_dir and template_dir: custom_template_dir = os.path.join(scratch_dir, os.path.basename(template_dir)) env = Environment(loader=FileSystemLoader( os.path.join(scratch_dir, os.path.basename(template_dir))), **environment_kwargs) elif scratch_dir: custom_template_dir = scratch_dir env = Environment(loader=FileSystemLoader(scratch_dir), **environment_kwargs) elif template_dir: custom_template_dir = os.path.abspath(template_dir) env = Environment(loader=FileSystemLoader( os.path.abspath(template_dir)), **environment_kwargs) else: try: base_path = sys._MEIPASS env = Environment(loader=FileSystemLoader( os.path.join(base_path, 'hokusai', 'templates'))) except: env = Environment(loader=PackageLoader('hokusai', 'templates')) required_templates = [ 'Dockerfile.j2', '.dockerignore.j2', 'hokusai/build.yml.j2', 'hokusai/development.yml.j2', 'hokusai/test.yml.j2', 'hokusai/staging.yml.j2', 'hokusai/production.yml.j2' ] template_context = { "project_name": config.project_name, "project_repo": ecr.project_repo } for s in template_vars: if '=' not in s: raise HokusaiError( "Error: template variables must be of the form 'key=value'") split = s.split('=', 1) template_context[split[0]] = split[1] try: for template in required_templates: if custom_template_dir and not os.path.isfile( os.path.join(custom_template_dir, template)): raise HokusaiError("Could not find required template file %s" % template) with open(os.path.join(CWD, template.rstrip('.j2')), 'w') as f: f.write(env.get_template(template).render(**template_context)) print_green("Created %s" % template.rstrip('.j2')) if custom_template_dir: for root, _, files in os.walk(custom_template_dir): subpath = os.path.relpath(root, custom_template_dir) if subpath is not '.': mkpath(os.path.join(CWD, subpath)) for file in files: if subpath is not '.': file_path = os.path.join(subpath, file) else: file_path = file if file_path in required_templates: continue if file_path.endswith('.j2'): with open(os.path.join(CWD, file_path.rstrip('.j2')), 'w') as f: f.write( env.get_template(file_path).render( **template_context)) else: copyfile(os.path.join(custom_template_dir, file_path), os.path.join(CWD, file_path)) print_green("Created %s" % file_path.rstrip('.j2')) finally: if scratch_dir: rmtree(scratch_dir)
class TestECR(HokusaiIntegrationTestCase): def setUp(self): self.ecr = ECR() @httpretty.activate def test_registry(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-repositories-response.json')).read(), content_type="application/x-amz-json-1.1") repositories = [ str(repo['repositoryName']) for repo in self.ecr.registry ] self.assertTrue('hello' in repositories) self.assertTrue('bar' in repositories) self.assertTrue('baz' in repositories) @httpretty.activate def test_project_repo_exists(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-repositories-response.json')).read(), content_type="application/x-amz-json-1.1") self.assertTrue(self.ecr.project_repo_exists()) @httpretty.activate def test_get_project_repo(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-repositories-response.json')).read(), content_type="application/x-amz-json-1.1") self.assertEqual(self.ecr.project_repo, '123456789012.dkr.ecr.us-east-1.amazonaws.com/hello') @httpretty.activate def test_create_project_repo(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-create-repository-response.json')).read(), content_type="application/x-amz-json-1.1") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-empty-repositories-response.json')).read(), content_type="application/x-amz-json-1.1") self.assertTrue(self.ecr.create_project_repo()) @httpretty.activate def test_get_login(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-authorization-response.json')).read(), content_type="application/x-amz-json-1.1") self.assertEqual( self.ecr.get_login(), 'docker login -u AWS -p 76W8YEUFHDSAE98DFDHSFSDFIUHSDAJKGKSADFGKDF https://123456789012.dkr.ecr.us-east-1.amazonaws.com' ) @httpretty.activate def test_get_images(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-images-response.json')).read(), content_type="application/x-amz-json-1.1") self.assertEqual( self.ecr.get_images()[0]['imageTags'], ['7shdn4f0f34bb8shdkb313cbeccb2fc031808duho', 'latest']) self.assertEqual( self.ecr.get_images()[0]['imageDigest'], 'sha256:8sh968hsn205e8bff53ba8ed1006c7f41dacd17db164efdn6d346204f997shdn' ) self.assertEqual(self.ecr.get_images()[0]['registryId'], '123456789012') self.assertEqual(self.ecr.get_images()[0]['repositoryName'], 'hello') @httpretty.activate def test_tag_exists(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-images-response.json')).read(), content_type="application/x-amz-json-1.1") self.assertTrue( self.ecr.tag_exists('7shdn4f0f34bb8shdkb313cbeccb2fc031808duho')) self.assertTrue(self.ecr.tag_exists('latest'))
class TestECR(HokusaiIntegrationTestCase): def setUp(self): self.ecr = ECR() @httpretty.activate def test_registry(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") repositories = [ str(repo['repositoryName']) for repo in self.ecr.registry ] self.assertTrue('hello' in repositories) self.assertTrue('bar' in repositories) self.assertTrue('baz' in repositories) @httpretty.activate def test_project_repo_exists(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") self.assertTrue(self.ecr.project_repo_exists()) @httpretty.activate def test_get_project_repo(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") self.assertEqual(self.ecr.project_repo, '123456789012.dkr.ecr.us-east-1.amazonaws.com/hello') @httpretty.activate def test_create_project_repo(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-create-repository-response.json'), content_type="application/x-amz-json-1.1") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-empty-repositories-response.json'), content_type="application/x-amz-json-1.1") self.assertTrue(self.ecr.create_project_repo()) @httpretty.activate def test_get_login(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-authorization-response.json'), content_type="application/x-amz-json-1.1") self.assertEqual( self.ecr.get_login(), 'docker login -u AWS -p 76W8YEUFHDSAE98DFDHSFSDFIUHSDAJKGKSADFGKDF https://123456789012.dkr.ecr.us-east-1.amazonaws.com' ) @httpretty.activate def test_images(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri(httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-images-response.json'), content_type="application/x-amz-json-1.1") self.assertEqual( self.ecr.images[0]['imageTags'], ['a2605e5b93ec4beecde122c53a3fdea18807459c', 'latest']) self.assertEqual( self.ecr.images[0]['imageDigest'], 'sha256:8sh968hsn205e8bff53ba8ed1006c7f41dacd17db164efdn6d346204f997shdn' ) self.assertEqual(self.ecr.images[0]['registryId'], '123456789012') self.assertEqual(self.ecr.images[0]['repositoryName'], 'hello') @httpretty.activate def test_tag_exists(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri(httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-images-response.json'), content_type="application/x-amz-json-1.1") self.assertTrue( self.ecr.tag_exists('a2605e5b93ec4beecde122c53a3fdea18807459c')) self.assertTrue(self.ecr.tag_exists('latest')) @httpretty.activate def test_tags(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri(httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-images-response.json'), content_type="application/x-amz-json-1.1") self.assertIn('a2605e5b93ec4beecde122c53a3fdea18807459c', self.ecr.tags()) self.assertIn('latest', self.ecr.tags()) @httpretty.activate def test_find_git_sha1_image_tag(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri(httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-images-response.json'), content_type="application/x-amz-json-1.1") self.assertEqual(self.ecr.find_git_sha1_image_tag('latest'), 'a2605e5b93ec4beecde122c53a3fdea18807459c') @httpretty.activate def test_image_digest_for_tag(self): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri(httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-images-response.json'), content_type="application/x-amz-json-1.1") self.assertEqual( self.ecr.image_digest_for_tag( 'a2605e5b93ec4beecde122c53a3fdea18807459c'), 'sha256:8sh968hsn205e8bff53ba8ed1006c7f41dacd17db164efdn6d346204f997shdn' ) self.assertEqual( self.ecr.image_digest_for_tag('latest'), 'sha256:8sh968hsn205e8bff53ba8ed1006c7f41dacd17db164efdn6d346204f997shdn' )