Esempio n. 1
0
def regist(stix_file_path, community, via, package_name=None):
    package_bean = get_package_bean(stix_file_path)

    if package_name:
        package_bean.package_name = package_name

    try:
        with open(stix_file_path, 'rb') as fp:
            stix_file_doc = StixFiles.create(package_bean, community, fp, via)
    except BaseException:
        os.remove(stix_file_path)
        traceback.print_exc()
        raise Exception('duplicate package_id:%s' % (package_bean.package_id))

    try:
        path = os.path.join(community.dir_path, COMMUNITY_ORIGIN_DIR_NAME,
                            str(stix_file_doc.content.grid_id))
        shutil.move(stix_file_path, path)
        stix_file_doc.origin_path = path
        stix_file_doc.save()
    except Exception as e:
        stix_file_doc.delete()
        traceback.print_exc()
        raise Exception(str(e))

    stix_file_doc.split_child_nodes()

    webhook_create(community, stix_file_doc)

    for cl in Communities.get_clients_from_community(community):
        cl.push(stix_file_doc)
    return
Esempio n. 2
0
def regist(stix_file_path, community, via, package_name=None):
    #stixの中身から STIX の情報を取得
    package_bean = get_package_bean(stix_file_path)

    #登録するパッケージ名はweb form指定があれば優先的に使用する
    if package_name is not None:
        package_bean.package_name = package_name

    #STIXFileコレクションに保存
    try:
        #StixFileドキュメントを作成
        with open(stix_file_path, 'r') as fp:
            stix_file_doc = StixFiles.create(package_bean, community, fp, via)
    except:  #おもにduplicateエラー処理 #stixは削除
        os.remove(stix_file_path)
        traceback.print_exc()
        raise Exception('duplicate package_id:%s' % (package_bean.package_id))

    #Fileをコミュニティ別のディレクトリに移動
    try:
        #file名はGridFsの方のidとする
        #stixファイルを移動
        path = os.path.join(community.dir_path, COMMUNITY_ORIGIN_DIR_NAME,
                            str(stix_file_doc.content.grid_id))
        shutil.move(stix_file_path, path)
        #path保存
        stix_file_doc.origin_path = path
        stix_file_doc.save()
    except Exception as e:
        #ドキュメント作成後のエラーはドキュメントを削除する
        #grid_fsもcascade削除される動作は確認済
        stix_file_doc.delete()
        traceback.print_exc()
        raise Exception(str(e))

    #Node分割+キャッシュ作成
    stix_file_doc.split_child_nodes()

    #CommunityのWebHooksの数だけPOSTする
    webhook_create(community, stix_file_doc)

    #push
    from ctirs.core.mongo.documents import Communities
    for cl in Communities.get_clients_from_community(community):
        cl.push(stix_file_doc)
    return