<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

chef_gem 'nats'

ruby_block 'create group and user' do
  block do
    require 'json'
    require 'nats/client'

    @ready = []

    def add_user
      Chef::Log.info('Admin user added')

      user = {
        'group_id' => 1,
        'username' => 'admin',
        'password' => node['ernest']['application']['user_password'],
        'admin' => true
      }

      NATS.request('user.set', user.to_json) do |msg|
        Chef::Log.info(msg)
        @ready << :user
        shutdown
      end
    end

    def add_group
      Chef::Log.info('Admin group added')

      NATS.request('group.set', '{"name": "admin"}') do |msg|
        Chef::Log.info(msg)
        @ready << :group
        shutdown
      end
    end

    def shutdown
      NATS.flush
      sleep 1
      NATS.stop if @ready.include?(:user) && @ready.include?(:group)
    end

    NATS.on_error { |err| puts "Server Error: #{err}" }

    NATS.start(uri: node['nats']['url'], autostart: true) do
      gf = NATS.request('group.find') { |msg| add_group unless msg.include? 'admin' }
      NATS.timeout(gf, 5, expected: 1) { Chef::Log.info('Group store not ready') }

      uf = NATS.request('user.find') { |msg| add_user unless msg.include? 'admin' }
      NATS.timeout(uf, 5, expected: 1) { Chef::Log.info('User store not ready') }

      rs = NATS.subscribe('register') do |msg|
        add_group if msg.include? 'group-store'
        add_user if msg.include? 'user-store'
      end

      NATS.timeout(rs, 120) { Chef::Application.fatal!('Could not register admin user and group') }
    end
  end
  action :run
end<commit_msg>using natsc for admin and group creation<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

ernest_path = "/opt/go/src/github.com/r3labs"

directory ernest_path do
  owner node['server']['user']
  group node['server']['group']
  mode '0755'
  action :create
end

git "#{ernest_path}/natsc" do
  user node['server']['user']
  group node['server']['group']
  repository "<EMAIL>:r3labs/natsc.git"
  revision 'master'
  action :sync
end

execute 'install natsc' do
  command "su #{node['server']['user']} -l -c 'cd #{ernest_path}/natsc && make deps && make install'"
  action :run
end

execute 'add admin group' do
  command "/opt/go/bin/natsc request -s #{node['nats']['url']} -t 5 -r 5 'group.set' '{\"id\":\"1\",\"name\": \"admin\"}'"
  action :run
end

execute 'add admin user' do
  command "/opt/go/bin/natsc request -s #{node['nats']['url']} -t 5 -r 5 'user.set' '{\"group_id\": 1, \"username\": \"admin\", \"password\": \"#{node['ernest']['application']['user_password']}\", \"admin\":true}'"
  action :run
end
{'fraction_non_alphanumeric': 0.1662755615152531, 'fraction_numerical': 0.009721756620851492, 'mean_word_length': 3.010752688172043, 'pattern_counts': {'":': 7, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3988703', 'n_tokens_mistral': 1041, 'n_tokens_neox': 997, 'n_words': 299}