ogre 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +20 -0
  4. data/.rubocop.yml +16 -0
  5. data/.travis.yml +3 -0
  6. data/Gemfile +4 -0
  7. data/README.md +102 -0
  8. data/Rakefile +18 -0
  9. data/bin/ogre +4 -0
  10. data/lib/ogre.rb +44 -0
  11. data/lib/ogre/associate.rb +57 -0
  12. data/lib/ogre/base.rb +21 -0
  13. data/lib/ogre/config.rb +18 -0
  14. data/lib/ogre/messages.rb +84 -0
  15. data/lib/ogre/org-create.rb +96 -0
  16. data/lib/ogre/org-delete.rb +29 -0
  17. data/lib/ogre/runner.rb +40 -0
  18. data/lib/ogre/set-private-key.rb +57 -0
  19. data/lib/ogre/skeletons/code_generator/files/default/chefignore +95 -0
  20. data/lib/ogre/skeletons/code_generator/files/default/cookbook_readmes/README-policy.md +9 -0
  21. data/lib/ogre/skeletons/code_generator/files/default/cookbook_readmes/README.md +54 -0
  22. data/lib/ogre/skeletons/code_generator/files/default/gitignore +16 -0
  23. data/lib/ogre/skeletons/code_generator/files/default/repo/README.md +66 -0
  24. data/lib/ogre/skeletons/code_generator/files/default/repo/cookbooks/example/attributes/default.rb +7 -0
  25. data/lib/ogre/skeletons/code_generator/files/default/repo/cookbooks/example/metadata.rb +3 -0
  26. data/lib/ogre/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb +8 -0
  27. data/lib/ogre/skeletons/code_generator/files/default/repo/data_bags/README.md +58 -0
  28. data/lib/ogre/skeletons/code_generator/files/default/repo/data_bags/example/example_item.json +4 -0
  29. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/README.md +9 -0
  30. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/_default.json +9 -0
  31. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/dev.json +21 -0
  32. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/example.json +13 -0
  33. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/int.json +21 -0
  34. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/prod.json +21 -0
  35. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/qa.json +21 -0
  36. data/lib/ogre/skeletons/code_generator/files/default/repo/environments/stage.json +21 -0
  37. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/README.md +9 -0
  38. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/active-base.json +12 -0
  39. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/example.json +13 -0
  40. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/loc_caeast.json +13 -0
  41. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/loc_canada.json +13 -0
  42. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/loc_cawest.json +13 -0
  43. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/loc_useast.json +13 -0
  44. data/lib/ogre/skeletons/code_generator/files/default/repo/roles/loc_uswest.json +13 -0
  45. data/lib/ogre/skeletons/code_generator/metadata.rb +8 -0
  46. data/lib/ogre/skeletons/code_generator/recipes/repo.rb +52 -0
  47. data/lib/ogre/skeletons/code_generator/templates/default/LICENSE.all_rights.erb +3 -0
  48. data/lib/ogre/skeletons/code_generator/templates/default/LICENSE.apache2.erb +201 -0
  49. data/lib/ogre/skeletons/code_generator/templates/default/LICENSE.gplv2.erb +339 -0
  50. data/lib/ogre/skeletons/code_generator/templates/default/LICENSE.gplv3.erb +674 -0
  51. data/lib/ogre/skeletons/code_generator/templates/default/LICENSE.mit.erb +21 -0
  52. data/lib/ogre/skeletons/code_generator/templates/default/README.md.erb +4 -0
  53. data/lib/ogre/skeletons/code_generator/templates/default/cookbook_file.erb +0 -0
  54. data/lib/ogre/skeletons/code_generator/templates/default/knife.rb.erb +44 -0
  55. data/lib/ogre/skeletons/code_generator/templates/default/metadata.rb.erb +8 -0
  56. data/lib/ogre/skeletons/code_generator/templates/default/repo/gitignore.erb +11 -0
  57. data/lib/ogre/user-create.rb +40 -0
  58. data/lib/ogre/user-delete.rb +39 -0
  59. data/lib/ogre/version.rb +5 -0
  60. data/ogre.gemspec +45 -0
  61. data/spec/fixtures/client_key/dummy.pem +27 -0
  62. data/spec/fixtures/vcr_cassettes/associate-no-org.yml +65 -0
  63. data/spec/fixtures/vcr_cassettes/associate-no-user.yml +65 -0
  64. data/spec/fixtures/vcr_cassettes/associate-user-exists.yml +135 -0
  65. data/spec/fixtures/vcr_cassettes/associate.yml +406 -0
  66. data/spec/fixtures/vcr_cassettes/org-create-exists.yml +69 -0
  67. data/spec/fixtures/vcr_cassettes/org-create.yml +71 -0
  68. data/spec/fixtures/vcr_cassettes/org-delete-no-org.yml +61 -0
  69. data/spec/fixtures/vcr_cassettes/org-delete.yml +68 -0
  70. data/spec/fixtures/vcr_cassettes/set-private-key.yml +239 -0
  71. data/spec/fixtures/vcr_cassettes/user-create-bad-email.yml +66 -0
  72. data/spec/fixtures/vcr_cassettes/user-create-exists.yml +70 -0
  73. data/spec/fixtures/vcr_cassettes/user-create-short-password.yml +66 -0
  74. data/spec/fixtures/vcr_cassettes/user-create.yml +72 -0
  75. data/spec/fixtures/vcr_cassettes/user-delete-not-found.yml +61 -0
  76. data/spec/fixtures/vcr_cassettes/user-delete.yml +136 -0
  77. data/spec/ogre/associate_spec.rb +67 -0
  78. data/spec/ogre/org-create_spec.rb +52 -0
  79. data/spec/ogre/org-delete_spec.rb +34 -0
  80. data/spec/ogre/set-private-key_spec.rb +16 -0
  81. data/spec/ogre/user-create_spec.rb +58 -0
  82. data/spec/ogre/user-delete_spec.rb +34 -0
  83. data/spec/spec_helper.rb +16 -0
  84. metadata +349 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4a6d483c682f21f5060bdbd3e0c1de49b3c6fff
4
+ data.tar.gz: 2a244953732b78a5bc112629328f8e78271f8399
5
+ SHA512:
6
+ metadata.gz: 6b7bea37adf94bc417f3285e201e1f15b92efe273a7fd8734b5678ea16ae8a632bf6239f1ddb48ddf181a9240706a790f9cae2a6771bc7be84e0765c1a6c15a9
7
+ data.tar.gz: 1e47e713424344b4647106da1d541271465ec545a54850674f7f46b737f44ee5e97d859f58c192cab810a47404c6c0b1a5e916960427a91020c3542877481898
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ /.idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /vendor/
11
+ /tmp/
12
+ *.bundle
13
+ *.so
14
+ *.o
15
+ *.a
16
+ mkmf.log
17
+
18
+ *.sw?
19
+
20
+ .ruby-version
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ Exclude:
3
+ - Guardfile
4
+ - vendor/**/*
5
+ - lib/ogre/skeletons/**/*
6
+ - tmp/**/*
7
+ Metrics/AbcSize:
8
+ Enabled: false
9
+ Metrics/LineLength:
10
+ Max: 120
11
+ Metrics/MethodLength:
12
+ Max: 50
13
+ Style/SpecialGlobalVars:
14
+ Enabled: false
15
+ Style/FileName:
16
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.1.4"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # gem dependencies in ogre.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ [![Build Status](https://travis-ci.org/activenetwork-automation/ogre.svg)](https://travis-ci.org/activenetwork-automation/ogre)
2
+ [![Coverage Status](https://coveralls.io/repos/activenetwork-automation/ogre/badge.svg)](https://coveralls.io/r/activenetwork-automation/ogre)
3
+ [![Dependency Status](https://gemnasium.com/activenetwork-automation/ogre.svg)](https://gemnasium.com/activenetwork-automation/ogre)
4
+ [![Inline docs](http://inch-ci.org/github/activenetwork-automation/ogre.png?branch=master)](http://inch-ci.org/github/activenetwork-automation/ogre)
5
+
6
+ # Ogre
7
+
8
+ This gem supports organization and user management for Enterprise Chef.
9
+
10
+ While this functionality already exists in [knife-opc](https://github.com/chef/knife-opc), we wanted to be able to create the chef policy repository as well and set the private key into vCenter Orchestrator all in one tool.
11
+
12
+ ## Installation
13
+
14
+ `gem install ogre`
15
+
16
+ ## Configuration
17
+
18
+ ### ~/.ogre/config.json
19
+
20
+ All of the parameters here are optional and can be passed in and/or overriden at the CLI. As of today, the `pivotal` user is the only user able to execute certain methods via the [Chef API](https://docs.chef.io/api_chef_server.html). The key can be found in `/etc/opscode/pivotal.pem` on the Enterprise Chef box.
21
+
22
+ ``` json
23
+ {
24
+ "run_as":"chef_username",
25
+ "key_path":"/path/to/key.pem",
26
+ "server_url":"https://chef.url",
27
+ "vco_url":"https://vco.url:8281/",
28
+ "vco_user": "domain\\user",
29
+ "vco_password":"password",
30
+ "vco_wf_name":"vco_workflow_name",
31
+ "vco_verify_ssl":"false"
32
+ }
33
+
34
+ ```
35
+
36
+ ## Usage
37
+ ogre org-create ORG DESCRIPTION (options)
38
+ ---
39
+ - `-p`, `--create-repo` Create Chef policy repository
40
+ - `-P`, `--repo-path` Chef policy repo path
41
+ - `-I`, `--license=LICENSE` Chef policy repository license
42
+ - `-m`, `--email=EMAIL` Chef policy repository e-mail
43
+ - `-C`, `--authors=AUTHORS` Chef policy repository authors
44
+
45
+ When using `-p`, Ogre will save the Chef policy repository as ~/.ogre/ORG-chef, otherwise it will output the validator key for the new organization.
46
+
47
+ ogre org-delete ORG (options)
48
+ ---
49
+ - `-f`, `--force` Delete without confirmation
50
+
51
+ ogre user-create USERNAME FIRST_NAME LAST_NAME EMAIL PASSWORD (options)
52
+ ---
53
+
54
+ ogre user-delete USERNAME (options)
55
+ ---
56
+ - `-f`, `--force` Delete without confirmation
57
+
58
+ ogre associate ORG USER (options)
59
+ ---
60
+ - `-a`, `--admin` Add user to admin group within organization
61
+
62
+ ogre set-private-key CHEF_HOSTNAME CHEF_VALIDATOR_NAME KEY_PATH (options)
63
+ ---
64
+
65
+ `set-private-key` is very opinionated to our needs. We have a vco workflow called `Set Private Key` which takes in `CHEF_HOSTNAME`, `CHEF_VALIDATOR_NAME`, and `KEY_PATH`. This is stored in Orchestrator so that our organzation has the correct permissions to bootstrap nodes.
66
+
67
+ - `--vco-url` vCenter Orchestrator URL
68
+ - `--vco-user` vCenter Orchestrator user
69
+ - `--vco-password` vCenter Orchestrator password
70
+ - `--vco-wf-name` vCenter Orchestrator workflow name
71
+ - `--vco-verify-ssl` vCenter Orchestrator verify ssl
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/activenetwork-automation/ogre/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
80
+
81
+ ### License and Authors
82
+
83
+ - [Joe Nguyen](https://github.com/joenguyen)
84
+
85
+ ## License ##
86
+
87
+ | | |
88
+ |:---------------------|:-----------------------------------------|
89
+ | **Copyright:** | Copyright 2015 ACTIVE Network, LLC
90
+ | **License:** | Apache License, Version 2.0
91
+
92
+ Licensed under the Apache License, Version 2.0 (the "License");
93
+ you may not use this file except in compliance with the License.
94
+ You may obtain a copy of the License at
95
+
96
+ http://www.apache.org/licenses/LICENSE-2.0
97
+
98
+ Unless required by applicable law or agreed to in writing, software
99
+ distributed under the License is distributed on an "AS IS" BASIS,
100
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
101
+ See the License for the specific language governing permissions and
102
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ require 'coveralls/rake/task'
4
+ require 'yard'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ Coveralls::RakeTask.new
9
+
10
+ task :style do
11
+ sh 'rubocop'
12
+ end
13
+
14
+ task :doc do
15
+ sh 'yard'
16
+ end
17
+
18
+ task default: [:spec, :style, :doc, 'coveralls:push']
data/bin/ogre ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ogre/runner'
3
+
4
+ Ogre::Runner.new(ARGV.dup).execute!
data/lib/ogre.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'thor'
2
+ require 'highline/import'
3
+ require 'ogre/messages'
4
+ require 'ogre/config'
5
+ require 'ogre/base'
6
+ require 'ogre/associate'
7
+ require 'ogre/org-create'
8
+ require 'ogre/org-delete'
9
+ require 'ogre/set-private-key'
10
+ require 'ogre/user-create'
11
+ require 'ogre/user-delete'
12
+
13
+ module Ogre
14
+ # Start of main CLI
15
+ class CLI < Thor
16
+ package_name 'ogre'
17
+ map '--version' => :version
18
+ map '-v' => :version
19
+
20
+ desc 'version, -v', DESC_VERSION
21
+ # Display the version of `ogre`
22
+ def version
23
+ puts VERSION
24
+ end
25
+
26
+ # rubocop:disable LineLength
27
+ # subcommand in Thor called as registered class
28
+ register(OrgCreate, 'org_create', 'org-create ' << OrgCreate.arguments.map(&:name).join(' ').upcase, DESC_CREATE)
29
+ register(OrgDelete, 'org_delete', 'org-delete ' << OrgDelete.arguments.map(&:name).join(' ').upcase, DESC_DELETE)
30
+ register(UserCreate, 'user_create', 'user-create ' << UserCreate.arguments.map(&:name).join(' ').upcase, DESC_CREATE_USER)
31
+ register(UserDelete, 'user_delete', 'user-delete ' << UserDelete.arguments.map(&:name).join(' ').upcase, DESC_DELETE_USER)
32
+ register(Associate, 'associate', 'associate ' << Associate.arguments.map(&:name).join(' ').upcase, DESC_ASSOCIATE_USERS)
33
+ register(SetPrivateKey, 'set_private_key', 'set-private-key ' << SetPrivateKey.arguments.map(&:name).join(' ').upcase, DESC_SET_PRIVATE_KEY)
34
+ # rubocop:enable LineLength
35
+
36
+ # Workarounds to include options in 'ogre help command'
37
+ tasks['user_create'].options = UserCreate.class_options
38
+ tasks['user_delete'].options = UserDelete.class_options
39
+ tasks['org_create'].options = OrgCreate.class_options
40
+ tasks['org_delete'].options = OrgDelete.class_options
41
+ tasks['associate'].options = Associate.class_options
42
+ tasks['set_private_key'].options = SetPrivateKey.class_options
43
+ end
44
+ end
@@ -0,0 +1,57 @@
1
+ module Ogre
2
+ # Associate user to org while bypassing the association request
3
+ class Associate < Ogre::Base
4
+ include Thor::Actions
5
+
6
+ # required
7
+ argument :org, type: :string, desc: DESC_ORG
8
+ argument :user, type: :string, desc: DESC_USER
9
+
10
+ # optional
11
+ class_option :admin, aliases: '-a', type: :boolean, desc: DESC_ASSOCIATE_ADMIN
12
+
13
+ # Associate user to org while bypassing the association request
14
+ def associate
15
+ begin
16
+ # associate (invite) user
17
+ request_body = { user: user }
18
+ response = chef_rest.post_rest "organizations/#{org}/association_requests", request_body
19
+
20
+ # add (force) user to org
21
+ association_id = response['uri'].split('/').last
22
+ chef_rest.put_rest "users/#{user}/association_requests/#{association_id}", response: 'accept'
23
+ rescue Net::HTTPServerException => e
24
+ # already exists -- i will allow it
25
+ if e.response.code == '409'
26
+ puts "User '#{user}' already associated with organization '#{org}'"
27
+ else
28
+ raise e
29
+ end
30
+ end
31
+
32
+ # add to admin?
33
+ groups = ['users']
34
+ groups << 'admins' if options[:admin]
35
+
36
+ # add user to group(s)
37
+ groups.each do |groupname|
38
+ group = chef_rest.get_rest "organizations/#{org}/groups/#{groupname}"
39
+ # check if user is in group
40
+ unless group['actors'].include?(user)
41
+ body_hash = {
42
+ groupname: "#{groupname}",
43
+ actors: {
44
+ users: group['actors'].concat([user]),
45
+ groups: group['groups']
46
+ }
47
+ }
48
+
49
+ # associate user
50
+ chef_rest.put_rest "organizations/#{org}/groups/#{groupname}", body_hash
51
+ puts "Successfully added '#{user}' to '#{groupname}' in the #{org} org"
52
+ end
53
+ next
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/ogre/base.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'chef/rest'
2
+
3
+ # Refer to README.md for use instructions
4
+ module Ogre
5
+ # Base ogre class includes common parameters used to authenticate with Chef::REST
6
+ class Base < Thor::Group
7
+ include Thor::Actions
8
+
9
+ # Chef Rest parameters
10
+ class_option :server_url, type: :string, desc: DESC_CHEF_SERVER_URL
11
+ class_option :run_as, type: :string, desc: DESC_RUN_AS
12
+ class_option :key_path, type: :string, desc: DESC_PRIVATE_KEY
13
+
14
+ # Parameters passed in from cli will take precedence
15
+ def chef_rest
16
+ Chef::REST.new(options[:server_url] || Config.options[:server_url],
17
+ options[:run_as] || Config.options[:run_as],
18
+ options[:key_path] || Config.options[:key_path])
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'fileutils'
2
+
3
+ # This is a simple class that puts options from a config file
4
+ # into an accessible object
5
+ module Ogre
6
+ # ogre home
7
+ OGRE_HOME = "#{ENV['HOME']}/.ogre"
8
+ # config path
9
+ CONFIG_PATH = "#{OGRE_HOME}/config.json"
10
+
11
+ # Static method to make config parameters available
12
+ class Config
13
+ # Read in defaults from config file
14
+ def self.options
15
+ (JSON.parse(File.read(CONFIG_PATH), symbolize_names: true) if File.exist?(CONFIG_PATH)) || {}
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,84 @@
1
+
2
+ # string constants for interactive messages
3
+ module Ogre
4
+ # version
5
+ DESC_VERSION = 'Display gem version'
6
+
7
+ # org create description
8
+ DESC_CREATE = 'Create an organization in Chef'
9
+
10
+ # org delete description
11
+ DESC_DELETE = 'Delete an organization in Chef'
12
+
13
+ # org short name
14
+ DESC_ORG = 'Organization short name'
15
+
16
+ # org description
17
+ DESC_ORG_DESC = 'Organization long name'
18
+
19
+ # private key path
20
+ DESC_PRIVATE_KEY = 'Path to private key file'
21
+
22
+ # chef server url
23
+ DESC_CHEF_SERVER_URL = 'Chef Server URL i.e. https://chef.server.domain'
24
+
25
+ # associate description
26
+ DESC_ASSOCIATE_USERS = 'Associate users to an organization'
27
+
28
+ # user description
29
+ DESC_USER = 'User name'
30
+
31
+ # associate to admin group description
32
+ DESC_ASSOCIATE_ADMIN = 'Add user to admin group within organization'
33
+
34
+ # chef policy repository license
35
+ DESC_REPO_LICENSE = 'Chef policy repository license'
36
+
37
+ # chef policy repository authors
38
+ DESC_REPO_AUTHORS = 'Chef policy repository authors'
39
+
40
+ # chef policy repository path
41
+ DESC_REPO_PATH = 'Chef policy repository path'
42
+
43
+ # chef policy repository e-mail
44
+ DESC_REPO_EMAIL = 'Chef policy repository e-mail'
45
+
46
+ # create Chef policy repository
47
+ DESC_CREATE_REPO = 'Create Chef policy repository'
48
+
49
+ # delete without confirmation
50
+ DESC_FORCE = 'Delete without confirmation'
51
+
52
+ # create new chef user
53
+ DESC_CREATE_USER = 'Create new chef user'
54
+
55
+ # Delete and disassociate chef user
56
+ DESC_DELETE_USER = 'Delete and disassociate chef user'
57
+
58
+ # chef run as user
59
+ DESC_RUN_AS = 'Chef user'
60
+
61
+ # vCenter Orchestrator URL
62
+ DESC_VCO_URL = 'vCenter Orchestrator URL'
63
+
64
+ # vCenter Orchestrator user
65
+ DESC_VCO_USER = 'vCenter Orchestrator user'
66
+
67
+ # vCenter Orchestrator password
68
+ DESC_VCO_PASSWORD = 'vCenter Orchestrator password'
69
+
70
+ # vCenter Orchestrator workflow name
71
+ DESC_VCO_WF_NAME = 'vCenter Orchestrator workflow name'
72
+
73
+ # Set chef validation key for VCO
74
+ DESC_SET_PRIVATE_KEY = 'Set chef validation key for VCO'
75
+
76
+ # Chef hostname
77
+ DESC_CHEF_HOSTNAME = 'Chef hostname'
78
+
79
+ # Chef validator username
80
+ DESC_CHEF_VALIDATOR = 'Chef validator user name'
81
+
82
+ # vCenter verify ssl param
83
+ DESC_VCO_VERIFY_SSL = 'vCenter Orchestrator verify ssl'
84
+ end
@@ -0,0 +1,96 @@
1
+ require 'chef-dk/command/generator_commands/repo'
2
+
3
+ module Ogre
4
+ # Create organization through Chef::REST with the option to create the
5
+ # Chef policy repository
6
+ class OrgCreate < Ogre::Base
7
+ # rubocop:disable CyclomaticComplexity, PerceivedComplexity
8
+ # required parameters
9
+ argument :org, type: :string, desc: DESC_ORG
10
+ argument :org_desc, type: :string, desc: DESC_ORG_DESC
11
+
12
+ # optional chef policy repo parameters
13
+ class_option :create_repo, aliases: '-p', type: :boolean, default: false, desc: DESC_CREATE_REPO
14
+ class_option :repo_path, aliases: '-P', type: :string, desc: DESC_REPO_PATH
15
+ class_option :license, aliases: '-I', default: 'apache2', type: :string, desc: DESC_REPO_LICENSE
16
+ class_option :email, aliases: '-m', type: :string, desc: DESC_REPO_EMAIL
17
+ class_option :authors, aliases: '-C', type: :string, desc: DESC_REPO_AUTHORS
18
+
19
+ # organization create method
20
+ def org_create
21
+ org_json = { name: "#{org}", full_name: "#{org_desc}" }
22
+ response = chef_rest.post_rest('/organizations', org_json)
23
+ puts "'#{org}' org has been created."
24
+
25
+ # use chef repo generate to create a chef policy repo
26
+ if options[:create_repo]
27
+
28
+ # create parent dir for chef policy repo
29
+ repo_path = options[:repo_path] ? options[:repo_path] : OGRE_HOME
30
+ Dir.mkdir repo_path unless File.exist?(repo_path)
31
+
32
+ # run cookbook generate
33
+ generate_cmd = ChefDK::Command::GeneratorCommands::Repo.new(generate_params(repo_path))
34
+ generate_cmd.run
35
+
36
+ File.open("#{repo_path}/#{org}-chef/.chef/#{response['clientname']}.pem", 'w') do |f|
37
+ f.print(response['private_key'])
38
+ end
39
+
40
+ else
41
+ puts response['private_key']
42
+ end
43
+
44
+ rescue Net::HTTPServerException => e
45
+
46
+ # already exists -- i will allow it
47
+ if e.response.code == '409'
48
+ puts "#{org} org already exists"
49
+ else
50
+ raise e
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ # concatenate parameters into a format ChefDK::Command::GeneratorCommands::Repo will accept
57
+ def generate_params(parent_path)
58
+ # chef policy repository parameters
59
+ generate_str = ["#{parent_path}/#{org}-chef"]
60
+
61
+ # org name
62
+ generate_str << '-a'
63
+ generate_str << "org=#{org}"
64
+
65
+ # chef server url
66
+ generate_str << '-a'
67
+ generate_str << "chef_server_url=#{options[:server_url] || Config.options[:server_url]}"
68
+
69
+ # generator skeleton
70
+ generate_str << '-g'
71
+ generate_str << 'lib/ogre/skeletons/code_generator'
72
+
73
+ # optional license
74
+ if options[:license]
75
+ generate_str << '-I'
76
+ generate_str << "#{options[:license]}"
77
+ end
78
+
79
+ # optional email
80
+ if options[:email]
81
+ generate_str << '-m'
82
+ generate_str << "#{options[:email]}"
83
+ end
84
+
85
+ # optional authors
86
+ if options[:authors]
87
+ generate_str << '-C'
88
+ generate_str << "\"#{options[:authors]}\""
89
+ end
90
+
91
+ generate_str
92
+ end
93
+ end
94
+ end
95
+
96
+ # rubocop:enable CyclomaticComplexity, PerceivedComplexity