Class: Ogre::OrgCreate

Inherits:
Base
  • Object
show all
Defined in:
lib/ogre/org-create.rb

Overview

Create organization through Chef::REST with the option to create the Chef policy repository

Instance Method Summary collapse

Methods inherited from Base

#chef_rest

Instance Method Details

#org_createObject

organization create method



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ogre/org-create.rb', line 22

def org_create
  org_json = { name: org.to_s, full_name: org_desc.to_s }
  response = chef_rest.post('/organizations', org_json)
  puts "'#{org}' org has been created."

  # use chef repo generate to create a chef policy repo
  if options[:create_repo]
    # check for policy repo - order of precedence: cli, config.json, default
    repo_url = Config.options[:repo_url] ? Config.options[:repo_url] : REPO_URL
    repo_url = options[:repo_url] ? options[:repo_url] : repo_url

    # get the repository name -- dependant on short name w/out '.git'
    skeleton_repo_path = "#{OGRE_HOME}/policy_repo/#{repo_url.split('/').last}"

    # get policy repo
    get_chef_policy_repo(repo_url, skeleton_repo_path)

    # create parent dir for chef policy repo
    repo_path = options[:repo_path] ? options[:repo_path] : OGRE_HOME
    Dir.mkdir repo_path unless File.exist?(repo_path)

    # run cookbook generate
    generate_cmd = ChefDK::Command::GeneratorCommands::Repo.new(generate_params(repo_path, skeleton_repo_path))
    generate_cmd.run

    # write out pem file
    File.open("#{repo_path}/#{org}-chef/.chef/#{response['clientname']}.pem", 'w') do |f|
      f.print(response['private_key'])
    end

  else
    puts response['private_key']
  end

rescue Net::HTTPServerException => e
  # already exists -- i will allow it
  if e.response.code == '409'
    puts "#{org} org already exists"
  else
    raise e
  end
end