Exemple #1
0
    def load(self):
        uri = urlparse(self.uri)
        if not uri.path.endswith('yaml') and not uri.path.endswith('yml'):
            raise HokusaiError('Uri must be of Yaml file type')

        tmpdir = tempfile.mkdtemp()
        tmp_configfile = os.path.join(tmpdir, 'config')

        try:
            if uri.scheme == 's3':
                client = boto3.client('s3', region_name=get_region_name())
                try:
                    client.download_file(uri.netloc, uri.path.lstrip('/'),
                                         tmp_configfile)
                except ClientError:
                    raise HokusaiError("Error fetching file %s" % self.uri)
            else:
                try:
                    copyfile(uri.path, tmp_configfile)
                except IOError:
                    raise HokusaiError("Error copying file %s" % self.uri)

            with open(tmp_configfile, 'r') as f:
                struct = yaml.safe_load(f.read())
                if type(struct) is not dict:
                    raise HokusaiError('Yaml is invalid')
                return struct

        except HokusaiError:
            raise

        finally:
            rmtree(tmpdir)
Exemple #2
0
 def aws_account_id(self):
     if self.__aws_account_id is None:
         self.__aws_account_id = boto3.client(
             'sts',
             region_name=get_region_name()).get_caller_identity().get(
                 'Account')
     return self.__aws_account_id
Exemple #3
0
def check():
    return_code = 0

    def check_ok(check_item):
        print_green(u'\u2714 ' + check_item + ' found')

    def check_err(check_item):
        print_red(u'\u2718 ' + check_item + ' not found')

    try:
        config.project_name
        check_ok('Config project-name')
    except HokusaiError:
        check_err('Config project-name')

    try:
        shout('which docker')
        check_ok('docker')
    except CalledProcessError:
        check_err('docker')
        return_code += 1

    try:
        shout('which docker-compose')
        check_ok('docker-compose')
    except CalledProcessError:
        check_err('docker-compose')
        return_code += 1

    try:
        shout('which kubectl')
        check_ok('kubectl')
    except CalledProcessError:
        check_err('kubectl')
        return_code += 1

    try:
        shout('which git')
        check_ok('git')
    except CalledProcessError:
        check_err('git')
        return_code += 1

    try:
        boto3.client('sts',
                     region_name=get_region_name()).get_caller_identity()
        check_ok('Valid AWS credentials')
    except botoexceptions.ClientError, botoexceptions.NoCredentialsError:
        check_err('Valid AWS credentials')
        return_code += 1
Exemple #4
0
def configure(kubectl_version, bucket_name, key_name, config_file, platform, install_to, install_config_to):
  if not ((bucket_name and key_name) or config_file):
    raise HokusaiError("Must define bucket_name and key_name or config_file")

  print_green("Downloading and installing kubectl...")
  urllib.urlretrieve("https://storage.googleapis.com/kubernetes-release/release/v%s/bin/%s/amd64/kubectl" % (kubectl_version, platform), os.path.join('/tmp', 'kubectl'))
  os.chmod(os.path.join('/tmp', 'kubectl'), 0755)
  shutil.move(os.path.join('/tmp', 'kubectl'), os.path.join(install_to, 'kubectl'))

  print_green("Configuring kubectl...")
  if not os.path.isdir(install_config_to):
    mkpath(install_config_to)

  if bucket_name and key_name:
    client = boto3.client('s3', region_name=get_region_name())
    client.download_file(bucket_name, key_name, os.path.join(install_config_to, 'config'))
  else:
    shutil.copy(config_file, os.path.join(install_config_to, 'config'))
Exemple #5
0
def configure(kubectl_version, bucket_name, key_name, config_file, install_to,
              install_config_to):
    if global_config.is_present(
    ) and global_config.kubectl_version is not None:
        kubectl_version = global_config.kubectl_version

    if not kubectl_version:
        raise HokusaiError("You must supply a kubectl_version")

    if global_config.is_present(
    ) and global_config.kubectl_config_file is not None:
        uri = urlparse(global_config.kubectl_config_file)
        if uri.scheme == 's3':
            bucket_name = uri.netloc
            key_name = uri.path
        if uri.scheme == 'file':
            key_name = uri.path

    if not ((bucket_name and key_name) or config_file):
        raise HokusaiError("You must define valid config_file")

    print_green("Downloading and installing kubectl...",
                newline_before=True,
                newline_after=True)
    tmpdir = tempfile.mkdtemp()
    urlretrieve(
        "https://storage.googleapis.com/kubernetes-release/release/v%s/bin/%s/amd64/kubectl"
        % (kubectl_version, platform.system().lower()),
        os.path.join(tmpdir, 'kubectl'))
    os.chmod(os.path.join(tmpdir, 'kubectl'), 0o755)
    shutil.move(os.path.join(tmpdir, 'kubectl'),
                os.path.join(install_to, 'kubectl'))
    shutil.rmtree(tmpdir)

    print_green("Configuring kubectl...", newline_after=True)
    if not os.path.isdir(install_config_to):
        mkpath(install_config_to)

    if bucket_name and key_name:
        client = boto3.client('s3', region_name=get_region_name())
        client.download_file(bucket_name, key_name.lstrip('/'),
                             os.path.join(install_config_to, 'config'))
    else:
        shutil.copy(config_file, os.path.join(install_config_to, 'config'))
Exemple #6
0
def check():
  return_code = 0

  def check_ok(check_item):
    print_green('\u2714 ' + check_item + ' found')

  def check_err(check_item):
    print_red('\u2718 ' + check_item + ' not found')

  try:
    config.project_name
    check_ok('Config project-name')
  except HokusaiError:
    check_err('Config project-name')

  try:
    shout('which docker')
    check_ok('docker')
  except CalledProcessError:
    check_err('docker')
    return_code += 1

  try:
    shout('which docker-compose')
    check_ok('docker-compose')
  except CalledProcessError:
    check_err('docker-compose')
    return_code += 1

  try:
    shout('which kubectl')
    check_ok('kubectl')
  except CalledProcessError:
    check_err('kubectl')
    return_code += 1

  try:
    shout('which git')
    check_ok('git')
  except CalledProcessError:
    check_err('git')
    return_code += 1

  try:
    boto3.client('sts', region_name=get_region_name()).get_caller_identity()
    check_ok('Valid AWS credentials')
  except (botoexceptions.ClientError, botoexceptions.NoCredentialsError):
    check_err('Valid AWS credentials')
    return_code += 1

  ecr = ECR()
  if ecr.project_repo_exists():
    check_ok("ECR repository '%s'" % config.project_name)
  else:
    check_err("ECR repository '%s'" % config.project_name)
    return_code += 1

  try:
    build_template = TemplateSelector().get(os.path.join(CWD, HOKUSAI_CONFIG_DIR, BUILD_YAML_FILE))
    check_ok(HOKUSAI_CONFIG_DIR + '/' + os.path.split(build_template)[-1])
  except HokusaiError:
    check_err('hokusai/build.*')
    return_code += 1

  try:
    development_template = TemplateSelector().get(os.path.join(CWD, HOKUSAI_CONFIG_DIR, DEVELOPMENT_YML_FILE))
    check_ok(HOKUSAI_CONFIG_DIR + '/' + os.path.split(development_template)[-1])
  except HokusaiError:
    check_err('hokusai/development.*')
    return_code += 1

  try:
    test_template = TemplateSelector().get(os.path.join(CWD, HOKUSAI_CONFIG_DIR, TEST_YML_FILE))
    check_ok(HOKUSAI_CONFIG_DIR + '/' + os.path.split(test_template)[-1])
  except HokusaiError:
    check_err('hokusai/test.*')
    return_code += 1

  for context in ['staging', 'production']:
    try:
      if context in Kubectl('staging').contexts():
        check_ok("kubectl context '%s'" % context)
      else:
        check_err("kubectl context '%s'" % context)
        return_code += 1
    except CalledProcessError:
      check_err('%s context' % context)
      return_code += 1

    try:
      context_template = TemplateSelector().get(os.path.join(CWD, HOKUSAI_CONFIG_DIR, context))
      check_ok(HOKUSAI_CONFIG_DIR + '/' + os.path.split(context_template)[-1])
    except HokusaiError:
      check_err("hokusai/%s.*" % context)
      return_code += 1

  return return_code
Exemple #7
0
 def __init__(self):
     self.client = boto3.client('ecr', region_name=get_region_name())
     self.__aws_account_id = None
     self.__registry = None
     self.__project_repo = None
     self.__images = None