<commit_before>#
# Cookbook Name:: extended_drbd
# Recipe:: drbd_fresh_install
# Copyright (C) 2012 <NAME>
#
# This program is free software; you can reistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
# USA.
#

include_recipe "#{@cookbook_name}"
drbd_primary_check          = "cat /proc/drbd |grep -q 'Primary/'"
drbd_secondary_check        = "cat /proc/drbd |grep -q 'Secondary/'"
resource                    = node[:drbd][:resource]
my_ip                       = node[:my_expected_ip].nil? ? node[:ipaddress] : node[:my_expected_ip]
node.set['drbd']['ssh_command'] = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

remote_ip = node[:server_partner_ip]

ruby_block "check if other server is primary" do
  block do
    node[:drbd][:master] = true
    Chef::Log.info("This is a DRBD master")
    unless Chef::Config[:solo]
      node.save
    end
  end
  only_if { node[:drbd][:primary][:fqdn].eql? node[:fqdn] }
end

execute "drbdadm create-md all" do
  command "echo 'Running create-md' ; yes yes |drbdadm create-md all"
  not_if {::File.exists?(node['drbd']['stop_file'])}
  action :run
  notifies :restart, resources(:service => 'drbd'), :immediately
  notifies :create, "extended_drbd_immutable_file[#{node[:drbd][:initialized][:stop_file]}]", :immediately
end

wait_til "drbd_initialized on other server" do
    command "#{node['drbd']['ssh_command']} -q #{remote_ip} [ -f #{node[:drbd][:initialized][:stop_file]} ] "
    message "Wait for drbd to be initialized on #{remote_ip}"
    wait_interval 5
    not_if {::File.exists?(node['drbd']['stop_file'])}
end

bash "setup drbd on master" do
  user "root"
  code <<-EOH
drbdadm -- --overwrite-data-of-peer primary #{resource}
echo 'Changing sync rate to 110M'
drbdsetup #{node[:drbd][:dev]} syncer -r 110M
  EOH
  only_if { (node['drbd']['master'] == true or system(drbd_primary_check)) and (not ::File.exists?(node['drbd']['stop_file'])) }
  notifies :run, "execute[setup xfs filesystem]", :immediately if node['drbd']['fs_type'] == "xfs"
  notifies :run, "execute[setup ext file system]", :immediately if node['drbd']['fs_type'] != "xfs"
end

execute "setup xfs filesystem" do
  command "mkfs.#{node['drbd']['fs_type']} -L #{resource} -f #{node[:drbd][:dev]}"
  timeout node['drbd']['command_timeout']
  action :nothing
end
execute "setup ext file system" do
  command "mkfs.#{node['drbd']['fs_type']} -m 1 -L #{resource} -T news #{node[:drbd][:dev]}"
  timeout node['drbd']['command_timeout']
  notifies :run, "execute[configure fs]", :immediately
  action :nothing
end<commit_msg>Updated code to fix logic error where it was incorrectly setting the flag drbd_master as true for all 'a' boxes<commit_after>#
# Cookbook Name:: extended_drbd
# Recipe:: drbd_fresh_install
# Copyright (C) 2012 <NAME>
#
# This program is free software; you can reistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
# USA.
#

include_recipe "#{@cookbook_name}"
drbd_primary_check          = "cat /proc/drbd |grep -q 'Primary/'"
drbd_secondary_check        = "cat /proc/drbd |grep -q 'Secondary/'"
resource                    = node[:drbd][:resource]
my_ip                       = node[:my_expected_ip].nil? ? node[:ipaddress] : node[:my_expected_ip]
node.set['drbd']['ssh_command'] = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

remote_ip = node[:server_partner_ip]

execute "drbdadm create-md all" do
  command "echo 'Running create-md' ; yes yes |drbdadm create-md all"
  not_if {::File.exists?(node['drbd']['stop_file'])}
  action :run
  notifies :restart, resources(:service => 'drbd'), :immediately
  notifies :create, "extended_drbd_immutable_file[#{node[:drbd][:initialized][:stop_file]}]", :immediately
end

wait_til "drbd_initialized on other server" do
    command "#{node['drbd']['ssh_command']} -q #{remote_ip} [ -f #{node[:drbd][:initialized][:stop_file]} ] "
    message "Wait for drbd to be initialized on #{remote_ip}"
    wait_interval 5
    not_if {::File.exists?(node['drbd']['stop_file'])}
end

execute "modprobe drbd"

ruby_block "check if other server is primary" do
  block do
    drbd_check = Chef::ShellOut.new("drbdadm role all").run_command.stdout
    if not drbd_check.include?("Secondary/Primary")
      node.set[:drbd][:master] = true
      Chef::Log.info("This is a DRBD master")
      unless Chef::Config[:solo]
        node.save
      end
    else
      node.set['drbd']['master'] = false
    end
  end
  only_if { node[:drbd][:primary][:fqdn].eql? node[:fqdn] }
end

bash "setup drbd on master" do
  user "root"
  code <<-EOH
drbdadm -- --overwrite-data-of-peer primary #{resource}
echo 'Changing sync rate to 110M'
drbdsetup #{node[:drbd][:dev]} syncer -r 110M
  EOH
  only_if { (node['drbd']['master'] == true or system(drbd_primary_check)) and (not ::File.exists?(node['drbd']['stop_file'])) }
  notifies :run, "execute[setup xfs filesystem]", :immediately if node['drbd']['fs_type'] == "xfs"
  notifies :run, "execute[setup ext file system]", :immediately if node['drbd']['fs_type'] != "xfs"
end

execute "setup xfs filesystem" do
  command "mkfs.#{node['drbd']['fs_type']} -L #{resource} -f #{node[:drbd][:dev]}"
  timeout node['drbd']['command_timeout']
  action :nothing
end
execute "setup ext file system" do
  command "mkfs.#{node['drbd']['fs_type']} -m 1 -L #{resource} -T news #{node[:drbd][:dev]}"
  timeout node['drbd']['command_timeout']
  notifies :run, "execute[configure fs]", :immediately
  action :nothing
end
{'fraction_non_alphanumeric': 0.14371617558628982, 'fraction_numerical': 0.0072158749248346366, 'mean_word_length': 4.562709030100335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1658715', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2169, 'n_words': 684}