<commit_before>my_private_ip = my_private_ip()
public_ip=my_public_ip()

template "/tmp/grafana_tables.sql" do
  source "grafana_tables.sql.erb"
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode 0650
end


exec = "#{node.ndb.scripts_dir}/mysql-client.sh"

bash 'create_mysql_table' do
    user "root"
    code <<-EOF
      set -e
      #{exec} -e \"CREATE DATABASE IF NOT EXISTS grafana CHARACTER SET utf8\";
      #{exec} grafana < /tmp/grafana_tables.sql
    EOF
    not_if "#{exec} grafana -e 'show tables' | grep session"
end


directory "#{node.grafana.base_dir}/dashboards" do
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode "750"
  action :create
end

directory "#{node.grafana.base_dir}/data" do
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode "750"
  action :create
end

directory "#{node.grafana.base_dir}/logs" do
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode "750"
  action :create
end


template "#{node.grafana.base_dir}/conf/defaults.ini" do
  source "grafana.ini.erb"
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode 0650
  variables({ 
     :public_ip => public_ip
           })
end

template "#{node.grafana.base_dir}/public/dashboards/spark.js" do
  source "spark.js.erb"
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode 0650
end

case node.platform
when "ubuntu"
 if node.platform_version.to_f <= 14.04
   node.override.grafana.systemd = "false"
 end
end


service_name="grafana"

if node.grafana.systemd == "true"

  service service_name do
    provider Chef::Provider::Service::Systemd
    supports :restart => true, :stop => true, :start => true, :status => true
    action :nothing
  end

  case node.platform_family
  when "rhel"
    systemd_script = "/usr/lib/systemd/system/#{service_name}.service" 
  when "debian"
    systemd_script = "/lib/systemd/system/#{service_name}.service"
  end

  template systemd_script do
    source "#{service_name}.service.erb"
    owner "root"
    group "root"
    mode 0754
    notifies :enable, resources(:service => service_name)
    notifies :start, resources(:service => service_name), :immediately
  end

  kagent_config "reload_grafana_daemon" do
    action :systemd_reload
  end  

else #sysv

  service service_name do
    provider Chef::Provider::Service::Init::Debian
    supports :restart => true, :stop => true, :start => true, :status => true
    action :nothing
  end

  template "/etc/init.d/#{service_name}" do
    source "#{service_name}.erb"
    owner node.hopsmonitor.user
    group node.hopsmonitor.group
    mode 0754
    notifies :enable, resources(:service => service_name)
    notifies :restart, resources(:service => service_name), :immediately
  end

end


if node.kagent.enabled == "true" 
   kagent_config service_name do
     service service_name
     log_file "#{node.grafana.base_dir}/grafana.log"
   end
end





bash 'add_grafan_index_for_influxdb' do
        user "root"
        code <<-EOH
            set -e
curl --user #{node.grafana.admin_user}:#{node.grafana.admin_password} 'http://localhost:3000/api/datasources' -H \"Content-Type: application/json\" -X POST -d '{\"Name\":\"influxdb\",\"Type\":\"influxdb\",\"url\":\"http://localhost:#{node.influxdb.http.port}\",\"Access\":\"proxy\",\"isDefault\":true,\"database\":\"graphite\",\"user\":#{node.influxdb.db_user},\"password\":#{node.influxdb.db_password}}'
        EOH
  retries 10
  retry_delay 4
#     not_if { }
end



# indexes_installed = "#{node.grafana.base_dir}/.indexes_installed"

#  http_request 'elastic-install-indexes' do
#    url "http://localhost:#{node.influxdb.http.port}/projects"
#    message '
#    {  
#     "mappings":{  
#         "proj":{  
#             "dynamic":"strict",
#             "properties":{  
#                 "description":{  <commit_msg>correct curl request for grafana<commit_after>my_private_ip = my_private_ip()
public_ip=my_public_ip()

template "/tmp/grafana_tables.sql" do
  source "grafana_tables.sql.erb"
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode 0650
end


exec = "#{node.ndb.scripts_dir}/mysql-client.sh"

bash 'create_mysql_table' do
    user "root"
    code <<-EOF
      set -e
      #{exec} -e \"CREATE DATABASE IF NOT EXISTS grafana CHARACTER SET utf8\";
      #{exec} grafana < /tmp/grafana_tables.sql
    EOF
    not_if "#{exec} grafana -e 'show tables' | grep session"
end


directory "#{node.grafana.base_dir}/dashboards" do
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode "750"
  action :create
end

directory "#{node.grafana.base_dir}/data" do
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode "750"
  action :create
end

directory "#{node.grafana.base_dir}/logs" do
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode "750"
  action :create
end


template "#{node.grafana.base_dir}/conf/defaults.ini" do
  source "grafana.ini.erb"
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode 0650
  variables({ 
     :public_ip => public_ip
           })
end

template "#{node.grafana.base_dir}/public/dashboards/spark.js" do
  source "spark.js.erb"
  owner node.hopsmonitor.user
  group node.hopsmonitor.group
  mode 0650
end

case node.platform
when "ubuntu"
 if node.platform_version.to_f <= 14.04
   node.override.grafana.systemd = "false"
 end
end


service_name="grafana"

if node.grafana.systemd == "true"

  service service_name do
    provider Chef::Provider::Service::Systemd
    supports :restart => true, :stop => true, :start => true, :status => true
    action :nothing
  end

  case node.platform_family
  when "rhel"
    systemd_script = "/usr/lib/systemd/system/#{service_name}.service" 
  when "debian"
    systemd_script = "/lib/systemd/system/#{service_name}.service"
  end

  template systemd_script do
    source "#{service_name}.service.erb"
    owner "root"
    group "root"
    mode 0754
    notifies :enable, resources(:service => service_name)
    notifies :start, resources(:service => service_name), :immediately
  end

  kagent_config "reload_grafana_daemon" do
    action :systemd_reload
  end  

else #sysv

  service service_name do
    provider Chef::Provider::Service::Init::Debian
    supports :restart => true, :stop => true, :start => true, :status => true
    action :nothing
  end

  template "/etc/init.d/#{service_name}" do
    source "#{service_name}.erb"
    owner node.hopsmonitor.user
    group node.hopsmonitor.group
    mode 0754
    notifies :enable, resources(:service => service_name)
    notifies :restart, resources(:service => service_name), :immediately
  end

end


if node.kagent.enabled == "true" 
   kagent_config service_name do
     service service_name
     log_file "#{node.grafana.base_dir}/grafana.log"
   end
end





bash 'add_grafan_index_for_influxdb' do
        user "root"
        code <<-EOH
            set -e
curl --user #{node.grafana.admin_user}:#{node.grafana.admin_password} 'http://localhost:3000/api/datasources' -H "Content-Type: application/json" -X POST -d '{"Name":"influxdb","Type":"influxdb","url":"http://localhost:#{node.influxdb.http.port}","Access":"proxy","isDefault":true,"database":"graphite","user":"#{node.influxdb.db_user}","password":"<PASSWORD>}"}'
        EOH
  retries 10
  retry_delay 4
#     not_if { }
end



# indexes_installed = "#{node.grafana.base_dir}/.indexes_installed"

#  http_request 'elastic-install-indexes' do
#    url "http://localhost:#{node.influxdb.http.port}/projects"
#    message '
#    {  
#     "mappings":{  
#         "proj":{  
#             "dynamic":"strict",
#             "properties":{  
#                 "description":{  
{'fraction_non_alphanumeric': 0.13392391870766024, 'fraction_numerical': 0.01068264721208963, 'mean_word_length': 3.4556006964596633, 'pattern_counts': {'":': 26, '<': 16, '<?xml version=': 0, '>': 30, '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': '4374428', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2611, 'n_words': 556}