From ce2b45cd2b2ac60f910a3a0487f489c6c76b20d0 Mon Sep 17 00:00:00 2001 From: Khosrow Moossavi Date: Fri, 14 Sep 2018 12:56:23 -0400 Subject: [PATCH] fix more cookstyle errors --- .rubocop.yml | 3 ++ Berksfile | 2 +- Thorfile | 4 +-- Vagrantfile | 9 +++-- libraries/database.rb | 2 +- libraries/global_setting.rb | 16 ++++----- libraries/system_template.rb | 4 +-- providers/api_keys.rb | 6 ++-- providers/global_setting.rb | 12 +++---- providers/setup_database.rb | 16 ++++----- providers/system_template.rb | 34 +++++++++---------- resources/api_keys.rb | 12 +++---- resources/global_setting.rb | 8 ++--- resources/setup_database.rb | 14 ++++---- resources/setup_management.rb | 2 +- resources/system_template.rb | 14 ++++---- spec/recipes/default_spec.rb | 3 +- .../default/serverspec/management_spec.rb | 2 -- .../default/serverspec/usage_spec.rb | 2 +- 19 files changed, 78 insertions(+), 87 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..0a21245 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,3 @@ +AllCops: + Exclude: + - 'Vagrantfile' diff --git a/Berksfile b/Berksfile index 967b9a7..34fea21 100644 --- a/Berksfile +++ b/Berksfile @@ -1,3 +1,3 @@ -source "https://supermarket.chef.io" +source 'https://supermarket.chef.io' metadata diff --git a/Thorfile b/Thorfile index 7a0c0ff..19aaaf2 100644 --- a/Thorfile +++ b/Thorfile @@ -5,8 +5,8 @@ require 'bundler/setup' require 'berkshelf/thor' begin - require "kitchen/thor_tasks" + require 'kitchen/thor_tasks' Kitchen::ThorTasks.new rescue LoadError - puts ">>>>> Kitchen gem not loaded, omitting tasks" unless ENV["CI"] + puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] end diff --git a/Vagrantfile b/Vagrantfile index c3d3164..c315958 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -18,7 +18,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # # $ vagrant plugin install vagrant-omnibus # - if Vagrant.has_plugin?("vagrant-omnibus") + if Vagrant.has_plugin?('vagrant-omnibus') config.omnibus.chef_version = 'latest' end @@ -27,7 +27,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # config.vm.box_url doesn't need to be specified. config.vm.box = 'chef/ubuntu-14.04' - # Assign this VM to a host-only network IP, allowing you to access it # via the IP. Host-only networks can talk to the host machine as well as # any other machines on the same network, but cannot be accessed (through this @@ -79,12 +78,12 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| mysql: { server_root_password: 'rootpass', server_debian_password: 'debpass', - server_repl_password: 'replpass' - } + server_repl_password: 'replpass', + }, } chef.run_list = [ - 'recipe[cloudstack::default]' + 'recipe[cloudstack::default]', ] end end diff --git a/libraries/database.rb b/libraries/database.rb index 0e2be5a..a249c73 100644 --- a/libraries/database.rb +++ b/libraries/database.rb @@ -43,7 +43,7 @@ def init_config_database def dbconf_exist? # test if db.properties as been modified from default installation file. if password encrypted, then we step there to not break anything. Chef::Log.debug 'Checking to see if database config db.properties as been configured' - conf_exist = Mixlib::ShellOut.new("cat /etc/cloudstack/management/db.properties |grep \"ENC(\"") + conf_exist = Mixlib::ShellOut.new('cat /etc/cloudstack/management/db.properties |grep "ENC("') conf_exist.run_command if conf_exist.exitstatus == 0 return true diff --git a/libraries/global_setting.rb b/libraries/global_setting.rb index a754398..596f0aa 100644 --- a/libraries/global_setting.rb +++ b/libraries/global_setting.rb @@ -20,27 +20,25 @@ module Cloudstack module GlobalSetting - # Support whyrun def whyrun_supported? false end - + def load_current_value(name) require 'cloudstack_ruby_client' # get CloudStack current value of the Global Setting client = CloudstackRubyClient::Client.new('http://localhost:8080/client/api/', @current_resource.admin_apikey, @current_resource.admin_secretkey, false) - client.list_configurations(:name => name)['configuration'].first['value'] + client.list_configurations(name: name)['configuration'].first['value'] end - + def update_setting(name, value) require 'cloudstack_ruby_client' client = CloudstackRubyClient::Client.new('http://localhost:8080/client/api/', @current_resource.admin_apikey, @current_resource.admin_secretkey, false) - client.update_configuration({ - :name => name, - :value => value - }) + client.update_configuration( + name: name, + value: value + ) end - end end diff --git a/libraries/system_template.rb b/libraries/system_template.rb index 2b17ae1..45f97f6 100644 --- a/libraries/system_template.rb +++ b/libraries/system_template.rb @@ -28,7 +28,7 @@ def get_template_id template_id = Mixlib::ShellOut.new(template_cmd) template_id.run_command Chef::Log.debug "template id = #{template_id.stdout.chomp}" - return template_id.stdout.chomp + template_id.stdout.chomp end # Create or mount secondary storage path @@ -46,7 +46,7 @@ def secondary_storage def download_systemvm_template # Create database configuration for cloudstack management server that will use and existing database. - #puts "Downloading system template from: #{@current_resource.url}" + # puts "Downloading system template from: #{@current_resource.url}" Chef::Log.info "Downloading system template for #{@current_resource.hypervisor}, this will take some time..." download_cmd = "/usr/share/cloudstack-common/scripts/storage/secondary/cloud-install-sys-tmplt -m #{@current_resource.nfs_path} -u #{@current_resource.url} -h #{@current_resource.hypervisor} -F" download_template = Mixlib::ShellOut.new(download_cmd) diff --git a/providers/api_keys.rb b/providers/api_keys.rb index 4fdc7c0..9ae0740 100644 --- a/providers/api_keys.rb +++ b/providers/api_keys.rb @@ -40,10 +40,10 @@ def whyrun_supported? action :create do wait_count = 0 - until cloudstack_api_is_running? or wait_count == 5 do + until cloudstack_api_is_running? || wait_count == 5 do cloudstack_api_is_running? sleep(5) - wait_count +=1 + wait_count += 1 if wait_count == 1 Chef::Log.info 'Waiting CloudStack to start' end @@ -54,7 +54,7 @@ def whyrun_supported? action :reset do # force generate new API keys - #load_current_resource + # load_current_resource if cloudstack_is_running? if @current_resource.username == 'admin' converge_by('Reseting admin api keys') do diff --git a/providers/global_setting.rb b/providers/global_setting.rb index 56f1a58..3fc93b8 100644 --- a/providers/global_setting.rb +++ b/providers/global_setting.rb @@ -33,7 +33,7 @@ unless @current_resource.admin_apikey.nil? unless @current_resource.exists converge_by("Update Global Setting: #{@current_resource.name} to #{@current_resource.value}") do - #test_connection?(@current_resource.admin_apikey, @current_resource.admin_secretkey) + # test_connection?(@current_resource.admin_apikey, @current_resource.admin_secretkey) update_setting(@current_resource.name, @current_resource.value) end end @@ -44,7 +44,7 @@ def load_current_resource require 'cloudstack_ruby_client' @current_resource = Chef::Resource::CloudstackGlobalSetting.new(@new_resource.name) @current_resource.name(@new_resource.name) - if $admin_apikey.nil? + if $admin_apikey.nil? @current_resource.admin_apikey(@new_resource.admin_apikey) @current_resource.admin_secretkey(@new_resource.admin_secretkey) else # if it's the first run on the server $admin_apikey will not be empty @@ -61,12 +61,10 @@ def load_current_resource current_value = load_current_value(@current_resource.name) if current_value.nil? Chef::Log.error "Global Setting: #{@current_resource.name} not found" + elsif @current_resource.value == current_value + @current_resource.exists = true else - if @current_resource.value == current_value - @current_resource.exists = true - else - @current_resource.exists = false - end + @current_resource.exists = false end end else diff --git a/providers/setup_database.rb b/providers/setup_database.rb index 4112da4..5dc6d9e 100644 --- a/providers/setup_database.rb +++ b/providers/setup_database.rb @@ -17,7 +17,7 @@ # limitations under the License. # -#include Chef::Mixin::ShellOut +# include Chef::Mixin::ShellOut include Cloudstack::Helper include Cloudstack::Database @@ -59,16 +59,14 @@ def load_current_resource if cloudstack_is_running? @current_resource.exists = true - else - if dbconf_exist? - if db_exist?(@current_resource.ip, @current_resource.user, @current_resource.password) - @current_resource.exists = true - else - Chef::Log.info 'Database server ready, not database found, creating it...' - @current_resource.exists = false - end + elsif dbconf_exist? + if db_exist?(@current_resource.ip, @current_resource.user, @current_resource.password) + @current_resource.exists = true else + Chef::Log.info 'Database server ready, not database found, creating it...' @current_resource.exists = false end + else + @current_resource.exists = false end end diff --git a/providers/system_template.rb b/providers/system_template.rb index 8ca2e98..a059d6a 100644 --- a/providers/system_template.rb +++ b/providers/system_template.rb @@ -30,7 +30,7 @@ # Chef::Log.info 'creating cloudstack database' unless @current_resource.exists converge_by("Downloading system template from: #{@current_resource.url}") do - #test_connection?(@current_resource.admin_apikey, @current_resource.admin_secretkey) + # test_connection?(@current_resource.admin_apikey, @current_resource.admin_secretkey) secondary_storage download_systemvm_template end @@ -51,24 +51,22 @@ def load_current_resource # if CloudStack management-server is running, it mean a systemvm template as been downloaded. if cloudstack_is_running? @current_resource.exists = true - else - if db_exist?(@current_resource.db_host, @current_resource.db_user, @current_resource.db_password) - if @current_resource.url.nil? - cmd = Mixlib::ShellOut.new("mysql -h #{@current_resource.db_host} --user=#{@current_resource.db_user} --password='#{@current_resource.db_password}' --skip-column-names -U cloud -e 'select max(url) from cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"#{@current_resource.hypervisor}\" and removed is null'") - cmd.run_command - cmd.error! - @current_resource.url(cmd.stdout.chomp) - end - template_id = get_template_id - Chef::Log.debug "looking for template in #{@current_resource.nfs_path}/template/tmpl/1/#{template_id}" - if ::File.exist?("#{@current_resource.nfs_path}/template/tmpl/1/#{template_id}/template.properties") - Chef::Log.debug "template exists in #{@current_resource.nfs_path}/template/tmpl/1/#{template_id}" - @current_resource.exists = true - else - @current_resource.exists = false - end + elsif db_exist?(@current_resource.db_host, @current_resource.db_user, @current_resource.db_password) + if @current_resource.url.nil? + cmd = Mixlib::ShellOut.new("mysql -h #{@current_resource.db_host} --user=#{@current_resource.db_user} --password='#{@current_resource.db_password}' --skip-column-names -U cloud -e 'select max(url) from cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"#{@current_resource.hypervisor}\" and removed is null'") + cmd.run_command + cmd.error! + @current_resource.url(cmd.stdout.chomp) + end + template_id = get_template_id + Chef::Log.debug "looking for template in #{@current_resource.nfs_path}/template/tmpl/1/#{template_id}" + if ::File.exist?("#{@current_resource.nfs_path}/template/tmpl/1/#{template_id}/template.properties") + Chef::Log.debug "template exists in #{@current_resource.nfs_path}/template/tmpl/1/#{template_id}" + @current_resource.exists = true else - Chef::Log.error 'Database not configured. Cannot retrieve Template URL' + @current_resource.exists = false end + else + Chef::Log.error 'Database not configured. Cannot retrieve Template URL' end end diff --git a/resources/api_keys.rb b/resources/api_keys.rb index 69b4238..188d00e 100644 --- a/resources/api_keys.rb +++ b/resources/api_keys.rb @@ -21,11 +21,11 @@ default_action :create -attribute :username , :name_attribute => true, :kind_of => String -attribute :url , :kind_of => String, :default => 'http://localhost:8080/client/api' -attribute :password , :kind_of => String, :default => 'password' -attribute :admin_apikey , :kind_of => String -attribute :admin_secretkey, :kind_of => String -attribute :ssl , :equal_to => [true, false, 'true', 'false'], :default => false +attribute :username, name_attribute: true, kind_of: String +attribute :url, kind_of: String, default: 'http://localhost:8080/client/api' +attribute :password, kind_of: String, default: 'password' +attribute :admin_apikey, kind_of: String +attribute :admin_secretkey, kind_of: String +attribute :ssl, equal_to: [true, false, 'true', 'false'], default: false attr_accessor :exists diff --git a/resources/global_setting.rb b/resources/global_setting.rb index 02a034d..9d3780c 100644 --- a/resources/global_setting.rb +++ b/resources/global_setting.rb @@ -23,9 +23,9 @@ default_action :update -attribute :name , :name_attribute => true, :kind_of => String -attribute :value , :kind_of => String -attribute :admin_apikey , :kind_of => String -attribute :admin_secretkey , :kind_of => String +attribute :name, name_attribute: true, kind_of: String +attribute :value, kind_of: String +attribute :admin_apikey, kind_of: String +attribute :admin_secretkey, kind_of: String attr_accessor :exists diff --git a/resources/setup_database.rb b/resources/setup_database.rb index 18514df..595b4de 100644 --- a/resources/setup_database.rb +++ b/resources/setup_database.rb @@ -20,12 +20,12 @@ default_action :create -attribute :ip , :name_attribute => true, :kind_of => String -attribute :user , :kind_of => String, :default => 'cloud' -attribute :password , :kind_of => String, :default => 'password' -attribute :root_user , :kind_of => String, :default => 'root' -attribute :root_password , :kind_of => String, :default => 'ilikerandompasswords' -attribute :management_server_key, :kind_of => String, :default => 'password' -attribute :database_key , :kind_of => String, :default => 'password' +attribute :ip, name_attribute: true, kind_of: String +attribute :user, kind_of: String, default: 'cloud' +attribute :password, kind_of: String, default: 'password' +attribute :root_user, kind_of: String, default: 'root' +attribute :root_password, kind_of: String, default: 'ilikerandompasswords' +attribute :management_server_key, kind_of: String, default: 'password' +attribute :database_key, kind_of: String, default: 'password' attr_accessor :exists diff --git a/resources/setup_management.rb b/resources/setup_management.rb index bfe6384..1b84565 100644 --- a/resources/setup_management.rb +++ b/resources/setup_management.rb @@ -35,6 +35,6 @@ bash 'cloudstack-setup-management' do code "/usr/bin/cloudstack-setup-management #{params}" - not_if { ::File.exists?('/etc/cloudstack/management/tomcat6.conf') || ::File.exists?('/etc/cloudstack/management/server.xml')} + not_if { ::File.exist?('/etc/cloudstack/management/tomcat6.conf') || ::File.exist?('/etc/cloudstack/management/server.xml') } end end diff --git a/resources/system_template.rb b/resources/system_template.rb index 13c2b64..b49998c 100644 --- a/resources/system_template.rb +++ b/resources/system_template.rb @@ -20,14 +20,14 @@ default_action :create -attribute :hypervisor , :name_attribute => true, :kind_of => String -attribute :nfs_path , :kind_of => String, :default => '/mnt/secondary' -attribute :nfs_server , :kind_of => String, :default => 'localhost' +attribute :hypervisor, name_attribute: true, kind_of: String +attribute :nfs_path, kind_of: String, default: '/mnt/secondary' +attribute :nfs_server, kind_of: String, default: 'localhost' # If URL net specify, can retreive template URL from database. -attribute :url , :kind_of => String -attribute :db_user , :kind_of => String, :default => 'cloud' # node['cloudstack']['db']['user'] -attribute :db_password , :kind_of => String, :default => 'password' # node['cloudstack']['db']['password'] -attribute :db_host , :kind_of => String, :default => 'localhost' # node['cloudstack']['db']['host'] +attribute :url, kind_of: String +attribute :db_user, kind_of: String, default: 'cloud' # node['cloudstack']['db']['user'] +attribute :db_password, kind_of: String, default: 'password' # node['cloudstack']['db']['password'] +attribute :db_host, kind_of: String, default: 'localhost' # node['cloudstack']['db']['host'] attr_accessor :exists diff --git a/spec/recipes/default_spec.rb b/spec/recipes/default_spec.rb index 73e29ce..e2bcf30 100644 --- a/spec/recipes/default_spec.rb +++ b/spec/recipes/default_spec.rb @@ -17,7 +17,6 @@ it 'does something' do expect(chef_run).to do_something('...') end - end describe 'cloudstack::cloudstack_management' do @@ -26,4 +25,4 @@ it 'installs cloudstack-management' do expect(chef_run).to install_package('cloudstack-management') end -end \ No newline at end of file +end diff --git a/test/integration/default/serverspec/management_spec.rb b/test/integration/default/serverspec/management_spec.rb index fcbad17..10e0bc7 100644 --- a/test/integration/default/serverspec/management_spec.rb +++ b/test/integration/default/serverspec/management_spec.rb @@ -6,8 +6,6 @@ it { should be_installed } end - describe user('cloud') do it { should exist } end - diff --git a/test/integration/default/serverspec/usage_spec.rb b/test/integration/default/serverspec/usage_spec.rb index b663146..85c1a00 100644 --- a/test/integration/default/serverspec/usage_spec.rb +++ b/test/integration/default/serverspec/usage_spec.rb @@ -5,4 +5,4 @@ describe package('cloudstack-usage') do it { should be_installed } -end \ No newline at end of file +end