Class: DeployRubygem::Workstation

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

Overview

Using Project to deploy and manage Project

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Workstation

Returns a new instance of Workstation.



13
14
15
16
17
18
19
20
21
# File 'lib/deploy_rubygem/workstation.rb', line 13

def initialize(options)
  @chef_server_url = options['chef_server_url']
  @nodename = options['node_name']
  @policyname = options['policyname']
  @policygroup = options['policygroup']
  @knife_name = options['knife_name']
  @chef_knife_key = options['chef_knife_key']
  @user_folder = options['user_folder']
end

Instance Attribute Details

#chef_client_keyObject (readonly)

Returns the value of attribute chef_client_key.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def chef_client_key
  @chef_client_key
end

#chef_knife_keyObject (readonly)

Returns the value of attribute chef_knife_key.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def chef_knife_key
  @chef_knife_key
end

#chef_server_urlObject (readonly)

Returns the value of attribute chef_server_url.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def chef_server_url
  @chef_server_url
end

#knife_nameObject (readonly)

Returns the value of attribute knife_name.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def knife_name
  @knife_name
end

#nodenameObject (readonly)

Returns the value of attribute nodename.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def nodename
  @nodename
end

#policygroupObject (readonly)

Returns the value of attribute policygroup.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def policygroup
  @policygroup
end

#policynameObject (readonly)

Returns the value of attribute policyname.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def policyname
  @policyname
end

#user_folderObject (readonly)

Returns the value of attribute user_folder.



11
12
13
# File 'lib/deploy_rubygem/workstation.rb', line 11

def user_folder
  @user_folder
end

Instance Method Details

#boostrap_workstationObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/deploy_rubygem/workstation.rb', line 57

def boostrap_workstation
  extend DeployRubygem

  knife_file = File.join(user_folder, '.chef', 'knife.rb')
  cicdpem_file = File.join(user_folder, '.chef', 'cicd.pem')
  cicdcredential_file = File.join(user_folder, '.chef', 'credentials')

  chef_home = File.join(user_folder, '.chef')
  FileUtils.mkdir_p(chef_home)
  File.write(knife_file, clientrb) unless File.exist?(knife_file)
  File.write(cicdpem_file, chef_knife_key.split('\\n').join("\n")) unless File.exist?(cicdpem_file)
  File.write(cicdcredential_file, credential) unless File.exist?(cicdcredential_file)

  [knife_file, cicdpem_file, cicdcredential_file].each do |file_path|
    FileUtils.chmod(0o600, file_path)
    # read_file(file_path)
  end

  system(chef_accept_cmd)
end

#clientrbObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deploy_rubygem/workstation.rb', line 23

def clientrb
  {
    log_location: File.join(Dir.home, '.chef', 'chef-client.log'),
    chef_server_url: chef_server_url,
    chef_license: 'accept',
    file_cache_path: File.join(Dir.home, '.chef', 'cache'),
    file_backup_path: File.join(Dir.home, '.chef', 'backup'),
    node_name: nodename,
    policy_name: policyname,
    policy_group: policygroup
  }.map do |key, value|
    abort("key #{key} need to have a value :: #{ARGV}") if value.nil? || value.empty?
    key_pair = [key, "'#{value}'"]
    key_pair.join(' ')
  end.join("\n")
end

#credentialObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/deploy_rubygem/workstation.rb', line 40

def credential
  (['[default]'] + {
    client_name: knife_name,
    client_key: File.join(user_folder, '.chef', 'cicd.pem'),
    chef_server_url: chef_server_url
    # secret_file: File.join(user_folder, '.chef', 'cicd.secret')
  }.map do |key, value|
    [key, '=', "'#{value}'"].join(' ')
  end).join("\n")
end

#read_file(file_path) ⇒ Object



51
52
53
54
55
# File 'lib/deploy_rubygem/workstation.rb', line 51

def read_file(file_path)
  puts "REading file #{file_path}"
  puts File.read(file_path)
  puts "Had read #{File.read(file_path).split('\n').length} lines"
end