knife-stash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ _yardoc
7
+ bin
8
+ coverage
9
+ doc/
10
+ Gemfile.lock
11
+ InstalledFiles
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.1.0
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem "faraday"
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # Knife Stash
2
+
3
+ A knife plugin for Atlassian Stash.
4
+
5
+ ## Installation
6
+
7
+ * Clone repo to ~/.chef/plugins/knife or path/to/cheforg/.chef/plugins/knife
8
+
9
+ ## Usage
10
+
11
+ Plugin will prompt for any missing passwords.
12
+
13
+ ### Common parameters
14
+
15
+ * `--noop` - Perform no modifying operations
16
+ * `--stash-username USERNAME` - Stash username, defaults to
17
+ `ENV['USER']`
18
+ * `--stash-password PASSWORD` - Stash password, defaults to nothing but will ask if necessary
19
+ * `--stash-hostname HOSTNAME` - Stash hostname, no default
20
+
21
+ ### knife stash project create KEY NAME
22
+
23
+ Creates a Stash project.
24
+
25
+ * `--description DESCRIPTION` - optionally set a description for the project
26
+
27
+ For example:
28
+
29
+ $ knife stash project create TEST "Test Project from API"
30
+ Created Stash Project: TEST (Test Project from API)
31
+
32
+ ### knife stash project delete KEY
33
+
34
+ Deletes a Stash project.
35
+
36
+ * `--delete-repos` - required if any repositories exist under project
37
+
38
+ For example:
39
+
40
+ $ knife stash project delete TEST --delete-repos
41
+ Deleted Stash Repository: TEST/repo1
42
+ Deleted Stash Repository: TEST/repo2
43
+ Deleted Stash Project: TEST
44
+
45
+ ### knife stash project repos KEY
46
+
47
+ Lists all Stash repositories in a project.
48
+
49
+ $ knife stash project repos COOKBOOKS
50
+ cessna (cessna)
51
+ netbackup (netbackup)
52
+ remoteaddr (remoteaddr)
53
+ template-cookbook (template-cookbook)
54
+ ...
55
+
56
+ ### knife stash projects
57
+
58
+ Lists all Stash projects.
59
+
60
+ $ knife stash projects
61
+ CHEF_BASEBOX: Chef Basebox
62
+ COOKBOOKS: Chef Cookbooks
63
+ CHEFORG_CORE_SYSTEMS: Chef Organization core-systems
64
+ CHEFORG_FNCE: Chef Organization FNCE
65
+ ...
66
+
67
+ ### knife stash repo create KEY REPO
68
+
69
+ Creates a Stash repository in a project.
70
+
71
+ $ knife stash repo create TEST repo1
72
+ Created Stash Repository: TEST/repo1
73
+ Available via (HTTPS): https://bflad@stash.example.com/scm/TEST/repo1.git
74
+ Available via (SSH): ssh://git@stash.example.com:7999/TEST/repo1.git
75
+
76
+ ### knife stash repo delete KEY REPO
77
+
78
+ Deletes a Stash repository in a project.
79
+
80
+ $ knife stash repo delete TEST repo1
81
+ Deleted Stash Repository: TEST/repo1
82
+
83
+ ### knife stash repos
84
+
85
+ Lists all Stash repositories in all projects.
86
+
87
+ $ knife stash repos
88
+ CHEF_BASEBOX/definitions
89
+ COOKBOOKS/cessna
90
+ COOKBOOKS/netbackup
91
+ COOKBOOKS/remoteaddr
92
+ COOKBOOKS/template-cookbook
93
+ CHEFORG_CORE_SYSTEMS/cheforg-core-systems
94
+ CHEFORG_FNCE/cheforg_fnce
95
+ ...
96
+
97
+ ## Contributing
98
+
99
+ Please use standard Github issues and pull requests.
100
+
101
+ ## License and Author
102
+
103
+ Author:: Brian Flad (<bflad417@gmail.com>)
104
+
105
+ Copyright:: 2013
106
+
107
+ Licensed under the Apache License, Version 2.0 (the "License");
108
+ you may not use this file except in compliance with the License.
109
+ You may obtain a copy of the License at
110
+
111
+ http://www.apache.org/licenses/LICENSE-2.0
112
+
113
+ Unless required by applicable law or agreed to in writing, software
114
+ distributed under the License is distributed on an "AS IS" BASIS,
115
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
116
+ See the License for the specific language governing permissions and
117
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/knife-stash/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Brian Flad"]
6
+ gem.email = ["bflad417@gmail.com"]
7
+ gem.description = %q{A knife plugin for Atlassian Stash.}
8
+ gem.summary = gem.summary
9
+ gem.homepage = "https://github.com/bflad/knife-stash"
10
+
11
+ gem.add_runtime_dependency "faraday"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "knife-stash"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Knife::Stash::VERSION
18
+ end
@@ -0,0 +1,93 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+ require 'highline/import'
10
+ require 'faraday'
11
+
12
+ class BaseStashCommand < Chef::Knife
13
+
14
+ deps do
15
+ require 'highline/import'
16
+ end
17
+
18
+ def self.get_common_options
19
+ unless defined? $default
20
+ $default = Hash.new
21
+ end
22
+
23
+ option :noop,
24
+ :long => "--noop",
25
+ :description => "Perform no modifying operations",
26
+ :boolean => false
27
+
28
+ option :stash_username,
29
+ :short => "-u USERNAME",
30
+ :long => "--stash-username USERNAME",
31
+ :description => "The username for Stash"
32
+ $default[:stash_username] = ENV['USER']
33
+
34
+ option :stash_password,
35
+ :short => "-p PASSWORD",
36
+ :long => "--stash-password PASSWORD",
37
+ :description => "The password for Stash"
38
+
39
+ option :stash_hostname,
40
+ :long => "--stash-hostname HOSTNAME",
41
+ :description => "The hostname for Stash"
42
+
43
+ end
44
+
45
+ def display_stash_error(message,response)
46
+ ui.fatal message
47
+ JSON.parse(response.body)['errors'].each do |error|
48
+ ui.fatal "#{error['context'] ? error['context'] + ": " : "" }#{error['message']}"
49
+ end
50
+ end
51
+
52
+ def get_all_values(stash,url,response)
53
+ r = JSON.parse(response.body)
54
+ yield r['values']
55
+ until r['isLastPage']
56
+ response = stash.get url, { :start => r['nextPageStart'] }
57
+ r = JSON.parse(response.body)
58
+ yield r['values']
59
+ end
60
+ end
61
+
62
+ def get_config(key)
63
+ key = key.to_sym
64
+ rval = config[key] || Chef::Config[:knife][key] || $default[key]
65
+ Chef::Log.debug("value for config item #{key}: #{rval}")
66
+ rval
67
+ end
68
+
69
+ def get_stash_connection
70
+ config[:stash_hostname] = ask("Stash Hostname: ") { |q| q.echo = "*" } unless get_config(:stash_hostname)
71
+ config[:stash_username] = ask("Stash Username for #{get_config(:stash_hostname)}: ") { |q| q.echo = "*" } unless get_config(:stash_username)
72
+ config[:stash_password] = ask("Stash Password for #{get_config(:stash_username)}: ") { |q| q.echo = "*" } unless get_config(:stash_password)
73
+
74
+ connection = Faraday.new(:url => "https://#{get_config(:stash_hostname)}", :ssl => {:verify => false}) do |faraday|
75
+ faraday.request :url_encoded # form-encode POST params
76
+ #faraday.response :logger # log requests to STDOUT
77
+ faraday.adapter :net_http # make requests with Net::HTTP
78
+ end
79
+ connection.basic_auth(get_config(:stash_username),get_config(:stash_password))
80
+ connection.url_prefix = "https://#{get_config(:stash_hostname)}/rest/api/1.0"
81
+ connection
82
+ end
83
+
84
+ def get_repo_https_url(project_key,repo)
85
+ "https://#{get_config(:stash_username)}@#{get_config(:stash_hostname)}/scm/#{project_key}/#{repo}.git"
86
+ end
87
+
88
+ def get_repo_ssh_url(project_key,repo)
89
+ "ssh://git@#{get_config(:stash_hostname)}:7999/#{project_key}/#{repo}.git"
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,68 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashProjectCreate < BaseStashCommand
11
+
12
+ banner "knife stash project create KEY NAME (options)"
13
+ category "stash"
14
+
15
+ option :description,
16
+ :short => "-d DESCRIPTION",
17
+ :long => "--description DESCRIPTION",
18
+ :description => "The description for the project"
19
+
20
+ get_common_options
21
+
22
+ def run
23
+
24
+ key = name_args.first
25
+
26
+ if key.nil?
27
+ ui.fatal "You need a project key!"
28
+ show_usage
29
+ exit 1
30
+ end
31
+
32
+ name = name_args[1]
33
+
34
+ if name.nil?
35
+ ui.fatal "You need a project name!"
36
+ show_usage
37
+ exit 1
38
+ end
39
+
40
+ args = name_args[2]
41
+ if args.nil?
42
+ args = ""
43
+ end
44
+
45
+ stash = get_stash_connection
46
+ project = { :name => name, :key => key }
47
+ project[:description] = get_config(:description) if get_config(:description)
48
+
49
+ if get_config(:noop)
50
+ ui.info "#{ui.color "Skipping project creation process because --noop specified.", :red}"
51
+ else
52
+ response = stash.post do |post|
53
+ post.url "projects"
54
+ post.headers['Content-Type'] = "application/json"
55
+ post.body = JSON.generate(project)
56
+ end
57
+ if response.success?
58
+ ui.info "Created Stash Project: #{key} (#{name})"
59
+ else
60
+ display_stash_error "Could not create Stash project!", response
61
+ exit 1
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashProjectDelete < BaseStashCommand
11
+
12
+ banner "knife stash project delete KEY (options)"
13
+ category "stash"
14
+
15
+ option :delete_repos,
16
+ :short => "-d",
17
+ :long => "--delete-repos",
18
+ :description => "Delete all repositories in project.",
19
+ :boolean => false
20
+
21
+ get_common_options
22
+
23
+ def run
24
+
25
+ key = name_args.first
26
+
27
+ if key.nil?
28
+ ui.fatal "You need a project key!"
29
+ show_usage
30
+ exit 1
31
+ end
32
+
33
+ stash = get_stash_connection
34
+
35
+ if get_config(:noop)
36
+ ui.info "#{ui.color "Skipping project deletion process because --noop specified.", :red}"
37
+ else
38
+ if get_config(:delete_repos)
39
+ url = "projects/#{key}/repos"
40
+ response = stash.get url
41
+ if response.success?
42
+ get_all_values stash,url,response do |values|
43
+ values.each do |repo|
44
+ repo_delete = StashRepoDelete.new
45
+ repo_delete.name_args = [ key, repo['name'] ]
46
+ repo_delete.run
47
+ end
48
+ end
49
+ else
50
+ display_stash_error "Could not delete Stash project repositories!", response
51
+ exit 1
52
+ end
53
+ end
54
+ response = stash.delete "projects/#{key}"
55
+ if response.success?
56
+ ui.info "Deleted Stash Project: #{key}"
57
+ else
58
+ display_stash_error "Could not delete Stash project!", response
59
+ exit 1
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,45 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashProjectRepos < BaseStashCommand
11
+
12
+ banner "knife stash project repos KEY (options)"
13
+ category "stash"
14
+
15
+ get_common_options
16
+
17
+ def run
18
+
19
+ key = name_args.first
20
+
21
+ if key.nil?
22
+ ui.fatal "You need a project key!"
23
+ show_usage
24
+ exit 1
25
+ end
26
+
27
+ stash = get_stash_connection
28
+ url = "projects/#{key}/repos"
29
+ response = stash.get url
30
+
31
+ if response.success?
32
+ get_all_values stash,url,response do |values|
33
+ values.each do |repo|
34
+ ui.info "#{repo['slug']} (#{repo['name']})"
35
+ end
36
+ end
37
+ else
38
+ display_stash_error "Could not list Stash project repositories!", response
39
+ exit 1
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashProjects < BaseStashCommand
11
+
12
+ banner "knife stash projects (options)"
13
+ category "stash"
14
+
15
+ get_common_options
16
+
17
+ def run
18
+
19
+ stash = get_stash_connection
20
+ url = "projects"
21
+ response = stash.get url
22
+
23
+ if response.success?
24
+ get_all_values stash,url,response do |values|
25
+ values.each do |project|
26
+ ui.info "#{project['key']}: #{project['name']}"
27
+ end
28
+ end
29
+ else
30
+ display_stash_error "Could not list Stash projects!", response
31
+ exit 1
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,64 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashRepoCreate < BaseStashCommand
11
+
12
+ banner "knife stash repo create PROJECT_KEY NAME (options)"
13
+ category "stash"
14
+
15
+ get_common_options
16
+
17
+ def run
18
+
19
+ project_key = name_args.first
20
+
21
+ if project_key.nil?
22
+ ui.fatal "You need a project key!"
23
+ show_usage
24
+ exit 1
25
+ end
26
+
27
+ name = name_args[1]
28
+
29
+ if name.nil?
30
+ ui.fatal "You need a repository name!"
31
+ show_usage
32
+ exit 1
33
+ end
34
+
35
+ args = name_args[2]
36
+ if args.nil?
37
+ args = ""
38
+ end
39
+
40
+ stash = get_stash_connection
41
+ repo = { :name => name, :scmId => "git" }
42
+
43
+ if get_config(:noop)
44
+ ui.info "#{ui.color "Skipping repo creation process because --noop specified.", :red}"
45
+ else
46
+ response = stash.post do |post|
47
+ post.url "projects/#{project_key}/repos"
48
+ post.headers['Content-Type'] = "application/json"
49
+ post.body = JSON.generate(repo)
50
+ end
51
+ if response.success?
52
+ ui.info "Created Stash Repository: #{project_key}/#{name}"
53
+ ui.info "Available via (HTTPS): #{get_repo_https_url(project_key,name)}"
54
+ ui.info "Available via (SSH): #{get_repo_ssh_url(project_key,name)}"
55
+ else
56
+ display_stash_error "Could not create Stash repository!", response
57
+ exit 1
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,52 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashRepoDelete < BaseStashCommand
11
+
12
+ banner "knife stash repo delete KEY REPO (options)"
13
+ category "stash"
14
+
15
+ get_common_options
16
+
17
+ def run
18
+
19
+ key = name_args.first
20
+
21
+ if key.nil?
22
+ ui.fatal "You need a project key!"
23
+ show_usage
24
+ exit 1
25
+ end
26
+
27
+ repo = name_args[1]
28
+
29
+ if repo.nil?
30
+ ui.fatal "You need a repository name!"
31
+ show_usage
32
+ exit 1
33
+ end
34
+
35
+ stash = get_stash_connection
36
+
37
+ if get_config(:noop)
38
+ ui.info "#{ui.color "Skipping project repository deletion process because --noop specified.", :red}"
39
+ else
40
+ response = stash.delete "projects/#{key}/repos/#{repo}"
41
+ if response.success?
42
+ ui.info "Deleted Stash Repository: #{key}/#{repo}"
43
+ else
44
+ display_stash_error "Could not delete Stash respository!", response
45
+ exit 1
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,49 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ module StashKnifePlugin
7
+
8
+ require 'chef/knife'
9
+
10
+ class StashRepos < BaseStashCommand
11
+
12
+ banner "knife stash repos (options)"
13
+ category "stash"
14
+
15
+ get_common_options
16
+
17
+ def run
18
+
19
+ stash = get_stash_connection
20
+ url = "projects"
21
+ project_response = stash.get url
22
+
23
+ if project_response.success?
24
+ get_all_values stash,url,project_response do |project_values|
25
+ project_values.each do |project|
26
+ url = "projects/#{project['key']}/repos"
27
+ repo_response = stash.get url
28
+
29
+ if repo_response.success?
30
+ get_all_values stash,url,repo_response do |repo_values|
31
+ repo_values.each do |repo|
32
+ ui.info "#{project['key']}/#{repo['slug']}"
33
+ end
34
+ end
35
+ else
36
+ display_stash_error "Could not list Stash project repositories!", repo_response
37
+ exit 1
38
+ end
39
+ end
40
+ end
41
+ else
42
+ display_stash_error "Could not list Stash projects!", project_response
43
+ exit 1
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Stash
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-stash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Flad
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A knife plugin for Atlassian Stash.
31
+ email:
32
+ - bflad417@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - CHANGELOG.md
39
+ - Gemfile
40
+ - README.md
41
+ - Rakefile
42
+ - knife-stash.gemspec
43
+ - lib/chef/knife/BaseStashCommand.rb
44
+ - lib/chef/knife/stash_project_create.rb
45
+ - lib/chef/knife/stash_project_delete.rb
46
+ - lib/chef/knife/stash_project_repos.rb
47
+ - lib/chef/knife/stash_projects.rb
48
+ - lib/chef/knife/stash_repo_create.rb
49
+ - lib/chef/knife/stash_repo_delete.rb
50
+ - lib/chef/knife/stash_repos.rb
51
+ - lib/knife-stash/version.rb
52
+ homepage: https://github.com/bflad/knife-stash
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.23
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: ''
76
+ test_files: []