Class: DTK::Client::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/configurator.rb

Constant Summary collapse

CONFIG_FILE =
File.join(OsUtil.dtk_local_folder, "client.conf")
CRED_FILE =
File.join(OsUtil.dtk_local_folder, ".connection")
DIRECT_ACCESS =
File.join(OsUtil.dtk_local_folder, ".add_direct_access")
NODE_SSH_CREDENTIALS =
File.join(OsUtil.dtk_local_folder, "ssh_credentials.yaml")

Class Method Summary collapse

Class Method Details

.add_current_user_to_direct_accessObject



124
125
126
127
128
129
130
131
132
# File 'lib/configurator.rb', line 124

def self.add_current_user_to_direct_access()
  username = client_username()

  File.open(DIRECT_ACCESS, 'a') do |file|
    file.puts(username)
  end

  true
end

.ask_catalog_credentialsObject



153
154
155
156
157
158
159
160
161
# File 'lib/configurator.rb', line 153

def self.ask_catalog_credentials()
  are_there_creds = Console.confirmation_prompt("Do you have DTK catalog credentials", true)
  property_template = {}
  if are_there_creds
    property_template = self.enter_catalog_credentials()
  end

  property_template
end

.check_config_existsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/configurator.rb', line 33

def self.check_config_exists
  exists = true
  if !File.exists?(CONFIG_FILE)
    puts "", "Please enter the DTK server address (example: instance.dtk.io)"
    header = File.read(File.expand_path('../lib/config/client.conf.header', File.dirname(__FILE__)))
    generate_conf_file(CONFIG_FILE, [['server_host', 'Server address']], header)
    exists = false
  end
  if !File.exists?(CRED_FILE)
    puts "", "Please enter your DTK login details"
    generate_conf_file(CRED_FILE, [['username', 'Username'], ['password', 'Password']], '')
    exists = false
  end

  exists
end

.check_direct_accessObject

return true/false, .add_direct_access file location and ssk key file location



60
61
62
63
64
65
# File 'lib/configurator.rb', line 60

def self.check_direct_access
  username_exists  = check_for_username_entry(client_username())
  ssh_key_path = SSHUtil.default_rsa_pub_key_path()

  {:username_exists => username_exists, :file_path => DIRECT_ACCESS, :ssh_key_path => ssh_key_path}
end

.check_for_username_entry(username) ⇒ Object

Method will check if there is username entry in DIRECT_ACCESS file



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/configurator.rb', line 141

def self.check_for_username_entry(username)
  if File.exists?(DIRECT_ACCESS)
    File.open(DIRECT_ACCESS).each do |line|
      if line.strip.eql?(username)
        return true
      end
    end
  end

  return false
end

.check_gitObject



50
51
52
53
54
55
56
57
# File 'lib/configurator.rb', line 50

def self.check_git
  if OsUtil.which('git') == nil
    OsUtil.put_warning "[WARNING]", "Can't find the 'git' command in you path. Please make sure git is installed in order to use all features of DTK Client.", :yellow
  else
    OsUtil.put_warning "[WARNING]", 'Git username not set. This can cause issues while using DTK Client. To set it, run `git config --global user.name "User Name"`', :yellow if `git config --get user.name` == ""
    OsUtil.put_warning "[WARNING]", 'Git email not set. This can cause issues while using DTK Client. To set it, run `git config --global user.email "[email protected]"`', :yellow if `git config --get user.email` == ""
  end
end

.client_usernameObject



134
135
136
# File 'lib/configurator.rb', line 134

def self.client_username()
  parse_key_value_file(CRED_FILE)[:username]
end

.create_missing_clone_dirsObject



99
100
101
102
103
104
# File 'lib/configurator.rb', line 99

def self.create_missing_clone_dirs
  FileUtils.mkdir(OsUtil.component_clone_location) unless File.directory?(OsUtil.component_clone_location)
  FileUtils.mkdir(OsUtil.service_clone_location) unless File.directory?(OsUtil.service_clone_location)
  FileUtils.mkdir(OsUtil.test_clone_location) unless File.directory?(OsUtil.test_clone_location)
  FileUtils.mkdir(OsUtil.backups_location) unless File.directory?(OsUtil.backups_location)
end

.enter_catalog_credentialsObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/configurator.rb', line 163

def self.enter_catalog_credentials()
  property_template = {}
  # needed to preserve the order for ruby 1.8.7
  # ruby 1.8 does not preserve order of insertation
  wizard_values = { :username => 'Catalog Username', :password => 'Catalog Password' }
  [:username, :password].each do |p|
    value = ask("#{wizard_values[p]}: ") { |q| q.echo = false if p == :password }
    property_template.store(p, value)
  end
  property_template
end

.generate_conf_file(file_path, properties, header) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/configurator.rb', line 67

def self.generate_conf_file(file_path, properties, header)
  require 'highline/import'
  property_template = []

  properties.each do |p,d|
    begin
      trap("INT") {
        puts "", "Exiting..."
        abort
      }
    end
    value = ask("#{d}: ") { |q| q.echo = false if p == 'password'}
    property_template << [p,value]
  end

  File.open(file_path, 'w') do |f|
    f.puts(header)
    property_template.each do |prop|
      f.puts("#{prop[0]}=#{prop[1]}")
    end
  end
end

.parse_key_value_file(file) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/configurator.rb', line 107

def self.parse_key_value_file(file)
  # adapted from mcollective config
  ret = Hash.new
  raise DTK::Client::DtkError,"Config file (#{file}) does not exists" unless File.exists?(file)
  File.open(file).each do |line|
    # strip blank spaces, tabs etc off the end of all lines
    line.gsub!(/\s*$/, "")
    unless line =~ /^#|^$/
      if (line =~ /(.+?)\s*=\s*(.+)/)
        key = $1
        val = $2
        ret[key.to_sym] = val
      end
    end
  end
  ret
end

.regenerate_conf_file(file_path, properties, header) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/configurator.rb', line 90

def self.regenerate_conf_file(file_path, properties, header)
  File.open(file_path, 'w') do |f|
    f.puts(header)
    properties.each do |prop|
      f.puts("#{prop[0]}=#{prop[1]}")
    end
  end
end