when assign account, config the default yaml for user
[why]
easier for user to config the default yaml file
[how]
write my info to the default yaml file
~/.config/compass-ci/default/account.yaml
include:
- my_email
- my_name
- my_uuid
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
container/assign-account/get_account_info.rb | 90 ++++++++++++--------
1 file changed, 53 insertions(+), 37 deletions(-)
diff --git a/container/assign-account/get_account_info.rb b/container/assign-account/get_account_info.rb
index 2f93d5b..239f709 100755
--- a/container/assign-account/get_account_info.rb
+++ b/container/assign-account/get_account_info.rb
@@ -7,17 +7,17 @@
ACCOUNT_DIR dir layout:
tree
-├── assigned-users
-│ ├── user1
-│ ├── user2
-│ ├── user3
-│ ├── ...
-├── available-users
-│ ├── user11
-│ ├── user12
-│ ├── user13
-│ ├── ...
-└── jumper-info
+|-- assigned-users
+| |-- user1
+| |-- user2
+| |-- user3
+| |-- ...
+|-- available-users
+| |-- user11
+| |-- user12
+| |-- user13
+| |-- ...
+|-- jumper-info
assigned-users: store assigned user files
available-users: store available user files
@@ -28,11 +28,12 @@ jumper-info: store jumper server ip:port for the service
API:
call graph:
-setup_jumper_account_info
+assign_new_account
read_account_info
- build_account_name
+ build_account_info
read_jumper_info
- setup_authorized_key
+ config_default_yaml
+ config_authorized_key
the returned data for setup_jumper_account_info like:
{
@@ -44,6 +45,8 @@ the returned data for setup_jumper_account_info like:
=end
+require 'fileutils'
+
# get jumper and account info
class AccountStorage
ACCOUNT_DIR = '/opt/account_data/'
@@ -61,12 +64,12 @@ class AccountStorage
message = 'no more available users'
raise message if files.empty?
- account_info = build_account_name(available_dir, files)
+ account_info = build_account_info(available_dir, files)
return account_info
end
- def build_account_name(available_dir, files)
+ def build_account_info(available_dir, files)
files.sort
account_info = []
account_info.push files[0]
@@ -90,38 +93,51 @@ class AccountStorage
return jumper_info
end
- def setup_jumper_account_info
+ def assign_new_account
account_info = read_account_info
jumper_info = read_jumper_info
- pub_key = @data['pub_key'] unless @data.nil?
-
- jumper_ip = jumper_info[0].chomp
- jumper_port = jumper_info[1].chomp
- account = account_info[0]
- passwd = if pub_key.nil?
- account_info[1]
- else
- 'Use pub_key to login'
- end
- jumper_account_info = {
- 'account' => account,
- 'passwd' => passwd,
- 'jumper_ip' => jumper_ip,
- 'jumper_port' => jumper_port
+ pub_key = @data['my_ssh_pubkey'] unless @data['my_ssh_pubkey'].nil?
+
+ login_name = account_info[0]
+ password = if pub_key.nil?
+ account_info[1]
+ else
+ 'Use pub_key to login'
+ end
+
+ new_account_info = {
+ 'my_login_name' => login_name,
+ 'my_password' => password,
+ 'jumper_host' => jumper_info[0].chomp,
+ 'jumper_port' => jumper_info[1].chomp
}
- setup_authorized_key(account, pub_key)
- return jumper_account_info
+ config_authorized_key(login_name, pub_key) unless pub_key.nil?
+ config_default_yaml(login_name)
+
+ return new_account_info
+ end
+
+ def config_default_yaml(login_name)
+ default_yaml_dir = File.join('/home', login_name, '.config/compass-ci/defaults')
+ FileUtils.mkdir_p default_yaml_dir
+
+ File.open("#{default_yaml_dir}/account.yaml", 'a') do |file|
+ file.puts "my_email: #{@data['my_email']}"
+ file.puts "my_name: #{@data['my_name']}"
+ file.puts "my_uuid: #{@data['my_uuid']}"
+ end
+ %x(chown -R #{login_name}:#{login_name} "/home/#{login_name}/.config")
end
- def setup_authorized_key(account, pub_key)
- ssh_dir = File.join('/home/', account, '.ssh')
+ def config_authorized_key(login_name, pub_key)
+ ssh_dir = File.join('/home/', login_name, '.ssh')
Dir.mkdir ssh_dir, 0o700
Dir.chdir ssh_dir
f = File.new('authorized_keys', 'w')
f.puts pub_key
f.close
File.chmod 0o600, 'authorized_keys'
- %x(chown -R #{account}:#{account} #{ssh_dir})
+ %x(chown -R #{login_name}:#{login_name} #{ssh_dir})
end
end
--
2.23.0