<commit_before>#!/bin/env ruby

require 'rubygems'
require 'couchrest'
require 'chef/log'

couchdb_uri = 'localhost:5984'
couchrest = CouchRest.new(couchdb_uri)
couchrest.database!('opscode_account')
couchrest.default_database = 'opscode_account'

require 'mixlib/authorization'
Mixlib::Authorization::Config.couchdb_uri = couchdb_uri
Mixlib::Authorization::Config.default_database = couchrest.default_database
Mixlib::Authorization::Config.private_key = OpenSSL::PKey::RSA.new(File.read('/etc/opscode/azs.pem'))
Mixlib::Authorization::Config.authorization_service_uri = 'http://localhost:5959'
Mixlib::Authorization::Config.certificate_service_uri = "http://localhost:5140/certificates"
require 'mixlib/authorization/auth_join'
require 'mixlib/authorization/models'

Mixlib::Authorization::Log.level = :fatal
Mixlib::Authentication::Log.level = :fatal
Chef::Log.level = :fatal

include Mixlib::Authorization::AuthHelper

orgname = ARGV[0]
org_database = database_from_orgname(orgname)
admin_name = ARGV[1] || Mixlib::Authorization::Models::Group.on(org_database).by_groupname(:key=>"admins").first["actor_and_group_names"]["users"].first

user_id = Mixlib::Authorization::Models::User.by_username(:key=>admin_name).first["_id"]
admin_id =  Mixlib::Authorization::AuthJoin.by_user_object_id(:key=>user_id).first.auth_object_id

o_nodes_query = "curl -s http://#{couchdb_uri}/#{org_database.name}/_design/nodes/_view/all_id"
nodes = JSON.parse(`#{o_nodes_query}`)
STDERR.puts nodes["total_rows"].to_i
if nodes["rows"]
  nodenames = nodes["rows"].map { |node| node["value"]}
  
  nodenames.each do |nodename|
    node_exists = !Mixlib::Authorization::Models::Node.on(org_database).by_name(:key=>nodename).first.nil?
    Mixlib::Authorization::Models::Node.on(org_database).new(:name=>nodename,:requester_id => admin_id, :orgname=>orgname).save unless node_exists
    STDERR.putc('.')
  end
  STDERR.puts "\nnodes done"
end

o_roles_query = "curl -s http://#{couchdb_uri}/#{org_database.name}/_design/roles/_view/all_id"
roles = JSON.parse(`#{o_roles_query}`)
STDERR.puts roles["total_rows"].to_i
if roles["rows"]
  rolenames = roles["rows"].map { |role| role["value"]}
  
  rolenames.each do |rolename|<commit_msg>show user & org info<commit_after>#!/bin/env ruby

require 'rubygems'
require 'couchrest'
require 'chef/log'

couchdb_uri = 'localhost:5984'
couchrest = CouchRest.new(couchdb_uri)
couchrest.database!('opscode_account')
couchrest.default_database = 'opscode_account'

require 'mixlib/authorization'
Mixlib::Authorization::Config.couchdb_uri = couchdb_uri
Mixlib::Authorization::Config.default_database = couchrest.default_database
Mixlib::Authorization::Config.private_key = OpenSSL::PKey::RSA.new(File.read('/etc/opscode/azs.pem'))
Mixlib::Authorization::Config.authorization_service_uri = 'http://localhost:5959'
Mixlib::Authorization::Config.certificate_service_uri = "http://localhost:5140/certificates"
require 'mixlib/authorization/auth_join'
require 'mixlib/authorization/models'

Mixlib::Authorization::Log.level = :fatal
Mixlib::Authentication::Log.level = :fatal
Chef::Log.level = :fatal

include Mixlib::Authorization::AuthHelper

orgname = ARGV[0]
org_database = database_from_orgname(orgname)
admin_name = ARGV[1] || Mixlib::Authorization::Models::Group.on(org_database).by_groupname(:key=>"admins").first["actor_and_group_names"]["users"].first

user_id = Mixlib::Authorization::Models::User.by_username(:key=>admin_name).first["_id"]
admin_id =  Mixlib::Authorization::AuthJoin.by_user_object_id(:key=>user_id).first.auth_object_id
STDERR.puts "Organization is #{orgname} and the admin user #{admin_name} with user id #{user_id} and auth id #{admin_id}"

o_nodes_query = "curl -s http://#{couchdb_uri}/#{org_database.name}/_design/nodes/_view/all_id"
nodes = JSON.parse(`#{o_nodes_query}`)
STDERR.puts nodes["total_rows"].to_i
if nodes["rows"]
  nodenames = nodes["rows"].map { |node| node["value"]}
  
  nodenames.each do |nodename|
    node_exists = !Mixlib::Authorization::Models::Node.on(org_database).by_name(:key=>nodename).first.nil?
    Mixlib::Authorization::Models::Node.on(org_database).new(:name=>nodename,:requester_id => admin_id, :orgname=>orgname).save unless node_exists
    STDERR.putc('.')
  end
  STDERR.puts "\nnodes done"
end

o_roles_query = "curl -s http://#{couchdb_uri}/#{org_database.name}/_design/roles/_view/all_id"
roles = JSON.parse(`#{o_roles_query}`)
STDERR.puts roles["total_rows"].to_i
if roles["rows"]
  rolenames = roles["rows"].map { |role| role["value"]}
  
  rolenames.each do |rolename|
{'fraction_non_alphanumeric': 0.16121238743685481, 'fraction_numerical': 0.006149791346365034, 'mean_word_length': 7.982248520710059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1146295', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1622, 'n_words': 213}