Exemple #1
0
def push(tag, force, overwrite):
  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_repository_exists():
    raise HokusaiError("ECR repository %s does not exist... did you run `hokusai setup` for this project?" % config.project_name)

  shout(ecr.get_login())
  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 remote repository.  Aborting." % tag)

  docker_compose_yml = os.path.join(os.getcwd(), 'hokusai/common.yml')
  shout("docker-compose -f %s -p hokusai build" % docker_compose_yml, print_output=True)
  build = "hokusai_%s:latest" % config.project_name

  shout("docker tag %s %s:%s" % (build, config.aws_ecr_registry, tag))
  shout("docker push %s:%s" % (config.aws_ecr_registry, tag), print_output=True)
  print_green("Pushed %s to %s:%s" % (build, config.aws_ecr_registry, tag))

  shout("docker tag %s %s:%s" % (build, config.aws_ecr_registry, 'latest'))
  shout("docker push %s:%s" % (config.aws_ecr_registry, 'latest'), print_output=True)
  print_green("Pushed %s to %s:%s" % (build, config.aws_ecr_registry, 'latest'))
Exemple #2
0
def push(tag, build, 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())
  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()

  build_tag = "hokusai_%s:latest" % config.project_name

  shout("docker tag %s %s:%s" % (build_tag, ecr.project_repo, tag))
  shout("docker push %s:%s" % (ecr.project_repo, tag), print_output=True)
  print_green("Pushed %s to %s:%s" % (build_tag, ecr.project_repo, tag), newline_after=True)

  if skip_latest: return

  shout("docker tag %s %s:%s" % (build_tag, ecr.project_repo, 'latest'))
  shout("docker push %s:%s" % (ecr.project_repo, 'latest'), print_output=True)
  print_green("Pushed %s to %s:%s" % (build_tag, ecr.project_repo, 'latest'), newline_after=True)
Exemple #3
0
class TestECR(HokusaiIntegrationTestCase):
  def setUp(self):
    self.ecr = ECR()

  @httpretty.activate
  def test_registry(self):
    httpretty.register_uri(httpretty.POST, "https://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('foo' in repositories)
    self.assertTrue('bar' in repositories)
    self.assertTrue('baz' in repositories)

  @httpretty.activate
  def test_project_repository_exists(self):
    httpretty.register_uri(httpretty.POST, "https://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_repository_exists())

  @httpretty.activate
  def test_create_project_repository(self):
    httpretty.register_uri(httpretty.POST, "https://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")
    self.assertTrue(self.ecr.create_project_repository())

  @httpretty.activate
  def test_get_login(self):
    httpretty.register_uri(httpretty.POST, "https://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://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'], 'foo')

  @httpretty.activate
  def test_tag_exists(self):
    httpretty.register_uri(httpretty.POST, "https://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'))
Exemple #4
0
def push(tag, build, 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())
    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_compose_yml = os.path.join(os.getcwd(), 'hokusai/build.yml')
        legacy_docker_compose_yml = os.path.join(os.getcwd(),
                                                 'hokusai/common.yml')
        if not os.path.isfile(docker_compose_yml) and not os.path.isfile(
                legacy_docker_compose_yml):
            raise HokusaiError("Yaml files %s / %s do not exist." %
                               (docker_compose_yml, legacy_docker_compose_yml))
        if os.path.isfile(docker_compose_yml):
            shout("docker-compose -f %s -p hokusai build" % docker_compose_yml,
                  print_output=True)
        if os.path.isfile(legacy_docker_compose_yml):
            shout("docker-compose -f %s -p hokusai build" %
                  legacy_docker_compose_yml,
                  print_output=True)

    build_tag = "hokusai_%s:latest" % config.project_name

    shout("docker tag %s %s:%s" % (build_tag, ecr.project_repo, tag))
    shout("docker push %s:%s" % (ecr.project_repo, tag), print_output=True)
    print_green("Pushed %s to %s:%s" % (build_tag, ecr.project_repo, tag))

    if skip_latest: return

    shout("docker tag %s %s:%s" % (build_tag, ecr.project_repo, 'latest'))
    shout("docker push %s:%s" % (ecr.project_repo, 'latest'),
          print_output=True)
    print_green("Pushed %s to %s:%s" % (build_tag, ecr.project_repo, 'latest'))
Exemple #5
0
def pull(tag, local_tag):
    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'))

    shout("docker pull %s:%s" % (ecr.project_repo, tag))

    shout("docker tag %s:%s hokusai_%s:%s" %
          (ecr.project_repo, tag, config.project_name, local_tag))

    print_green("Pulled %s:%s to hokusai_%s:%s" %
                (ecr.project_repo, tag, config.project_name, local_tag),
                newline_after=True)
Exemple #6
0
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_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.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=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('a2605e5b93ec4beecde122c53a3fdea18807459c'))
        self.assertTrue(self.ecr.tag_exists('latest'))

    @httpretty.activate
    def test_tags(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.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=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.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=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.image_digest_for_tag(
                'a2605e5b93ec4beecde122c53a3fdea18807459c'),
            'sha256:8sh968hsn205e8bff53ba8ed1006c7f41dacd17db164efdn6d346204f997shdn'
        )
        self.assertEqual(
            self.ecr.image_digest_for_tag('latest'),
            'sha256:8sh968hsn205e8bff53ba8ed1006c7f41dacd17db164efdn6d346204f997shdn'
        )