# frozen_string_literal: true require 'fileutils' require_relative 'chef_admin' # DeployRubygem - deploy a gem using rake # Containing a class module DeployRubygem # Using Project to deploy and manage Project class Workstation attr_reader :chef_server_url, :nodename, :policyname, :policygroup, :knife_name, :chef_client_key, :chef_knife_key, :user_folder 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 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 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 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 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') Dir.mkdir(File.join(user_folder, '.chef')) File.write(knife_file, clientrb) File.write(cicdpem_file, chef_knife_key.split('\\n').join("\n")) File.write(cicdcredential_file, credential) [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 end end