<commit_before>#
# Cookbook Name:: arcgis-server
# Provider:: portal
#
# Copyright 2015 Esri
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'fileutils'
require 'pathname'
require 'json'

if RUBY_PLATFORM =~ /mswin|mingw32|windows/
  require 'win32/service'
end

use_inline_resources

action :system do
  case node['platform']
  when 'windows'
  when 'redhat', 'centos'
    ['fontconfig', 'freetype', 'gettext', 'libxkbfile', 'libXtst', 'libXrender', 'dos2unix'].each do |pckg|
      yum_package @new_resource.recipe_name + ':portal:' + pckg do
        options '--enablerepo=*-optional'
        package_name pckg
        action :install
      end
    end
  when 'suse'
    ['libGLU1', 'libXdmcp6', 'xorg-x11-server', 'libXfont1'].each do |pckg|
      package @new_resource.recipe_name + ':portal:' + pckg do
        package_name pckg
        action :install
      end
    end
  else
    # NOTE: ArcGIS products are not officially supported on debian platform family
    ['xserver-common', 'xvfb', 'libfreetype6', 'fontconfig', 'libxfont1',
     'libpixman-1-0', 'libgl1-mesa-dri', 'libgl1-mesa-glx', 'libglu1-mesa',
     'libpng12-0', 'x11-xkb-utils', 'libapr1', 'libxrender1', 'libxi6',
     'libxtst6', 'libaio1', 'nfs-kernel-server', 'autofs', 'libxkbfile1',
     'dos2unix'].each do |pckg|
      package @new_resource.recipe_name + ':portal:' + pckg do
        package_name pckg
        action :install
      end
    end
  end

  new_resource.updated_by_last_action(true)
end

action :install do
  if node['platform'] == 'windows'
    cmd = @new_resource.setup
    args = "/qb INSTALLDIR=\"#{@new_resource.install_dir}\" "\
           "CONTENTDIR=\"#{node['arcgis']['portal']['data_dir']}\" "\
           "USER_NAME=\"#{@new_resource.run_as_user}\" "\
           "PASSWORD=\"#{<PASSWORD>}\""

    cmd = Mixlib::ShellOut.new("\"#{cmd}\" #{args}", { :timeout => 7200 })
    cmd.run_command
    cmd.error!

    if  !node['arcgis']['portal']['preferredidentifier'].nil?
      hostidentifier_properties_path = ::File.join(@new_resource.install_dir,
                                                   'framework', 'runtime', 'ds', 'framework', 'etc',
                                                   'hostidentifier.properties')
      if ::File.exists?(hostidentifier_properties_path)
        file = Chef::Util::FileEdit.new(hostidentifier_properties_path)
        file.search_file_replace(/^#preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.search_file_replace(/^preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.write_file
      else
        ::File.open(hostidentifier_properties_path, 'w') { |f| f.write("preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}") }
      end
    end
  else
    cmd = @new_resource.setup
    args = "-m silent -l yes -d \"#{@new_resource.install_dir}\""
    install_subdir = ::File.join(@new_resource.install_dir,
                                 node['arcgis']['portal']['install_subdir'])
    dir_data = @new_resource.data_dir
    run_as_user = @new_resource.run_as_user

    subdir = @new_resource.install_dir
    node['arcgis']['portal']['install_subdir'].split("/").each do |path|
      subdir = ::File.join(subdir, path)
      FileUtils.mkdir_p(subdir) unless ::File.directory?(subdir)
      FileUtils.chmod 0700, subdir
      FileUtils.chown run_as_user, nil, subdir
    end

    if node['arcgis']['run_as_superuser']
      cmd = Mixlib::ShellOut.new("su #{run_as_user} -c \"#{cmd} #{args}\"", { :timeout => 7200 })
    else
      cmd = Mixlib::ShellOut.new("#{cmd} #{args}", {:user => run_as_user, :timeout => 7200})
    end
    cmd.run_command
    cmd.error!

    if dir_data != node.default['arcgis']['portal']['data_dir']
      properties_filename = ::File.join(install_subdir,
                                        'framework', 'etc',
                                        'portal-config.properties')

      if ::File.exist?(properties_filename)
        file = Chef::Util::FileEdit.new(properties_filename)
        file.search_file_replace_line(/dir.data.*/, "dir.data=#{dir_data}")
        file.write_file
      else
        ::File.open(properties_filename, 'w') { |f| f.write("dir.data=#{dir_data}") }
      end
    end

    if !node['arcgis']['portal']['preferredidentifier'].nil?
        hostidentifier_properties_path = ::File.join(install_subdir,
                                                     'framework', 'runtime', 'ds', 'framework', 'etc',
                                                     'hostidentifier.properties')
      if ::File.exists?(hostidentifier_properties_path)
        file = Chef::Util::FileEdit.new(hostidentifier_properties_path)
        file.search_file_replace(/^#preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.search_file_replace(/^preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.write_file
      else
        ::File.open(hostidentifier_properties_path, 'w') { |f| f.write("preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}") }
      end
    end
  end

  # Wait for Portal installation to finish
  sleep(900.0)

  new_resource.updated_by_last_action(true)
end

action :uninstall do
  if node['platform'] == 'windows'
    cmd = 'msiexec'
    args = "/qb /x #{@new_resource.product_code}"

    cmd = Mixlib::ShellOut.new("\"#{cmd}\" #{args}", {:timeout => 10800})<commit_msg>#58 Fixed broken 10.4.1 backward compatibility in portal/install<commit_after>#
# Cookbook Name:: arcgis-server
# Provider:: portal
#
# Copyright 2015 Esri
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'fileutils'
require 'pathname'
require 'json'

if RUBY_PLATFORM =~ /mswin|mingw32|windows/
  require 'win32/service'
end

use_inline_resources

action :system do
  case node['platform']
  when 'windows'
  when 'redhat', 'centos'
    ['fontconfig', 'freetype', 'gettext', 'libxkbfile', 'libXtst', 'libXrender', 'dos2unix'].each do |pckg|
      yum_package @new_resource.recipe_name + ':portal:' + pckg do
        options '--enablerepo=*-optional'
        package_name pckg
        action :install
      end
    end
  when 'suse'
    ['libGLU1', 'libXdmcp6', 'xorg-x11-server', 'libXfont1'].each do |pckg|
      package @new_resource.recipe_name + ':portal:' + pckg do
        package_name pckg
        action :install
      end
    end
  else
    # NOTE: ArcGIS products are not officially supported on debian platform family
    ['xserver-common', 'xvfb', 'libfreetype6', 'fontconfig', 'libxfont1',
     'libpixman-1-0', 'libgl1-mesa-dri', 'libgl1-mesa-glx', 'libglu1-mesa',
     'libpng12-0', 'x11-xkb-utils', 'libapr1', 'libxrender1', 'libxi6',
     'libxtst6', 'libaio1', 'nfs-kernel-server', 'autofs', 'libxkbfile1',
     'dos2unix'].each do |pckg|
      package @new_resource.recipe_name + ':portal:' + pckg do
        package_name pckg
        action :install
      end
    end
  end

  new_resource.updated_by_last_action(true)
end

action :install do
  if node['platform'] == 'windows'
    cmd = @new_resource.setup
    args = "/qb INSTALLDIR=\"#{@new_resource.install_dir}\" "\
           "CONTENTDIR=\"#{node['arcgis']['portal']['data_dir']}\" "\
           "USER_NAME=\"#{@new_resource.run_as_user}\" "\
           "PASSWORD=\"#{<PASSWORD>_resource.run_as_password}\""

    cmd = Mixlib::ShellOut.new("\"#{cmd}\" #{args}", { :timeout => 7200 })
    cmd.run_command
    cmd.error!

    if node['arcgis']['portal']['preferredidentifier'] != 'hostname'
      hostidentifier_properties_path = ::File.join(@new_resource.install_dir,
                                                   'framework', 'runtime', 'ds', 'framework', 'etc',
                                                   'hostidentifier.properties')
      if ::File.exists?(hostidentifier_properties_path)
        file = Chef::Util::FileEdit.new(hostidentifier_properties_path)
        file.search_file_replace(/^#preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.search_file_replace(/^preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.write_file
      else
        begin
          ::File.open(hostidentifier_properties_path, 'w') { |f| f.write("preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}") }
        rescue Exception => e
          Chef::Log.warn "Failed to set preferredidentifier property. " + e.message
        end
      end
    end
  else
    cmd = @new_resource.setup
    args = "-m silent -l yes -d \"#{@new_resource.install_dir}\""
    install_subdir = ::File.join(@new_resource.install_dir,
                                 node['arcgis']['portal']['install_subdir'])
    dir_data = @new_resource.data_dir
    run_as_user = @new_resource.run_as_user

    subdir = @new_resource.install_dir
    node['arcgis']['portal']['install_subdir'].split("/").each do |path|
      subdir = ::File.join(subdir, path)
      FileUtils.mkdir_p(subdir) unless ::File.directory?(subdir)
      FileUtils.chmod 0700, subdir
      FileUtils.chown run_as_user, nil, subdir
    end

    if node['arcgis']['run_as_superuser']
      cmd = Mixlib::ShellOut.new("su #{run_as_user} -c \"#{cmd} #{args}\"", { :timeout => 7200 })
    else
      cmd = Mixlib::ShellOut.new("#{cmd} #{args}", {:user => run_as_user, :timeout => 7200})
    end
    cmd.run_command
    cmd.error!

    if dir_data != node.default['arcgis']['portal']['data_dir']
      properties_filename = ::File.join(install_subdir,
                                        'framework', 'etc',
                                        'portal-config.properties')

      if ::File.exist?(properties_filename)
        file = Chef::Util::FileEdit.new(properties_filename)
        file.search_file_replace_line(/dir.data.*/, "dir.data=#{dir_data}")
        file.write_file
      else
        ::File.open(properties_filename, 'w') { |f| f.write("dir.data=#{dir_data}") }
      end
    end

    if node['arcgis']['portal']['preferredidentifier'] != 'hostname'
        hostidentifier_properties_path = ::File.join(install_subdir,
                                                     'framework', 'runtime', 'ds', 'framework', 'etc',
                                                     'hostidentifier.properties')
      if ::File.exists?(hostidentifier_properties_path)
        file = Chef::Util::FileEdit.new(hostidentifier_properties_path)
        file.search_file_replace(/^#preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.search_file_replace(/^preferredidentifier.*/, "preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}")
        file.write_file
      else
        begin
          ::File.open(hostidentifier_properties_path, 'w') { |f| f.write("preferredidentifier=#{node['arcgis']['portal']['preferredidentifier']}") }
        rescue Exception => e
          Chef::Log.warn "Failed to set preferredidentifier property. " + e.message
        end
      end
    end
  end

  # Wait for Portal installation to finish
  sleep(900.0)

  new_resource.updated_by_last_action(true)
end

action :uninstall do
  if node['platform'] == 'windows'
    cmd = 'msiexec'
    args = "/qb /x #{@new_resource.product_code}"

    cmd = Mixlib::ShellOut.new("\"#{cmd}\" #{args}", {:timeout => 10800})
{'fraction_non_alphanumeric': 0.14921906287545053, 'fraction_numerical': 0.010412494993992792, 'mean_word_length': 3.0951131518530666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5990377', 'n_tokens_mistral': 3924, 'n_tokens_neox': 3873, 'n_words': 788}